馃 distributed transcription service thistle.dunkirk.sh
1import { afterEach, beforeEach, describe, expect, test } from "bun:test"; 2import db from "../db/schema"; 3import { createSession, createUser } from "./auth"; 4import { AppError } from "./errors"; 5import { hasActiveSubscription, requireSubscription } from "./middleware"; 6 7describe("middleware", () => { 8 let testUserId: number; 9 let sessionId: string; 10 11 beforeEach(async () => { 12 // Create test user 13 const user = await createUser( 14 `test-${Date.now()}@example.com`, 15 "0".repeat(64), 16 "Test User", 17 ); 18 testUserId = user.id; 19 sessionId = createSession(testUserId, "127.0.0.1", "test"); 20 }); 21 22 afterEach(() => { 23 // Cleanup 24 db.run("DELETE FROM users WHERE id = ?", [testUserId]); 25 db.run("DELETE FROM subscriptions WHERE user_id = ?", [testUserId]); 26 }); 27 28 describe("hasActiveSubscription", () => { 29 test("returns false when user has no subscription", () => { 30 expect(hasActiveSubscription(testUserId)).toBe(false); 31 }); 32 33 test("returns true when user has active subscription", () => { 34 db.run( 35 "INSERT INTO subscriptions (id, user_id, customer_id, status) VALUES (?, ?, ?, ?)", 36 ["test-sub-1", testUserId, "test-customer", "active"], 37 ); 38 39 expect(hasActiveSubscription(testUserId)).toBe(true); 40 }); 41 42 test("returns true when user has trialing subscription", () => { 43 db.run( 44 "INSERT INTO subscriptions (id, user_id, customer_id, status) VALUES (?, ?, ?, ?)", 45 ["test-sub-2", testUserId, "test-customer", "trialing"], 46 ); 47 48 expect(hasActiveSubscription(testUserId)).toBe(true); 49 }); 50 51 test("returns false when user has canceled subscription", () => { 52 db.run( 53 "INSERT INTO subscriptions (id, user_id, customer_id, status) VALUES (?, ?, ?, ?)", 54 ["test-sub-3", testUserId, "test-customer", "canceled"], 55 ); 56 57 expect(hasActiveSubscription(testUserId)).toBe(false); 58 }); 59 }); 60 61 describe("requireSubscription", () => { 62 test("throws AppError when user has no subscription", () => { 63 const req = new Request("http://localhost/api/test", { 64 headers: { 65 Cookie: `session=${sessionId}`, 66 }, 67 }); 68 69 try { 70 requireSubscription(req); 71 expect(true).toBe(false); // Should not reach here 72 } catch (error) { 73 expect(error instanceof AppError).toBe(true); 74 if (error instanceof AppError) { 75 expect(error.statusCode).toBe(403); 76 expect(error.message).toContain("subscription"); 77 } 78 } 79 }); 80 81 test("succeeds when user has active subscription", () => { 82 db.run( 83 "INSERT INTO subscriptions (id, user_id, customer_id, status) VALUES (?, ?, ?, ?)", 84 ["test-sub-4", testUserId, "test-customer", "active"], 85 ); 86 87 const req = new Request("http://localhost/api/test", { 88 headers: { 89 Cookie: `session=${sessionId}`, 90 }, 91 }); 92 93 const user = requireSubscription(req); 94 expect(user.id).toBe(testUserId); 95 }); 96 97 test("succeeds when user is admin without subscription", () => { 98 // Make user admin 99 db.run("UPDATE users SET role = ? WHERE id = ?", ["admin", testUserId]); 100 101 const req = new Request("http://localhost/api/test", { 102 headers: { 103 Cookie: `session=${sessionId}`, 104 }, 105 }); 106 107 const user = requireSubscription(req); 108 expect(user.id).toBe(testUserId); 109 expect(user.role).toBe("admin"); 110 }); 111 }); 112});