From 1773c91c5022e7b9ec6809e2bbc11717b57bc2d8 Mon Sep 17 00:00:00 2001 From: Avital Oliver Date: Mon, 9 Jul 2012 15:29:41 -0700 Subject: [PATCH] Support users with no email + more tests --- packages/accounts/accounts_server.js | 9 +++------ packages/accounts/accounts_tests.js | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/accounts/accounts_server.js b/packages/accounts/accounts_server.js index d747c409c1..0def08c528 100644 --- a/packages/accounts/accounts_server.js +++ b/packages/accounts/accounts_server.js @@ -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 })); } diff --git a/packages/accounts/accounts_tests.js b/packages/accounts/accounts_tests.js index e675324ca5..a020fbb228 100644 --- a/packages/accounts/accounts_tests.js +++ b/packages/accounts/accounts_tests.js @@ -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, []); });