From a3a3b10fb320992966d6b34842a969d2977f1c28 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 18 Jun 2021 11:49:01 -0400 Subject: [PATCH] test(emails): fixing broken tests introduced by e5ff68acd --- test/socket.io.js | 9 --- test/user.js | 182 ++++++++++++++++++++++++---------------------- 2 files changed, 94 insertions(+), 97 deletions(-) diff --git a/test/socket.io.js b/test/socket.io.js index 63829fc7eb..7135d5a3a9 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -274,18 +274,9 @@ describe('socket.io', () => { }); }); - it('should error if email validation is not required', (done) => { - socketAdmin.user.sendValidationEmail({ uid: adminUid }, [regularUid], (err) => { - assert.equal(err.message, '[[error:email-confirmations-are-disabled]]'); - done(); - }); - }); - it('should send validation email', (done) => { - meta.config.requireEmailConfirmation = 1; socketAdmin.user.sendValidationEmail({ uid: adminUid }, [regularUid], (err) => { assert.ifError(err); - meta.config.requireEmailConfirmation = 0; done(); }); }); diff --git a/test/user.js b/test/user.js index 2b68a4415c..2b76eaeb80 100644 --- a/test/user.js +++ b/test/user.js @@ -18,7 +18,7 @@ const Password = require('../src/password'); const groups = require('../src/groups'); const helpers = require('./helpers'); const meta = require('../src/meta'); -const plugins = require('../src/plugins'); +const events = require('../src/events'); const socketUser = require('../src/socket.io/user'); describe('User', () => { @@ -812,16 +812,14 @@ describe('User', () => { let uid; let jar; - before((done) => { - User.create({ username: 'updateprofile', email: 'update@me.com', password: '123456' }, (err, newUid) => { - assert.ifError(err); - uid = newUid; - helpers.loginUser('updateprofile', '123456', (err, _jar) => { - assert.ifError(err); - jar = _jar; - done(); - }); - }); + before(async () => { + const newUid = await User.create({ username: 'updateprofile', email: 'update@me.com', password: '123456' }); + uid = newUid; + + await User.email.confirmByUid(uid); + + const _jar = await helpers.loginUser('updateprofile', '123456'); + jar = _jar; }); it('should return error if data is invalid', (done) => { @@ -838,43 +836,62 @@ describe('User', () => { }); }); - it('should update a user\'s profile', (done) => { - User.create({ username: 'justforupdate', email: 'just@for.updated', password: '123456' }, (err, uid) => { - assert.ifError(err); - const data = { - uid: uid, - username: 'updatedUserName', - email: 'updatedEmail@me.com', - fullname: 'updatedFullname', - website: 'http://nodebb.org', - location: 'izmir', - groupTitle: 'testGroup', - birthday: '01/01/1980', - signature: 'nodebb is good', - password: '123456', - }; - socketUser.updateProfile({ uid: uid }, { ...data, password: '123456', invalid: 'field' }, (err, result) => { - assert.ifError(err); + describe('.updateProfile()', () => { + let uid; - assert.equal(result.username, 'updatedUserName'); - assert.equal(result.userslug, 'updatedusername'); - assert.equal(result.email, 'updatedEmail@me.com'); + it('should update a user\'s profile', (done) => { + User.create({ username: 'justforupdate', email: 'just@for.updated', password: '123456' }, (err, _uid) => { + uid = _uid; - db.getObject(`user:${uid}`, (err, userData) => { + assert.ifError(err); + const data = { + uid: uid, + username: 'updatedUserName', + email: 'updatedEmail@me.com', + fullname: 'updatedFullname', + website: 'http://nodebb.org', + location: 'izmir', + groupTitle: 'testGroup', + birthday: '01/01/1980', + signature: 'nodebb is good', + password: '123456', + }; + socketUser.updateProfile({ uid: uid }, { ...data, password: '123456', invalid: 'field' }, (err, result) => { assert.ifError(err); - Object.keys(data).forEach((key) => { - if (key !== 'password') { - assert.equal(data[key], userData[key]); - } else { - assert(userData[key].startsWith('$2a$')); - } + console.log(result); + + assert.equal(result.username, 'updatedUserName'); + assert.equal(result.userslug, 'updatedusername'); + assert.equal(result.location, 'izmir'); + + db.getObject(`user:${uid}`, (err, userData) => { + assert.ifError(err); + Object.keys(data).forEach((key) => { + if (key === 'email') { + assert.strictEqual(userData.email, 'just@for.updated'); // email remains the same until confirmed + } else if (key !== 'password') { + assert.equal(data[key], userData[key]); + } else { + assert(userData[key].startsWith('$2a$')); + } + }); + // updateProfile only saves valid fields + assert.strictEqual(userData.invalid, undefined); + done(); }); - // updateProfile only saves valid fields - assert.strictEqual(userData.invalid, undefined); - done(); }); }); }); + + it('should also generate an email confirmation code for the changed email', async () => { + const confirmSent = await db.get(`uid:${uid}:confirm:email:sent`); + const event = (await events.getEvents('email-confirmation-sent', 0, 0)).pop(); + console.log(event); + assert.strictEqual(parseInt(confirmSent, 10), 1); + assert(event); + assert.strictEqual(event.email, 'updatedEmail@me.com'); + assert.strictEqual(parseInt(event.uid, 10), uid); + }); }); it('should change a user\'s password', (done) => { @@ -964,7 +981,10 @@ describe('User', () => { await socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, username: longName, email: 'verylong@name.com' }); const userData = await db.getObject(`user:${uid}`); assert.strictEqual(userData.username, longName); - assert.strictEqual(userData.email, 'verylong@name.com'); + + const event = (await events.getEvents('email-confirmation-sent', 0, 0)).pop(); + assert.strictEqual(parseInt(event.uid, 10), uid); + assert.strictEqual(event.email, 'verylong@name.com'); }); it('should not update a user\'s username if it did not change', (done) => { @@ -991,18 +1011,13 @@ describe('User', () => { assert.strictEqual(_err.message, '[[error:invalid-password]]'); }); - it('should change email', (done) => { - User.create({ username: 'pooremailupdate', email: 'poor@update.me', password: '123456' }, (err, uid) => { - assert.ifError(err); - socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, email: 'updatedAgain@me.com', password: '123456' }, (err) => { - assert.ifError(err); - db.getObjectField(`user:${uid}`, 'email', (err, email) => { - assert.ifError(err); - assert.equal(email, 'updatedAgain@me.com'); - done(); - }); - }); - }); + it('should send validation email', async () => { + const uid = await User.create({ username: 'pooremailupdate', email: 'poor@update.me', password: '123456' }); + await socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, email: 'updatedAgain@me.com', password: '123456' }); + + const event = (await events.getEvents('email-confirmation-sent', 0, 0)).pop(); + assert.strictEqual(parseInt(event.uid, 10), uid); + assert.strictEqual(event.email, 'updatedAgain@me.com'); }); it('should error if email is identical', async () => { @@ -1295,32 +1310,34 @@ describe('User', () => { }); }); - it('should load edit/email page', (done) => { - request(`${nconf.get('url')}/api/user/updatedagain/edit/email`, { jar: jar, json: true }, (err, res, body) => { - assert.ifError(err); - assert.equal(res.statusCode, 200); - assert(body); - done(); + it('should load edit/email page', async () => { + const res = await requestAsync(`${nconf.get('url')}/api/user/updatedagain/edit/email`, { jar: jar, json: true, resolveWithFullResponse: true }); + assert.strictEqual(res.statusCode, 200); + assert(res.body); + + // Accessing this page will mark the user's account as needing an updated email, below code undo's. + await requestAsync({ + uri: `${nconf.get('url')}/register/abort`, + jar, + method: 'POST', + simple: false, }); }); - it('should load user\'s groups page', (done) => { - groups.create({ + it('should load user\'s groups page', async () => { + await groups.create({ name: 'Test', description: 'Foobar!', - }, (err) => { - assert.ifError(err); - groups.join('Test', uid, (err) => { - assert.ifError(err); - request(`${nconf.get('url')}/api/user/updatedagain/groups`, { jar: jar, json: true }, (err, res, body) => { - assert.ifError(err); - assert.equal(res.statusCode, 200); - assert(Array.isArray(body.groups)); - assert.equal(body.groups[0].name, 'Test'); - done(); - }); - }); }); + const derp = await User.getUserData(uid); + console.log(derp); + + await groups.join('Test', uid); + const body = await requestAsync(`${nconf.get('url')}/api/user/updatedagain/groups`, { jar: jar, json: true }); + console.log(body); + + assert(Array.isArray(body.groups)); + assert.equal(body.groups[0].name, 'Test'); }); }); @@ -1766,20 +1783,9 @@ describe('User', () => { meta.config.allowAccountDeletion = oldValue; }); - it('should error if requireEmailConfirmation is disabled', (done) => { - socketUser.emailConfirm({ uid: testUid }, {}, (err) => { - assert.equal(err.message, '[[error:email-confirmations-are-disabled]]'); - done(); - }); - }); - - it('should send email confirm', (done) => { - meta.config.requireEmailConfirmation = 1; - socketUser.emailConfirm({ uid: testUid }, {}, (err) => { - assert.ifError(err); - meta.config.requireEmailConfirmation = 0; - done(); - }); + it('should send email confirm', async () => { + await db.delete(`uid:${testUid}:confirm:email:sent`); + await socketUser.emailConfirm({ uid: testUid }, {}); }); it('should send reset email', (done) => {