From cfba9458c494ea95eeef454742757091aa683b21 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Tue, 20 Dec 2022 18:06:24 -0300 Subject: [PATCH] tests: formated and solved add email & findUserByEmail --- packages/accounts-password/password_tests.js | 572 ++++++++++--------- 1 file changed, 295 insertions(+), 277 deletions(-) diff --git a/packages/accounts-password/password_tests.js b/packages/accounts-password/password_tests.js index 09230abd87..6080c6d012 100644 --- a/packages/accounts-password/password_tests.js +++ b/packages/accounts-password/password_tests.js @@ -14,16 +14,16 @@ const simplePollAsync = new Promise((resolve, reject) => simplePoll(testFn, () => { - resolve(true) - }, () => { - reject(false) - })) + resolve(true) + }, () => { + reject(false) + })) function hashPassword(password) { return { digest: SHA256(password), - algorithm: "sha-256" + algorithm: "sha-256" }; } @@ -66,11 +66,11 @@ if (Meteor.isClient && false) (() => { const createUserStep = function (test, expect) { // Hack because Tinytest does not clean the database between tests/runs this.randomSuffix = Random.id(10); - this.username = `AdaLovelace${this.randomSuffix}`; - this.email = `Ada-intercept@lovelace.com${this.randomSuffix}`; + this.username = `AdaLovelace${ this.randomSuffix }`; + this.email = `Ada-intercept@lovelace.com${ this.randomSuffix }`; this.password = 'password'; Accounts.createUser( - {username: this.username, email: this.email, password: this.password}, + { username: this.username, email: this.email, password: this.password }, loggedInAs(this.username, test, expect)); }; const logoutStep = (test, expect) => @@ -135,11 +135,11 @@ if (Meteor.isClient && false) (() => { function (test, expect) { // setup this.username = Random.id(); - this.email = `${Random.id()}-intercept@example.com`; + this.email = `${ Random.id() }-intercept@example.com`; this.password = 'password'; Accounts.createUser( - {username: this.username, email: this.email, password: this.password}, + { username: this.username, email: this.email, password: this.password }, loggedInAs(this.username, test, expect)); }, function (test, expect) { @@ -148,7 +148,7 @@ if (Meteor.isClient && false) (() => { logoutStep, function (test, expect) { Meteor.loginWithPassword(this.username, this.password, - loggedInAs(this.username, test, expect)); + loggedInAs(this.username, test, expect)); }, logoutStep, // This next step tests reactive contexts which are reactive on @@ -176,18 +176,18 @@ if (Meteor.isClient && false) (() => { }, logoutStep, function (test, expect) { - Meteor.loginWithPassword({username: this.username}, this.password, - loggedInAs(this.username, test, expect)); + Meteor.loginWithPassword({ username: this.username }, this.password, + loggedInAs(this.username, test, expect)); }, logoutStep, function (test, expect) { Meteor.loginWithPassword(this.email, this.password, - loggedInAs(this.username, test, expect)); + loggedInAs(this.username, test, expect)); }, logoutStep, function (test, expect) { - Meteor.loginWithPassword({email: this.email}, this.password, - loggedInAs(this.username, test, expect)); + Meteor.loginWithPassword({ email: this.email }, this.password, + loggedInAs(this.username, test, expect)); }, logoutStep ]); @@ -198,65 +198,65 @@ if (Meteor.isClient && false) (() => { // We should be able to log in with the username in lower case function (test, expect) { Meteor.loginWithPassword( - { username: `adalovelace${this.randomSuffix}` }, + { username: `adalovelace${ this.randomSuffix }` }, this.password, loggedInAs(this.username, test, expect)); } ]); testAsyncMulti("passwords - logging in with case insensitive username " + - "with non-ASCII characters", [ + "with non-ASCII characters", [ function (test, expect) { // Hack because Tinytest does not clean the database between tests/runs this.randomSuffix = Random.id(10); - this.username = `ÁdaLØvela😈e${this.randomSuffix}`; + this.username = `ÁdaLØvela😈e${ this.randomSuffix }`; this.password = 'password'; Accounts.createUser( - {username: this.username, email: this.email, password: this.password}, + { username: this.username, email: this.email, password: this.password }, loggedInAs(this.username, test, expect)); }, logoutStep, // We should be able to log in with the username in lower case function (test, expect) { Meteor.loginWithPassword( - { username: `ádaløvela😈e${this.randomSuffix}` }, + { username: `ádaløvela😈e${ this.randomSuffix }` }, this.password, loggedInAs(this.username, test, expect)); } ]); testAsyncMulti("passwords - logging in with case insensitive username " + - "should escape regex special characters", [ + "should escape regex special characters", [ createUserStep, logoutStep, // We shouldn't be able to log in with a regex expression for the username function (test, expect) { Meteor.loginWithPassword( - { username: `.+${this.randomSuffix}` }, + { username: `.+${ this.randomSuffix }` }, this.password, expectUserNotFound(test, expect)); } ]); testAsyncMulti("passwords - logging in with case insensitive username " + - "should require a match of the full string", [ + "should require a match of the full string", [ createUserStep, logoutStep, // We shouldn't be able to log in with a partial match for the username function (test, expect) { Meteor.loginWithPassword( - { username: `lovelace${this.randomSuffix}` }, + { username: `lovelace${ this.randomSuffix }` }, this.password, expectUserNotFound(test, expect)); } ]); testAsyncMulti("passwords - logging in with case insensitive username when " + - "there are multiple matches", [ + "there are multiple matches", [ createUserStep, logoutStep, function (test, expect) { - this.otherUsername = `Adalovelace${this.randomSuffix}`; + this.otherUsername = `Adalovelace${ this.randomSuffix }`; addSkipCaseInsensitiveChecksForTest(this.otherUsername, test, expect); }, // Create another user with a username that only differs in case @@ -271,7 +271,7 @@ if (Meteor.isClient && false) (() => { // We shouldn't be able to log in with the username in lower case function (test, expect) { Meteor.loginWithPassword( - { username: `adalovelace${this.randomSuffix}` }, + { username: `adalovelace${ this.randomSuffix }` }, this.password, expectUserNotFound(test, expect)); }, @@ -285,13 +285,13 @@ if (Meteor.isClient && false) (() => { ]); testAsyncMulti("passwords - creating users with the same case insensitive " + - "username", [ + "username", [ createUserStep, logoutStep, // Attempting to create another user with a username that only differs in // case should fail function (test, expect) { - this.newUsername = `adalovelace${this.randomSuffix}`; + this.newUsername = `adalovelace${ this.randomSuffix }`; Accounts.createUser( { username: this.newUsername, password: this.password }, expectError( @@ -305,7 +305,7 @@ if (Meteor.isClient && false) (() => { { username: this.newUsername }, expect(function (error, result) { test.equal(result, 0); - })); + })); } ]); @@ -315,53 +315,55 @@ if (Meteor.isClient && false) (() => { // We should be able to log in with the email in lower case function (test, expect) { Meteor.loginWithPassword( - { email: `ada-intercept@lovelace.com${this.randomSuffix}` }, + { email: `ada-intercept@lovelace.com${ this.randomSuffix }` }, this.password, loggedInAs(this.username, test, expect)); } ]); testAsyncMulti("passwords - logging in with case insensitive email should " + - "escape regex special characters", [ + "escape regex special characters", [ createUserStep, logoutStep, // We shouldn't be able to log in with a regex expression for the email function (test, expect) { Meteor.loginWithPassword( - { email: `.+${this.randomSuffix}` }, + { email: `.+${ this.randomSuffix }` }, this.password, expectUserNotFound(test, expect)); } ]); testAsyncMulti("passwords - logging in with case insensitive email should " + - "require a match of the full string", [ + "require a match of the full string", [ createUserStep, logoutStep, // We shouldn't be able to log in with a partial match for the email function (test, expect) { Meteor.loginWithPassword( - { email: `com${this.randomSuffix}` }, + { email: `com${ this.randomSuffix }` }, this.password, expectUserNotFound(test, expect)); } ]); testAsyncMulti("passwords - logging in with case insensitive email when " + - "there are multiple matches", [ + "there are multiple matches", [ createUserStep, logoutStep, function (test, expect) { - this.otherUsername = `AdaLovelace${Random.id(10)}`; - this.otherEmail = `ADA-intercept@lovelace.com${this.randomSuffix}`; + this.otherUsername = `AdaLovelace${ Random.id(10) }`; + this.otherEmail = `ADA-intercept@lovelace.com${ this.randomSuffix }`; addSkipCaseInsensitiveChecksForTest(this.otherEmail, test, expect); }, // Create another user with an email that only differs in case function (test, expect) { Accounts.createUser( - { username: this.otherUsername, + { + username: this.otherUsername, email: this.otherEmail, - password: this.password }, + password: this.password + }, loggedInAs(this.otherUsername, test, expect)); }, function (test, expect) { @@ -371,7 +373,7 @@ if (Meteor.isClient && false) (() => { // We shouldn't be able to log in with the email in lower case function (test, expect) { Meteor.loginWithPassword( - { email: `ada-intercept@lovelace.com${this.randomSuffix}` }, + { email: `ada-intercept@lovelace.com${ this.randomSuffix }` }, this.password, expectUserNotFound(test, expect)); }, @@ -385,20 +387,20 @@ if (Meteor.isClient && false) (() => { ]); testAsyncMulti("passwords - creating users with the same case insensitive " + - "email", [ + "email", [ createUserStep, logoutStep, // Create user error without callback should throw error function (test, expect) { - this.newUsername = `adalovelace${this.randomSuffix}`; - test.throws(function(){ + this.newUsername = `adalovelace${ this.randomSuffix }`; + test.throws(function () { Accounts.createUser({ username: this.newUsername, password: '' }); }, /Password may not be empty/); }, // Attempting to create another user with an email that only differs in // case should fail function (test, expect) { - this.newEmail = `ada-intercept@lovelace.com${this.randomSuffix}`; + this.newEmail = `ada-intercept@lovelace.com${ this.randomSuffix }`; Accounts.createUser( { email: this.newEmail, password: this.password }, expectError( @@ -410,7 +412,7 @@ if (Meteor.isClient && false) (() => { function (test, expect) { Meteor.callAsync('countUsersOnServer', { 'emails.address': this.newEmail }, - expect (function (error, result) { + expect(function (error, result) { test.equal(result, 0); }) ); @@ -421,7 +423,7 @@ if (Meteor.isClient && false) (() => { function (test, expect) { // setup this.username = Random.id(); - this.email = `${Random.id()}-intercept@example.com`; + this.email = `${ Random.id() }-intercept@example.com`; this.password = 'password'; this.password2 = 'password2'; @@ -434,8 +436,8 @@ if (Meteor.isClient && false) (() => { function (test, expect) { Meteor.call("forgotPassword", { email: this.email }, expect(error => { - test.isFalse(error); - })); + test.isFalse(error); + })); }, function (test, expect) { Meteor.callAsync("getResetToken", expect((err, token) => { @@ -461,7 +463,7 @@ if (Meteor.isClient && false) (() => { // change password with good old password. function (test, expect) { Accounts.changePassword(this.password, this.password2, - loggedInAs(this.username, test, expect)); + loggedInAs(this.username, test, expect)); }, function (test, expect) { Meteor.callAsync("getResetToken", expect((err, token) => { @@ -480,7 +482,7 @@ if (Meteor.isClient && false) (() => { // new password, success function (test, expect) { Meteor.loginWithPassword(this.email, this.password2, - loggedInAs(this.username, test, expect)); + loggedInAs(this.username, test, expect)); }, logoutStep ]); @@ -488,7 +490,7 @@ if (Meteor.isClient && false) (() => { testAsyncMulti("passwords - changing password logs out other clients", [ function (test, expect) { this.username = Random.id(); - this.email = `${Random.id()}-intercept@example.com`; + this.email = `${ Random.id() }-intercept@example.com`; this.password = 'password'; this.password2 = 'password2'; Accounts.createUser( @@ -500,21 +502,21 @@ if (Meteor.isClient && false) (() => { function (test, expect) { this.secondConn = DDP.connect(Meteor.absoluteUrl()); this.secondConn.call('login', - { user: { username: this.username }, password: hashPassword(this.password) }, - expect((err, result) => { - test.isFalse(err); - this.secondConn.setUserId(result.id); - test.isTrue(this.secondConn.userId()); + { user: { username: this.username }, password: hashPassword(this.password) }, + expect((err, result) => { + test.isFalse(err); + this.secondConn.setUserId(result.id); + test.isTrue(this.secondConn.userId()); - this.secondConn.onReconnect = () => - this.secondConn.apply( - 'login', - [{ resume: result.token }], - { wait: true }, - (err, result) => - this.secondConn.setUserId(result && result.id || null) - ); - })); + this.secondConn.onReconnect = () => + this.secondConn.apply( + 'login', + [{ resume: result.token }], + { wait: true }, + (err, result) => + this.secondConn.setUserId(result && result.id || null) + ); + })); }, function (test, expect) { Accounts.changePassword( @@ -575,7 +577,8 @@ if (Meteor.isClient && false) (() => { 'Method call should have 2 arguments since no callback is passed in' ); - Accounts.forgotPassword({ email: 'test@meteor.com' }, () => {}); + Accounts.forgotPassword({ email: 'test@meteor.com' }, () => { + }); test.equal( methodCallArgumentCount, 3, @@ -648,27 +651,31 @@ if (Meteor.isClient && false) (() => { function (test, expect) { // setup this.username = Random.id(); - this.email = `${Random.id()}-intercept@example.com`; + this.email = `${ Random.id() }-intercept@example.com`; this.password = 'password'; }, // test Accounts.validateNewUser - function(test, expect) { + function (test, expect) { Accounts.createUser( - {username: this.username, password: this.password, - // should fail the new user validators - profile: {invalid: true}}, + { + username: this.username, password: this.password, + // should fail the new user validators + profile: { invalid: true } + }, expect(error => { test.equal(error.error, 403); test.equal(error.reason, "User validation failed"); })); }, logoutStep, - function(test, expect) { + function (test, expect) { Accounts.createUser( - {username: this.username, password: this.password, - // should fail the new user validator with a special - // exception - profile: {invalidAndThrowException: true}}, + { + username: this.username, password: this.password, + // should fail the new user validator with a special + // exception + profile: { invalidAndThrowException: true } + }, expect(error => test.equal( error.reason, @@ -678,13 +685,15 @@ if (Meteor.isClient && false) (() => { ); }, // test Accounts.onCreateUser - function(test, expect) { + function (test, expect) { Accounts.createUser( - {username: this.username, password: this.password, - testOnCreateUserHook: true}, + { + username: this.username, password: this.password, + testOnCreateUserHook: true + }, loggedInAs(this.username, test, expect)); }, - function(test, expect) { + function (test, expect) { test.equal(Meteor.user().profile.touchedByOnCreateUser, true); }, logoutStep @@ -698,14 +707,16 @@ if (Meteor.isClient && false) (() => { this.password = 'password'; Accounts.createUser( - {username: this.username, password: this.password, - testOnCreateUserHook: true}, + { + username: this.username, password: this.password, + testOnCreateUserHook: true + }, loggedInAs(this.username, test, expect)); }, // test Meteor.user(). This test properly belongs in // accounts-base/accounts_tests.js, but this is where the tests that // actually log in are. - function(test, expect) { + function (test, expect) { const clientUser = Meteor.user(); Accounts.connection.call('testMeteorUser', expect((err, result) => { test.equal(result._id, clientUser._id); @@ -715,16 +726,16 @@ if (Meteor.isClient && false) (() => { test.equal(err, undefined); })); }, - function(test, expect) { + function (test, expect) { // Test that even with no published fields, we still have a document. Accounts.connection.call('clearUsernameAndProfile', expect(() => { test.isTrue(Meteor.userId()); const user = Meteor.user(); - test.equal(user, {_id: Meteor.userId()}); + test.equal(user, { _id: Meteor.userId() }); })); }, logoutStep, - function(test, expect) { + function (test, expect) { const clientUser = Meteor.user(); test.equal(clientUser, null); test.equal(Meteor.userId(), null); @@ -740,8 +751,10 @@ if (Meteor.isClient && false) (() => { function (test, expect) { this.otherUsername = Random.id(); Accounts.createUser( - {username: this.otherUsername, password: 'dontcare', - testOnCreateUserHook: true}, + { + username: this.otherUsername, password: 'dontcare', + testOnCreateUserHook: true + }, loggedInAs(this.otherUsername, test, expect)); }, function (test, expect) { @@ -753,20 +766,22 @@ if (Meteor.isClient && false) (() => { this.password = 'password'; Accounts.createUser( - {username: this.username, password: this.password, - testOnCreateUserHook: true}, + { + username: this.username, password: this.password, + testOnCreateUserHook: true + }, loggedInAs(this.username, test, expect)); }, // test the default Meteor.users allow rule. This test properly belongs in // accounts-base/accounts_tests.js, but this is where the tests that // actually log in are. - function(test, expect) { + function (test, expect) { this.userId = Meteor.userId(); test.notEqual(this.userId, null); test.notEqual(this.userId, this.otherUserId); // Can't update fields other than profile. Meteor.users.update( - this.userId, {$set: {disallowed: true, 'profile.updated': 42}}, + this.userId, { $set: { disallowed: true, 'profile.updated': 42 } }, expect(err => { test.isTrue(err); test.equal(err.error, 403); @@ -774,26 +789,26 @@ if (Meteor.isClient && false) (() => { test.isFalse(Object.prototype.hasOwnProperty.call(Meteor.user().profile, 'updated')); })); }, - function(test, expect) { + function (test, expect) { // Can't update another user. Meteor.users.update( - this.otherUserId, {$set: {'profile.updated': 42}}, + this.otherUserId, { $set: { 'profile.updated': 42 } }, expect(err => { test.isTrue(err); test.equal(err.error, 403); })); }, - function(test, expect) { + function (test, expect) { // Can't update using a non-ID selector. (This one is thrown client-side.) test.throws(() => Meteor.users.update( - {username: this.username}, {$set: {'profile.updated': 42}} + { username: this.username }, { $set: { 'profile.updated': 42 } } )); test.isFalse(Object.prototype.hasOwnProperty.call(Meteor.user().profile, 'updated')); }, - function(test, expect) { + function (test, expect) { // Can update own profile using ID. Meteor.users.update( - this.userId, {$set: {'profile.updated': 42}}, + this.userId, { $set: { 'profile.updated': 42 } }, expect(err => { test.isFalse(err); test.equal(42, Meteor.user().profile.updated); @@ -810,7 +825,7 @@ if (Meteor.isClient && false) (() => { this.password = 'password'; Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, @@ -881,7 +896,7 @@ if (Meteor.isClient && false) (() => { Meteor.logoutOtherClients(err => { test.isFalse(err); secondConn.call('login', { resume: token }, - expectSecondConnLoggedOut); + expectSecondConnLoggedOut); Accounts.connection.call('login', { resume: Accounts._storedLoginToken() }, expectAccountsConnLoggedIn); @@ -896,7 +911,7 @@ if (Meteor.isClient && false) (() => { token = Accounts._storedLoginToken(); test.isTrue(token); secondConn.call('login', { resume: token }, - expectSecondConnLoggedIn); + expectSecondConnLoggedIn); }) ); }, @@ -909,7 +924,7 @@ if (Meteor.isClient && false) (() => { this.password = "password"; Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, logoutStep, @@ -958,7 +973,7 @@ if (Meteor.isClient && false) (() => { this.password = "password"; Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, function (test, expect) { @@ -986,7 +1001,7 @@ if (Meteor.isClient && false) (() => { }); Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, function (test, expect) { @@ -1005,7 +1020,7 @@ if (Meteor.isClient && false) (() => { this.password = "password"; Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, logoutStep, @@ -1028,13 +1043,14 @@ if (Meteor.isClient && false) (() => { this.onLogout = Accounts.onLogout(() => this.logoutSuccess = true); Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, logoutStep, function (test, expect) { test.isTrue(this.logoutSuccess); - expect(function() {})(); + expect(function () { + })(); } ]); @@ -1044,7 +1060,7 @@ if (Meteor.isClient && false) (() => { this.password = "password"; Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, logoutStep, @@ -1103,7 +1119,7 @@ if (Meteor.isClient && false) (() => { this.onLoginFailure = Accounts.onLoginFailure(() => this.attempt = true); Accounts.createUser( - {username: this.username, password: this.password}, + { username: this.username, password: this.password }, loggedInAs(this.username, test, expect)); }, logoutStep, @@ -1123,13 +1139,13 @@ if (Meteor.isClient && false) (() => { expect(() => ({}))(); } ]); -}) (); +})(); if (Meteor.isServer) (() => { Tinytest.add('passwords - setup more than one onCreateUserHook', test => { - test.throws(() => Accounts.onCreateUser(() => ({}))); + test.throws(() => Accounts.onCreateUser(() => ({}))); }); @@ -1153,9 +1169,9 @@ if (Meteor.isServer) (() => { 'passwords - setPassword', async test => { const username = Random.id(); - const email = `${username}-intercept@example.com`; + const email = `${ username }-intercept@example.com`; - const userId = await Accounts.createUser({username: username, email: email}); + const userId = await Accounts.createUser({ username: username, email: email }); let user = await Meteor.users.findOne(userId); // no services yet. @@ -1175,7 +1191,7 @@ if (Meteor.isServer) (() => { test.isTrue(user2.services.resume.loginTokens); // reset with the same password, see we get a different salted hash - await Accounts.setPasswordAsync(userId, 'new password', {logout: false}); + await Accounts.setPasswordAsync(userId, 'new password', { logout: false }); user = await Meteor.users.findOne(userId); const newSaltedHash = user.services.password.bcrypt; test.isTrue(newSaltedHash); @@ -1266,7 +1282,7 @@ if (Meteor.isServer) (() => { "passwords - reset password doesn't work if email changed after email sent", async test => { const username = Random.id() + 'reset-password-doesnt-work-if-email-changed' - const email = `${username}-intercept@example.com`; + const email = `${ username }-intercept@example.com`; const userId = await Accounts.createUser({ username: username, @@ -1285,23 +1301,23 @@ if (Meteor.isServer) (() => { test.isTrue(match); const resetPasswordToken = match[1]; - const newEmail = `${Random.id()}-new@example.com`; - await Meteor.users.update(userId, {$set: {"emails.0.address": newEmail}}); + const newEmail = `${ Random.id() }-new@example.com`; + await Meteor.users.update(userId, { $set: { "emails.0.address": newEmail } }); await test.throwsAsync( async () => - await Meteor.callAsync("resetPassword", resetPasswordToken, hashPassword("new-password")), + await Meteor.callAsync("resetPassword", resetPasswordToken, hashPassword("new-password")), /Token has invalid email address/ ); await test.throwsAsync( async () => await Meteor.callAsync( - "login", - { - user: { username: username }, - password: hashPassword("new-password") - } - ), + "login", + { + user: { username: username }, + password: hashPassword("new-password") + } + ), /Incorrect password/); }); @@ -1408,13 +1424,13 @@ if (Meteor.isServer) (() => { 'Something went wrong. Please check your credentials' ) - Accounts._options.ambiguousErrorMessages = false - await test.throwsAsync( - async ()=> await Meteor.callAsync('forgotPassword', wrongOptions), - /Can't find user/ - ) - // return accounts as it were - Accounts._options = options + Accounts._options.ambiguousErrorMessages = false + await test.throwsAsync( + async () => await Meteor.callAsync('forgotPassword', wrongOptions), + /Can't find user/ + ) + // return accounts as it were + Accounts._options = options }); Tinytest.addAsync( @@ -1466,7 +1482,7 @@ if (Meteor.isServer) (() => { 'passwords - enroll password should work when token is not expired', async test => { const username = Random.id(); - const email = `${username}-intercept@example.com`; + const email = `${ username }-intercept@example.com`; const userId = await Accounts.createUser({ username: username, @@ -1506,7 +1522,7 @@ if (Meteor.isServer) (() => { 'passwords - enroll password should not work when token is expired', async test => { const username = Random.id(); - const email = `${username}-intercept@example.com`; + const email = `${ username }-intercept@example.com`; const userId = await Accounts.createUser({ username: username, @@ -1515,7 +1531,7 @@ if (Meteor.isServer) (() => { const user = await Meteor.users.findOne(userId); - await Accounts.sendEnrollmentEmail(userId, email); + await Accounts.sendEnrollmentEmail(userId, email); const [enrollPasswordEmailOptions] = await Meteor.callAsync("getInterceptedEmails", email); @@ -1525,7 +1541,7 @@ if (Meteor.isServer) (() => { test.isTrue(match); const enrollPasswordToken = match[1]; - await Meteor.users.update(userId, {$set: {"services.password.enroll.when": new Date(Date.now() + -35 * 24 * 3600 * 1000) }}); + await Meteor.users.update(userId, { $set: { "services.password.enroll.when": new Date(Date.now() + -35 * 24 * 3600 * 1000) } }); await test.throwsAsync( async () => await Meteor.callAsync("resetPassword", enrollPasswordToken, hashPassword("new-password")), @@ -1534,23 +1550,23 @@ if (Meteor.isServer) (() => { }); Tinytest.addAsync('passwords - enroll tokens get cleaned up', - async test => { - const email = `${test.id}-intercept@example.com`; - const userId = - await Accounts.createUser({email: email, password: hashPassword('password')}); + async test => { + const email = `${ test.id }-intercept@example.com`; + const userId = + await Accounts.createUser({ email: email, password: hashPassword('password') }); - await Accounts.sendEnrollmentEmail(userId, email); - const user1 = await Meteor.users.findOne(userId); - test.isTrue(!!user1.services.password.enroll); - await Accounts._expirePasswordEnrollTokens(new Date(), userId); - const user2 = await Meteor.users.findOne(userId); - test.isUndefined(user2.services.password.enroll); - }); + await Accounts.sendEnrollmentEmail(userId, email); + const user1 = await Meteor.users.findOne(userId); + test.isTrue(!!user1.services.password.enroll); + await Accounts._expirePasswordEnrollTokens(new Date(), userId); + const user2 = await Meteor.users.findOne(userId); + test.isUndefined(user2.services.password.enroll); + }); Tinytest.addAsync( "passwords - enroll tokens don't get cleaned up when reset tokens are cleaned up", async test => { - const email = `${test.id}-intercept@example.com`; + const email = `${ test.id }-intercept@example.com`; const userId = await Accounts.createUser({ email: email, @@ -1559,7 +1575,7 @@ if (Meteor.isServer) (() => { await Accounts.sendEnrollmentEmail(userId, email); const user1 = await Meteor.users.findOne(userId); - const enrollToken = user1.services.password.enroll; + const enrollToken = user1.services.password.enroll; test.isTrue(enrollToken); await Accounts._expirePasswordResetTokens(new Date(), userId); @@ -1589,47 +1605,47 @@ if (Meteor.isServer) (() => { // We should be able to change the username Tinytest.addAsync("passwords - change username & findUserByUsername", async test => { - const username = Random.id(); - const ignoreFieldName = "profile"; - const userId = await Accounts.createUser({ - username, - [ignoreFieldName]: {name: 'foo'}, + const username = Random.id(); + const ignoreFieldName = "profile"; + const userId = await Accounts.createUser({ + username, + [ignoreFieldName]: { name: 'foo' }, + }); + + test.isTrue(userId); + + const newUsername = Random.id(); + await Accounts.setUsername(userId, newUsername); + const u1 = await Accounts._findUserByQuery({ id: userId }) + test.equal(u1.username, newUsername); + + // Test findUserByUsername as well while we're here + let user = await Accounts.findUserByUsername(newUsername); + test.equal(user._id, userId, 'userId - ignore'); + test.isNotUndefined(user[ignoreFieldName], 'field - no ignore'); + + // Test default field selector + const options = Accounts._options; + Accounts._options = { defaultFieldSelector: { [ignoreFieldName]: 0 } }; + user = await Accounts.findUserByUsername(newUsername); + test.equal(user.username, newUsername, 'username - default ignore'); + test.isUndefined(user[ignoreFieldName], 'field - default ignore'); + + // Test default field selector over-ride + user = await Accounts.findUserByUsername(newUsername, { + fields: { + [ignoreFieldName]: 1 + } + }); + test.isUndefined(user.username, 'username - override'); + test.isNotUndefined(user[ignoreFieldName], 'field - override'); + + Accounts._options = options; }); - test.isTrue(userId); - - const newUsername = Random.id(); - await Accounts.setUsername(userId, newUsername); - const u1 = await Accounts._findUserByQuery({id: userId}) - test.equal(u1.username, newUsername); - - // Test findUserByUsername as well while we're here - let user = await Accounts.findUserByUsername(newUsername); - test.equal(user._id, userId, 'userId - ignore'); - test.isNotUndefined(user[ignoreFieldName], 'field - no ignore'); - - // Test default field selector - const options = Accounts._options; - Accounts._options = {defaultFieldSelector: {[ignoreFieldName]: 0}}; - user = await Accounts.findUserByUsername(newUsername); - test.equal(user.username, newUsername, 'username - default ignore'); - test.isUndefined(user[ignoreFieldName], 'field - default ignore'); - - // Test default field selector over-ride - user = await Accounts.findUserByUsername(newUsername, { - fields: { - [ignoreFieldName]: 1 - } - }); - test.isUndefined(user.username, 'username - override'); - test.isNotUndefined(user[ignoreFieldName], 'field - override'); - - Accounts._options = options; - }); - Tinytest.addAsync("passwords - change username to a new one only differing " + - "in case", async test => { - const username = `${Random.id()}user`; + "in case", async test => { + const username = `${ Random.id() }user`; const userId = await Accounts.createUser({ username: username.toUpperCase() }); @@ -1638,125 +1654,127 @@ if (Meteor.isServer) (() => { const newUsername = username.toLowerCase(); await Accounts.setUsername(userId, newUsername); - const u1 = await Accounts._findUserByQuery({id: userId}) + const u1 = await Accounts._findUserByQuery({ id: userId }) test.equal(u1.username, newUsername); }); // We should not be able to change the username to one that only // differs in case from an existing one Tinytest.addAsync("passwords - change username should fail when there are " + - "existing users with a username only differing in case", async test => { - const username = `${Random.id()}user`; - const usernameUpper = username.toUpperCase(); + "existing users with a username only differing in case", + async test => { + const username = `${ Random.id() }user`; + const usernameUpper = username.toUpperCase(); - const userId1 = await Accounts.createUser({ - username: username + const userId1 = await Accounts.createUser({ + username: username + }); + + const user2OriginalUsername = Random.id(); + const userId2 = await Accounts.createUser({ + username: user2OriginalUsername + }); + + test.isTrue(userId1); + test.isTrue(userId2); + + await test.throwsAsync( + async () => await Accounts.setUsername(userId2, usernameUpper), + /Username already exists/ + ); + + const u2 = await Accounts._findUserByQuery({ id: userId2 }) + test.equal(u2.username, user2OriginalUsername); }); - const user2OriginalUsername = Random.id(); - const userId2 = await Accounts.createUser({ - username: user2OriginalUsername + Tinytest.addAsync("passwords - add email & findUserByEmail", + async test => { + const origEmail = `${ Random.id() }@turing.com`; + const username = Random.id(); + const ignoreFieldName = "profile"; + const userId = await Accounts.createUser({ + email: origEmail, + username, + [ignoreFieldName]: { name: 'foo' }, + }); + + const newEmail = `${ Random.id() }@turing.com`; + await Accounts.addEmail(userId, newEmail); + + const thirdEmail = `${ Random.id() }@turing.com`; + await Accounts.addEmail(userId, thirdEmail, true); + const u1 = await Accounts._findUserByQuery({ id: userId }) + test.equal(u1.emails, [ + { address: origEmail, verified: false }, + { address: newEmail, verified: false }, + { address: thirdEmail, verified: true } + ]); + + // Test findUserByEmail as well while we're here + let user = await Accounts.findUserByEmail(origEmail); + test.equal(user._id, userId); + test.isNotUndefined(user[ignoreFieldName], 'field - no ignore'); + + // Test default field selector + const options = Accounts._options; + Accounts._options = { defaultFieldSelector: { [ignoreFieldName]: 0 } }; + user = await Accounts.findUserByEmail(origEmail); + test.equal(user.username, username, 'username - default ignore'); + test.isUndefined(user[ignoreFieldName], 'field - default ignore'); + + // Test default field selector over-ride + user = await Accounts.findUserByEmail(origEmail, { + fields: { + [ignoreFieldName]: 1 + } + }); + test.equal(user._id, userId, 'userId - override'); + test.isUndefined(user.username, 'username - override'); + test.isNotUndefined(user[ignoreFieldName], 'field - override'); + + Accounts._options = options; }); - test.isTrue(userId1); - test.isTrue(userId2); - - await test.throwsAsync( - async () => await Accounts.setUsername(userId2, usernameUpper), - /Username already exists/ - ); - - const u2 = await Accounts._findUserByQuery({id: userId2}) - test.equal(u2.username, user2OriginalUsername); - }); - - Tinytest.add("passwords - add email & findUserByEmail", test => { - const origEmail = `${Random.id()}@turing.com`; - const username = Random.id(); - const ignoreFieldName = "profile"; - const userId = Accounts.createUser({ - email: origEmail, - username, - [ignoreFieldName]: {name: 'foo'}, - }); - - const newEmail = `${Random.id()}@turing.com`; - Accounts.addEmail(userId, newEmail); - - const thirdEmail = `${Random.id()}@turing.com`; - Accounts.addEmail(userId, thirdEmail, true); - - test.equal(Accounts._findUserByQuery({id: userId}).emails, [ - { address: origEmail, verified: false }, - { address: newEmail, verified: false }, - { address: thirdEmail, verified: true } - ]); - - // Test findUserByEmail as well while we're here - let user = Accounts.findUserByEmail(origEmail); - test.equal(user._id, userId); - test.isNotUndefined(user[ignoreFieldName], 'field - no ignore'); - - // Test default field selector - const options = Accounts._options; - Accounts._options = {defaultFieldSelector: {[ignoreFieldName]: 0}}; - user = Accounts.findUserByEmail(origEmail); - test.equal(user.username, username, 'username - default ignore'); - test.isUndefined(user[ignoreFieldName], 'field - default ignore'); - - // Test default field selector over-ride - user = Accounts.findUserByEmail(origEmail, { - fields: { - [ignoreFieldName]: 1 - } - }); - test.equal(user._id, userId, 'userId - override'); - test.isUndefined(user.username, 'username - override'); - test.isNotUndefined(user[ignoreFieldName], 'field - override'); - - Accounts._options = options; - }); - Tinytest.add("passwords - add email when user has not an existing email", test => { const userId = Accounts.createUser({ - username: `user${Random.id()}` + username: `user${ Random.id() }` }); - const newEmail = `${Random.id()}@turing.com`; + const newEmail = `${ Random.id() }@turing.com`; Accounts.addEmail(userId, newEmail); - test.equal(Accounts._findUserByQuery({id: userId}).emails, [ + test.equal(Accounts._findUserByQuery({ id: userId }).emails, [ { address: newEmail, verified: false }, ]); }); Tinytest.add("passwords - add email when the user has an existing email " + - "only differing in case", test => { - const origEmail = `${Random.id()}@turing.com`; + "only differing in case", test => { + const origEmail = `${ Random.id() }@turing.com`; const userId = Accounts.createUser({ email: origEmail }); - const newEmail = `${Random.id()}@turing.com`; + const newEmail = `${ Random.id() }@turing.com`; Accounts.addEmail(userId, newEmail); const thirdEmail = origEmail.toUpperCase(); Accounts.addEmail(userId, thirdEmail, true); - test.equal(Accounts._findUserByQuery({id: userId}).emails, [ + test.equal(Accounts._findUserByQuery({ id: userId }).emails, [ { address: thirdEmail, verified: true }, { address: newEmail, verified: false } ]); }); Tinytest.add("passwords - add email should fail when there is an existing " + - "user with an email only differing in case", test => { - const user1Email = `${Random.id()}@turing.com`; + "user with an email only differing in case", test => { + const user1Email = `${ Random.id() }@turing.com`; const userId1 = Accounts.createUser({ email: user1Email }); - const user2Email = `${Random.id()}@turing.com`; + const user2Email = `${ Random.id() }@turing.com`; const userId2 = Accounts.createUser({ email: user2Email }); @@ -1767,28 +1785,28 @@ if (Meteor.isServer) (() => { /Email already exists/ ); - test.equal(Accounts._findUserByQuery({id: userId1}).emails, [ + test.equal(Accounts._findUserByQuery({ id: userId1 }).emails, [ { address: user1Email, verified: false } ]); - test.equal(Accounts._findUserByQuery({id: userId2}).emails, [ + test.equal(Accounts._findUserByQuery({ id: userId2 }).emails, [ { address: user2Email, verified: false } ]); }); Tinytest.add("passwords - remove email", test => { - const origEmail = `${Random.id()}@turing.com`; + const origEmail = `${ Random.id() }@turing.com`; const userId = Accounts.createUser({ email: origEmail }); - const newEmail = `${Random.id()}@turing.com`; + const newEmail = `${ Random.id() }@turing.com`; Accounts.addEmail(userId, newEmail); - const thirdEmail = `${Random.id()}@turing.com`; + const thirdEmail = `${ Random.id() }@turing.com`; Accounts.addEmail(userId, thirdEmail, true); - test.equal(Accounts._findUserByQuery({id: userId}).emails, [ + test.equal(Accounts._findUserByQuery({ id: userId }).emails, [ { address: origEmail, verified: false }, { address: newEmail, verified: false }, { address: thirdEmail, verified: true } @@ -1796,14 +1814,14 @@ if (Meteor.isServer) (() => { Accounts.removeEmail(userId, newEmail); - test.equal(Accounts._findUserByQuery({id: userId}).emails, [ + test.equal(Accounts._findUserByQuery({ id: userId }).emails, [ { address: origEmail, verified: false }, { address: thirdEmail, verified: true } ]); Accounts.removeEmail(userId, origEmail); - test.equal(Accounts._findUserByQuery({id: userId}).emails, [ + test.equal(Accounts._findUserByQuery({ id: userId }).emails, [ { address: thirdEmail, verified: true } ]); }); @@ -1856,7 +1874,7 @@ if (Meteor.isServer) (() => { Tinytest.add('passwords - extra params in email urls', (test) => { const username = Random.id(); - const email = `${username}-intercept@example.com`; + const email = `${ username }-intercept@example.com`; const userId = Accounts.createUser({ username: username, @@ -1864,7 +1882,7 @@ if (Meteor.isServer) (() => { }); const extraParams = { test: 'success' }; - Accounts.sendEnrollmentEmail(userId, email, null, extraParams); + Accounts.sendEnrollmentEmail(userId, email, null, extraParams); const [enrollPasswordEmailOptions] = Meteor.callAsync("getInterceptedEmails", email); @@ -1875,4 +1893,4 @@ if (Meteor.isServer) (() => { test.equal(url.searchParams.get('test'), extraParams.test); }); -}) (); +})();