forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization.integration.spec.ts
More file actions
394 lines (333 loc) · 13.4 KB
/
Copy pathauthorization.integration.spec.ts
File metadata and controls
394 lines (333 loc) · 13.4 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import {
ExecutionContext,
ForbiddenException,
UnauthorizedException,
} from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { Test, TestingModule } from "@nestjs/testing";
import { AuthorizationGuard } from "../guards/authorization.guard";
import { AuthorizationService } from "../services/authorization.service";
import { ReceiptPolicyService } from "../../receipts/receipt-policy.service";
describe("Authorization Integration Tests", () => {
let authorizationGuard: AuthorizationGuard;
let authorizationService: AuthorizationService;
let receiptPolicyService: ReceiptPolicyService;
let reflector: Reflector;
const mockUser1 = { id: "user-1", walletAddress: "wallet-1" };
const mockUser2 = { id: "user-2", walletAddress: "wallet-2" };
const mockSplitId = "split-123";
const mockReceiptId = "receipt-123";
const mockDisputeId = "dispute-123";
const mockGroupId = "group-123";
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
AuthorizationGuard,
AuthorizationService,
Reflector,
{
provide: ReceiptPolicyService,
useValue: {
canCreateReceipt: jest.fn(),
canAccessReceipt: jest.fn(),
canDeleteReceipt: jest.fn(),
},
},
],
})
.overrideProvider(AuthorizationService)
.useValue({
canAccessSplit: jest.fn(),
canCreatePayment: jest.fn(),
canAddParticipant: jest.fn(),
canRemoveParticipant: jest.fn(),
canCreatePaymentForParticipant: jest.fn(),
canAccessParticipantPayments: jest.fn(),
canAccessReceipt: jest.fn(),
canAccessDispute: jest.fn(),
isAdmin: jest.fn(),
canAccessGroup: jest.fn(),
canManageGroupMembers: jest.fn(),
canCreateGroupSplit: jest.fn(),
})
.compile();
authorizationGuard = module.get<AuthorizationGuard>(AuthorizationGuard);
authorizationService =
module.get<AuthorizationService>(AuthorizationService);
receiptPolicyService = module.get<ReceiptPolicyService>(ReceiptPolicyService);
reflector = module.get<Reflector>(Reflector);
});
describe("Cross-user access prevention", () => {
it("should prevent user from accessing another user's split", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
const permissions = [{ resource: "split" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canAccessSplit")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should prevent user from creating payment for another user's split", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
const permissions = [
{ resource: "split" as const, action: "create_payment" },
];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canCreatePayment")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should prevent user from accessing another user's receipt", async () => {
const mockContext = createMockContext(mockUser1, {
receiptId: mockReceiptId,
});
const permissions = [{ resource: "receipt" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canAccessReceipt")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should allow standalone receipt uploads for authenticated users", async () => {
const mockContext = createMockContext(mockUser1, {});
const permissions = [{ resource: "receipt" as const, action: "create" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(receiptPolicyService, "canCreateReceipt")
.mockResolvedValue(true);
const result = await authorizationGuard.canActivate(mockContext);
expect(result).toBe(true);
expect(receiptPolicyService.canCreateReceipt).toHaveBeenCalledWith(
mockUser1.id,
undefined,
);
});
it("should enforce split-specific receipt upload permissions", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
const permissions = [{ resource: "receipt" as const, action: "create" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(receiptPolicyService, "canCreateReceipt")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should prevent user from accessing another user's dispute", async () => {
const mockContext = createMockContext(mockUser1, {
disputeId: mockDisputeId,
});
const permissions = [{ resource: "dispute" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canAccessDispute")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should prevent user from managing another user's group", async () => {
const mockContext = createMockContext(mockUser1, { id: mockGroupId });
const permissions = [
{ resource: "group" as const, action: "add_member" },
];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canManageGroupMembers")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should allow access when user has proper permissions", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
const permissions = [{ resource: "split" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canAccessSplit")
.mockResolvedValue(true);
const result = await authorizationGuard.canActivate(mockContext);
expect(result).toBe(true);
});
it("should allow access when no permissions are required", async () => {
const mockContext = createMockContext(mockUser1, {});
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(undefined);
const result = await authorizationGuard.canActivate(mockContext);
expect(result).toBe(true);
});
it("should throw UnauthorizedException when user is not authenticated", async () => {
const mockContext = createMockContext(null, { splitId: mockSplitId });
const permissions = [{ resource: "split" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
UnauthorizedException,
);
});
});
describe("Permission-based access control", () => {
it("should check multiple permissions when required", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
const permissions = [
{ resource: "split" as const, action: "read" },
{ resource: "split" as const, action: "create_payment" },
];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canAccessSplit")
.mockResolvedValue(true);
jest
.spyOn(authorizationService, "canCreatePayment")
.mockResolvedValue(true);
const result = await authorizationGuard.canActivate(mockContext);
expect(result).toBe(true);
expect(authorizationService.canAccessSplit).toHaveBeenCalledWith(
mockUser1.id,
mockSplitId,
);
expect(authorizationService.canCreatePayment).toHaveBeenCalledWith(
mockUser1.id,
mockSplitId,
);
});
it("should deny access when any required permission fails", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
const permissions = [
{ resource: "split" as const, action: "read" },
{ resource: "split" as const, action: "create_payment" },
];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canAccessSplit")
.mockResolvedValue(true);
jest
.spyOn(authorizationService, "canCreatePayment")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
});
describe("Resource-specific authorization", () => {
it("should handle split-specific authorization correctly", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
// Test read permission
jest
.spyOn(reflector, "getAllAndOverride")
.mockReturnValue([{ resource: "split" as const, action: "read" }]);
jest
.spyOn(authorizationService, "canAccessSplit")
.mockResolvedValue(true);
const result = await authorizationGuard.canActivate(mockContext);
expect(result).toBe(true);
// Test update permission
jest
.spyOn(reflector, "getAllAndOverride")
.mockReturnValue([{ resource: "split" as const, action: "update" }]);
jest
.spyOn(authorizationService, "canAccessSplit")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should handle payment-specific authorization correctly", async () => {
const participantId = "participant-123";
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
participantId,
});
// Test payment creation for participant
jest
.spyOn(reflector, "getAllAndOverride")
.mockReturnValue([{ resource: "payment" as const, action: "create" }]);
jest
.spyOn(authorizationService, "canCreatePaymentForParticipant")
.mockResolvedValue(true);
const result = await authorizationGuard.canActivate(mockContext);
expect(result).toBe(true);
expect(
authorizationService.canCreatePaymentForParticipant,
).toHaveBeenCalledWith(mockUser1.id, mockSplitId, participantId);
});
it("should handle group-specific authorization correctly", async () => {
const mockContext = createMockContext(mockUser1, { id: mockGroupId });
// Test group management
jest
.spyOn(reflector, "getAllAndOverride")
.mockReturnValue([
{ resource: "group" as const, action: "add_member" },
]);
jest
.spyOn(authorizationService, "canManageGroupMembers")
.mockResolvedValue(false);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
});
describe("Edge cases and error handling", () => {
it("should handle missing resource IDs gracefully", async () => {
const mockContext = createMockContext(mockUser1, {});
const permissions = [{ resource: "split" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
ForbiddenException,
);
});
it("should handle null/undefined user context", async () => {
const mockContext = createMockContext(null, { splitId: mockSplitId });
const permissions = [{ resource: "split" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
UnauthorizedException,
);
});
it("should handle authorization service errors gracefully", async () => {
const mockContext = createMockContext(mockUser1, {
splitId: mockSplitId,
});
const permissions = [{ resource: "split" as const, action: "read" }];
jest.spyOn(reflector, "getAllAndOverride").mockReturnValue(permissions);
jest
.spyOn(authorizationService, "canAccessSplit")
.mockRejectedValue(new Error("Database error"));
await expect(authorizationGuard.canActivate(mockContext)).rejects.toThrow(
"Database error",
);
});
});
function createMockContext(user: any, params: any): ExecutionContext {
return {
switchToHttp: () => ({
getRequest: () => ({
user,
params,
body: {},
}),
}),
getHandler: () => () => null,
getClass: () => () => null,
} as unknown as ExecutionContext;
}
});