@@ -8,22 +8,23 @@ const app = require('./index');
88
99let server ;
1010
11- // Start the server before all tests
12- before ( function ( done ) {
13- // The app should export the Express app, not start the server directly
14- server = app . listen ( 3001 , ( ) => {
15- console . log ( 'Test server started on port 3001' ) ;
16- done ( ) ;
11+ describe ( 'Express server' , function ( ) {
12+ // Start the server before all tests
13+ before ( function ( done ) {
14+ // The app should export the Express app, not start the server directly
15+ server = app . listen ( 3001 , ( ) => {
16+ console . log ( 'Test server started on port 3001' ) ;
17+ done ( ) ;
18+ } ) ;
1719 } ) ;
18- } ) ;
1920
20- // Close the server after all tests
21- after ( function ( done ) {
22- server . close ( ( ) => {
23- console . log ( 'Test server closed' ) ;
24- done ( ) ;
21+ // Close the server after all tests
22+ after ( function ( done ) {
23+ server . close ( ( ) => {
24+ console . log ( 'Test server closed' ) ;
25+ done ( ) ;
26+ } ) ;
2527 } ) ;
26- } ) ;
2728
2829// Helper function to make requests using Node's built-in http
2930function request ( method , path , body , headers = { } ) {
@@ -38,19 +39,19 @@ function request(method, path, body, headers = {}) {
3839 ...headers
3940 }
4041 } ;
41-
42+
4243 if ( body ) {
4344 const bodyData = JSON . stringify ( body ) ;
4445 options . headers [ 'Content-Type' ] = 'application/json' ;
4546 options . headers [ 'Content-Length' ] = Buffer . byteLength ( bodyData ) ;
4647 }
47-
48+
4849 const req = http . request ( options , ( res ) => {
4950 let data = '' ;
5051 res . on ( 'data' , ( chunk ) => {
5152 data += chunk ;
5253 } ) ;
53-
54+
5455 res . on ( 'end' , ( ) => {
5556 res . body = data ;
5657 try {
@@ -61,43 +62,79 @@ function request(method, path, body, headers = {}) {
6162 resolve ( res ) ;
6263 } ) ;
6364 } ) ;
64-
65+
6566 req . on ( 'error' , ( error ) => {
6667 reject ( error ) ;
6768 } ) ;
68-
69+
6970 if ( body ) {
7071 req . write ( JSON . stringify ( body ) ) ;
7172 }
7273 req . end ( ) ;
7374 } ) ;
7475}
7576
76- describe ( 'Express server' , function ( ) {
7777 // Test the GET / endpoint
7878 describe ( 'GET /' , function ( ) {
7979 it ( 'should return a 200 status code' , async function ( ) {
8080 const response = await request ( 'GET' , '/' ) ;
81- assert . equal ( response . statusCode , 200 ) ; // Changed from status to statusCode
81+ assert . equal ( response . statusCode , 200 ) ;
8282 } ) ;
8383 } ) ;
8484
8585 // Test that unauthorized users cannot access the /users endpoint
8686 describe ( 'GET /users nonauth' , function ( ) {
8787 it ( 'should return a 401 status code' , async function ( ) {
8888 const response = await request ( 'GET' , '/api/users' ) ;
89- assert . equal ( response . statusCode , 401 ) ; // Changed from status to statusCode
89+ assert . equal ( response . statusCode , 401 ) ;
9090 } ) ;
9191 } ) ;
9292
9393 // Test that login works
9494 describe ( 'POST /login' , function ( ) {
9595 it ( 'should return a 200 status code' , async function ( ) {
96- const response = await request ( 'POST' , '/api/login' , {
97- username : 'admin' ,
98- password : 'admin'
96+ const response = await request ( 'POST' , '/api/login' , {
97+ username : 'admin' ,
98+ password : 'admin'
9999 } ) ;
100- assert . equal ( response . statusCode , 200 ) ; // Changed from status to statusCode
100+ assert . equal ( response . statusCode , 200 ) ;
101+ } ) ;
102+ } ) ;
103+
104+ // Test /api/capturestillphoto for camera not active or not photo mode
105+ describe ( 'POST /api/capturestillphoto' , function ( ) {
106+ it ( 'should return a 400 if camera is not active or not in photo mode' , async function ( ) {
107+ // No auth token provided, will be rejected as 401
108+ const response = await request ( 'POST' , '/api/capturestillphoto' ) ;
109+ // Accept either 400 or 401 (depends if middleware or logic triggers first)
110+ assert ( [ 400 , 401 ] . includes ( response . statusCode ) ) ;
111+ } ) ;
112+ } ) ;
113+
114+ // Test /api/camera/start validation error=
115+ describe ( 'POST /api/camera/start validation' , function ( ) {
116+ it ( 'should return 422 for missing required fields' , async function ( ) {
117+ const loginRes = await request ( 'POST' , '/api/login' , {
118+ username : 'admin' ,
119+ password : 'admin'
120+ } ) ;
121+ const token = ( loginRes . json && loginRes . json . token ) || '' ;
122+
123+ const response = await request (
124+ 'POST' ,
125+ '/api/camera/start' ,
126+ { cameraMode : 'streaming' } ,
127+ { Authorization : 'Bearer ' + token }
128+ ) ;
129+ assert . equal ( response . statusCode , 422 ) ;
130+ } ) ;
131+ } ) ;
132+
133+ // Test that /api/camera/stop returns 401 without authorization
134+ describe ( 'POST /api/camera/stop' , function ( ) {
135+ it ( 'should return 401 without authentication' , async function ( ) {
136+ const response = await request ( 'POST' , '/api/camera/stop' ) ;
137+ assert . equal ( response . statusCode , 401 ) ;
101138 } ) ;
102139 } ) ;
103140} ) ;
0 commit comments