Skip to content

Commit e33bcfa

Browse files
committed
fix: use env variables in tests
fastapi/full-stack-fastapi-template#1266
1 parent 17eaa67 commit e33bcfa

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

frontend/package-lock.json

Lines changed: 2 additions & 3 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
@@ -32,6 +32,7 @@
3232
"clsx": "^2.1.1",
3333
"cssnano": "^7.0.3",
3434
"date-fns": "^3.6.0",
35+
"dotenv": "^16.4.5",
3536
"form-data": "4.0.0",
3637
"framer-motion": "^10.16.16",
3738
"lucide-react": "^0.399.0",

frontend/tests/auth.setup.ts

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

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

56
setup("authenticate", async ({ page }) => {
67
await page.goto("/login")
7-
await page.getByPlaceholder("Email").fill("[email protected]")
8-
await page.getByPlaceholder("Password").fill("changethis")
8+
await page.getByPlaceholder("Email").fill(firstSuperuser)
9+
await page.getByPlaceholder("Password").fill(firstSuperuserPassword)
910
await page.getByRole("button", { name: "Log In" }).click()
1011
await page.waitForURL("/")
1112
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
@@ -26,6 +26,6 @@
2626
"noUnusedParameters": true,
2727
"noFallthroughCasesInSwitch": true
2828
},
29-
"include": ["src"],
29+
"include": ["src", "*.ts", "**/*.ts"],
3030
"references": [{ "path": "./tsconfig.node.json" }]
3131
}

0 commit comments

Comments
 (0)