🔧 Update Playwright config and tests to use env variables (#1266)

This commit is contained in:
Alejandra
2024-07-23 14:59:39 -05:00
committed by GitHub
parent aff2146d7e
commit 06a2ec8a2b
7 changed files with 54 additions and 9 deletions

View File

@@ -1,11 +1,13 @@
import { test as setup } from "@playwright/test"
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
const authFile = "playwright/.auth/user.json"
setup("authenticate", async ({ page }) => {
await page.goto("/login")
await page.getByPlaceholder("Email").fill("admin@example.com")
await page.getByPlaceholder("Password").fill("changethis")
await page.getByPlaceholder("Email").fill(firstSuperuser)
await page.getByPlaceholder("Password").fill(firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
await page.context().storageState({ path: authFile })

21
frontend/tests/config.ts Normal file
View File

@@ -0,0 +1,21 @@
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config({ path: path.join(__dirname, '../../.env') });
const { FIRST_SUPERUSER, FIRST_SUPERUSER_PASSWORD } = process.env;
if (typeof FIRST_SUPERUSER !== "string") {
throw new Error("Environment variable FIRST_SUPERUSER is undefined");
}
if (typeof FIRST_SUPERUSER_PASSWORD !== "string") {
throw new Error("Environment variable FIRST_SUPERUSER_PASSWORD is undefined");
}
export const firstSuperuser = FIRST_SUPERUSER as string;
export const firstSuperuserPassword = FIRST_SUPERUSER_PASSWORD as string;

View File

@@ -1,4 +1,5 @@
import { type Page, expect, test } from "@playwright/test"
import { firstSuperuser, firstSuperuserPassword } from "./config.ts"
test.use({ storageState: { cookies: [], origins: [] } })
@@ -46,7 +47,7 @@ test("Forgot Password link is visible", async ({ page }) => {
test("Log in with valid email and password ", async ({ page }) => {
await page.goto("/login")
await fillForm(page, "admin@example.com", "changethis")
await fillForm(page, firstSuperuser, firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
@@ -59,7 +60,7 @@ test("Log in with valid email and password ", async ({ page }) => {
test("Log in with invalid email", async ({ page }) => {
await page.goto("/login")
await fillForm(page, "invalidemail", "changethis")
await fillForm(page, "invalidemail", firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await expect(page.getByText("Invalid email address")).toBeVisible()
@@ -67,8 +68,8 @@ test("Log in with invalid email", async ({ page }) => {
test("Log in with invalid password", async ({ page }) => {
await page.goto("/login")
await fillForm(page, "admin@example.com", "changethat")
// TODO: Add a random password utility
await fillForm(page, firstSuperuser, "changethat")
await page.getByRole("button", { name: "Log In" }).click()
await expect(page.getByText("Incorrect email or password")).toBeVisible()
@@ -79,7 +80,7 @@ test("Log in with invalid password", async ({ page }) => {
test("Successful log out", async ({ page }) => {
await page.goto("/login")
await fillForm(page, "admin@example.com", "changethis")
await fillForm(page, firstSuperuser, firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")
@@ -96,7 +97,7 @@ test("Successful log out", async ({ page }) => {
test("Logged-out user cannot access protected routes", async ({ page }) => {
await page.goto("/login")
await fillForm(page, "admin@example.com", "changethis")
await fillForm(page, firstSuperuser, firstSuperuserPassword)
await page.getByRole("button", { name: "Log In" }).click()
await page.waitForURL("/")