11const request = require ( 'supertest' )
22const { generateStaticReports } = require ( '../../src/reports' )
3+ const knexInit = require ( 'knex' )
4+ const { getConfig } = require ( '../../src/config' )
5+ const { resetDatabase, initializeStore } = require ( '../../__utils__' )
36const pkg = require ( '../../package.json' )
47const serverModule = require ( '../../src/httpServer' )
8+ const { dbSettings } = getConfig ( 'test' )
59let server
610let serverStop
711let app
12+ let knex
13+ let getAllProjects
814
915// Mocks
1016jest . mock ( '../../src/reports' , ( ) => ( {
@@ -17,17 +23,26 @@ beforeAll(async () => {
1723 server = await serverInstance . start ( )
1824 serverStop = serverInstance . stop
1925 app = request ( server )
26+ knex = knexInit ( dbSettings ) ;
27+ ( {
28+ getAllProjects
29+ } = initializeStore ( knex ) )
2030} )
2131
2232afterAll ( async ( ) => {
2333 // Cleanup after all tests
2434 await serverStop ?. ( )
35+ await resetDatabase ( knex )
36+ await knex . destroy ( )
2537} )
2638
27- beforeEach ( ( ) => {
39+ beforeEach ( async ( ) => {
40+ await resetDatabase ( knex )
2841 jest . clearAllMocks ( )
2942} )
3043
44+ afterEach ( jest . clearAllMocks )
45+
3146describe ( 'HTTP Server API V1' , ( ) => {
3247 describe ( 'GET /api/v1/__health' , ( ) => {
3348 test ( 'should return status ok' , async ( ) => {
@@ -44,6 +59,62 @@ describe('HTTP Server API V1', () => {
4459 } )
4560 } )
4661
62+ describe ( 'POST /api/v1/project' , ( ) => {
63+ test ( 'should return 200 and create a new project' , async ( ) => {
64+ // Initial state
65+ let projects = await getAllProjects ( )
66+ expect ( projects . length ) . toBe ( 0 )
67+ // Request
68+ const newProject = { name : 'eslint' }
69+ const response = await app . post ( '/api/v1/project' ) . send ( newProject )
70+ // Database changes
71+ projects = await getAllProjects ( )
72+ expect ( projects . length ) . toBe ( 1 )
73+ expect ( projects [ 0 ] . name ) . toBe ( 'eslint' )
74+ // Response details
75+ expect ( response . status ) . toBe ( 201 )
76+ expect ( response . headers ) . toHaveProperty ( 'location' , `/api/v1/project/${ projects [ 0 ] . id } ` )
77+ expect ( response . body ) . toHaveProperty ( 'id' )
78+ expect ( response . body ) . toHaveProperty ( 'name' , newProject . name )
79+ expect ( response . body ) . toHaveProperty ( 'created_at' )
80+ expect ( response . body ) . toHaveProperty ( 'updated_at' )
81+ } )
82+ test ( 'should return 400 for invalid project name' , async ( ) => {
83+ // Initial state
84+ let projects = await getAllProjects ( )
85+ expect ( projects . length ) . toBe ( 0 )
86+ // Request
87+ const invalidProject = { name : 'Invalid Name!' }
88+ const response = await app . post ( '/api/v1/project' ) . send ( invalidProject )
89+ // Database changes
90+ projects = await getAllProjects ( )
91+ expect ( projects . length ) . toBe ( 0 )
92+ // Response details
93+ expect ( response . status ) . toBe ( 400 )
94+ expect ( response . body ) . toStrictEqual ( { errors : [ { errorCode : 'pattern.openapi.validation' , message : 'must match pattern "^[a-zA-Z0-9_-]+$"' , path : '/body/name' } ] , name : 'Bad Request' , path : '/api/v1/project' , status : 400 } )
95+ } )
96+ test ( 'should return 409 if the project already exists' , async ( ) => {
97+ // Initial state
98+ let projects = await getAllProjects ( )
99+ expect ( projects . length ) . toBe ( 0 )
100+ // Create the project first
101+ const existingProject = { name : 'eslint' }
102+ await app . post ( '/api/v1/project' ) . send ( existingProject )
103+ projects = await getAllProjects ( )
104+ expect ( projects . length ) . toBe ( 1 )
105+ // Request to create the same project again
106+ const response = await app . post ( '/api/v1/project' ) . send ( existingProject )
107+ // Database changes
108+ projects = await getAllProjects ( )
109+ expect ( projects . length ) . toBe ( 1 ) // Still only one project
110+ // Response details
111+ expect ( response . status ) . toBe ( 409 )
112+ expect ( response . body ) . toStrictEqual ( { errors : [ { message : 'Project already exists.' } ] } )
113+ } )
114+
115+ test . todo ( 'should return 500 for internal server error' )
116+ } )
117+
47118 describe ( 'POST /api/v1/generate-reports' , ( ) => {
48119 test ( 'should return status completed when report generation succeeds' , async ( ) => {
49120 generateStaticReports . mockResolvedValueOnce ( )
0 commit comments