Skip to content

Commit 91b80b7

Browse files
committed
Update auth setup
1 parent 2a7e34e commit 91b80b7

2 files changed

Lines changed: 71 additions & 7 deletions

File tree

load_testing/auth.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
import { SharedArray } from "k6/data"
22

3-
export function getAccessToken(): String {
3+
export type AuthCredential = {
4+
username: string
5+
password: string
6+
}
7+
8+
export function getAccessToken(): string | null {
49
return __ENV.AUTH_ACCESS_TOKEN
510
}
611

7-
export const users = new SharedArray("users", function () {
8-
if (!__ENV.USERS_JSON_FILE) {
9-
return []
12+
function _validate_credentials(credentials) {
13+
if (typeof credentials !== "array") {
14+
throw Error("Expected an array of credentials")
15+
}
16+
17+
for (let index = 0; index < credentials.length; index++) {
18+
const credential = credentials[index]
19+
if (!Object.hasOwn(credential, "email")) {
20+
throw Error(`User entry is missing 'email' at index ${index}`)
21+
}
22+
if (!Object.hasOwn(credential, "password")) {
23+
throw Error(`User entry is missing 'password' at index ${index}`)
24+
}
1025
}
26+
}
27+
28+
export const credentials: AuthCredential[] = new SharedArray(
29+
"credentials",
30+
function () {
31+
if (!__ENV.USERS_JSON_FILE) {
32+
return []
33+
}
34+
35+
const parsed = JSON.parse(open(__ENV.USERS_JSON_FILE))
36+
37+
_validate_credentials(parsed)
1138

12-
return JSON.parse(open(__ENV.USERS_JSON_FILE))
13-
})
39+
return parsed
40+
},
41+
)

load_testing/frontend/test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { check } from "k6"
22
import { browser, Page } from "k6/browser"
3-
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.2.0/index.js"
3+
import {
4+
randomIntBetween,
5+
randomItem,
6+
} from "https://jslib.k6.io/k6-utils/1.2.0/index.js"
47
import { BROWSER_CONTEXT_OPTIONS, FRONTEND_BASE_URL } from "../config.ts"
8+
import { AuthCredential, credentials } from "../auth.ts"
59

610
async function home(page: Page) {
711
await page.goto(FRONTEND_BASE_URL)
@@ -38,15 +42,47 @@ async function units(page: Page) {
3842
await page.goto(`${FRONTEND_BASE_URL}/units`)
3943
}
4044

45+
async function login(page: Page) {
46+
const credential: AuthCredential = randomItem(credentials)
47+
48+
if (!!credential) {
49+
log.debug("Skipping login because no credentials provided")
50+
return
51+
}
52+
53+
const loginButton = page.getByTestId("login-button-desktop")
54+
await loginButton.first().click()
55+
56+
await loginKeycloak(page)
57+
58+
await page.waitForURL(/.*\/dashboard.*/)
59+
}
60+
61+
async function loginKeycloak(page: Page, credential: AuthCredential) {
62+
await page.waitForURL(
63+
/https:\/sso(\-qa)?\.ol\.mit\.edu\/realms\/olapps\/protocol\/openid-connect\/auth.*/,
64+
)
65+
66+
const credentialnameInput = await page.locator("input#username")
67+
await credentialnameInput.type(user.email)
68+
await page.locator("button#kc-login").click()
69+
70+
const passwordInput = await page.locator("input#password")
71+
await passwordInput.type(credential.password)
72+
await page.locator("button#kc-login").click()
73+
}
74+
4175
export async function testFrontend() {
4276
const page = await browser.newPage(BROWSER_CONTEXT_OPTIONS)
4377

4478
try {
79+
// always home page first
4580
await home(page)
4681
await search(page)
4782
await topics(page)
4883
await departments(page)
4984
await units(page)
85+
await login(page)
5086
} finally {
5187
await page.close()
5288
}

0 commit comments

Comments
 (0)