Skip to content

Commit a43cc84

Browse files
author
Lasim
committed
Refactor response handling in tests to use JSON.stringify for consistency
- Updated all relevant test cases to ensure responses are sent as JSON strings. - Added type handling for mock replies to specify 'application/json'. - Ensured that all assertions for response data are correctly parsing the JSON string before validation. - Enhanced error handling tests to align with the new response format.
1 parent a859239 commit a43cc84

File tree

13 files changed

+968
-481
lines changed

13 files changed

+968
-481
lines changed

services/backend/tests/unit/hooks/authHook.test.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('authHook', () => {
4949
// Setup mock reply
5050
mockReply = {
5151
setCookie: vi.fn(),
52-
};
52+
} as any;
5353

5454
// Setup mock Lucia
5555
mockLucia = {
@@ -106,6 +106,7 @@ describe('authHook', () => {
106106
configured: false,
107107
initialized: false,
108108
dialect: null,
109+
type: null,
109110
});
110111

111112
await authHook(mockRequest as FastifyRequest, mockReply as FastifyReply);
@@ -122,6 +123,7 @@ describe('authHook', () => {
122123
configured: true,
123124
initialized: false,
124125
dialect: null,
126+
type: null,
125127
});
126128

127129
await authHook(mockRequest as FastifyRequest, mockReply as FastifyReply);
@@ -138,6 +140,7 @@ describe('authHook', () => {
138140
configured: true,
139141
initialized: true,
140142
dialect: 'sqlite',
143+
type: 'sqlite',
141144
});
142145
});
143146

@@ -325,6 +328,7 @@ describe('requireAuthHook', () => {
325328

326329
mockReply = {
327330
status: vi.fn().mockReturnThis(),
331+
type: vi.fn().mockReturnThis(),
328332
send: vi.fn(),
329333
};
330334
});
@@ -339,9 +343,12 @@ describe('requireAuthHook', () => {
339343
);
340344

341345
expect(mockReply.status).toHaveBeenCalledWith(401);
342-
expect(mockReply.send).toHaveBeenCalledWith({
343-
error: 'Unauthorized: Authentication required.',
344-
});
346+
expect(mockReply.send).toHaveBeenCalledWith(
347+
JSON.stringify({
348+
success: false,
349+
error: 'Unauthorized: Authentication required.'
350+
})
351+
);
345352
});
346353

347354
it('should return 401 when user exists but session is null', async () => {
@@ -354,9 +361,12 @@ describe('requireAuthHook', () => {
354361
);
355362

356363
expect(mockReply.status).toHaveBeenCalledWith(401);
357-
expect(mockReply.send).toHaveBeenCalledWith({
358-
error: 'Unauthorized: Authentication required.',
359-
});
364+
expect(mockReply.send).toHaveBeenCalledWith(
365+
JSON.stringify({
366+
success: false,
367+
error: 'Unauthorized: Authentication required.'
368+
})
369+
);
360370
});
361371

362372
it('should return 401 when session exists but user is null', async () => {
@@ -369,9 +379,12 @@ describe('requireAuthHook', () => {
369379
);
370380

371381
expect(mockReply.status).toHaveBeenCalledWith(401);
372-
expect(mockReply.send).toHaveBeenCalledWith({
373-
error: 'Unauthorized: Authentication required.',
374-
});
382+
expect(mockReply.send).toHaveBeenCalledWith(
383+
JSON.stringify({
384+
success: false,
385+
error: 'Unauthorized: Authentication required.'
386+
})
387+
);
375388
});
376389

377390
it('should complete successfully when user and session are both present', async () => {

services/backend/tests/unit/middleware/roleMiddleware.test.ts

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('Role Middleware', () => {
5555

5656
mockReply = {
5757
status: vi.fn().mockReturnThis(),
58+
type: vi.fn().mockReturnThis(),
5859
send: vi.fn().mockReturnThis(),
5960
};
6061

@@ -91,10 +92,11 @@ describe('Role Middleware', () => {
9192
await middleware(mockRequest as FastifyRequest, mockReply as FastifyReply);
9293

9394
expect(mockReply.status).toHaveBeenCalledWith(401);
94-
expect(mockReply.send).toHaveBeenCalledWith({
95+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
96+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
9597
success: false,
96-
error: 'Authentication required',
97-
});
98+
error: 'Authentication required'
99+
}));
98100
expect(mockRoleServiceInstance.userHasPermission).not.toHaveBeenCalled();
99101
});
100102

@@ -106,11 +108,12 @@ describe('Role Middleware', () => {
106108

107109
expect(mockRoleServiceInstance.userHasPermission).toHaveBeenCalledWith('user-123', 'users.delete');
108110
expect(mockReply.status).toHaveBeenCalledWith(403);
109-
expect(mockReply.send).toHaveBeenCalledWith({
111+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
112+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
110113
success: false,
111114
error: 'Insufficient permissions',
112-
required_permission: 'users.delete',
113-
});
115+
required_permission: 'users.delete'
116+
}));
114117
});
115118

