Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Playwright test improvements #28100

Merged
merged 7 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/actions/build-docker-image/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ runs:
echo "Build ${{ inputs.release }} Docker image"
docker build -t $IMAGE_NAME .

echo "::set-output name=image-name-base::${IMAGE_NAME_BASE}"
echo "::set-output name=image-name::${IMAGE_NAME}"
echo "image-name-base=${IMAGE_NAME_BASE}" >> $GITHUB_OUTPUT
echo "image-name=${IMAGE_NAME}" >> $GITHUB_OUTPUT

- name: Login to GitHub Container Registry
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'release' || github.ref == 'refs/heads/develop'
Expand Down
26 changes: 13 additions & 13 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
RELEASE="release-candidate"
fi
echo "RELEASE: ${RELEASE}"
echo "::set-output name=release::${RELEASE}"
echo "release=${RELEASE}" >> $GITHUB_OUTPUT

- id: latest
run: |
Expand All @@ -46,7 +46,7 @@ jobs:
awk -F/ '$NF !~ /rc|beta/ { print $NF; exit }'
)"
echo "LATEST_RELEASE: ${LATEST_RELEASE}"
echo "::set-output name=latest-release::${LATEST_RELEASE}"
echo "latest-release=${LATEST_RELEASE}" >> $GITHUB_OUTPUT

- id: docker
run: |
Expand All @@ -56,7 +56,7 @@ jobs:
DOCKER_TAG="gh-${{ github.run_id }}"
fi
echo "DOCKER_TAG: ${DOCKER_TAG}"
echo "::set-output name=gh-docker-tag::${DOCKER_TAG}"
echo "gh-docker-tag=${DOCKER_TAG}" >> $GITHUB_OUTPUT

build:
runs-on: ubuntu-20.04
Expand Down Expand Up @@ -335,7 +335,7 @@ jobs:
LOWERCASE_REPOSITORY=$(echo "${{ github.repository_owner }}" | tr "[:upper:]" "[:lower:]")

echo "LOWERCASE_REPOSITORY: ${LOWERCASE_REPOSITORY}"
echo "::set-output name=lowercase-repo::${LOWERCASE_REPOSITORY}"
echo "lowercase-repo=${LOWERCASE_REPOSITORY}" >> $GITHUB_OUTPUT

# test alpine image on mongo 6.0 (no special reason to be mongo 6.0 but we need to test alpine at least once)
if [[ '${{ matrix.mongodb-version }}' = '6.0' ]]; then
Expand All @@ -347,10 +347,10 @@ jobs:
fi;

echo "RC_DOCKERFILE: ${RC_DOCKERFILE}"
echo "::set-output name=rc-dockerfile::${RC_DOCKERFILE}"
echo "rc-dockerfile=${RC_DOCKERFILE}" >> $GITHUB_OUTPUT

echo "RC_DOCKER_TAG: ${RC_DOCKER_TAG}"
echo "::set-output name=rc-docker-tag::${RC_DOCKER_TAG}"
echo "rc-docker-tag=${RC_DOCKER_TAG}" >> $GITHUB_OUTPUT

- uses: actions/checkout@v3

Expand Down Expand Up @@ -485,7 +485,7 @@ jobs:
LOWERCASE_REPOSITORY=$(echo "${{ github.repository_owner }}" | tr "[:upper:]" "[:lower:]")

echo "LOWERCASE_REPOSITORY: ${LOWERCASE_REPOSITORY}"
echo "::set-output name=lowercase-repo::${LOWERCASE_REPOSITORY}"
echo "lowercase-repo=${LOWERCASE_REPOSITORY}" >> $GITHUB_OUTPUT

# test alpine image on mongo 6.0 (no special reason to be mongo 6.0 but we need to test alpine at least once)
if [[ '${{ matrix.mongodb-version }}' = '6.0' ]]; then
Expand All @@ -497,10 +497,10 @@ jobs:
fi;

echo "RC_DOCKERFILE: ${RC_DOCKERFILE}"
echo "::set-output name=rc-dockerfile::${RC_DOCKERFILE}"
echo "rc-dockerfile=${RC_DOCKERFILE}" >> $GITHUB_OUTPUT

echo "RC_DOCKER_TAG: ${RC_DOCKER_TAG}"
echo "::set-output name=rc-docker-tag::${RC_DOCKER_TAG}"
echo "rc-docker-tag=${RC_DOCKER_TAG}" >> $GITHUB_OUTPUT

- uses: actions/checkout@v3

Expand Down Expand Up @@ -639,7 +639,7 @@ jobs:
LOWERCASE_REPOSITORY=$(echo "${{ github.repository_owner }}" | tr "[:upper:]" "[:lower:]")

echo "LOWERCASE_REPOSITORY: ${LOWERCASE_REPOSITORY}"
echo "::set-output name=lowercase-repo::${LOWERCASE_REPOSITORY}"
echo "lowercase-repo=${LOWERCASE_REPOSITORY}" >> $GITHUB_OUTPUT

