From e946b48af1e1f3a128a0dffa0f5cc7c8e3e3fdc5 Mon Sep 17 00:00:00 2001 From: tanner-ricks <182143365+tanner-ricks@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:16:04 -0500 Subject: [PATCH 1/2] Initial commit --- playwright.config.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/playwright.config.ts b/playwright.config.ts index c1def5e0c..8fdb4921f 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -23,7 +23,9 @@ const config: PlaywrightTestConfig = { reporter: 'html', /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { - baseURL: 'http://localhost:8899/', + baseURL: `${process.env.SBL_PLAYWRIGHT_TEST_TARGET_PROTOCOL ?? 'http'}://${ + process.env.SBL_PLAYWRIGHT_TEST_TARGET_HOST ?? 'localhost' + }:${process.env.SBL_PLAYWRIGHT_TEST_TARGET_PORT ?? '8899'}/`, /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', From 580234963096617c658aea87aa3547e4f6fce2b2 Mon Sep 17 00:00:00 2001 From: tanner-ricks <182143365+tanner-ricks@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:37:30 -0500 Subject: [PATCH 2/2] Added envars for all playwright test stuff --- .env.example | 13 +++++++++- .github/workflows/test.yml | 11 +++++++++ ENV-GUIDE.md | 11 +++++++++ e2e/utils/authConstants.ts | 43 +++++++++++++++++++++++++++++++++ e2e/utils/createKeycloakUser.ts | 17 +++++++------ e2e/utils/getKeycloakToken.ts | 16 +++++++----- playwright.config.ts | 6 ++--- src/vite-env.d.ts | 11 +++++++++ 8 files changed, 110 insertions(+), 18 deletions(-) create mode 100644 e2e/utils/authConstants.ts diff --git a/.env.example b/.env.example index 29fde1bcd..7711e1588 100644 --- a/.env.example +++ b/.env.example @@ -9,4 +9,15 @@ SBL_LOGOUT_REDIRECT_URL="" SBL_VALIDATION_TIMEOUT_SECONDS="1200" SBL_LONGPOLLING_DELAY_SECONDS="backoff" SBL_UPLOAD_FILE_SIZE_LIMIT_BYTES="50000000" -SBL_ENABLE_PLAYWRIGHT_TEST_SETTINGS="false" \ No newline at end of file +SBL_ENABLE_PLAYWRIGHT_TEST_SETTINGS="false" +SBL_PLAYWRIGHT_TEST_TARGET="http://localhost:8899" +SBL_PLAYWRIGHT_TEST_KC_TARGET="http://localhost:8880/" +SBL_PLAYWRIGHT_TEST_KC_REALM="regtech" +SBL_PLAYWRIGHT_TEST_KC_CLI_USERNAME="admin" +SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_ID="admin-cli" +SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_SECRET="local_test_only" +SBL_PLAYWRIGHT_TEST_KC_CLI_GRANT_TYPE="client_credentials" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_USERNAME="admin1" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_PASSWORD="admin" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_CLIENT_ID="regtech-client" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_GRANT_TYPE="password" \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c1e684ca9..cb915dd15 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,6 +19,17 @@ env: SBL_LONGPOLLING_DELAY_SECONDS: "backoff" SBL_UPLOAD_FILE_SIZE_LIMIT_BYTES: "50000000" SBL_ENABLE_PLAYWRIGHT_TEST_SETTINGS: "false" + SBL_PLAYWRIGHT_TEST_TARGET: "http://localhost:8899" + SBL_PLAYWRIGHT_TEST_KC_TARGET: "http://localhost:8880/" + SBL_PLAYWRIGHT_TEST_KC_REALM: "regtech" + SBL_PLAYWRIGHT_TEST_KC_CLI_USERNAME: "admin" + SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_ID: "admin-cli" + SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_SECRET: "local_test_only" + SBL_PLAYWRIGHT_TEST_KC_CLI_GRANT_TYPE: "client_credentials" + SBL_PLAYWRIGHT_TEST_KC_ADMIN_USERNAME: "admin1" + SBL_PLAYWRIGHT_TEST_KC_ADMIN_PASSWORD: "admin" + SBL_PLAYWRIGHT_TEST_KC_ADMIN_CLIENT_ID: "regtech-client" + SBL_PLAYWRIGHT_TEST_KC_ADMIN_GRANT_TYPE: "password" jobs: React: diff --git a/ENV-GUIDE.md b/ENV-GUIDE.md index d683079b3..64fe0a815 100644 --- a/ENV-GUIDE.md +++ b/ENV-GUIDE.md @@ -17,6 +17,17 @@ SBL_VALIDATION_TIMEOUT_SECONDS="1200" SBL_LONGPOLLING_DELAY_SECONDS="backoff" SBL_UPLOAD_FILE_SIZE_LIMIT_BYTES="50000000" SBL_ENABLE_PLAYWRIGHT_TEST_SETTINGS="false" +SBL_PLAYWRIGHT_TEST_TARGET="http://localhost:8899" +SBL_PLAYWRIGHT_TEST_KC_TARGET="http://localhost:8880/" +SBL_PLAYWRIGHT_TEST_KC_REALM="regtech" +SBL_PLAYWRIGHT_TEST_KC_CLI_USERNAME="admin" +SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_ID="admin-cli" +SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_SECRET="local_test_only" +SBL_PLAYWRIGHT_TEST_KC_CLI_GRANT_TYPE="client_credentials" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_USERNAME="admin1" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_PASSWORD="admin" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_CLIENT_ID="regtech-client" +SBL_PLAYWRIGHT_TEST_KC_ADMIN_GRANT_TYPE="password" ``` ### To add a new environment variable diff --git a/e2e/utils/authConstants.ts b/e2e/utils/authConstants.ts new file mode 100644 index 000000000..7434d3e9f --- /dev/null +++ b/e2e/utils/authConstants.ts @@ -0,0 +1,43 @@ +import type { GrantTypes } from '@keycloak/keycloak-admin-client/lib/utils/auth'; + +export interface KeycloakCLIConfig { + username: string; + clientId: string; + clientSecret: string; + grantType: GrantTypes; +} + +export interface KeycloakAdminConfig { + username: string; + password: string; + clientId: string; + grantType: GrantTypes; +} + +export interface KeycloakConfig { + target: string; + realm: string; + admin: KeycloakAdminConfig; + cli: KeycloakCLIConfig; +} + +export const config: KeycloakConfig = { + target: process.env.SBL_PLAYWRIGHT_TEST_KC_TARGET ?? 'http://localhost:8880/', + realm: process.env.SBL_PLAYWRIGHT_TEST_KC_REALM ?? 'regtech', + admin: { + username: process.env.SBL_PLAYWRIGHT_TEST_KC_ADMIN_USERNAME ?? 'admin1', + password: process.env.SBL_PLAYWRIGHT_TEST_KC_ADMIN_PASSWORD ?? 'admin', + clientId: + process.env.SBL_PLAYWRIGHT_TEST_KC_ADMIN_CLIENT_ID ?? 'regtech-client', + grantType: (process.env.SBL_PLAYWRIGHT_TEST_KC_ADMIN_GRANT_TYPE ?? + 'password') as GrantTypes, + }, + cli: { + username: process.env.SBL_PLAYWRIGHT_TEST_KC_CLI_USERNAME ?? 'admin', + clientSecret: + process.env.SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_SECRET ?? 'local_test_only', + clientId: process.env.SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_ID ?? 'admin-cli', + grantType: (process.env.SBL_PLAYWRIGHT_TEST_KC_CLI_GRANT_TYPE ?? + 'client_credentials') as GrantTypes, + }, +}; diff --git a/e2e/utils/createKeycloakUser.ts b/e2e/utils/createKeycloakUser.ts index f40941be7..334ef02e8 100644 --- a/e2e/utils/createKeycloakUser.ts +++ b/e2e/utils/createKeycloakUser.ts @@ -1,19 +1,20 @@ import { KeycloakAdminClient } from '@s3pweb/keycloak-admin-client-cjs'; +import { config } from './authConstants'; export class KeycloakService { private readonly kcAdminClient: KeycloakAdminClient; constructor() { this.kcAdminClient = new KeycloakAdminClient({ - baseUrl: 'http://localhost:8880/', - realmName: 'regtech', + baseUrl: config.target, + realmName: config.realm, }); } } const kcAdminClient = new KeycloakAdminClient({ - baseUrl: 'http://localhost:8880/', - realmName: 'regtech', + baseUrl: config.target, + realmName: config.realm, }); interface CreateKeycloakUserProperties { @@ -35,10 +36,10 @@ export default async function createKeycloakUser({ // Authorize with username / password try { await kcAdminClient.auth({ - username: 'admin', - clientId: 'admin-cli', - clientSecret: 'local_test_only', - grantType: 'client_credentials', + username: config.cli.username, + clientId: config.cli.clientId, + clientSecret: config.cli.clientSecret, + grantType: config.cli.grantType, }); } catch (error) { // eslint-disable-next-line no-console diff --git a/e2e/utils/getKeycloakToken.ts b/e2e/utils/getKeycloakToken.ts index e97c48c96..f01a5d185 100644 --- a/e2e/utils/getKeycloakToken.ts +++ b/e2e/utils/getKeycloakToken.ts @@ -1,15 +1,16 @@ import axios from 'axios'; +import { config } from './authConstants'; export default async function getAdminKeycloakToken(): Promise { const encodedParameters = new URLSearchParams(); - encodedParameters.set('username', 'admin1'); - encodedParameters.set('password', 'admin'); - encodedParameters.set('grant_type', 'password'); - encodedParameters.set('client_id', 'regtech-client'); + encodedParameters.set('username', config.admin.username); + encodedParameters.set('password', config.admin.password); + encodedParameters.set('grant_type', config.admin.grantType); + encodedParameters.set('client_id', config.admin.clientId); const optionsForGetAdminKeycloakToken = { method: 'POST', - url: 'http://localhost:8880/realms/regtech/protocol/openid-connect/token', + url: `${config.target}/realms/${config.realm}/protocol/openid-connect/token`, data: encodedParameters, }; try { @@ -17,7 +18,10 @@ export default async function getAdminKeycloakToken(): Promise { return data.access_token as string; } catch (error) { // eslint-disable-next-line no-console - console.log('error when trying to fetch an admin token from keycloak :>> ', error); + console.log( + 'error when trying to fetch an admin token from keycloak :>> ', + error, + ); throw error; } } diff --git a/playwright.config.ts b/playwright.config.ts index 8fdb4921f..5e255ca82 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -23,9 +23,9 @@ const config: PlaywrightTestConfig = { reporter: 'html', /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { - baseURL: `${process.env.SBL_PLAYWRIGHT_TEST_TARGET_PROTOCOL ?? 'http'}://${ - process.env.SBL_PLAYWRIGHT_TEST_TARGET_HOST ?? 'localhost' - }:${process.env.SBL_PLAYWRIGHT_TEST_TARGET_PORT ?? '8899'}/`, + baseURL: `${ + process.env.SBL_PLAYWRIGHT_TEST_TARGET ?? 'http://localhost:8899' + }/`, /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 6607d7cce..0f42ab885 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -18,6 +18,17 @@ interface ImportMetaEnv { readonly SBL_LONGPOLLING_DELAY_SECONDS: string; readonly SBL_UPLOAD_FILE_SIZE_LIMIT_BYTES: string; readonly SBL_ENABLE_PLAYWRIGHT_TEST_SETTINGS: string; + readonly SBL_PLAYWRIGHT_TEST_TARGET: string; + readonly SBL_PLAYWRIGHT_TEST_KC_TARGET: string; + readonly SBL_PLAYWRIGHT_TEST_KC_REALM: string; + readonly SBL_PLAYWRIGHT_TEST_KC_CLI_USERNAME: string; + readonly SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_ID: string; + readonly SBL_PLAYWRIGHT_TEST_KC_CLI_CLIENT_SECRET: string; + readonly SBL_PLAYWRIGHT_TEST_KC_CLI_GRANT_TYPE: string; + readonly SBL_PLAYWRIGHT_TEST_KC_ADMIN_USERNAME: string; + readonly SBL_PLAYWRIGHT_TEST_KC_ADMIN_PASSWORD: string; + readonly SBL_PLAYWRIGHT_TEST_KC_ADMIN_CLIENT_ID: string; + readonly SBL_PLAYWRIGHT_TEST_KC_ADMIN_GRANT_TYPE: string; } interface ImportMeta {