From d76d531985c393c571b2966db2b47a886f0467db Mon Sep 17 00:00:00 2001 From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> Date: Tue, 27 Jun 2023 09:57:52 -0300 Subject: [PATCH] chore: avoid use all seats availables on the server (#5049) --- e2e/helpers/data_setup.ts | 58 ++++++++++++++++++++- e2e/tests/assorted/01-e2eencryption.spec.ts | 9 +++- e2e/tests/assorted/07-changeserver.spec.ts | 9 +++- e2e/tests/assorted/10-deleteserver.spec.ts | 8 ++- e2e/tests/assorted/11-deeplinking.spec.ts | 17 +++++- e2e/tests/assorted/15-change-avatar.spec.ts | 4 +- 6 files changed, 97 insertions(+), 8 deletions(-) diff --git a/e2e/helpers/data_setup.ts b/e2e/helpers/data_setup.ts index 9537115b8..3dafb7e92 100644 --- a/e2e/helpers/data_setup.ts +++ b/e2e/helpers/data_setup.ts @@ -137,7 +137,61 @@ export const post = async (endpoint: string, body: any, user: ITestUser) => { return rocketchat.post(endpoint, body); }; -export const getProfileInfo = async (userId: string) => { - const result = await get(`users.info?userId=${userId}`); +interface UserId { + userId: string; +} + +interface UserName { + username: string; +} + +export const getProfileInfo = async (param: UserId | UserName) => { + let query = ''; + if ('userId' in param) { + query += `userId=${param.userId}`; + } else if ('username' in param) { + query += `username=${param.username}`; + } + const result = await get(`users.info?${query}`); return result.data.user; }; + +export interface IDeleteCreateUser { + server: string; + username: string; +} + +const deleteCreatedUser = async ({ server, username: usernameToDelete }: IDeleteCreateUser) => { + const serverConnection = axios.create({ + baseURL: `${server}/api/v1/`, + headers: { + 'Content-Type': 'application/json;charset=UTF-8' + } + }); + console.log(`Logging in as admin in ${server}`); + const response = await serverConnection.post('login', { + user: data.adminUser, + password: data.adminPassword + }); + const { authToken, userId } = response.data.data; + serverConnection.defaults.headers.common['X-User-Id'] = userId; + serverConnection.defaults.headers.common['X-Auth-Token'] = authToken; + + console.log(`Get user info: users.info?username=${usernameToDelete}`); + const result = await serverConnection.get(`users.info?username=${usernameToDelete}`); + const userIdToDelete = result.data.user._id; + + const body = { userId: userIdToDelete, confirmRelinquish: false }; + console.log(`Delete user: users.delete ${JSON.stringify(body)}`); + const responsePost = await serverConnection.post('users.delete', body); + return responsePost.data; +}; + +// Delete created users to avoid use all the Seats Available on the server +export const deleteCreatedUsers = async (deleteUsersAfterAll: IDeleteCreateUser[]) => { + if (deleteUsersAfterAll.length) { + for await (const deleteUser of deleteUsersAfterAll) { + await deleteCreatedUser(deleteUser); + } + } +}; diff --git a/e2e/tests/assorted/01-e2eencryption.spec.ts b/e2e/tests/assorted/01-e2eencryption.spec.ts index 3daf3b3c1..e63c3e636 100644 --- a/e2e/tests/assorted/01-e2eencryption.spec.ts +++ b/e2e/tests/assorted/01-e2eencryption.spec.ts @@ -15,7 +15,7 @@ import { tryTapping } from '../../helpers/app'; import data from '../../data'; -import { createRandomUser, ITestUser } from '../../helpers/data_setup'; +import { createRandomUser, deleteCreatedUsers, IDeleteCreateUser, ITestUser } from '../../helpers/data_setup'; import random from '../../helpers/random'; const checkServer = async (server: string) => { @@ -83,6 +83,8 @@ describe('E2E Encryption', () => { let alertButtonType: string; let textMatcher: TTextMatcher; + const deleteUsersAfterAll: IDeleteCreateUser[] = []; + beforeAll(async () => { user = await createRandomUser(); otherUser = await createRandomUser(); @@ -92,6 +94,10 @@ describe('E2E Encryption', () => { await login(user.username, user.password); }); + afterAll(async () => { + await deleteCreatedUsers(deleteUsersAfterAll); + }); + describe('Banner', () => { describe('Render', () => { it('should have encryption badge', async () => { @@ -396,6 +402,7 @@ describe('E2E Encryption', () => { await element(by.id('register-view-password')).replaceText(randomUser.password); await element(by.id('register-view-password')).tapReturnKey(); await expectValidRegisterOrRetry(device.getPlatform()); + deleteUsersAfterAll.push({ server: data.alternateServer, username: randomUser.username }); await checkServer(data.alternateServer); }); diff --git a/e2e/tests/assorted/07-changeserver.spec.ts b/e2e/tests/assorted/07-changeserver.spec.ts index 4d0b32aa4..bef8fc836 100644 --- a/e2e/tests/assorted/07-changeserver.spec.ts +++ b/e2e/tests/assorted/07-changeserver.spec.ts @@ -2,7 +2,7 @@ import { device, waitFor, element, by } from 'detox'; import data from '../../data'; import { navigateToLogin, login, checkServer, expectValidRegisterOrRetry } from '../../helpers/app'; -import { createRandomRoom, createRandomUser, ITestUser } from '../../helpers/data_setup'; +import { createRandomRoom, createRandomUser, deleteCreatedUsers, IDeleteCreateUser, ITestUser } from '../../helpers/data_setup'; const reopenAndCheckServer = async (server: string) => { await device.launchApp({ permissions: { notifications: 'YES' }, newInstance: true }); @@ -16,6 +16,8 @@ describe('Change server', () => { let user: ITestUser; let room: string; + const deleteUsersAfterAll: IDeleteCreateUser[] = []; + beforeAll(async () => { user = await createRandomUser(); ({ name: room } = await createRandomRoom(user)); @@ -24,6 +26,10 @@ describe('Change server', () => { await login(user.username, user.password); }); + afterAll(async () => { + await deleteCreatedUsers(deleteUsersAfterAll); + }); + it('should open the dropdown button, have the server add button and create workspace button', async () => { await element(by.id('rooms-list-header-server-dropdown-button')).tap(); await waitFor(element(by.id('rooms-list-header-server-dropdown'))) @@ -72,6 +78,7 @@ describe('Change server', () => { await element(by.id('register-view-password')).replaceText(randomUser.password); await element(by.id('register-view-password')).tapReturnKey(); await expectValidRegisterOrRetry(device.getPlatform()); + deleteUsersAfterAll.push({ server: data.alternateServer, username: randomUser.username }); await waitFor(element(by.id(`rooms-list-view-item-${room}`))) .toBeNotVisible() diff --git a/e2e/tests/assorted/10-deleteserver.spec.ts b/e2e/tests/assorted/10-deleteserver.spec.ts index a0b3ef8cd..22d601f1c 100644 --- a/e2e/tests/assorted/10-deleteserver.spec.ts +++ b/e2e/tests/assorted/10-deleteserver.spec.ts @@ -10,12 +10,13 @@ import { TTextMatcher, expectValidRegisterOrRetry } from '../../helpers/app'; -import { createRandomUser, ITestUser } from '../../helpers/data_setup'; +import { createRandomUser, deleteCreatedUsers, IDeleteCreateUser, ITestUser } from '../../helpers/data_setup'; describe('Delete server', () => { let alertButtonType: string; let textMatcher: TTextMatcher; let user: ITestUser; + const deleteUsersAfterAll: IDeleteCreateUser[] = []; beforeAll(async () => { user = await createRandomUser(); @@ -25,6 +26,10 @@ describe('Delete server', () => { await login(user.username, user.password); }); + afterAll(async () => { + await deleteCreatedUsers(deleteUsersAfterAll); + }); + it('should be logged in main server', async () => { await checkServer(data.server); }); @@ -58,6 +63,7 @@ describe('Delete server', () => { await element(by.id('register-view-password')).replaceText(randomUser.password); await element(by.id('register-view-password')).tapReturnKey(); await expectValidRegisterOrRetry(device.getPlatform()); + deleteUsersAfterAll.push({ server: data.alternateServer, username: randomUser.username }); await checkServer(data.alternateServer); }); diff --git a/e2e/tests/assorted/11-deeplinking.spec.ts b/e2e/tests/assorted/11-deeplinking.spec.ts index 102536a27..18c6a37f1 100644 --- a/e2e/tests/assorted/11-deeplinking.spec.ts +++ b/e2e/tests/assorted/11-deeplinking.spec.ts @@ -10,7 +10,14 @@ import { TTextMatcher, expectValidRegisterOrRetry } from '../../helpers/app'; -import { createRandomRoom, createRandomUser, login, sendMessage } from '../../helpers/data_setup'; +import { + IDeleteCreateUser, + createRandomRoom, + createRandomUser, + deleteCreatedUsers, + login, + sendMessage +} from '../../helpers/data_setup'; import random from '../../helpers/random'; const DEEPLINK_METHODS = { AUTH: 'auth', ROOM: 'room' }; @@ -32,6 +39,8 @@ describe('Deep linking', () => { let room: string; const threadMessage = `to-thread-${random()}`; + const deleteUsersAfterAll: IDeleteCreateUser[] = []; + beforeAll(async () => { const user = await createRandomUser(); ({ _id: rid, name: room } = await createRandomRoom(user, 'p')); @@ -46,6 +55,10 @@ describe('Deep linking', () => { await sendMessage(user, result.message.rid, random(), threadId); }); + afterAll(async () => { + await deleteCreatedUsers(deleteUsersAfterAll); + }); + describe('Authentication', () => { it('should run a deep link to an invalid account and raise error', async () => { await device.launchApp({ @@ -91,6 +104,8 @@ describe('Deep linking', () => { await element(by.id('register-view-password')).replaceText(randomUser.password); await element(by.id('register-view-password')).tapReturnKey(); await expectValidRegisterOrRetry(device.getPlatform()); + deleteUsersAfterAll.push({ server: data.alternateServer, username: randomUser.username }); + await authAndNavigate(); }); }); diff --git a/e2e/tests/assorted/15-change-avatar.spec.ts b/e2e/tests/assorted/15-change-avatar.spec.ts index a3ca766a6..f83037e56 100644 --- a/e2e/tests/assorted/15-change-avatar.spec.ts +++ b/e2e/tests/assorted/15-change-avatar.spec.ts @@ -68,7 +68,7 @@ describe('Profile screen', () => { it('should change the avatar through a base64 image mocked', async () => { await element(by.type(scrollViewType)).atIndex(1).swipe('down'); await element(by.id('avatar-edit-button')).tap(); - const previousUserInfo = await getProfileInfo(userId); + const previousUserInfo = await getProfileInfo({ userId }); const previousAvatarEtag = previousUserInfo.avatarETag; await sleep(500); await waitFor(element(by.id('change-avatar-view-upload-image'))) @@ -83,7 +83,7 @@ describe('Profile screen', () => { .toBeVisible() .withTimeout(2000); await sleep(300); - const newUserInfo = await getProfileInfo(userId); + const newUserInfo = await getProfileInfo({ userId }); const newAvatarEtag = newUserInfo.avatarETag; await sleep(500); if (previousAvatarEtag === newAvatarEtag) {