|
| 1 | +import { beforeEach, describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + app, |
| 4 | + prismaMock, |
| 5 | + request, |
| 6 | + resetAllMocks, |
| 7 | + setAuthenticatedUser, |
| 8 | + TEST_USER, |
| 9 | +} from "./helpers"; |
| 10 | + |
| 11 | +const UUID = "00000000-0000-0000-0000-000000000001"; |
| 12 | + |
| 13 | +describe("Auth Middleware Integration Tests", () => { |
| 14 | + beforeEach(() => { |
| 15 | + resetAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + // ───────────────────────────────────────────── |
| 19 | + // Authentication enforcement |
| 20 | + // ───────────────────────────────────────────── |
| 21 | + |
| 22 | + describe("Protected routes reject unauthenticated requests", () => { |
| 23 | + it("GET /forms returns 401", async () => { |
| 24 | + setAuthenticatedUser(null); |
| 25 | + const res = await app.handle(request("/forms")); |
| 26 | + expect(res.status).toBe(401); |
| 27 | + }); |
| 28 | + |
| 29 | + it("POST /forms returns 401", async () => { |
| 30 | + setAuthenticatedUser(null); |
| 31 | + const res = await app.handle( |
| 32 | + request("/forms", { |
| 33 | + method: "POST", |
| 34 | + body: JSON.stringify({ title: "X" }), |
| 35 | + }), |
| 36 | + ); |
| 37 | + expect(res.status).toBe(401); |
| 38 | + }); |
| 39 | + |
| 40 | + it("GET /forms/:id returns 401", async () => { |
| 41 | + setAuthenticatedUser(null); |
| 42 | + const res = await app.handle(request(`/forms/${UUID}`)); |
| 43 | + expect(res.status).toBe(401); |
| 44 | + }); |
| 45 | + |
| 46 | + it("PUT /forms/:id returns 401", async () => { |
| 47 | + setAuthenticatedUser(null); |
| 48 | + const res = await app.handle( |
| 49 | + request(`/forms/${UUID}`, { |
| 50 | + method: "PUT", |
| 51 | + body: JSON.stringify({ title: "X" }), |
| 52 | + }), |
| 53 | + ); |
| 54 | + expect(res.status).toBe(401); |
| 55 | + }); |
| 56 | + |
| 57 | + it("DELETE /forms/:id returns 401", async () => { |
| 58 | + setAuthenticatedUser(null); |
| 59 | + const res = await app.handle( |
| 60 | + request(`/forms/${UUID}`, { method: "DELETE" }), |
| 61 | + ); |
| 62 | + expect(res.status).toBe(401); |
| 63 | + }); |
| 64 | + |
| 65 | + it("POST /forms/publish/:id returns 401", async () => { |
| 66 | + setAuthenticatedUser(null); |
| 67 | + const res = await app.handle( |
| 68 | + request(`/forms/publish/${UUID}`, { method: "POST" }), |
| 69 | + ); |
| 70 | + expect(res.status).toBe(401); |
| 71 | + }); |
| 72 | + |
| 73 | + it("POST /forms/unpublish/:id returns 401", async () => { |
| 74 | + setAuthenticatedUser(null); |
| 75 | + const res = await app.handle( |
| 76 | + request(`/forms/unpublish/${UUID}`, { method: "POST" }), |
| 77 | + ); |
| 78 | + expect(res.status).toBe(401); |
| 79 | + }); |
| 80 | + |
| 81 | + it("GET /fields/:formId returns 401", async () => { |
| 82 | + setAuthenticatedUser(null); |
| 83 | + const res = await app.handle(request(`/fields/${UUID}`)); |
| 84 | + expect(res.status).toBe(401); |
| 85 | + }); |
| 86 | + |
| 87 | + it("POST /fields/:formId returns 401", async () => { |
| 88 | + setAuthenticatedUser(null); |
| 89 | + const res = await app.handle( |
| 90 | + request(`/fields/${UUID}`, { |
| 91 | + method: "POST", |
| 92 | + body: JSON.stringify({ |
| 93 | + fieldName: "x", |
| 94 | + fieldValueType: "string", |
| 95 | + fieldType: "text", |
| 96 | + }), |
| 97 | + }), |
| 98 | + ); |
| 99 | + expect(res.status).toBe(401); |
| 100 | + }); |
| 101 | + |
| 102 | + it("GET /responses/:formId returns 401", async () => { |
| 103 | + setAuthenticatedUser(null); |
| 104 | + const res = await app.handle(request(`/responses/${UUID}`)); |
| 105 | + expect(res.status).toBe(401); |
| 106 | + }); |
| 107 | + |
| 108 | + it("GET /responses/my returns 401", async () => { |
| 109 | + setAuthenticatedUser(null); |
| 110 | + const res = await app.handle(request("/responses/my")); |
| 111 | + expect(res.status).toBe(401); |
| 112 | + }); |
| 113 | + |
| 114 | + it("POST /forms/:id/analytics returns 401", async () => { |
| 115 | + setAuthenticatedUser(null); |
| 116 | + const res = await app.handle( |
| 117 | + request(`/forms/${UUID}/analytics`, { method: "POST" }), |
| 118 | + ); |
| 119 | + expect(res.status).toBe(401); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + // ───────────────────────────────────────────── |
| 124 | + // Public routes allow unauthenticated access |
| 125 | + // ───────────────────────────────────────────── |
| 126 | + |
| 127 | + describe("Public routes allow unauthenticated access", () => { |
| 128 | + it("GET /forms/public/:formId works without auth", async () => { |
| 129 | + setAuthenticatedUser(null); |
| 130 | + prismaMock.form.findFirst.mockResolvedValue({ |
| 131 | + id: UUID, |
| 132 | + title: "Public Form", |
| 133 | + description: "Open", |
| 134 | + isPublished: true, |
| 135 | + createdAt: new Date(), |
| 136 | + }); |
| 137 | + prismaMock.formFields.findMany.mockResolvedValue([]); |
| 138 | + |
| 139 | + const res = await app.handle(request(`/forms/public/${UUID}`)); |
| 140 | + const body = await res.json(); |
| 141 | + |
| 142 | + expect(res.status).toBe(200); |
| 143 | + expect(body.success).toBe(true); |
| 144 | + }); |
| 145 | + |
| 146 | + it("GET /fields/public/:formId works without auth", async () => { |
| 147 | + setAuthenticatedUser(null); |
| 148 | + prismaMock.form.count.mockResolvedValue(1); |
| 149 | + prismaMock.formFields.findMany.mockResolvedValue([]); |
| 150 | + |
| 151 | + const res = await app.handle(request(`/fields/public/${UUID}`)); |
| 152 | + const body = await res.json(); |
| 153 | + |
| 154 | + expect(res.status).toBe(200); |
| 155 | + expect(body.success).toBe(true); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + // ───────────────────────────────────────────── |
| 160 | + // Authenticated requests pass user context |
| 161 | + // ───────────────────────────────────────────── |
| 162 | + |
| 163 | + describe("Authenticated requests pass user context", () => { |
| 164 | + it("user.id is available in protected controllers", async () => { |
| 165 | + setAuthenticatedUser(TEST_USER); |
| 166 | + prismaMock.form.findMany.mockResolvedValue([]); |
| 167 | + |
| 168 | + const res = await app.handle(request("/forms")); |
| 169 | + const body = await res.json(); |
| 170 | + |
| 171 | + expect(res.status).toBe(200); |
| 172 | + expect(body.success).toBe(true); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments