@@ -7,14 +7,24 @@ class AppError extends Error {
77 }
88}
99
10+ const mockAppError = AppError ;
11+
1012jest . mock ( '@urbackend/common' , ( ) => ( {
11- AppError,
13+ AppError : mockAppError ,
1214 Project : {
1315 findOne : jest . fn ( )
1416 } ,
1517 getProjectAccessQuery : jest . fn ( ( userId ) => ( { $or : [ { owner : userId } , { "members.user" : userId } ] } ) )
1618} ) ) ;
1719
20+ jest . mock ( 'mongoose' , ( ) => ( {
21+ Types : {
22+ ObjectId : {
23+ isValid : jest . fn ( ( value ) => value === '507f1f77bcf86cd799439011' )
24+ }
25+ }
26+ } ) ) ;
27+
1828const { Project } = require ( '@urbackend/common' ) ;
1929const loadProjectForAdmin = require ( '../middlewares/loadProjectForAdmin' ) ;
2030
@@ -43,13 +53,26 @@ describe('loadProjectForAdmin Middleware', () => {
4353 expect ( Project . findOne ) . not . toHaveBeenCalled ( ) ;
4454 } ) ;
4555
56+ it ( 'should call next with AppError(400) if projectId is invalid' , async ( ) => {
57+ req . params . projectId = 'bad-id' ;
58+
59+ await loadProjectForAdmin ( req , res , next ) ;
60+
61+ expect ( next ) . toHaveBeenCalledTimes ( 1 ) ;
62+ expect ( next ) . toHaveBeenCalledWith ( expect . any ( AppError ) ) ;
63+ const error = next . mock . calls [ 0 ] [ 0 ] ;
64+ expect ( error . statusCode ) . toBe ( 400 ) ;
65+ expect ( error . message ) . toBe ( "Invalid project ID format" ) ;
66+ expect ( Project . findOne ) . not . toHaveBeenCalled ( ) ;
67+ } ) ;
68+
4669 it ( 'should call next with AppError(404) if project is not found' , async ( ) => {
47- req . params . projectId = 'proj123 ' ;
70+ req . params . projectId = '507f1f77bcf86cd799439011 ' ;
4871 Project . findOne . mockResolvedValueOnce ( null ) ;
4972
5073 await loadProjectForAdmin ( req , res , next ) ;
5174
52- expect ( Project . findOne ) . toHaveBeenCalledWith ( { _id : 'proj123 ' , $or : [ { owner : 'user123' } , { "members.user" : 'user123' } ] } ) ;
75+ expect ( Project . findOne ) . toHaveBeenCalledWith ( { _id : '507f1f77bcf86cd799439011 ' , $or : [ { owner : 'user123' } , { "members.user" : 'user123' } ] } ) ;
5376 expect ( next ) . toHaveBeenCalledTimes ( 1 ) ;
5477 expect ( next ) . toHaveBeenCalledWith ( expect . any ( AppError ) ) ;
5578 const error = next . mock . calls [ 0 ] [ 0 ] ;
@@ -58,13 +81,13 @@ describe('loadProjectForAdmin Middleware', () => {
5881 } ) ;
5982
6083 it ( 'should set req.project and call next without error if project is found' , async ( ) => {
61- req . params . projectId = 'proj123 ' ;
62- const mockProject = { _id : 'proj123 ' , name : 'Test Project' } ;
84+ req . params . projectId = '507f1f77bcf86cd799439011 ' ;
85+ const mockProject = { _id : '507f1f77bcf86cd799439011 ' , name : 'Test Project' } ;
6386 Project . findOne . mockResolvedValueOnce ( mockProject ) ;
6487
6588 await loadProjectForAdmin ( req , res , next ) ;
6689
67- expect ( Project . findOne ) . toHaveBeenCalledWith ( { _id : 'proj123 ' , $or : [ { owner : 'user123' } , { "members.user" : 'user123' } ] } ) ;
90+ expect ( Project . findOne ) . toHaveBeenCalledWith ( { _id : '507f1f77bcf86cd799439011 ' , $or : [ { owner : 'user123' } , { "members.user" : 'user123' } ] } ) ;
6891 expect ( req . project ) . toEqual ( mockProject ) ;
6992 expect ( next ) . toHaveBeenCalledTimes ( 1 ) ;
7093 expect ( next ) . toHaveBeenCalledWith ( ) ;
0 commit comments