Visitenbuch/tests/e2e/login.test.ts
2024-05-14 00:40:10 +02:00

49 lines
1.8 KiB
TypeScript

import { test, expect } from "@playwright/test";
import {
isLoggedIn, loginIfNecessary, loginWithToken, USERNAME, OIDC_BASE_URL,
} from "$tests/helpers/login";
test.describe.configure({ mode: "serial" }); // Parallel account creation may cause issues
test("login", async ({ page }) => {
await page.goto("/");
await loginIfNecessary(page);
await expect(page).toHaveTitle("Visitenbuch");
await expect(page.locator("h1.heading")).toHaveText("Hallo, Lucy Login");
// Test cases may create more entries
expect(parseInt(await page.getByTestId("n-entries-todo").innerText()))
.toBeGreaterThanOrEqual(193);
});
test("loginWithToken", async ({ page }) => {
await loginWithToken(page);
await page.goto("/");
await expect(page).toHaveTitle("Visitenbuch");
await expect(page.locator("h1.heading")).toHaveText("Hallo, " + USERNAME);
// Test cases may create more entries
expect(parseInt(await page.getByTestId("n-entries-todo").innerText()))
.toBeGreaterThanOrEqual(193);
});
test("logout", async ({ page, baseURL }) => {
await page.goto("/");
await loginIfNecessary(page);
await page.goto("/logout");
await page.getByTestId("btn-logout").click();
await page.locator('button[value="yes"]').click();
await page.waitForURL("/login?noAuto=1");
await expect(page.getByTestId("btn-login")).toBeVisible();
expect(await isLoggedIn(page)).toBe(false);
// Check if application is not accessible if unauthorized
await page.goto("/plan");
expect(page.url() === baseURL + "/login?returnURL=%2Fplan" || page.url().startsWith(OIDC_BASE_URL)).toBe(true);
// Check if TRPC API is not accessible if unauthorized
const apiResponse = await page.context().request.get("/trpc/savedFilter.getAll");
expect(apiResponse.status()).toBe(401);
expect(await apiResponse.json()).toMatchObject({ error: { message: "not logged in" } });
});