Skip to content

Commit 06a2ec8

Browse files
authored
🔧 Update Playwright config and tests to use env variables (#1266)
1 parent aff2146 commit 06a2ec8

File tree

7 files changed

+54
-9
lines changed

7 files changed

+54
-9
lines changed

frontend/package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@types/react": "^18.2.37",
3838
"@types/react-dom": "^18.2.15",
3939
"@vitejs/plugin-react-swc": "^3.5.0",
40+
"dotenv": "^16.4.5",
4041
"typescript": "^5.2.2",
4142
"vite": "^5.0.13"
4243
}

frontend/playwright.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig, devices } from '@playwright/test';
22

3+
34
/**
45
* Read environment variables from file.
56
* https://github.com/motdotla/dotenv

frontend/tests/auth.setup.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { test as setup } from "@playwright/test"
2+
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
23

34
const authFile = "playwright/.auth/user.json"
45

6+
57
setup("authenticate", async ({ page }) => {
68
await page.goto("/login")
7-
await page.getByPlaceholder("Email").fill("[email protected]")
8-
await page.getByPlaceholder("Password").fill("changethis")
9+
await page.getByPlaceholder("Email").fill(firstSuperuser)
10+
await page.getByPlaceholder("Password").fill(firstSuperuserPassword)
911
await page.getByRole("button", { name: "Log In" }).click()
1012
await page.waitForURL("/")
1113
await page.context().storageState({ path: authFile })

frontend/tests/config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import dotenv from 'dotenv';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = path.dirname(__filename);
7+
8+
dotenv.config({ path: path.join(__dirname, '../../.env') });
9+
10+
const { FIRST_SUPERUSER, FIRST_SUPERUSER_PASSWORD } = process.env;
11+
12+
if (typeof FIRST_SUPERUSER !== "string") {
13+
throw new Error("Environment variable FIRST_SUPERUSER is undefined");
14+
}
15+
16+
if (typeof FIRST_SUPERUSER_PASSWORD !== "string") {
17+
throw new Error("Environment variable FIRST_SUPERUSER_PASSWORD is undefined");
18+
}
19+
20+
export const firstSuperuser = FIRST_SUPERUSER as string;
21+
export const firstSuperuserPassword = FIRST_SUPERUSER_PASSWORD as string;

frontend/tests/login.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type Page, expect, test } from "@playwright/test"
2+
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
23

34
test.use({ storageState: { cookies: [], origins: [] } })
45

@@ -46,7 +47,7 @@ test("Forgot Password link is visible", async ({ page }) => {
4647
test("Log in with valid email and password ", async ({ page }) => {
4748
await page.goto("/login")
4849

49-
await fillForm(page, "[email protected]", "changethis")
50+
await fillForm(page, firstSuperuser, firstSuperuserPassword)
5051
await page.getByRole("button", { name: "Log In" }).click()
5152

5253
await page.waitForURL("/")
@@ -59,16 +60,16 @@ test("Log in with valid email and password ", async ({ page }) => {
5960
test("Log in with invalid email", async ({ page }) => {
6061
await page.goto("/login")
6162

62-
await fillForm(page, "invalidemail", "changethis")
63+
await fillForm(page, "invalidemail", firstSuperuserPassword)
6364
await page.getByRole("button", { name: "Log In" }).click()
6465

6566
await expect(page.getByText("Invalid email address")).toBeVisible()
6667
})
6768

6869
test("Log in with invalid password", async ({ page }) => {
6970
await page.goto("/login")
70-
71-
await fillForm(page, "[email protected]", "changethat")
71+
// TODO: Add a random password utility
72+
await fillForm(page, firstSuperuser, "changethat")
7273
await page.getByRole("button", { name: "Log In" }).click()
7374

7475
await expect(page.getByText("Incorrect email or password")).toBeVisible()
@@ -79,7 +80,7 @@ test("Log in with invalid password", async ({ page }) => {
7980
test("Successful log out", async ({ page }) => {
8081
await page.goto("/login")
8182

82-
await fillForm(page, "[email protected]", "changethis")
83+
await fillForm(page, firstSuperuser, firstSuperuserPassword)
8384
await page.getByRole("button", { name: "Log In" }).click()
8485

8586
await page.waitForURL("/")
@@ -96,7 +97,7 @@ test("Successful log out", async ({ page }) => {
9697
test("Logged-out user cannot access protected routes", async ({ page }) => {
9798
await page.goto("/login")
9899

99-
await fillForm(page, "[email protected]", "changethis")
100+
await fillForm(page, firstSuperuser, firstSuperuserPassword)
100101
await page.getByRole("button", { name: "Log In" }).click()
101102

102103
await page.waitForURL("/")

frontend/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
"noUnusedParameters": true,
2121
"noFallthroughCasesInSwitch": true
2222
},
23-
"include": ["src"],
23+
"include": ["src", "*.ts", "**/*.ts"],
2424
"references": [{ "path": "./tsconfig.node.json" }]
2525
}

0 commit comments

Comments
 (0)