Fix: Add testing

This commit is contained in:
Aron Malcher 2024-03-01 20:50:11 +01:00
parent d022d9fcf6
commit ed6195e1e2
Signed by: aronmal
GPG key ID: 816B7707426FC612
22 changed files with 338 additions and 87 deletions

122
e2e/auth.spec.ts Normal file
View file

@ -0,0 +1,122 @@
import { createId } from "@paralleldrive/cuid2";
import { expect, test, type BrowserContext, type Page } from "@playwright/test";
import { Lucia, type Cookie } from "lucia";
import createClient from "openapi-fetch";
import * as schema from "~/drizzle/schema";
import { paths } from "~/types/discord";
import { DrizzlePostgreSQLAdapter } from "@lucia-auth/adapter-drizzle";
import "dotenv/config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
const queryClient = postgres(process.env.DATABASE_URL!);
const db = drizzle(queryClient, {
schema,
});
const adapter = new DrizzlePostgreSQLAdapter(db, schema.sessions, schema.users);
export const lucia = new Lucia(adapter, {
getUserAttributes: (attributes) => attributes,
});
let context: BrowserContext;
let page: Page;
let sessionCookie: Cookie | undefined;
test.describe.serial("User auth process", () => {
test.beforeAll(async ({ browser }) => {
context = await browser.newContext();
page = await context.newPage();
});
test.beforeEach(async () => {
if (!sessionCookie) return;
const sameSiteProps = {
lax: "Lax",
strict: "Strict",
none: "None",
} as const;
const expires = sessionCookie.attributes.expires
? sessionCookie.attributes.expires.getTime() / 1000
: undefined;
const sameSite = sessionCookie.attributes.sameSite
? sameSiteProps[sessionCookie.attributes.sameSite]
: undefined;
await context.addCookies([
{
name: sessionCookie.name,
value: sessionCookie.value,
...sessionCookie.attributes,
sameSite,
expires,
secure: false,
domain: "localhost",
path: "/",
},
]);
});
test.afterAll(async () => {
await context.close();
});
test("Landing page", async () => {
await page.goto("/");
await page.waitForLoadState("load");
expect(await page.screenshot()).toMatchSnapshot("landing_page.png");
});
test("Unauthorized Access Redirect Test", async () => {
await page.goto("/config");
await page.waitForURL("/");
});
test("Generate auth session for further tests", async () => {
const { GET } = createClient<paths>({
baseUrl: "https://discord.com/api/v10",
});
const discordUserResponse = await GET("/users/@me", {
headers: {
Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN}`,
},
});
if (discordUserResponse.error) throw discordUserResponse.error;
const discordUser = discordUserResponse.data;
const userId = createId();
await db.insert(schema.users).values({
id: userId,
discord_id: discordUser.id,
name: discordUser.global_name,
image: discordUser.avatar,
});
const session = await lucia.createSession(
userId,
{},
{ sessionId: createId() },
);
sessionCookie = lucia.createSessionCookie(session.id);
await db
.insert(schema.discordTokens)
.values({
userId,
accessToken: "tokens.accessToken",
expiresAt: sessionCookie.attributes.expires ?? new Date(),
refreshToken: "tokens.refreshToken",
})
.returning()
.execute();
});
test("Landing page when logged in", async () => {
await page.goto("/");
await page.waitForLoadState("load");
expect(await page.screenshot()).toMatchSnapshot(
"landing_page_logged_in.png",
);
});
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 331 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 716 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 710 KiB