-
Notifications
You must be signed in to change notification settings - Fork 357
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
test: env variable config and automatic setServerAddress [TESTENG-49] #9582
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e07edaa
nothing to see here
JComins000 2993b13
automatic setServerAddress and clean env variables
JComins000 43db0a6
placeholder
JComins000 532c27b
do not require hard-coded row id
johnkim-det 4897c5e
placeholder
JComins000 c19df87
finalizing env var names
JComins000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,29 @@ | ||
import { APIRequest, APIRequestContext, Browser, BrowserContext, Page } from '@playwright/test'; | ||
import { v4 } from 'uuid'; | ||
|
||
import { baseUrl, password, username } from 'e2e/utils/envVars'; | ||
|
||
export class ApiAuthFixture { | ||
apiContext?: APIRequestContext; // we can't get this until login, so may be undefined | ||
readonly request: APIRequest; | ||
readonly browser: Browser; | ||
readonly baseURL: string; | ||
readonly testId = v4(); | ||
_page?: Page; | ||
get page(): Page { | ||
if (this._page === undefined) { | ||
throw new Error('Accessing page object before initialization in authentication'); | ||
} | ||
return this._page; | ||
} | ||
readonly #STATE_FILE_SUFFIX = 'state.json'; | ||
readonly #USERNAME: string; | ||
readonly #PASSWORD: string; | ||
context?: BrowserContext; | ||
readonly #stateFile = `${this.testId}-${this.#STATE_FILE_SUFFIX}`; | ||
browserContext?: BrowserContext; | ||
|
||
constructor(request: APIRequest, browser: Browser, baseURL?: string, existingPage?: Page) { | ||
if (process.env.PW_USER_NAME === undefined) { | ||
throw new Error('username must be defined'); | ||
} | ||
if (process.env.PW_PASSWORD === undefined) { | ||
throw new Error('password must be defined'); | ||
} | ||
if (baseURL === undefined) { | ||
throw new Error('baseURL must be defined in playwright config to use API requests.'); | ||
} | ||
this.#USERNAME = process.env.PW_USER_NAME; | ||
this.#PASSWORD = process.env.PW_PASSWORD; | ||
constructor(request: APIRequest, browser: Browser, baseURL: string, existingPage?: Page) { | ||
this.request = request; | ||
this.browser = browser; | ||
this.baseURL = baseURL; | ||
this._page = existingPage; | ||
} | ||
|
||
async getBearerToken(): Promise<string> { | ||
async getBearerToken(noBearer = false): Promise<string> { | ||
const cookies = (await this.apiContext?.storageState())?.cookies ?? []; | ||
const authToken = cookies.find((cookie) => { | ||
return cookie.name === 'auth'; | ||
|
@@ -48,6 +33,7 @@ export class ApiAuthFixture { | |
'Attempted to retrieve the auth token from the PW apiContext, but it does not exist. Have you called apiAuth.login() yet?', | ||
); | ||
} | ||
if (noBearer) return authToken; | ||
return `Bearer ${authToken}`; | ||
} | ||
|
||
|
@@ -56,10 +42,8 @@ export class ApiAuthFixture { | |
* fixture, the bearer token will be attached to that context. If not a new | ||
* browser context will be created with the cookie. | ||
*/ | ||
async login({ | ||
creds = { password: this.#PASSWORD, username: this.#USERNAME }, | ||
} = {}): Promise<void> { | ||
this.apiContext = this.apiContext || (await this.request.newContext()); | ||
async loginApi({ creds = { password: password(), username: username() } } = {}): Promise<void> { | ||
this.apiContext = this.apiContext || (await this.request.newContext({ baseURL: this.baseURL })); | ||
const resp = await this.apiContext.post('/api/v1/auth/login', { | ||
data: { | ||
...creds, | ||
|
@@ -69,12 +53,26 @@ export class ApiAuthFixture { | |
if (resp.status() !== 200) { | ||
throw new Error(`Login API request has failed with status code ${resp.status()}`); | ||
} | ||
} | ||
|
||
async loginBrowser(page: Page): Promise<void> { | ||
if (this.apiContext === undefined) { | ||
throw new Error('Cannot login browser without first logging in API'); | ||
} | ||
// Save cookie state into the file. | ||
const state = await this.apiContext.storageState({ path: this.#stateFile }); | ||
if (this._page !== undefined) { | ||
const state = await this.apiContext.storageState(); | ||
// add cookies to current page's existing context | ||
this.context = this._page.context(); | ||
await this.context.addCookies(state.cookies); | ||
this.browserContext = this._page.context(); | ||
// replace the domain of api base url with browser base url | ||
state.cookies.forEach((cookie) => { | ||
if (cookie.name === 'auth' && cookie.domain === new URL(this.baseURL).hostname) { | ||
cookie.domain = new URL(baseUrl()).hostname; | ||
} | ||
}); | ||
await this.browserContext.addCookies(state.cookies); | ||
const token = JSON.stringify(await this.getBearerToken(true)); | ||
await page.evaluate((token) => localStorage.setItem('global/auth-token', token), token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was the magic i needed |
||
} | ||
} | ||
|
||
|
@@ -86,6 +84,6 @@ export class ApiAuthFixture { | |
*/ | ||
async dispose(): Promise<void> { | ||
await this.apiContext?.dispose(); | ||
await this.context?.close(); | ||
await this.browserContext?.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { expect as baseExpect, test as baseTest, Page } from '@playwright/test'; | ||
|
||
import { apiUrl, isEE } from 'e2e/utils/envVars'; | ||
import { safeName } from 'e2e/utils/naming'; | ||
import { V1PostUserRequest } from 'services/api-ts-sdk/api'; | ||
|
||
// eslint-disable-next-line no-restricted-imports | ||
import playwrightConfig from '../../../playwright.config'; | ||
|
||
import { ApiAuthFixture } from './api.auth.fixture'; | ||
import { ApiUserFixture } from './api.user.fixture'; | ||
|
@@ -14,6 +14,7 @@ import { UserFixture } from './user.fixture'; | |
|
||
type CustomFixtures = { | ||
dev: DevFixture; | ||
devSetup: Page; | ||
auth: AuthFixture; | ||
apiAuth: ApiAuthFixture; | ||
user: UserFixture; | ||
|
@@ -30,10 +31,9 @@ type CustomWorkerFixtures = { | |
// https://playwright.dev/docs/test-fixtures | ||
export const test = baseTest.extend<CustomFixtures, CustomWorkerFixtures>({ | ||
// get the auth but allow yourself to log in through the api manually. | ||
apiAuth: async ({ playwright, browser, dev, baseURL, newAdmin }, use) => { | ||
await dev.setServerAddress(); | ||
const apiAuth = new ApiAuthFixture(playwright.request, browser, baseURL, dev.page); | ||
await apiAuth.login({ | ||
apiAuth: async ({ playwright, browser, newAdmin, devSetup }, use) => { | ||
const apiAuth = new ApiAuthFixture(playwright.request, browser, apiUrl(), devSetup); | ||
await apiAuth.loginApi({ | ||
creds: { | ||
password: newAdmin.password!, | ||
username: newAdmin.user!.username, | ||
|
@@ -54,6 +54,7 @@ export const test = baseTest.extend<CustomFixtures, CustomWorkerFixtures>({ | |
|
||
// get the existing page but with auth cookie already logged in | ||
authedPage: async ({ apiAuth }, use) => { | ||
await apiAuth.loginBrowser(apiAuth.page); | ||
await use(apiAuth.page); | ||
}, | ||
|
||
|
@@ -64,20 +65,16 @@ export const test = baseTest.extend<CustomFixtures, CustomWorkerFixtures>({ | |
*/ | ||
backgroundApiAuth: [ | ||
async ({ playwright, browser }, use) => { | ||
const backgroundApiAuth = new ApiAuthFixture( | ||
playwright.request, | ||
browser, | ||
playwrightConfig.use?.baseURL, | ||
); | ||
await backgroundApiAuth.login(); | ||
const backgroundApiAuth = new ApiAuthFixture(playwright.request, browser, apiUrl()); | ||
await backgroundApiAuth.loginApi(); | ||
await use(backgroundApiAuth); | ||
await backgroundApiAuth.dispose(); | ||
}, | ||
{ scope: 'worker' }, | ||
], | ||
/** | ||
* Allows calling the user api without a page so that it can run in beforeAll(). You will need to get a bearer | ||
* token by calling backgroundApiUser.apiAuth.login(). This will also provision a page in the background which | ||
* token by calling backgroundApiUser.apiAuth.loginAPI(). This will also provision a page in the background which | ||
* will be disposed of logout(). Before using the page,you need to call dev.setServerAddress() manually and | ||
* then login() again, since setServerAddress logs out as a side effect. | ||
*/ | ||
|
@@ -88,10 +85,18 @@ export const test = baseTest.extend<CustomFixtures, CustomWorkerFixtures>({ | |
}, | ||
{ scope: 'worker' }, | ||
], | ||
dev: async ({ page }, use) => { | ||
const dev = new DevFixture(page); | ||
// eslint-disable-next-line no-empty-pattern | ||
dev: async ({}, use) => { | ||
const dev = new DevFixture(); | ||
await use(dev); | ||
}, | ||
devSetup: [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now we automatically use a dev command on the UI for each page we get (every test). the dev command is set to local storage |
||
async ({ dev, page }, use) => { | ||
await dev.setServerAddress(page); | ||
await use(page); | ||
}, | ||
{ auto: true }, | ||
], | ||
/** | ||
* Creates an admin and logs in as that admin for the duraction of the test suite | ||
*/ | ||
|
@@ -108,11 +113,11 @@ export const test = baseTest.extend<CustomFixtures, CustomWorkerFixtures>({ | |
}, | ||
}), | ||
); | ||
await backgroundApiUser.apiAuth.login({ | ||
await backgroundApiUser.apiAuth.loginApi({ | ||
creds: { password: adminUser.password!, username: adminUser.user!.username }, | ||
}); | ||
await use(adminUser); | ||
await backgroundApiUser.apiAuth.login(); | ||
await backgroundApiUser.apiAuth.loginApi(); | ||
await backgroundApiUser.patchUser(adminUser.user!.id!, { active: false }); | ||
}, | ||
{ scope: 'worker' }, | ||
|
@@ -130,8 +135,7 @@ export const expect = baseExpect.extend({ | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let matcherResult: any; | ||
|
||
const isEE = Boolean(JSON.parse(process.env.PW_EE ?? '""')); | ||
const appTitle = isEE ? 'HPE Machine Learning Development Environment' : 'Determined'; | ||
const appTitle = isEE() ? 'HPE Machine Learning Development Environment' : 'Determined'; | ||
|
||
const getFullTitle = (prefix: string = '') => { | ||
if (prefix === '') { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johnkim-det can you try
.env
with this change?