From 961a6650374ec990a4d857e2c8ea66851a59e3f5 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 17 Oct 2023 09:59:47 -0300 Subject: [PATCH 1/3] test: add cleanup to users tests --- apps/meteor/tests/data/api-data.js | 9 +- apps/meteor/tests/data/custom-fields.js | 18 +- apps/meteor/tests/data/users.helper.js | 17 +- apps/meteor/tests/end-to-end/api/01-users.js | 477 ++++++++++--------- 4 files changed, 280 insertions(+), 241 deletions(-) diff --git a/apps/meteor/tests/data/api-data.js b/apps/meteor/tests/data/api-data.js index 25e89c2ef99a..7df4f5c95d81 100644 --- a/apps/meteor/tests/data/api-data.js +++ b/apps/meteor/tests/data/api-data.js @@ -13,10 +13,10 @@ export function wait(cb, time) { return () => setTimeout(cb, time); } -export const apiUsername = `api${username}`; -export const apiEmail = `api${email}`; -export const apiPublicChannelName = `api${publicChannelName}`; -export const apiPrivateChannelName = `api${privateChannelName}`; +export const apiUsername = `api${username}-${Date.now()}`; +export const apiEmail = `api${email}-${Date.now()}`; +export const apiPublicChannelName = `api${publicChannelName}-${Date.now()}`; +export const apiPrivateChannelName = `api${privateChannelName}-${Date.now()}`; export const apiRoleNameUsers = `api${roleNameUsers}`; export const apiRoleNameSubscriptions = `api${roleNameSubscriptions}`; @@ -25,7 +25,6 @@ export const apiRoleScopeSubscriptions = `${roleScopeSubscriptions}`; export const apiRoleDescription = `api${roleDescription}`; export const reservedWords = ['admin', 'administrator', 'system', 'user']; -export const targetUser = {}; export const channel = {}; export const group = {}; export const message = {}; diff --git a/apps/meteor/tests/data/custom-fields.js b/apps/meteor/tests/data/custom-fields.js index 2509dddf5d84..e2e175429b4c 100644 --- a/apps/meteor/tests/data/custom-fields.js +++ b/apps/meteor/tests/data/custom-fields.js @@ -1,4 +1,4 @@ -import { getCredentials, request, api, credentials } from './api-data.js'; +import { credentials, request, api } from './api-data.js'; export const customFieldText = { type: 'text', @@ -7,18 +7,12 @@ export const customFieldText = { maxLength: 10, }; -export function setCustomFields(customFields, done) { - getCredentials((error) => { - if (error) { - return done(error); - } +export function setCustomFields(customFields) { + const stringified = customFields ? JSON.stringify(customFields) : ''; - const stringified = customFields ? JSON.stringify(customFields) : ''; - - request.post(api('settings/Accounts_CustomFields')).set(credentials).send({ value: stringified }).expect(200).end(done); - }); + return request.post(api('settings/Accounts_CustomFields')).set(credentials).send({ value: stringified }).expect(200); } -export function clearCustomFields(done = () => {}) { - setCustomFields(null, done); +export function clearCustomFields() { + return setCustomFields(null); } diff --git a/apps/meteor/tests/data/users.helper.js b/apps/meteor/tests/data/users.helper.js index 92425902cb5b..82ab8446547d 100644 --- a/apps/meteor/tests/data/users.helper.js +++ b/apps/meteor/tests/data/users.helper.js @@ -33,16 +33,13 @@ export const login = (username, password) => }); }); -export const deleteUser = (user) => - new Promise((resolve) => { - request - .post(api('users.delete')) - .set(credentials) - .send({ - userId: user._id, - }) - .end(resolve); - }); +export const deleteUser = async (user) => + request + .post(api('users.delete')) + .set(credentials) + .send({ + userId: user._id, + }); export const getUserByUsername = (username) => new Promise((resolve) => { diff --git a/apps/meteor/tests/end-to-end/api/01-users.js b/apps/meteor/tests/end-to-end/api/01-users.js index d99fa68a036f..277f86e87127 100644 --- a/apps/meteor/tests/end-to-end/api/01-users.js +++ b/apps/meteor/tests/end-to-end/api/01-users.js @@ -5,23 +5,12 @@ import { expect } from 'chai'; import { after, afterEach, before, beforeEach, describe, it } from 'mocha'; import { sleep } from '../../../lib/utils/sleep'; -import { - getCredentials, - api, - request, - credentials, - apiEmail, - apiUsername, - targetUser, - log, - wait, - reservedWords, -} from '../../data/api-data.js'; +import { getCredentials, api, request, credentials, apiEmail, apiUsername, log, wait, reservedWords } from '../../data/api-data.js'; import { MAX_BIO_LENGTH, MAX_NICKNAME_LENGTH } from '../../data/constants.ts'; import { customFieldText, clearCustomFields, setCustomFields } from '../../data/custom-fields.js'; import { imgURL } from '../../data/interactions.js'; import { updatePermission, updateSetting } from '../../data/permissions.helper'; -import { createRoom } from '../../data/rooms.helper'; +import { createRoom, deleteRoom } from '../../data/rooms.helper'; import { adminEmail, preferences, password, adminUsername } from '../../data/user'; import { createUser, login, deleteUser, getUserStatus, getUserByUsername } from '../../data/users.helper.js'; @@ -39,11 +28,59 @@ async function joinChannel(userCredentials, roomId) { }); } +const targetUser = {}; + describe('[Users]', function () { this.retries(0); before((done) => getCredentials(done)); + before('should create a new user', async () => { + await request + .post(api('users.create')) + .set(credentials) + .send({ + email: apiEmail, + name: apiUsername, + username: apiUsername, + password, + active: true, + roles: ['user'], + joinDefaultChannels: true, + verified: true, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.nested.property('user.username', apiUsername); + expect(res.body).to.have.nested.property('user.emails[0].address', apiEmail); + expect(res.body).to.have.nested.property('user.active', true); + expect(res.body).to.have.nested.property('user.name', apiUsername); + expect(res.body).to.not.have.nested.property('user.e2e'); + + expect(res.body).to.not.have.nested.property('user.customFields'); + + targetUser._id = res.body.user._id; + targetUser.username = res.body.user.username; + }); + }); + + // before(async () => { + // await request + // .post(api('login')) + // .send({ + // user: apiUsername, + // password, + // }) + // .expect('Content-Type', 'application/json') + // .expect(200); + // }); + + after(async () => { + await deleteUser(targetUser); + }); + it('enabling E2E in server and generating keys to user...', async () => { await updateSetting('E2E_Enable', true); await request @@ -71,145 +108,101 @@ describe('[Users]', function () { }); describe('[/users.create]', () => { - before((done) => clearCustomFields(done)); - after((done) => clearCustomFields(done)); + before(async () => clearCustomFields()); + after(async () => clearCustomFields()); + + it('should create a new user with custom fields', async () => { + await setCustomFields({ customFieldText }); + + const username = `customField_${apiUsername}`; + const email = `customField_${apiEmail}`; + const customFields = { customFieldText: 'success' }; + + let user; - it('should create a new user', async () => { await request .post(api('users.create')) .set(credentials) .send({ - email: apiEmail, - name: apiUsername, - username: apiUsername, + email, + name: username, + username, password, active: true, roles: ['user'], joinDefaultChannels: true, verified: true, + customFields, }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('user.username', apiUsername); - expect(res.body).to.have.nested.property('user.emails[0].address', apiEmail); + expect(res.body).to.have.nested.property('user.username', username); + expect(res.body).to.have.nested.property('user.emails[0].address', email); expect(res.body).to.have.nested.property('user.active', true); - expect(res.body).to.have.nested.property('user.name', apiUsername); + expect(res.body).to.have.nested.property('user.name', username); + expect(res.body).to.have.nested.property('user.customFields.customFieldText', 'success'); expect(res.body).to.not.have.nested.property('user.e2e'); - expect(res.body).to.not.have.nested.property('user.customFields'); - - targetUser._id = res.body.user._id; - targetUser.username = res.body.user.username; + user = res.body.user; }); - await request - .post(api('login')) - .send({ - user: apiUsername, - password, - }) - .expect('Content-Type', 'application/json') - .expect(200); + await deleteUser(user); }); - it('should create a new user with custom fields', (done) => { - setCustomFields({ customFieldText }, (error) => { - if (error) { - return done(error); - } - - const username = `customField_${apiUsername}`; - const email = `customField_${apiEmail}`; - const customFields = { customFieldText: 'success' }; - + function failCreateUser(name) { + it(`should not create a new user if username is the reserved word ${name}`, (done) => { request .post(api('users.create')) .set(credentials) .send({ - email, - name: username, - username, + email: `create_user_fail_${apiEmail}`, + name: `create_user_fail_${apiUsername}`, + username: name, password, active: true, roles: ['user'], joinDefaultChannels: true, verified: true, - customFields, }) .expect('Content-Type', 'application/json') - .expect(200) + .expect(400) .expect((res) => { - expect(res.body).to.have.property('success', true); - expect(res.body).to.have.nested.property('user.username', username); - expect(res.body).to.have.nested.property('user.emails[0].address', email); - expect(res.body).to.have.nested.property('user.active', true); - expect(res.body).to.have.nested.property('user.name', username); - expect(res.body).to.have.nested.property('user.customFields.customFieldText', 'success'); - expect(res.body).to.not.have.nested.property('user.e2e'); + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('error', `${name} is blocked and can't be used! [error-blocked-username]`); }) .end(done); }); - }); + } - function failCreateUser(name) { - it(`should not create a new user if username is the reserved word ${name}`, (done) => { - request + function failUserWithCustomField(field) { + it(`should not create a user if a custom field ${field.reason}`, async () => { + await setCustomFields({ customFieldText }); + + const customFields = {}; + customFields[field.name] = field.value; + + await request .post(api('users.create')) .set(credentials) .send({ - email: `create_user_fail_${apiEmail}`, - name: `create_user_fail_${apiUsername}`, - username: name, + email: `customField_fail_${apiEmail}`, + name: `customField_fail_${apiUsername}`, + username: `customField_fail_${apiUsername}`, password, active: true, roles: ['user'], joinDefaultChannels: true, verified: true, + customFields, }) .expect('Content-Type', 'application/json') .expect(400) .expect((res) => { expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('error', `${name} is blocked and can't be used! [error-blocked-username]`); - }) - .end(done); - }); - } - - function failUserWithCustomField(field) { - it(`should not create a user if a custom field ${field.reason}`, (done) => { - setCustomFields({ customFieldText }, (error) => { - if (error) { - return done(error); - } - - const customFields = {}; - customFields[field.name] = field.value; - - request - .post(api('users.create')) - .set(credentials) - .send({ - email: `customField_fail_${apiEmail}`, - name: `customField_fail_${apiUsername}`, - username: `customField_fail_${apiUsername}`, - password, - active: true, - roles: ['user'], - joinDefaultChannels: true, - verified: true, - customFields, - }) - .expect('Content-Type', 'application/json') - .expect(400) - .expect((res) => { - expect(res.body).to.have.property('success', false); - expect(res.body).to.have.property('errorType', 'error-user-registration-custom-field'); - }) - .end(done); - }); + expect(res.body).to.have.property('errorType', 'error-user-registration-custom-field'); + }); }); } @@ -226,12 +219,16 @@ describe('[Users]', function () { }); describe('users default roles configuration', () => { + const users = []; + before(async () => { await updateSetting('Accounts_Registration_Users_Default_Roles', 'user,admin'); }); after(async () => { await updateSetting('Accounts_Registration_Users_Default_Roles', 'user'); + + await Promise.all(users.map((user) => deleteUser(user))); }); it('should create a new user with default roles', (done) => { @@ -256,6 +253,8 @@ describe('[Users]', function () { expect(res.body).to.have.nested.property('user.active', true); expect(res.body).to.have.nested.property('user.name', username); expect(res.body.user.roles).to.have.members(['user', 'admin']); + + users.push(res.body.user); }) .end(done); }); @@ -283,6 +282,8 @@ describe('[Users]', function () { expect(res.body).to.have.nested.property('user.active', true); expect(res.body).to.have.nested.property('user.name', username); expect(res.body.user.roles).to.have.members(['guest']); + + users.push(res.body.user); }) .end(done); }); @@ -292,6 +293,10 @@ describe('[Users]', function () { describe('[/users.register]', () => { const email = `email@email${Date.now()}.com`; const username = `myusername${Date.now()}`; + let user; + + after(async () => deleteUser(user)); + it('should register new user', (done) => { request .post(api('users.register')) @@ -308,6 +313,7 @@ describe('[Users]', function () { expect(res.body).to.have.nested.property('user.username', username); expect(res.body).to.have.nested.property('user.active', true); expect(res.body).to.have.nested.property('user.name', 'name'); + user = res.body.user; }) .end(done); }); @@ -331,9 +337,11 @@ describe('[Users]', function () { }); describe('[/users.info]', () => { - after(() => { - updatePermission('view-other-user-channels', ['admin']); - updatePermission('view-full-other-user-info', ['admin']); + after(async () => { + await Promise.all([ + updatePermission('view-other-user-channels', ['admin']), + updatePermission('view-full-other-user-info', ['admin']), + ]); }); it('should return an error when the user does not exist', (done) => { @@ -476,26 +484,30 @@ describe('[Users]', function () { }); it('should correctly route users that have `ufs` in their username', async () => { + const ufsUsername = `ufs-${Date.now()}`; + let user; + await request .post(api('users.create')) .set(credentials) .send({ - email: 'me@email.com', + email: `me-${Date.now()}@email.com`, name: 'testuser', - username: 'ufs', + username: ufsUsername, password: '1234', }) .expect('Content-Type', 'application/json') .expect(200) .expect((res) => { expect(res.body).to.have.property('success', true); + user = res.body.user; }); await request .get(api('users.info')) .set(credentials) .query({ - username: 'ufs', + username: ufsUsername, }) .expect('Content-Type', 'application/json') .expect(200) @@ -503,9 +515,11 @@ describe('[Users]', function () { expect(res.body).to.have.property('success', true); expect(res.body.user).to.have.property('type', 'user'); expect(res.body.user).to.have.property('name', 'testuser'); - expect(res.body.user).to.have.property('username', 'ufs'); + expect(res.body.user).to.have.property('username', ufsUsername); expect(res.body.user).to.have.property('active', true); }); + + await deleteUser(user); }); }); describe('[/users.getPresence]', () => { @@ -549,10 +563,10 @@ describe('[Users]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('full', true); - expect(res.body) - .to.have.property('users') - .to.have.property('0') - .to.deep.have.all.keys('_id', 'avatarETag', 'username', 'name', 'status', 'utcOffset'); + + const user = res.body.users.find((user) => user.username === 'rocket.cat'); + + expect(user).to.have.all.keys('_id', 'avatarETag', 'username', 'name', 'status', 'utcOffset'); }) .end(done); }); @@ -583,10 +597,10 @@ describe('[Users]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); expect(res.body).to.have.property('full', true); - expect(res.body) - .to.have.property('users') - .to.have.property('0') - .to.deep.have.all.keys('_id', 'avatarETag', 'username', 'name', 'status', 'utcOffset'); + + const user = res.body.users.find((user) => user.username === 'rocket.cat'); + + expect(user).to.have.all.keys('_id', 'avatarETag', 'username', 'name', 'status', 'utcOffset'); }) .end(done); }); @@ -599,68 +613,59 @@ describe('[Users]', function () { let user2; let user2Credentials; - before((done) => { - const createDeactivatedUser = async () => { - const username = `deactivated_${Date.now()}${apiUsername}`; - const email = `deactivated_+${Date.now()}${apiEmail}`; - - const userData = { - email, - name: username, - username, - password, - active: false, - }; - - deactivatedUser = await createUser(userData); - - expect(deactivatedUser).to.not.be.null; - expect(deactivatedUser).to.have.nested.property('username', username); - expect(deactivatedUser).to.have.nested.property('emails[0].address', email); - expect(deactivatedUser).to.have.nested.property('active', false); - expect(deactivatedUser).to.have.nested.property('name', username); - expect(deactivatedUser).to.not.have.nested.property('e2e'); + before(async () => { + const username = `deactivated_${Date.now()}${apiUsername}`; + const email = `deactivated_+${Date.now()}${apiEmail}`; + + const userData = { + email, + name: username, + username, + password, + active: false, }; - createDeactivatedUser().then(done); - }); - before((done) => - setCustomFields({ customFieldText }, async (error) => { - if (error) { - return done(error); - } - - const username = `customField_${Date.now()}${apiUsername}`; - const email = `customField_+${Date.now()}${apiEmail}`; - const customFields = { customFieldText: 'success' }; + deactivatedUser = await createUser(userData); - const userData = { - email, - name: username, - username, - password, - active: true, - roles: ['user'], - joinDefaultChannels: true, - verified: true, - customFields, - }; + expect(deactivatedUser).to.not.be.null; + expect(deactivatedUser).to.have.nested.property('username', username); + expect(deactivatedUser).to.have.nested.property('emails[0].address', email); + expect(deactivatedUser).to.have.nested.property('active', false); + expect(deactivatedUser).to.have.nested.property('name', username); + expect(deactivatedUser).to.not.have.nested.property('e2e'); + }); - user = await createUser(userData); + before(async () => { + await setCustomFields({ customFieldText }); + + const username = `customField_${Date.now()}${apiUsername}`; + const email = `customField_+${Date.now()}${apiEmail}`; + const customFields = { customFieldText: 'success' }; + + const userData = { + email, + name: username, + username, + password, + active: true, + roles: ['user'], + joinDefaultChannels: true, + verified: true, + customFields, + }; - expect(user).to.not.be.null; - expect(user).to.have.nested.property('username', username); - expect(user).to.have.nested.property('emails[0].address', email); - expect(user).to.have.nested.property('active', true); - expect(user).to.have.nested.property('name', username); - expect(user).to.have.nested.property('customFields.customFieldText', 'success'); - expect(user).to.not.have.nested.property('e2e'); + user = await createUser(userData); - return done(); - }), - ); + expect(user).to.not.be.null; + expect(user).to.have.nested.property('username', username); + expect(user).to.have.nested.property('emails[0].address', email); + expect(user).to.have.nested.property('active', true); + expect(user).to.have.nested.property('name', username); + expect(user).to.have.nested.property('customFields.customFieldText', 'success'); + expect(user).to.not.have.nested.property('e2e'); + }); - after((done) => clearCustomFields(done)); + after(async () => clearCustomFields()); before(async () => { user2 = await createUser({ joinDefaultChannels: false }); @@ -668,6 +673,8 @@ describe('[Users]', function () { }); after(async () => { + await deleteUser(deactivatedUser); + await deleteUser(user); await deleteUser(user2); user2 = undefined; @@ -1281,26 +1288,23 @@ describe('[Users]', function () { }); }); - it('should update the user name when the required permission is applied', (done) => { - updatePermission('edit-other-user-info', ['admin']).then(() => { - updateSetting('Accounts_AllowUsernameChange', false).then(() => { - request - .post(api('users.update')) - .set(credentials) - .send({ - userId: targetUser._id, - data: { - username: 'fake.name', - }, - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); + it('should update the user name when the required permission is applied', async () => { + await Promise.all([updatePermission('edit-other-user-info', ['admin']), updateSetting('Accounts_AllowUsernameChange', false)]); + + await request + .post(api('users.update')) + .set(credentials) + .send({ + userId: targetUser._id, + data: { + username: `fake.name.${Date.now()}`, + }, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); }); - }); }); it('should return an error when trying update user real name and it is not allowed', (done) => { @@ -2297,6 +2301,8 @@ describe('[Users]', function () { .end(done); }); + after(async () => deleteUser(targetUser)); + it('should return an username suggestion', (done) => { request .get(api('users.getUsernameSuggestion')) @@ -2326,7 +2332,7 @@ describe('[Users]', function () { const testUsername = `test-username-123456-${+new Date()}`; let targetUser; let userCredentials; - it('register a new user...', (done) => { + before((done) => { request .post(api('users.register')) .set(credentials) @@ -2343,7 +2349,7 @@ describe('[Users]', function () { }) .end(done); }); - it('Login...', (done) => { + before((done) => { request .post(api('login')) .send({ @@ -2360,6 +2366,8 @@ describe('[Users]', function () { .end(done); }); + after(async () => deleteUser(targetUser)); + it('should return true if the username is the same user username set', (done) => { request .get(api('users.checkUsernameAvailability')) @@ -2410,7 +2418,7 @@ describe('[Users]', function () { const testUsername = `testuser${+new Date()}`; let targetUser; let userCredentials; - it('register a new user...', (done) => { + before((done) => { request .post(api('users.register')) .set(credentials) @@ -2427,7 +2435,7 @@ describe('[Users]', function () { }) .end(done); }); - it('Login...', (done) => { + before((done) => { request .post(api('login')) .send({ @@ -2444,6 +2452,8 @@ describe('[Users]', function () { .end(done); }); + after(async () => deleteUser(targetUser)); + it('Enable "Accounts_AllowDeleteOwnAccount" setting...', (done) => { request .post('/api/v1/settings/Accounts_AllowDeleteOwnAccount') @@ -2472,23 +2482,22 @@ describe('[Users]', function () { .end(done); }); - it('should delete user own account when the SHA256 hash is in upper case', (done) => { - createUser().then((user) => { - login(user.username, password).then((createdUserCredentials) => { - request - .post(api('users.deleteOwnAccount')) - .set(createdUserCredentials) - .send({ - password: crypto.createHash('sha256').update(password, 'utf8').digest('hex').toUpperCase(), - }) - .expect('Content-Type', 'application/json') - .expect(200) - .expect((res) => { - expect(res.body).to.have.property('success', true); - }) - .end(done); + it('should delete user own account when the SHA256 hash is in upper case', async () => { + const user = await createUser(); + const createdUserCredentials = await login(user.username, password); + await request + .post(api('users.deleteOwnAccount')) + .set(createdUserCredentials) + .send({ + password: crypto.createHash('sha256').update(password, 'utf8').digest('hex').toUpperCase(), + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); }); - }); + + await deleteUser(user); }); it('should return an error when trying to delete user own account if user is the last room owner', async () => { @@ -2542,6 +2551,9 @@ describe('[Users]', function () { expect(res.body).to.have.property('error', '[user-last-owner]'); expect(res.body).to.have.property('errorType', 'user-last-owner'); }); + + await deleteRoom({ type: 'c', roomId: room._id }); + await deleteUser(user); }); it('should delete user own account if the user is the last room owner and `confirmRelinquish` is set to `true`', async () => { @@ -2594,6 +2606,8 @@ describe('[Users]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); }); + await deleteRoom({ type: 'c', roomId: room._id }); + await deleteUser(user); }); it('should assign a new owner to the room if the last room owner is deleted', async () => { @@ -2661,6 +2675,8 @@ describe('[Users]', function () { expect(res.body.roles[0].roles).to.eql(['owner']); expect(res.body.roles[0].u).to.have.property('_id', credentials['X-User-Id']); }); + await deleteRoom({ type: 'c', roomId: room._id }); + await deleteUser(user); }); }); @@ -2763,6 +2779,8 @@ describe('[Users]', function () { expect(res.body).to.have.property('error', '[user-last-owner]'); expect(res.body).to.have.property('errorType', 'user-last-owner'); }); + + await deleteRoom({ type: 'c', roomId: room._id }); }); it('should delete user account if the user is the last room owner and `confirmRelinquish` is set to `true`', async () => { @@ -2813,6 +2831,8 @@ describe('[Users]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); }); + + await deleteRoom({ type: 'c', roomId: room._id }); }); it('should delete user account when logged user has "delete-user" permission', async () => { @@ -2893,6 +2913,8 @@ describe('[Users]', function () { expect(res.body.roles[0].roles).to.eql(['owner']); expect(res.body.roles[0].u).to.have.property('_id', credentials['X-User-Id']); }); + + await deleteRoom({ type: 'c', roomId: room._id }); }); }); @@ -3241,6 +3263,8 @@ describe('[Users]', function () { expect(res.body).to.have.property('error', '[user-last-owner]'); expect(res.body).to.have.property('errorType', 'user-last-owner'); }); + + await deleteRoom({ type: 'c', roomId: room._id }); }); it('should set other user status to inactive if the user is the last owner of a room and `confirmRelinquish` is set to `true`', async () => { @@ -3305,6 +3329,8 @@ describe('[Users]', function () { .expect((res) => { expect(res.body).to.have.property('success', true); }); + + await deleteRoom({ type: 'c', roomId: room._id }); }); it('should set other user as room owner if the last owner of a room is deactivated and `confirmRelinquish` is set to `true`', async () => { @@ -3396,6 +3422,8 @@ describe('[Users]', function () { expect(res.body.roles[1].roles).to.eql(['owner']); expect(res.body.roles[1].u).to.have.property('_id', credentials['X-User-Id']); }); + + await deleteRoom({ type: 'c', roomId: room._id }); }); it('should return an error when trying to set other user active status and has not the necessary permission(edit-other-user-active-status)', (done) => { @@ -3464,6 +3492,8 @@ describe('[Users]', function () { expect(user).to.have.property('roles'); expect(user.roles).to.be.an('array').of.length(2); expect(user.roles).to.include('user', 'livechat-agent'); + + await deleteUser(testUser); }); }); @@ -3516,6 +3546,10 @@ describe('[Users]', function () { .end(done); }); + after(async () => { + await deleteUser(testUser); + }); + it('should fail to deactivate if user doesnt have edit-other-user-active-status permission', (done) => { updatePermission('edit-other-user-active-status', []).then(() => { request @@ -3563,7 +3597,7 @@ describe('[Users]', function () { .expect(200) .expect((res) => { expect(res.body).to.have.property('success', true); - expect(res.body).to.have.property('count', 2); + expect(res.body).to.have.property('count', 1); }) .end(done); }); @@ -3690,7 +3724,7 @@ describe('[Users]', function () { updatePermission('view-outside-room', ['admin', 'owner', 'moderator', 'user']); }); - describe('[without permission]', () => { + describe('[without permission]', function () { let user; let userCredentials; let user2; @@ -3711,6 +3745,12 @@ describe('[Users]', function () { roomId = await createChannel(userCredentials, `channel.autocomplete.${Date.now()}`); }); + after(async () => { + await deleteRoom({ type: 'c', roomId }); + await deleteUser(user); + await deleteUser(user2); + }); + it('should return an empty list when the user does not have any subscription', (done) => { request .get(api('users.autocomplete?selector={}')) @@ -4126,6 +4166,10 @@ describe('[Users]', function () { .then(() => done()); }); + after(async () => { + await deleteUser(testUser); + }); + it('should list both channels', (done) => { request .get(api('users.listTeams')) @@ -4151,14 +4195,19 @@ describe('[Users]', function () { describe('[/users.logout]', () => { let user; let otherUser; + let userCredentials; + before(async () => { user = await createUser(); otherUser = await createUser(); }); + before(async () => { + userCredentials = await login(user.username, password); + }); + after(async () => { await deleteUser(user); await deleteUser(otherUser); - user = undefined; }); it('should throw unauthorized error to user w/o "logout-other-user" permission', (done) => { @@ -4187,7 +4236,7 @@ describe('[Users]', function () { it('should logout the requester', (done) => { updatePermission('logout-other-user', []).then(() => { - request.post(api('users.logout')).set(credentials).expect('Content-Type', 'application/json').expect(200).end(done); + request.post(api('users.logout')).set(userCredentials).expect('Content-Type', 'application/json').expect(200).end(done); }); }); }); From 0714217854fc0907121421db9e61db39274305ac Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 17 Oct 2023 11:12:10 -0300 Subject: [PATCH 2/3] test: fix usage of deleteUser --- apps/meteor/tests/end-to-end/api/10-subscriptions.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/meteor/tests/end-to-end/api/10-subscriptions.js b/apps/meteor/tests/end-to-end/api/10-subscriptions.js index f547895eb8a4..531291a99216 100644 --- a/apps/meteor/tests/end-to-end/api/10-subscriptions.js +++ b/apps/meteor/tests/end-to-end/api/10-subscriptions.js @@ -236,7 +236,8 @@ describe('[Subscriptions]', function () { before(async () => { user = await createUser({ username: 'testthread123', password: 'testthread123' }); threadUserCredentials = await login('testthread123', 'testthread123'); - request + + const res = await request .post(api('chat.sendMessage')) .set(threadUserCredentials) .send({ @@ -244,14 +245,13 @@ describe('[Subscriptions]', function () { rid: testChannel._id, msg: 'Starting a Thread', }, - }) - .end((_, res) => { - threadId = res.body.message._id; }); + + threadId = res.body.message._id; }); - after((done) => { - deleteUser(user).then(done); + after(async () => { + await deleteUser(user); }); it('should mark threads as read', async () => { From 2ee32933671841df6e0e04301a316307db72ee38 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 17 Oct 2023 13:29:47 -0300 Subject: [PATCH 3/3] remove comments --- apps/meteor/tests/end-to-end/api/01-users.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/apps/meteor/tests/end-to-end/api/01-users.js b/apps/meteor/tests/end-to-end/api/01-users.js index 277f86e87127..90448916fc80 100644 --- a/apps/meteor/tests/end-to-end/api/01-users.js +++ b/apps/meteor/tests/end-to-end/api/01-users.js @@ -66,17 +66,6 @@ describe('[Users]', function () { }); }); - // before(async () => { - // await request - // .post(api('login')) - // .send({ - // user: apiUsername, - // password, - // }) - // .expect('Content-Type', 'application/json') - // .expect(200); - // }); - after(async () => { await deleteUser(targetUser); });