116119
it('should return 500 when permission check fails', async () => {
@@ -125,11 +128,12 @@ describe('Role Middleware', () => {
125128
'Error checking user permissions for permission: users.view'
126129
);
127130
expect(mockReply.status).toHaveBeenCalledWith(500);
128-
expect(mockReply.send).toHaveBeenCalledWith({
131+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
132+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
129133
success: false,
130134
error: 'Internal server error',
131-
details: 'Database connection failed',
132-
});
135+
details: 'Database connection failed'
136+
}));
133137
});
134138

135139
it('should handle non-Error objects in catch block', async () => {
@@ -139,11 +143,12 @@ describe('Role Middleware', () => {
139143
await middleware(mockRequest as FastifyRequest, mockReply as FastifyReply);
140144

141145
expect(mockReply.status).toHaveBeenCalledWith(500);
142-
expect(mockReply.send).toHaveBeenCalledWith({
146+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
147+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
143148
success: false,
144149
error: 'Internal server error',
145-
details: 'Unknown error',
146-
});
150+
details: 'Unknown error'
151+
}));
147152
});
148153
});
149154

@@ -181,9 +186,11 @@ describe('Role Middleware', () => {
181186
await middleware(mockRequest as FastifyRequest, mockReply as FastifyReply);
182187

183188
expect(mockReply.status).toHaveBeenCalledWith(401);
184-
expect(mockReply.send).toHaveBeenCalledWith({
185-
error: 'Authentication required',
186-
});
189+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
190+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
191+
success: false,
192+
error: 'Authentication required'
193+
}));
187194
expect(mockRoleServiceInstance.userHasPermission).not.toHaveBeenCalled();
188195
});
189196

@@ -196,10 +203,12 @@ describe('Role Middleware', () => {
196203
expect(mockRoleServiceInstance.userHasPermission).toHaveBeenCalledWith('user-123', 'users.delete');
197204
expect(mockRoleServiceInstance.userHasPermission).toHaveBeenCalledWith('user-123', 'users.create');
198205
expect(mockReply.status).toHaveBeenCalledWith(403);
199-
expect(mockReply.send).toHaveBeenCalledWith({
206+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
207+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
208+
success: false,
200209
error: 'Insufficient permissions',
201-
required_permissions: ['users.delete', 'users.create'],
202-
});
210+
required_permissions: ['users.delete', 'users.create']
211+
}));
203212
});
204213

205214
it('should return 500 when permission check fails', async () => {
@@ -211,20 +220,24 @@ describe('Role Middleware', () => {
211220

212221
expect(mockRequest.log?.error).toHaveBeenCalledWith(error, 'Error checking user permissions');
213222
expect(mockReply.status).toHaveBeenCalledWith(500);
214-
expect(mockReply.send).toHaveBeenCalledWith({
215-
error: 'Internal server error',
216-
});
223+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
224+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
225+
success: false,
226+
error: 'Internal server error'
227+
}));
217228
});
218229

219230
it('should handle empty permissions array', async () => {
220231
const middleware = requireAnyPermission([]);
221232
await middleware(mockRequest as FastifyRequest, mockReply as FastifyReply);
222233

223234
expect(mockReply.status).toHaveBeenCalledWith(403);
224-
expect(mockReply.send).toHaveBeenCalledWith({
235+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
236+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
237+
success: false,
225238
error: 'Insufficient permissions',
226-
required_permissions: [],
227-
});
239+
required_permissions: []
240+
}));
228241
expect(mockRoleServiceInstance.userHasPermission).not.toHaveBeenCalled();
229242
});
230243
});
@@ -253,10 +266,11 @@ describe('Role Middleware', () => {
253266
await middleware(mockRequest as FastifyRequest, mockReply as FastifyReply);
254267

255268
expect(mockReply.status).toHaveBeenCalledWith(401);
256-
expect(mockReply.send).toHaveBeenCalledWith({
269+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
270+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
257271
success: false,
258-
error: 'Authentication required',
259-
});
272+
error: 'Authentication required'
273+
}));
260274
expect(mockRoleServiceInstance.getUserRole).not.toHaveBeenCalled();
261275
});
262276

@@ -268,12 +282,13 @@ describe('Role Middleware', () => {
268282

269283
expect(mockRoleServiceInstance.getUserRole).toHaveBeenCalledWith('user-123');
270284
expect(mockReply.status).toHaveBeenCalledWith(403);
271-
expect(mockReply.send).toHaveBeenCalledWith({
285+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
286+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
272287
success: false,
273288
error: 'Insufficient permissions',
274289
required_role: 'admin',
275-
user_role: null,
276-
});
290+
user_role: null
291+
}));
277292
});
278293

279294
it('should return 403 when user has different role', async () => {
@@ -289,12 +304,13 @@ describe('Role Middleware', () => {
289304

290305
expect(mockRoleServiceInstance.getUserRole).toHaveBeenCalledWith('user-123');
291306
expect(mockReply.status).toHaveBeenCalledWith(403);
292-
expect(mockReply.send).toHaveBeenCalledWith({
307+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
308+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
293309
success: false,
294310
error: 'Insufficient permissions',
295311
required_role: 'admin',
296-
user_role: 'user',
297-
});
312+
user_role: 'user'
313+
}));
298314
});
299315

300316
it('should return 500 when role check fails', async () => {
@@ -312,11 +328,12 @@ describe('Role Middleware', () => {
312328
error
313329
}, '❌ Error checking user role: Database error');
314330
expect(mockReply.status).toHaveBeenCalledWith(500);
315-
expect(mockReply.send).toHaveBeenCalledWith({
331+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
332+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
316333
success: false,
317334
error: 'Internal server error',
318-
details: 'Database error',
319-
});
335+
details: 'Database error'
336+
}));
320337
});
321338
});
322339

@@ -349,12 +366,13 @@ describe('Role Middleware', () => {
349366
await middleware(mockRequest as FastifyRequest, mockReply as FastifyReply);
350367

351368
expect(mockReply.status).toHaveBeenCalledWith(403);
352-
expect(mockReply.send).toHaveBeenCalledWith({
369+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
370+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
353371
success: false,
354372
error: 'Insufficient permissions',
355373
required_role: 'global_admin',
356-
user_role: 'user',
357-
});
374+
user_role: 'user'
375+
}));
358376
});
359377
});
360378

@@ -394,9 +412,11 @@ describe('Role Middleware', () => {
394412
await middleware(mockRequest as FastifyRequest, mockReply as FastifyReply);
395413

396414
expect(mockReply.status).toHaveBeenCalledWith(401);
397-
expect(mockReply.send).toHaveBeenCalledWith({
398-
error: 'Authentication required',
399-
});
415+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
416+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
417+
success: false,
418+
error: 'Authentication required'
419+
}));
400420
});
401421

402422
it('should return 403 when user is not owner and not admin', async () => {
@@ -408,9 +428,11 @@ describe('Role Middleware', () => {
408428

409429
expect(mockRoleServiceInstance.userHasPermission).toHaveBeenCalledWith('user-123', 'system.admin');
410430
expect(mockReply.status).toHaveBeenCalledWith(403);
411-
expect(mockReply.send).toHaveBeenCalledWith({
412-
error: 'Can only access your own resources or requires admin permissions',
413-
});
431+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
432+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
433+
success: false,
434+
error: 'Can only access your own resources or requires admin permissions'
435+
}));
414436
});
415437

416438
it('should return 500 when admin permission check fails', async () => {
@@ -423,9 +445,11 @@ describe('Role Middleware', () => {
423445

424446
expect(mockRequest.log?.error).toHaveBeenCalledWith(error, 'Error checking user permissions');
425447
expect(mockReply.status).toHaveBeenCalledWith(500);
426-
expect(mockReply.send).toHaveBeenCalledWith({
427-
error: 'Internal server error',
428-
});
448+
expect(mockReply.type).toHaveBeenCalledWith('application/json');
449+
expect(mockReply.send).toHaveBeenCalledWith(JSON.stringify({
450+
success: false,
451+
error: 'Internal server error'
452+
}));
429453
});
430454
});
431455

0 commit comments

Comments
 (0)