From e8512977d3b09cb53526120bf6925313d06ddab9 Mon Sep 17 00:00:00 2001 From: souzaramon Date: Thu, 19 May 2022 01:04:54 -0300 Subject: [PATCH] Test: Migrate 13-permissions from cypress to playwright (#25558) --- apps/meteor/tests/e2e/12-settings.spec.ts | 32 +++--- apps/meteor/tests/e2e/13-permissions.spec.ts | 97 +++++++++++++++++++ .../tests/e2e/utils/interfaces/Login.ts | 1 + .../e2e/utils/mocks/userAndPasswordMock.ts | 7 ++ .../e2e/utils/pageobjects/Administration.ts | 8 ++ .../tests/e2e/utils/pageobjects/FlexTab.ts | 7 +- .../e2e/utils/pageobjects/MainContent.ts | 2 +- .../tests/e2e/utils/pageobjects/SideNav.ts | 6 ++ .../tests/e2e/utils/pageobjects/index.ts | 9 ++ 9 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 apps/meteor/tests/e2e/13-permissions.spec.ts create mode 100644 apps/meteor/tests/e2e/utils/pageobjects/index.ts diff --git a/apps/meteor/tests/e2e/12-settings.spec.ts b/apps/meteor/tests/e2e/12-settings.spec.ts index 5a328152ce96..d0893272dc61 100644 --- a/apps/meteor/tests/e2e/12-settings.spec.ts +++ b/apps/meteor/tests/e2e/12-settings.spec.ts @@ -58,7 +58,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(edit) not be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); @@ -77,7 +77,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(edit) be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); @@ -98,7 +98,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(delete) not be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); @@ -117,7 +117,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(delete) be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); @@ -138,7 +138,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(upload audio) not be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); expect(await mainContent.recordBtn().isVisible()).toBeFalsy(); }); @@ -155,7 +155,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(upload audio) be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); expect(await mainContent.recordBtn().isVisible()).toBeTruthy(); }); @@ -174,7 +174,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(upload video) not be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.openMoreActionMenu(); expect(await page.isVisible('.rc-popover__content [data-id="video-message"]')).toBeFalsy(); @@ -192,7 +192,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(upload video) be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.openMoreActionMenu(); expect(await page.isVisible('.rc-popover__content [data-id="video-message"]')).toBeTruthy(); @@ -225,7 +225,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect badword be censored', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(unauthorizedWord); await mainContent.waitForLastMessageEqualsText('*'.repeat(unauthorizedWord.length)); @@ -243,7 +243,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect badword not be censored', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(unauthorizedWord); await mainContent.waitForLastMessageEqualsText(unauthorizedWord); @@ -263,7 +263,7 @@ test.describe.skip('[Settings]', async () => { }); test.skip('(UI) expect option(star message) not be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); @@ -282,7 +282,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(star message) be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); @@ -303,7 +303,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(upload file) not be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.openMoreActionMenu(); expect(await page.isVisible('[data-qa-id="file-upload"]')).toBeFalsy(); @@ -321,7 +321,7 @@ test.describe.skip('[Settings]', async () => { }); test('(UI) expect option(upload file) be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.openMoreActionMenu(); expect(await page.isVisible('[data-qa-id="file-upload"]')).toBeTruthy(); @@ -436,7 +436,7 @@ test.describe.skip('[Settings (admin)]', async () => { }); test('(UI) expect option(pin message) not be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); @@ -455,7 +455,7 @@ test.describe.skip('[Settings (admin)]', async () => { }); test('(UI) expect option(pin message) be visible', async () => { - await mainContent.reload(); + await mainContent.doReload(); await mainContent.sendMessage(`any_message_${uuid()}`); await mainContent.openMessageActionMenu(); diff --git a/apps/meteor/tests/e2e/13-permissions.spec.ts b/apps/meteor/tests/e2e/13-permissions.spec.ts new file mode 100644 index 000000000000..0b4edda751b7 --- /dev/null +++ b/apps/meteor/tests/e2e/13-permissions.spec.ts @@ -0,0 +1,97 @@ +import { Page, test, expect } from '@playwright/test'; +import { v4 as uuid } from 'uuid'; + +import { LoginPage, FlexTab, Administration, MainContent, SideNav } from './utils/pageobjects'; +import { adminLogin, createRegisterUser } from './utils/mocks/userAndPasswordMock'; +import { BACKSPACE } from './utils/mocks/keyboardKeyMock'; + +test.describe('[Permissions]', () => { + let page: Page; + + let loginPage: LoginPage; + let admin: Administration; + let flexTab: FlexTab; + let sideNav: SideNav; + let mainContent: MainContent; + + const userToBeCreated = createRegisterUser(); + + test.beforeAll(async ({ browser }) => { + const context = await browser.newContext(); + page = await context.newPage(); + + loginPage = new LoginPage(page); + admin = new Administration(page); + flexTab = new FlexTab(page); + sideNav = new SideNav(page); + mainContent = new MainContent(page); + + await page.goto('/'); + await loginPage.login(adminLogin); + await sideNav.general().click(); + await page.goto('/admin/users'); + }); + + test('expect create a user via admin view', async () => { + await flexTab.usersAddUserTab().click(); + await flexTab.usersAddUserName().type(userToBeCreated.name); + await flexTab.usersAddUserUsername().type(userToBeCreated.username ?? ''); + await flexTab.usersAddUserEmail().type(userToBeCreated.email); + await flexTab.usersAddUserVerifiedCheckbox().click(); + await flexTab.usersAddUserPassword().type(userToBeCreated.password); + await flexTab.doAddRole('user'); + await flexTab.usersButtonSave().click(); + }); + + test('expect user be show on list', async () => { + await admin.usersFilter().type(userToBeCreated.email, { delay: 200 }); + expect(await admin.userInTable(userToBeCreated.email).isVisible()).toBeTruthy(); + }); + + test.describe('disable "userToBeCreated" permissions', () => { + test('expect open permissions table', async () => { + await admin.permissionsLink().click(); + }); + + test('expect remove "mention all" permission from user', async () => { + await admin.inputPermissionsSearch().type('all'); + + if (await admin.getCheckboxPermission('Mention All').locator('input').isChecked()) { + await admin.getCheckboxPermission('Mention All').click(); + } + }); + + test('expect remove "delete message" permission from user', async () => { + await admin.inputPermissionsSearch().click({ clickCount: 3 }); + await page.keyboard.press(BACKSPACE); + await admin.inputPermissionsSearch().type('delete'); + + if (await admin.getCheckboxPermission('Delete Own Message').locator('input').isChecked()) { + await admin.getCheckboxPermission('Delete Own Message').click(); + } + }); + }); + + test.describe('assert "userToBeCreated" permissions', () => { + test.beforeAll(async () => { + await sideNav.doLogout(); + await loginPage.goto('/'); + await loginPage.login(userToBeCreated); + await sideNav.general().click(); + }); + + test('expect not be abble to "mention all"', async () => { + await mainContent.sendMessage('@all any_message'); + + expect(mainContent.lastMessage()).toContainText('not allowed'); + }); + + test('expect not be abble to "delete own message"', async () => { + await mainContent.doReload(); + await mainContent.sendMessage(`any_message_${uuid()}`); + await mainContent.openMessageActionMenu(); + + expect(await page.isVisible('[data-qa-id="delete-message"]')).toBeFalsy(); + }); + }); +}); diff --git a/apps/meteor/tests/e2e/utils/interfaces/Login.ts b/apps/meteor/tests/e2e/utils/interfaces/Login.ts index fdba700a1851..d49214aa628a 100644 --- a/apps/meteor/tests/e2e/utils/interfaces/Login.ts +++ b/apps/meteor/tests/e2e/utils/interfaces/Login.ts @@ -7,4 +7,5 @@ export interface IRegister { email: string; password: string; name: string; + username?: string; } diff --git a/apps/meteor/tests/e2e/utils/mocks/userAndPasswordMock.ts b/apps/meteor/tests/e2e/utils/mocks/userAndPasswordMock.ts index 251dd75ee87a..68aaadc80950 100644 --- a/apps/meteor/tests/e2e/utils/mocks/userAndPasswordMock.ts +++ b/apps/meteor/tests/e2e/utils/mocks/userAndPasswordMock.ts @@ -28,6 +28,13 @@ export const registerUser: IRegister = { name: faker.name.findName(), }; +export const createRegisterUser = (): IRegister => ({ + email: validEmail, + password: 'any_password', + name: faker.name.findName(), + username: faker.internet.userName(), +}); + export const validUser: ILogin = { email: validEmail, password: 'any_password', diff --git a/apps/meteor/tests/e2e/utils/pageobjects/Administration.ts b/apps/meteor/tests/e2e/utils/pageobjects/Administration.ts index 391bedcc9536..d61ba6c522b8 100644 --- a/apps/meteor/tests/e2e/utils/pageobjects/Administration.ts +++ b/apps/meteor/tests/e2e/utils/pageobjects/Administration.ts @@ -695,6 +695,14 @@ export default class Administration extends BasePage { await this.buttonSave().click(); } + public inputPermissionsSearch(): Locator { + return this.getPage().locator('.main-content input[placeholder="Search"]'); + } + + public getCheckboxPermission(label: string, column = 6): Locator { + return this.getPage().locator(`tr td:has-text("${label}") ~ td:nth-child(${column})`).locator('label').first(); + } + public userInfoActions(): Locator { return this.getPage().locator('[data-qa-id="UserInfoActions"]'); } diff --git a/apps/meteor/tests/e2e/utils/pageobjects/FlexTab.ts b/apps/meteor/tests/e2e/utils/pageobjects/FlexTab.ts index 038d0e00d794..ca31a404944d 100644 --- a/apps/meteor/tests/e2e/utils/pageobjects/FlexTab.ts +++ b/apps/meteor/tests/e2e/utils/pageobjects/FlexTab.ts @@ -316,7 +316,7 @@ class FlexTab extends BasePage { } public usersAddUserEmail(): Locator { - return this.getPage().locator('//label[text()="Email"]/following-sibling::span//input/following-sibling::span//i'); + return this.getPage().locator('//label[text()="Email"]/following-sibling::span//input').first(); } public usersAddUserRoleList(): Locator { @@ -548,6 +548,11 @@ class FlexTab extends BasePage { 'div.thread-list.js-scroll-thread ul.thread [data-qa-type="message"]:last-child div.message-body-wrapper [data-qa-type="message-body"]', ); } + + public async doAddRole(role: string): Promise { + await this.usersAddUserRoleList().click(); + await this.getPage().locator(`li[value=${role}]`).click(); + } } export default FlexTab; diff --git a/apps/meteor/tests/e2e/utils/pageobjects/MainContent.ts b/apps/meteor/tests/e2e/utils/pageobjects/MainContent.ts index 4ddfc5065821..5008609aaf8f 100644 --- a/apps/meteor/tests/e2e/utils/pageobjects/MainContent.ts +++ b/apps/meteor/tests/e2e/utils/pageobjects/MainContent.ts @@ -440,7 +440,7 @@ export default class MainContent extends BasePage { return this.getPage().locator('[data-qa="UserCard"] a'); } - public async reload(): Promise { + public async doReload(): Promise { await this.getPage().reload({ waitUntil: 'load' }); await this.getPage().waitForSelector('.messages-box'); } diff --git a/apps/meteor/tests/e2e/utils/pageobjects/SideNav.ts b/apps/meteor/tests/e2e/utils/pageobjects/SideNav.ts index fac4223bb61b..685417245dee 100644 --- a/apps/meteor/tests/e2e/utils/pageobjects/SideNav.ts +++ b/apps/meteor/tests/e2e/utils/pageobjects/SideNav.ts @@ -245,6 +245,12 @@ class SideNav extends BasePage { await this.searchInput().type(target, { delay: 300 }); await this.getPage().keyboard.press(ENTER); } + + public async doLogout(): Promise { + await this.getPage().goto('/home'); + await this.sidebarUserMenu().click(); + await this.logout().click(); + } } export default SideNav; diff --git a/apps/meteor/tests/e2e/utils/pageobjects/index.ts b/apps/meteor/tests/e2e/utils/pageobjects/index.ts new file mode 100644 index 000000000000..3a0e4236cff2 --- /dev/null +++ b/apps/meteor/tests/e2e/utils/pageobjects/index.ts @@ -0,0 +1,9 @@ +export { default as Administration } from './Administration'; +export { default as ChannelCreation } from './ChannelCreation'; +export { default as FlexTab } from './FlexTab'; +export { default as Global } from './Global'; +export { default as LoginPage } from './LoginPage'; +export { default as MainContent } from './MainContent'; +export { default as PreferencesMainContent } from './PreferencesMainContent'; +export { default as SetupWizard } from './SetupWizard'; +export { default as SideNav } from './SideNav';