@@ -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