diff --git a/server/src/app.ts b/server/src/app.ts index 1174446..fc939ad 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -131,15 +131,6 @@ app.get("/feed", async (req: Request, res: Response) => { }); -app.get("/auth/test", async (req: Request, res: Response) => { - const supabase = createClient(req, res); - const user = await authenticate(supabase); - - console.log(user); - - res.status(200); -}); - app.get("/auth/confirm", async (req: Request, res: Response) => { const tokenHash = req.query.token_hash; const typeString = req.query.type; diff --git a/server/test/auth-confirm.ts b/server/test/auth-confirm.ts new file mode 100644 index 0000000..ebf16a9 --- /dev/null +++ b/server/test/auth-confirm.ts @@ -0,0 +1,76 @@ +import { jest, describe, expect, test } from '@jest/globals'; +import express, { type Express, type Request, type Response } from 'express'; +import request from "supertest"; +import { getMockReq, getMockRes } from '@jest-mock/express'; + +jest.unstable_mockModule('../src/database.ts', () => ({ + authenticate: () => {}, + createClient: () => ({ + from: (table) => ({ + insert: ({ id, username }) => ({}) + }), + auth: { + verifyOtp: ({ type, token_hash }) => { + if (token_hash == "bad") return Promise.resolve({ + data: null, + error: "bad hash" + }); + return Promise.resolve({ + data: { + user: { + user_metadata: { + sub: null, + display_name: null + } + } + }, + error: null + }); + } + } + }) +})); +const { default: app } = await import("../src/app.ts"); + +describe("User Email Verification", () => { + test("no params", async () => { + const res = await request(app).get('/auth/confirm'); + expect(res.statusCode).toBe(500); + }); + + test("only token hash", async () => { + const res = await request(app) + .get('/auth/confirm') + .query("token_hash=test"); + expect(res.statusCode).toBe(500); + }); + + test("only token type", async () => { + const res = await request(app) + .get('/auth/confirm') + .query("type=test"); + expect(res.statusCode).toBe(500); + }); + + test("bad auth data", async () => { + const res = await request(app) + .get('/auth/confirm') + .query('token_hash=bad') + .query('type=test'); + expect(res.statusCode).toBe(303); + expect(res.headers.location).toContain('/auth/error'); + + const errorRes = await request(app) + .get('/auth/error'); + expect(errorRes.statusCode).toBe(401); + }); + + test("success auth", async () => { + const res = await request(app) + .get('/auth/confirm') + .query('token_hash=test') + .query('type=test') + expect(res.statusCode).toBe(303); + expect(res.headers.location).not.toContain('/auth/error'); + }); +});