Skip to content

Commit

Permalink
🪰 Add auth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse committed Feb 19, 2024
1 parent 7a682e6 commit 41497f8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
3 changes: 3 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { defineConfig, devices } from "@playwright/test";
import { config } from "dotenv";
config();

const baseURL = "http://127.0.0.1:3000";

Expand All @@ -16,6 +18,7 @@ export default defineConfig({
baseURL,
trace: "on-first-retry",
},
globalSetup: "./tests/e2e/globalSetup.ts",

projects: [
{
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/.auth/member.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"cookies": [
{
"name": "authjs.session-token",
"value": "regularUserSession",
"domain": "127.0.0.1",
"path": "/",
"expires": -1,
"httpOnly": true,
"secure": false,
"sameSite": "Lax"
}
],
"origins": []
}
28 changes: 28 additions & 0 deletions tests/e2e/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { db } from "@/drizzle/db";
import { users, sessions, accounts } from "@/drizzle/schema";
import { addDays } from "date-fns";
import { sql } from "drizzle-orm";

async function globalSetup() {
await db.execute(sql`TRUNCATE ${users} CASCADE`);

const [{ userId }] = await db
.insert(users)
.values({ id: "regularUserId", email: "[email protected]" })
.returning({ userId: users.id });

await db.insert(sessions).values({
userId,
sessionToken: "regularUserSession",
expires: addDays(new Date(), 180),
});

await db.insert(accounts).values({
userId,
provider: "mock",
providerAccountId: "mockId",
type: "email",
});
}

export default globalSetup;
27 changes: 26 additions & 1 deletion tests/e2e/homepage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import { test, expect } from "@playwright/test";

test("renders project name", async ({ page }) => {
await page.goto("http://localhost:3000/");
await page.goto("/");

await expect(page).toHaveTitle(/POPGDP/);
});

test("renders without user signed in", async ({ page }) => {
await page.goto("/");

await expect(page.getByRole("button", { name: "Sign in" })).toBeVisible();

await page.click("button:has-text('Sign in')");

await expect(page.getByRole("button", { name: "Sign in" })).toBeVisible();

await new Promise((resolve) => setTimeout(resolve, 5000));

await page.context().storageState({ path: "lol.json" });
});

test("renders with user signed in", async ({ browser }) => {
const context = await browser.newContext({
storageState: "./tests/e2e/.auth/member.json",
});
const page = await context.newPage();

await page.goto("/");

await expect(page.getByRole("button", { name: "Sign out" })).toBeVisible();
});

0 comments on commit 41497f8

Please sign in to comment.