-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapprove-profile.test.ts
More file actions
209 lines (176 loc) · 6.55 KB
/
approve-profile.test.ts
File metadata and controls
209 lines (176 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { describe, expect, it, mock } from "bun:test";
import { Timestamp } from "firebase-admin/firestore";
import {
NotFoundError,
ValidationError,
} from "../../shared-api/errors/http-error.js";
import { handleRequest } from "../../test-utils/handle-request.js";
import type { MemberDocument } from "../../types/member-document.js";
import type { SetProfileEditingPermissionResult } from "../services/approve-profile.js";
import { createAdminTestPlugin } from "../test-utils/create-admin-test-plugin.js";
describe("POST /:memberId/profile/approve", () => {
interface SetupOptions {
body?: Record<string, unknown>;
memberId?: string;
authToken?: string | null;
memberNotFound?: boolean;
serverError?: boolean;
isAdminLookupFails?: boolean;
}
function setup({
body = { allowProfileEditing: true },
memberId = "test-member-id",
authToken = "admin-token",
memberNotFound = false,
serverError = false,
isAdminLookupFails = false,
}: SetupOptions = {}) {
const mockApproveProfile = mock(
({
memberId: approvedMemberId,
allowProfileEditing,
}: {
memberId: string;
allowProfileEditing: boolean;
}): Promise<SetProfileEditingPermissionResult> => {
if (memberNotFound) {
return Promise.reject(
new NotFoundError(`Member with ID ${memberId} not found`),
);
}
if (serverError) {
return Promise.reject(new Error("Firestore unavailable"));
}
const member: MemberDocument = {
uid: approvedMemberId,
email: "member@example.com",
createdAt: Timestamp.now(),
membershipActive: true,
allowProfileEditing,
};
return Promise.resolve({ member });
},
);
const testApp = createAdminTestPlugin({
memberAdminService: {
approveProfile: mockApproveProfile,
isAdmin: mock((): Promise<boolean> => {
if (isAdminLookupFails) {
return Promise.reject(new ValidationError("Lookup failed"));
}
return Promise.resolve(false);
}),
},
});
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (authToken) {
headers["Authorization"] = `Bearer ${authToken}`;
}
const request = new Request(`http://localhost/${memberId}/profile/approve`, {
method: "POST",
headers,
body: JSON.stringify(body),
});
return { testApp, request };
}
describe("Authentication", () => {
it("should return 401 when no authorization header is provided", async () => {
const { testApp, request } = setup({ authToken: null });
const response = await handleRequest(testApp, request);
expect(response.status).toBe(401);
const body = (await response.json()) as { error?: string };
expect(body.error).toBe("Missing Authorization header");
});
it("should return 403 when non-admin user tries to approve profile work", async () => {
const { testApp, request } = setup({ authToken: "non-admin-token" });
const response = await handleRequest(testApp, request);
expect(response.status).toBe(403);
const body = (await response.json()) as { error?: string };
expect(body.error).toBe("Admin privileges required");
});
});
describe("Input validation", () => {
it("should return 422 when allowProfileEditing is missing", async () => {
const { testApp, request } = setup({ body: {} });
const response = await handleRequest(testApp, request);
expect(response.status).toBe(422);
});
it("should return 422 when allowProfileEditing is not a boolean", async () => {
const { testApp, request } = setup({
body: { allowProfileEditing: "yes" },
});
const response = await handleRequest(testApp, request);
expect(response.status).toBe(422);
});
});
describe("Success", () => {
it("should return success with updated member data", async () => {
const { testApp, request } = setup();
const response = await handleRequest(testApp, request);
expect(response.status).toBe(200);
const body = (await response.json()) as {
success?: boolean;
member?: {
uid?: string;
email?: string;
allowProfileEditing?: boolean;
isAdmin?: boolean;
};
};
expect(body.success).toBe(true);
expect(body.member?.uid).toBe("test-member-id");
expect(body.member?.email).toBe("member@example.com");
expect(body.member?.allowProfileEditing).toBe(true);
expect(body.member?.isAdmin).toBe(false);
});
it("should return success when profile editing is disabled", async () => {
const { testApp, request } = setup({
body: { allowProfileEditing: false },
});
const response = await handleRequest(testApp, request);
expect(response.status).toBe(200);
const body = (await response.json()) as {
success?: boolean;
member?: {
allowProfileEditing?: boolean;
};
};
expect(body.success).toBe(true);
expect(body.member?.allowProfileEditing).toBe(false);
});
it("should still return success when isAdmin lookup fails after approval", async () => {
const { testApp, request } = setup({ isAdminLookupFails: true });
const response = await handleRequest(testApp, request);
expect(response.status).toBe(200);
const body = (await response.json()) as {
success?: boolean;
member?: {
allowProfileEditing?: boolean;
isAdmin?: boolean;
};
};
expect(body.success).toBe(true);
expect(body.member?.allowProfileEditing).toBe(true);
expect(body.member?.isAdmin).toBe(false);
});
});
describe("Error handling", () => {
it("should return 404 when member not found", async () => {
const { testApp, request } = setup({ memberNotFound: true });
const response = await handleRequest(testApp, request);
expect(response.status).toBe(404);
const body = (await response.json()) as { error?: string };
expect(body.error).toContain("not found");
});
it("should return 500 for unexpected errors", async () => {
const { testApp, request } = setup({ serverError: true });
const response = await handleRequest(testApp, request);
expect(response.status).toBe(500);
const body = (await response.json()) as { error?: string };
expect(body.error).toBeDefined();
expect(body.error).not.toContain("Firestore unavailable");
});
});
});