Support users with no email + more tests

This commit is contained in:
Avital Oliver
2012-07-09 15:29:41 -07:00
committed by Nick Martin
parent 9db26049d1
commit 1773c91c50
2 changed files with 20 additions and 8 deletions

View File

@@ -98,10 +98,7 @@
Meteor.users.update(user, {$set: newAttrs});
};
if (!email)
throw new Meteor.Error("We don't yet support email-less users");
var userByEmail = Meteor.users.findOne({emails: email});
var userByEmail = email && Meteor.users.findOne({emails: email});
var user;
if (userByEmail) {
@@ -126,7 +123,7 @@
var userByServiceUserId = Meteor.users.findOne(selector);
if (userByServiceUserId) {
user = userByServiceUserId;
if (user.emails.indexOf(email) === -1) {
if (email && user.emails.indexOf(email) === -1) {
// The user may have changed the email address associated with
// this service. Store the new one in addition to the old one.
Meteor.users.update(user, {$push: {emails: email}});
@@ -140,7 +137,7 @@
var attrs = {};
attrs[serviceName] = _.extend({id: serviceUserId}, serviceData);
return Meteor.users.insert(_.extend({}, userData, {
emails: [email],
emails: (email ? [email] : []),
services: attrs
}));
}

View File

@@ -1,12 +1,27 @@
Tinytest.add('accounts - updateOrCreateUser', function (test) {
Meteor.users.remove({});
// test that emails are matched correctly for users logging in
// through different services
Meteor.users.remove({});
Meteor.accounts.updateOrCreateUser('foo@bar.com', {}, 'facebook', 1, {});
Meteor.accounts.updateOrCreateUser('foo@bar.com', {}, 'google', 2, {});
test.equal(
Meteor.users.findOne({emails: 'foo@bar.com'}).services.facebook.id, 1);
test.equal(
Meteor.users.findOne({emails: 'foo@bar.com'}).services.google.id, 2);
// test that if the user changes their email on the login service
// we store the new one in addition to the old one
Meteor.accounts.updateOrCreateUser('foo2@bar.com', {}, 'facebook', 1, {});
test.equal(
Meteor.users.findOne({emails: 'foo@bar.com'}).emails,
['foo@bar.com', 'foo2@bar.com']);
// users with no email (such as on weibo)
Meteor.users.remove({});
Meteor.accounts.updateOrCreateUser(null, {foo: 1}, 'weibo', 1, {});
Meteor.accounts.updateOrCreateUser(null, {bar: 2}, 'weibo', 1, {});
test.equal(Meteor.users.find().count(), 1);
test.equal(Meteor.users.findOne().foo, 1);
test.equal(Meteor.users.findOne().bar, 2);
test.equal(Meteor.users.findOne().emails, []);
});