- name: Start containers
env:
Expand Down Expand Up @@ -777,7 +777,7 @@ jobs:
LOWERCASE_REPOSITORY=$(echo "${{ github.repository_owner }}" | tr "[:upper:]" "[:lower:]")

echo "LOWERCASE_REPOSITORY: ${LOWERCASE_REPOSITORY}"
echo "::set-output name=lowercase-repo::${LOWERCASE_REPOSITORY}"
echo "lowercase-repo=${LOWERCASE_REPOSITORY}" >> $GITHUB_OUTPUT

- name: Start containers
env:
Expand Down Expand Up @@ -992,7 +992,7 @@ jobs:

echo "GH_IMAGE_NAME: $GH_IMAGE_NAME"

echo "::set-output name=gh-image-name::${GH_IMAGE_NAME}"
echo "gh-image-name=${GH_IMAGE_NAME}" >> $GITHUB_OUTPUT

- name: Pull Docker image
run: docker pull ${{ steps.gh-docker.outputs.gh-image-name }}
Expand Down Expand Up @@ -1069,7 +1069,7 @@ jobs:

echo "GH_IMAGE_NAME: $GH_IMAGE_NAME"

echo "::set-output name=gh-image-name::${GH_IMAGE_NAME}"
echo "gh-image-name=${GH_IMAGE_NAME}" >> $GITHUB_OUTPUT

- name: Pull Docker image
run: docker pull ${{ steps.gh-docker.outputs.gh-image-name }}
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/2fa/server/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function isAuthorizedForToken(connection: IMethodConnection, user: IUser,
return false;
}

if (tokenObject.bypassTwoFactor === true) {
if ('bypassTwoFactor' in tokenObject && tokenObject.bypassTwoFactor === true) {
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

Expand Down
25 changes: 13 additions & 12 deletions apps/meteor/app/api/server/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { Match, check } from 'meteor/check';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import type { IExportOperation, IPersonalAccessToken, IUser } from '@rocket.chat/core-typings';
import type { IExportOperation, ILoginToken, IPersonalAccessToken, IUser } from '@rocket.chat/core-typings';
import { Users as UsersRaw } from '@rocket.chat/models';
import type { Filter } from 'mongodb';
import { Team, api } from '@rocket.chat/core-services';
Expand Down Expand Up @@ -673,16 +673,17 @@ API.v1.addRoute(

const user = Users.getLoginTokensByUserId(this.userId).fetch()[0] as IUser | undefined;

const isPersonalAccessToken = (loginToken: ILoginToken | IPersonalAccessToken): loginToken is IPersonalAccessToken =>
'type' in loginToken && loginToken.type === 'personalAccessToken';

return API.v1.success({
tokens:
user?.services?.resume?.loginTokens
?.filter((loginToken: any) => loginToken.type === 'personalAccessToken')
.map((loginToken: IPersonalAccessToken) => ({
name: loginToken.name,
createdAt: loginToken.createdAt.toISOString(),
lastTokenPart: loginToken.lastTokenPart,
bypassTwoFactor: Boolean(loginToken.bypassTwoFactor),
})) || [],
user?.services?.resume?.loginTokens?.filter(isPersonalAccessToken).map((loginToken) => ({
name: loginToken.name,
createdAt: loginToken.createdAt.toISOString(),
lastTokenPart: loginToken.lastTokenPart,
bypassTwoFactor: Boolean(loginToken.bypassTwoFactor),
})) || [],
});
},
},
Expand Down Expand Up @@ -862,12 +863,12 @@ API.v1.addRoute(

const token = me.services?.resume?.loginTokens?.find((token) => token.hashedToken === hashedToken);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const tokenExpires = new Date(token!.when.getTime() + settings.get<number>('Accounts_LoginExpiration') * 1000);
const tokenExpires =
(token && 'when' in token && new Date(token.when.getTime() + settings.get<number>('Accounts_LoginExpiration') * 1000)) || undefined;

return API.v1.success({
token: xAuthToken,
tokenExpires: tokenExpires.toISOString() || '',
tokenExpires: tokenExpires?.toISOString() || '',
});
},
},
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/client/lib/userData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export const synchronizeUserData = async (uid: Meteor.User['_id']): Promise<RawU
...(resume.loginTokens && {
loginTokens: resume.loginTokens.map((token) => ({
...token,
when: new Date(token.when),
createdAt: (token.createdAt ? new Date(token.createdAt) : undefined) as Date,
when: new Date('when' in token ? token.when : ''),
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
createdAt: ('createdAt' in token ? new Date(token.createdAt) : undefined) as Date,
twoFactorAuthorizedUntil: token.twoFactorAuthorizedUntil ? new Date(token.twoFactorAuthorizedUntil) : undefined,
})),
}),
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
trace: 'retain-on-failure',
baseURL: constants.BASE_URL,
screenshot: process.env.CI ? 'off' : 'only-on-failure',
video: process.env.CI ? 'off' : 'retain-on-failure',
channel: 'chrome',
launchOptions: {
// force GPU hardware acceleration
Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/tests/e2e/administration-menu.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IS_EE } from './config/constants';
import { Users } from './fixtures/userStates';
import { HomeDiscussion } from './page-objects';
import { test, expect } from './utils/test';

