|
| 1 | +import { describe, expect, it, mock } from "bun:test"; |
| 2 | +import { |
| 3 | + NotFoundError, |
| 4 | + ValidationError, |
| 5 | +} from "../../shared-api/errors/http-error.js"; |
| 6 | +import { handleRequest } from "../../test-utils/handle-request.js"; |
| 7 | +import type { DeleteDraftProfileResult } from "../services/delete-draft-profile.js"; |
| 8 | +import { createAdminTestPlugin } from "../test-utils/create-admin-test-plugin.js"; |
| 9 | + |
| 10 | +describe("POST /:memberId/profile/delete-draft", () => { |
| 11 | + interface SetupOptions { |
| 12 | + memberId?: string; |
| 13 | + authToken?: string | null; |
| 14 | + memberNotFound?: boolean; |
| 15 | + noSlug?: boolean; |
| 16 | + profileNotDraft?: boolean; |
| 17 | + profileNotFound?: boolean; |
| 18 | + serverError?: boolean; |
| 19 | + deleteResult?: DeleteDraftProfileResult; |
| 20 | + } |
| 21 | + |
| 22 | + function setup({ |
| 23 | + memberId = "test-member-id", |
| 24 | + authToken = "admin-token", |
| 25 | + memberNotFound = false, |
| 26 | + noSlug = false, |
| 27 | + profileNotDraft = false, |
| 28 | + profileNotFound = false, |
| 29 | + serverError = false, |
| 30 | + deleteResult, |
| 31 | + }: SetupOptions = {}) { |
| 32 | + const defaultResult: DeleteDraftProfileResult = { |
| 33 | + slug: "test-slug", |
| 34 | + profileDeleted: true, |
| 35 | + profileImageDeleted: true, |
| 36 | + memberUpdated: true, |
| 37 | + hugoRebuildTriggered: true, |
| 38 | + }; |
| 39 | + |
| 40 | + const mockDeleteDraftProfile = mock( |
| 41 | + (): Promise<DeleteDraftProfileResult> => { |
| 42 | + if (memberNotFound) { |
| 43 | + return Promise.reject( |
| 44 | + new NotFoundError(`Member with ID ${memberId} not found`), |
| 45 | + ); |
| 46 | + } |
| 47 | + if (noSlug) { |
| 48 | + return Promise.reject( |
| 49 | + new ValidationError( |
| 50 | + "Member does not have a profile slug. Cannot delete draft profile.", |
| 51 | + ), |
| 52 | + ); |
| 53 | + } |
| 54 | + if (profileNotDraft) { |
| 55 | + return Promise.reject( |
| 56 | + new ValidationError( |
| 57 | + 'Profile for slug "test-slug" is published and cannot be deleted with this action.', |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | + if (profileNotFound) { |
| 62 | + return Promise.reject( |
| 63 | + new NotFoundError("Profile not found for slug: test-slug"), |
| 64 | + ); |
| 65 | + } |
| 66 | + if (serverError) { |
| 67 | + return Promise.reject(new Error("Firestore unavailable")); |
| 68 | + } |
| 69 | + return Promise.resolve(deleteResult ?? defaultResult); |
| 70 | + }, |
| 71 | + ); |
| 72 | + |
| 73 | + const testApp = createAdminTestPlugin({ |
| 74 | + memberAdminService: { |
| 75 | + deleteDraftProfile: mockDeleteDraftProfile, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const headers: Record<string, string> = {}; |
| 80 | + if (authToken) { |
| 81 | + headers["Authorization"] = `Bearer ${authToken}`; |
| 82 | + } |
| 83 | + |
| 84 | + const request = new Request( |
| 85 | + `http://localhost/${memberId}/profile/delete-draft`, |
| 86 | + { |
| 87 | + method: "POST", |
| 88 | + headers, |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + return { testApp, request }; |
| 93 | + } |
| 94 | + |
| 95 | + describe("Authentication", () => { |
| 96 | + it("should return 401 when no authorization header is provided", async () => { |
| 97 | + const { testApp, request } = setup({ authToken: null }); |
| 98 | + |
| 99 | + const response = await handleRequest(testApp, request); |
| 100 | + |
| 101 | + expect(response.status).toBe(401); |
| 102 | + const body = (await response.json()) as { error?: string }; |
| 103 | + expect(body.error).toBe("Missing Authorization header"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should return 403 when non-admin user tries to delete draft profile", async () => { |
| 107 | + const { testApp, request } = setup({ authToken: "non-admin-token" }); |
| 108 | + |
| 109 | + const response = await handleRequest(testApp, request); |
| 110 | + |
| 111 | + expect(response.status).toBe(403); |
| 112 | + const body = (await response.json()) as { error?: string }; |
| 113 | + expect(body.error).toBe("Admin privileges required"); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe("Successful delete", () => { |
| 118 | + it("should return deletion statuses and slug on success", async () => { |
| 119 | + const { testApp, request } = setup({ |
| 120 | + deleteResult: { |
| 121 | + slug: "jane-doe", |
| 122 | + profileDeleted: true, |
| 123 | + profileImageDeleted: true, |
| 124 | + memberUpdated: true, |
| 125 | + hugoRebuildTriggered: true, |
| 126 | + }, |
| 127 | + }); |
| 128 | + |
| 129 | + const response = await handleRequest(testApp, request); |
| 130 | + |
| 131 | + expect(response.status).toBe(200); |
| 132 | + const body = (await response.json()) as { |
| 133 | + success?: boolean; |
| 134 | + slug?: string; |
| 135 | + profileDeleted?: boolean; |
| 136 | + profileImageDeleted?: boolean; |
| 137 | + memberUpdated?: boolean; |
| 138 | + warning?: string; |
| 139 | + }; |
| 140 | + expect(body.success).toBe(true); |
| 141 | + expect(body.slug).toBe("jane-doe"); |
| 142 | + expect(body.profileDeleted).toBe(true); |
| 143 | + expect(body.profileImageDeleted).toBe(true); |
| 144 | + expect(body.memberUpdated).toBe(true); |
| 145 | + expect(body.warning).toBeUndefined(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should include warning when Hugo rebuild fails", async () => { |
| 149 | + const { testApp, request } = setup({ |
| 150 | + deleteResult: { |
| 151 | + slug: "jane-doe", |
| 152 | + profileDeleted: true, |
| 153 | + profileImageDeleted: true, |
| 154 | + memberUpdated: true, |
| 155 | + hugoRebuildTriggered: false, |
| 156 | + }, |
| 157 | + }); |
| 158 | + |
| 159 | + const response = await handleRequest(testApp, request); |
| 160 | + |
| 161 | + expect(response.status).toBe(200); |
| 162 | + const body = (await response.json()) as { |
| 163 | + success?: boolean; |
| 164 | + warning?: string; |
| 165 | + }; |
| 166 | + expect(body.success).toBe(true); |
| 167 | + expect(body.warning).toContain("Hugo rebuild failed"); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe("Error handling", () => { |
| 172 | + it("should return 404 when member not found", async () => { |
| 173 | + const { testApp, request } = setup({ memberNotFound: true }); |
| 174 | + |
| 175 | + const response = await handleRequest(testApp, request); |
| 176 | + |
| 177 | + expect(response.status).toBe(404); |
| 178 | + const body = (await response.json()) as { error?: string }; |
| 179 | + expect(body.error).toContain("not found"); |
| 180 | + }); |
| 181 | + |
| 182 | + it("should return 400 when member has no slug", async () => { |
| 183 | + const { testApp, request } = setup({ noSlug: true }); |
| 184 | + |
| 185 | + const response = await handleRequest(testApp, request); |
| 186 | + |
| 187 | + expect(response.status).toBe(400); |
| 188 | + const body = (await response.json()) as { error?: string }; |
| 189 | + expect(body.error).toContain("profile slug"); |
| 190 | + }); |
| 191 | + |
| 192 | + it("should return 400 when profile is not draft", async () => { |
| 193 | + const { testApp, request } = setup({ profileNotDraft: true }); |
| 194 | + |
| 195 | + const response = await handleRequest(testApp, request); |
| 196 | + |
| 197 | + expect(response.status).toBe(400); |
| 198 | + const body = (await response.json()) as { error?: string }; |
| 199 | + expect(body.error).toContain("published"); |
| 200 | + }); |
| 201 | + |
| 202 | + it("should return 404 when profile not found", async () => { |
| 203 | + const { testApp, request } = setup({ profileNotFound: true }); |
| 204 | + |
| 205 | + const response = await handleRequest(testApp, request); |
| 206 | + |
| 207 | + expect(response.status).toBe(404); |
| 208 | + const body = (await response.json()) as { error?: string }; |
| 209 | + expect(body.error).toContain("Profile not found"); |
| 210 | + }); |
| 211 | + |
| 212 | + it("should return 500 for unexpected errors", async () => { |
| 213 | + const { testApp, request } = setup({ serverError: true }); |
| 214 | + |
| 215 | + const response = await handleRequest(testApp, request); |
| 216 | + |
| 217 | + expect(response.status).toBe(500); |
| 218 | + const body = (await response.json()) as { error?: string }; |
| 219 | + expect(body.error).toBeDefined(); |
| 220 | + expect(body.error).not.toContain("Firestore unavailable"); |
| 221 | + }); |
| 222 | + }); |
| 223 | +}); |
0 commit comments