@@ -8,20 +8,31 @@ const mockFindById = jest.fn();
88const mockSelect = jest . fn ( ) ;
99const mockLean = jest . fn ( ) ;
1010
11- jest . mock ( '@urbackend/common' , ( ) => ( {
12- getConnection : jest . fn ( ) . mockResolvedValue ( { } ) ,
13- getCompiledModel : jest . fn ( ) . mockReturnValue ( {
14- findById : ( ...args ) => {
15- mockFindById ( ...args ) ;
16- return {
17- select : ( field ) => {
18- mockSelect ( field ) ;
19- return { lean : mockLean } ;
20- } ,
21- } ;
22- } ,
23- } ) ,
24- } ) ) ;
11+ jest . mock ( '@urbackend/common' , ( ) => {
12+ class MockAppError extends Error {
13+ constructor ( statusCode , message , error ) {
14+ super ( message ) ;
15+ this . statusCode = statusCode ;
16+ this . error = error ;
17+ this . isOperational = true ;
18+ }
19+ }
20+ return {
21+ AppError : MockAppError ,
22+ getConnection : jest . fn ( ) . mockResolvedValue ( { } ) ,
23+ getCompiledModel : jest . fn ( ) . mockReturnValue ( {
24+ findById : ( ...args ) => {
25+ mockFindById ( ...args ) ;
26+ return {
27+ select : ( field ) => {
28+ mockSelect ( field ) ;
29+ return { lean : mockLean } ;
30+ } ,
31+ } ;
32+ } ,
33+ } ) ,
34+ } ;
35+ } ) ;
2536
2637jest . mock ( 'mongoose' , ( ) => ( {
2738 Types : {
@@ -138,10 +149,8 @@ describe('authorizeWriteOperation middleware', () => {
138149
139150 await authorizeWriteOperation ( req , res , next ) ;
140151
141- expect ( res . statusCode ) . toBe ( 403 ) ;
142- expect ( res . body . success ) . toBe ( false ) ;
143- expect ( res . body . error ) . toBe ( 'Write blocked for publishable key' ) ;
144- expect ( next ) . not . toHaveBeenCalled ( ) ;
152+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( { statusCode : 403 , error : 'Write blocked for publishable key' } ) ) ;
153+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
145154 } ) ;
146155
147156 test ( 'blocked with 401 when RLS enabled but no auth token provided' , async ( ) => {
@@ -150,10 +159,8 @@ describe('authorizeWriteOperation middleware', () => {
150159
151160 await authorizeWriteOperation ( req , res , next ) ;
152161
153- expect ( res . statusCode ) . toBe ( 401 ) ;
154- expect ( res . body . success ) . toBe ( false ) ;
155- expect ( res . body . error ) . toBe ( 'Authentication required' ) ;
156- expect ( next ) . not . toHaveBeenCalled ( ) ;
162+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( { statusCode : 401 } ) ) ;
163+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
157164 } ) ;
158165 } ) ;
159166
@@ -171,10 +178,8 @@ describe('authorizeWriteOperation middleware', () => {
171178
172179 await authorizeWriteOperation ( req , res , next ) ;
173180
174- expect ( res . statusCode ) . toBe ( 403 ) ;
175- expect ( res . body . success ) . toBe ( false ) ;
176- expect ( res . body . error ) . toBe ( 'RLS owner mismatch' ) ;
177- expect ( next ) . not . toHaveBeenCalled ( ) ;
181+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( { statusCode : 403 , error : 'RLS owner mismatch' } ) ) ;
182+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
178183 } ) ;
179184
180185 // In the new atomic design, the middleware does not block based on document owner
@@ -293,10 +298,8 @@ describe('authorizeWriteOperation middleware', () => {
293298
294299 await authorizeWriteOperation ( req , res , next ) ;
295300
296- expect ( res . statusCode ) . toBe ( 404 ) ;
297- expect ( res . body . success ) . toBe ( false ) ;
298- expect ( res . body . error ) . toBe ( 'Collection not found' ) ;
299- expect ( next ) . not . toHaveBeenCalled ( ) ;
301+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( { statusCode : 404 , error : 'Collection not found' } ) ) ;
302+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
300303 } ) ;
301304
302305 test ( 'returns 403 when RLS ownerField is _id for a POST insert' , async ( ) => {
@@ -325,10 +328,8 @@ describe('authorizeWriteOperation middleware', () => {
325328
326329 await authorizeWriteOperation ( req , res , next ) ;
327330
328- expect ( res . statusCode ) . toBe ( 403 ) ;
329- expect ( res . body . success ) . toBe ( false ) ;
330- expect ( res . body . error ) . toBe ( 'Insert denied' ) ;
331- expect ( next ) . not . toHaveBeenCalled ( ) ;
331+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( { statusCode : 403 , error : 'Insert denied' } ) ) ;
332+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
332333 } ) ;
333334
334335 test ( 'returns 400 for an invalid document id on PUT' , async ( ) => {
@@ -342,10 +343,8 @@ describe('authorizeWriteOperation middleware', () => {
342343
343344 await authorizeWriteOperation ( req , res , next ) ;
344345
345- expect ( res . statusCode ) . toBe ( 400 ) ;
346- expect ( res . body . success ) . toBe ( false ) ;
347- expect ( res . body . error ) . toBe ( 'Invalid ID format.' ) ;
348- expect ( next ) . not . toHaveBeenCalled ( ) ;
346+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( { statusCode : 400 , error : 'Invalid ID format' } ) ) ;
347+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
349348 } ) ;
350349
351350
@@ -361,10 +360,8 @@ describe('authorizeWriteOperation middleware', () => {
361360
362361 await authorizeWriteOperation ( req , res , next ) ;
363362
364- expect ( res . statusCode ) . toBe ( 403 ) ;
365- expect ( res . body . success ) . toBe ( false ) ;
366- expect ( res . body . error ) . toBe ( 'Owner field immutable' ) ;
367- expect ( next ) . not . toHaveBeenCalled ( ) ;
363+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( { statusCode : 403 , error : 'Owner field immutable' } ) ) ;
364+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
368365 } ) ;
369366 } ) ;
370367} ) ;
0 commit comments