Skip to content

Commit

Permalink
PMM-7 added portal setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadym Yarosh committed Aug 17, 2023
1 parent a3fd444 commit 07fcb4b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/portal-tests-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
uses: ./.github/workflows/pmm-version-getter.yml
with:
repository: ${{ inputs.repository || 'dev-latest'}}
matrix_range: 10
matrix_range: 5

portal:
name: 'Portal / Integration'
Expand Down
11 changes: 9 additions & 2 deletions playwright-tests/api/helpers/portalApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ export interface RequestParams {
path: string;
data?: any;
accessToken?: string;
/**
* Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
*/
timeout?: number;
}

interface ContextOptions {
baseURL: string;
extraHTTPHeaders?: { [key: string]: string; };
timeout?: number;
}

const getRequestContext = async ({ accessToken }: { baseURL?: string; accessToken?: string; }): Promise<APIRequestContext> => {
const options: ContextOptions = { baseURL: Constants.portal.url };
const getRequestContext = async (
{ accessToken, timeout }: { accessToken?: string; timeout?: number; },
): Promise<APIRequestContext> => {
const options: ContextOptions = { baseURL: Constants.portal.url, timeout };
if (accessToken) {
options.extraHTTPHeaders = {
Authorization: `Bearer ${accessToken}`,
Expand Down
1 change: 1 addition & 0 deletions playwright-tests/api/portalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const portalAPI = {
return await portalAPIHelper.post({
path: '/v1/orgs',
accessToken,
timeout: 60_000,
data: {
name: orgName,
},
Expand Down
20 changes: 13 additions & 7 deletions playwright-tests/helpers/portalHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { PortalUserRoles } from '@helpers/enums/portalUserRoles';
import { fileHelper } from '@helpers/fileHelper';
import { PortalUser } from '@helpers/types/PortalUser';
import { Constants } from '@helpers/Constants';
import * as dotenv from 'dotenv';
import { ServiceNowResponse } from '@helpers/types/serviceNowResponse.interface';
import { api } from '@api/api';

dotenv.config();

/**
* Collection of methods for Portal tests setup.
*/
Expand All @@ -22,8 +19,7 @@ export const portalHelper = {
let technicalUser: PortalUser;

if (fileHelper.fileExists(Constants.portal.credentialsFile)) {
console.log(`Using existing users from file: ${Constants.portal.credentialsFile}`);
[firstAdmin, secondAdmin, technicalUser] = JSON.parse(fileHelper.readFile(Constants.portal.credentialsFile));
[firstAdmin, secondAdmin, technicalUser] = portalHelper.loadUsersFromFile();
if (!Object.hasOwn(firstAdmin, 'org')) {
const adminToken = await api.portal.getUserAccessToken(firstAdmin.email, firstAdmin.password);
const orgId = Object.hasOwn(firstAdmin, 'org') ? firstAdmin.org!.id
Expand All @@ -39,6 +35,14 @@ export const portalHelper = {
return [firstAdmin, secondAdmin, technicalUser];
},

/**
* Just a wrapper to hold constants
*/
loadUsersFromFile: (): [PortalUser, PortalUser, PortalUser, PortalUser] => {
console.log(`Using existing users from file: ${Constants.portal.credentialsFile}`);
return JSON.parse(fileHelper.readFile(Constants.portal.credentialsFile)) as [PortalUser, PortalUser, PortalUser, PortalUser];
},

/**
* Encapsulates all actions required to create Portal user for tests.
*/
Expand All @@ -47,18 +51,20 @@ export const portalHelper = {
const firstAdmin: PortalUser = await api.okta.createTestUser(credentials.contacts.admin1.email);
const secondAdmin: PortalUser = await api.okta.createTestUser(credentials.contacts.admin2.email);
const technicalUser: PortalUser = await api.okta.createTestUser(credentials.contacts.technical.email);
const freeUser: PortalUser = await api.okta.createTestUser();

const adminToken = await api.portal.getUserAccessToken(firstAdmin.email, firstAdmin.password);
const { org } = await api.portal.createOrg(adminToken);

await api.portal.inviteUserToOrg(adminToken, org.id, secondAdmin.email, PortalUserRoles.admin);
await api.portal.inviteUserToOrg(adminToken, org.id, technicalUser.email, PortalUserRoles.technical);

await api.portal.inviteUserToOrg(adminToken, org.id, freeUser.email, PortalUserRoles.admin);
firstAdmin.org = { id: org.id, role: PortalUserRoles.admin };
secondAdmin.org = { id: org.id, role: PortalUserRoles.admin };
technicalUser.org = { id: org.id, role: PortalUserRoles.technical };
freeUser.org = { id: org.id, role: PortalUserRoles.admin };

return [firstAdmin, secondAdmin, technicalUser];
return [firstAdmin, secondAdmin, technicalUser, freeUser];
},

/**
Expand Down
36 changes: 31 additions & 5 deletions playwright-tests/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import { devices } from '@playwright/test';
import * as dotenv from 'dotenv';
import Duration from '@helpers/enums/Duration';

dotenv.config({ path: '.env.local' });
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
dotenv.config();
dotenv.config({ path: '.env.local' });

const config: PlaywrightTestConfig = {
testDir: './tests',
timeout: Duration.FiveMinutes,
expect: {
timeout: 10_000,
},

fullyParallel: false,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 1 : 0,
workers: 1,
reporter: [
['github'],
['list'],
['html', { open: 'never', outputFolder: './playwright-report' }]
['html', { open: 'never', outputFolder: './playwright-report' }],
],

use: {
Expand All @@ -33,14 +36,37 @@ const config: PlaywrightTestConfig = {
projects: [
{
name: 'chromium',
testDir: `./tests`,
testDir: './tests',
testIgnore: 'tests/portal/*.spec.ts',
use: {
contextOptions: {
ignoreHTTPSErrors: true,
},
screenshot: 'on',
...devices['Desktop Chrome'],
viewport: {
width: 1920, height: 1080,
},
},
},
{
name: 'Portal Setup',
testMatch: 'tests/portal/testUsers.setup.ts',
},
{
name: 'Portal',
dependencies: ['Portal Setup'],
testMatch: 'tests/portal/*.spec.ts',
retries: 0,
use: {
contextOptions: {
ignoreHTTPSErrors: true,
},
screenshot: 'on',
...devices['Desktop Chrome'],
viewport: { width: 1920, height: 1080 },
viewport: {
width: 1920, height: 1080,
},
},
},
],
Expand Down
7 changes: 2 additions & 5 deletions playwright-tests/tests/portal/connectPMM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ test.describe('Spec file for connecting PMM to the portal', async () => {
let secondAdmin: PortalUser;
let technicalUser: PortalUser;

test.beforeAll(async ({ baseURL }) => {
test.beforeAll(async () => {
pmmVersion = (await api.pmm.serverV1.getPmmVersion()).minor;
await api.pmm.settingsV1.changeSettings({
pmm_public_address: baseURL!.replace(/(^\w+:|^)\/\//, ''),
});
[firstAdmin, secondAdmin, technicalUser] = await portalHelper.loadTestUsers();
[firstAdmin, secondAdmin, technicalUser] = portalHelper.loadUsersFromFile();
});

test.beforeEach(async ({ page }) => {
Expand Down
3 changes: 1 addition & 2 deletions playwright-tests/tests/portal/postPmmConnect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ test.describe('Spec file for PMM connected the portal', async () => {
if (!pmmVersion) {
pmmVersion = (await api.pmm.serverV1.getPmmVersion()).minor;
}
[firstAdmin, secondAdmin, technicalUser] = await portalHelper.loadTestUsers();
freeUser = await portalHelper.addRandomUserToOrg(firstAdmin, PortalUserRoles.admin);
[firstAdmin, secondAdmin, technicalUser, freeUser] = portalHelper.loadUsersFromFile();
});

test.beforeEach(async ({ page }) => {
Expand Down
30 changes: 30 additions & 0 deletions playwright-tests/tests/portal/testUsers.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { test as setup } from '@playwright/test';
import { api } from '@api/api';
import { portalHelper } from '@helpers/portalHelper';
import { fileHelper } from '@helpers/fileHelper';
import { Constants } from '@helpers/Constants';

/**
* Extension point: before Portal tests.
* Note that there are only 2 ways to pass any artifacts to tests: environment variables and files
*/
setup('Setup Portal tests', async ({ baseURL }) => {
await setup.step('Add pmm-server settings', async () => {
await api.pmm.settingsV1.changeSettings({
pmm_public_address: baseURL!.replace(/(^\w+:|^)\/\//, ''),
});
});
await setup.step('Remove old credentials file if it\'s there', async () => {
if (fileHelper.fileExists(Constants.portal.credentialsFile)) {
console.log('Found file with Portal test users! Removing...');
await fileHelper.removeFile(Constants.portal.credentialsFile);
}
});
await setup.step('Generate new users and save to file', async () => {
const [firstAdmin, secondAdmin, technicalUser, freeUser] = await portalHelper.createNewUsers();
fileHelper.writeToFile(
Constants.portal.credentialsFile,
JSON.stringify([firstAdmin, secondAdmin, technicalUser, freeUser]),
);
});
});

0 comments on commit 07fcb4b

Please sign in to comment.