test.use({ storageState: 'admin-session.json' });
test.use({ storageState: Users.admin.state });

test.describe.serial('administration-menu', () => {
let poHomeDiscussion: HomeDiscussion;
Expand Down Expand Up @@ -33,7 +34,7 @@ test.describe.serial('administration-menu', () => {
});

test.describe('user', () => {
test.use({ storageState: 'user1-session.json' });
test.use({ storageState: Users.user1.state });

test('expect to not render administration menu when no permission', async ({ page }) => {
await expect(page.locator('role=button[name="Administration"]')).not.toBeVisible();
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/tests/e2e/administration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { faker } from '@faker-js/faker';

import { IS_EE } from './config/constants';
import { Users } from './fixtures/userStates';
import { Admin } from './page-objects';
import { test, expect } from './utils/test';

test.use({ storageState: 'admin-session.json' });
test.use({ storageState: Users.admin.state });

test.describe.parallel('administration', () => {
let poAdmin: Admin;
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/tests/e2e/apps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Users } from './fixtures/userStates';
import { HomeChannel } from './page-objects';
import { expect, test } from './utils/test';

test.use({ storageState: 'user1-session.json' });
test.use({ storageState: Users.user1.state });

test.describe.serial('Apps', () => {
let poHomeChannel: HomeChannel;
Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/tests/e2e/channel-management.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import faker from '@faker-js/faker';
import type { Page } from '@playwright/test';

import { Users } from './fixtures/userStates';
import { HomeChannel } from './page-objects';
import { createTargetChannel } from './utils';
import { test, expect } from './utils/test';

test.use({ storageState: 'admin-session.json' });
test.use({ storageState: Users.admin.state });

test.describe.serial('channel-management', () => {
let poHomeChannel: HomeChannel;
Expand All @@ -14,7 +15,7 @@ test.describe.serial('channel-management', () => {

test.beforeAll(async ({ api, browser }) => {
targetChannel = await createTargetChannel(api);
regularUserPage = await browser.newPage({ storageState: 'user2-session.json' });
regularUserPage = await browser.newPage({ storageState: Users.user2.state });
await regularUserPage.goto('/home');
await regularUserPage.waitForSelector('[data-qa-id="home-header"]');
});
Expand Down
43 changes: 1 addition & 42 deletions apps/meteor/tests/e2e/config/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,8 @@
/* eslint no-await-in-loop: 0 */

import { chromium, expect } from '@playwright/test';

import injectInitialData from '../fixtures/inject-initial-data';
import insertApp from '../fixtures/insert-apps';
import * as constants from './constants';

const loginProcedure = async (credentials: { name: string; username: string; password: string }) => {
const browser = await chromium.launch();
const page = await browser.newPage();

await page.goto(constants.BASE_URL);

await page.locator('[name=username]').type(credentials.username);
await page.locator('[name=password]').type(credentials.password);
await page.locator('role=button >> text="Login"').click();

await expect(await page.locator('role=button >> text="Login"')).toHaveCount(0);

await page.context().storageState({ path: `${credentials.name}-session.json` });

await browser.close();
};

export default async function (): Promise<void> {
const browser = await chromium.launch();
const page = await browser.newPage();

await page.goto(constants.BASE_URL);

await loginProcedure({
name: 'admin',
username: constants.ADMIN_CREDENTIALS.email,
password: constants.ADMIN_CREDENTIALS.password,
});

const { usersFixtures } = await injectInitialData();

for (const user of usersFixtures) {
await loginProcedure({
username: user.username,
password: 'any_password',
name: user.username,
});
}
await injectInitialData();

await insertApp();
}
3 changes: 2 additions & 1 deletion apps/meteor/tests/e2e/create-channel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { faker } from '@faker-js/faker';

import { Users } from './fixtures/userStates';
import { HomeChannel } from './page-objects';
import { test, expect } from './utils/test';

test.use({ storageState: 'admin-session.json' });
test.use({ storageState: Users.admin.state });

test.describe.serial('channel-management', () => {
let poHomeChannel: HomeChannel;
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/tests/e2e/create-direct.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Users } from './fixtures/userStates';
import { HomeChannel } from './page-objects';
import { test, expect } from './utils/test';

test.use({ storageState: 'admin-session.json' });
test.use({ storageState: Users.admin.state });

test.describe.serial('channel-direct-message', () => {
let poHomeChannel: HomeChannel;
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/tests/e2e/create-discussion.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { faker } from '@faker-js/faker';

import { Users } from './fixtures/userStates';
import { HomeDiscussion } from './page-objects';
import { test, expect } from './utils/test';

test.use({ storageState: 'admin-session.json' });
test.use({ storageState: Users.admin.state });

test.describe.serial('create-discussion', () => {
let poHomeDiscussion: HomeDiscussion;
Expand Down
Loading