1- // app/tests/auth.full.test.js
1+ /* eslint-env mocha */
22import request from 'supertest' ;
33import { expect } from 'chai' ;
44import mongoose from 'mongoose' ;
@@ -74,7 +74,7 @@ describe('Auth API - Full Supertest Suite', function () {
7474 if ( r . note ) console . log ( ` note: ${ r . note } ` ) ;
7575 } ) ;
7676 } finally {
77- // leave DB intact by request (you said you want to inspect data)
77+ // leave DB intact by request
7878 await mongoose . disconnect ( ) ;
7979 console . log ( 'Disconnected from MongoDB' ) ;
8080 }
@@ -86,7 +86,6 @@ describe('Auth API - Full Supertest Suite', function () {
8686 const res = await request ( app ) . get ( '/health' ) ;
8787 expect ( res . status ) . to . equal ( 200 ) ;
8888 expect ( res . body ) . to . have . property ( 'status' ) ;
89- // Accept both 'OK' and 'Ok' or 'ok' depending on your implementation
9089 results . push ( { name : testName , status : 'passed' , note : `status=${ res . body . status } ` } ) ;
9190 } catch ( err ) {
9291 console . error ( 'TEST ERROR - Health check:' , err ) ;
@@ -106,6 +105,10 @@ describe('Auth API - Full Supertest Suite', function () {
106105 expect ( res . body ) . to . have . property ( 'accessToken' ) ;
107106 expect ( res . body ) . to . have . property ( 'refreshToken' ) ;
108107
108+ // 🔹 Update latest tokens so refresh test always uses the fresh DB one
109+ adminToken = res . body . accessToken ;
110+ adminRefreshToken = res . body . refreshToken ;
111+
109112 results . push ( { name : testName , status : 'passed' } ) ;
110113 } catch ( err ) {
111114 console . error ( 'TEST ERROR - Admin login:' , err ) ;
@@ -117,7 +120,6 @@ describe('Auth API - Full Supertest Suite', function () {
117120 it ( 'Register viewer (admin) — POST /auth/register with admin token' , async ( ) => {
118121 const testName = 'Register viewer (admin)' ;
119122 try {
120- // ensure uniqueness (we already set viewerEmail unique)
121123 const res = await request ( app )
122124 . post ( '/auth/register' )
123125 . set ( 'Authorization' , `Bearer ${ adminToken } ` )
@@ -130,13 +132,10 @@ describe('Auth API - Full Supertest Suite', function () {
130132 addedBy : adminId
131133 } ) ;
132134
133- // 201 expected on create; backend returns 400 if user exists
134135 expect ( res . status ) . to . equal ( 201 ) ;
135136 results . push ( { name : testName , status : 'passed' , note : `created ${ viewerEmail } ` } ) ;
136137 } catch ( err ) {
137- // If backend replies with 400 user already exists (shouldn't happen because unique email)
138138 console . error ( 'TEST ERROR - Register viewer:' , err ) ;
139- // capture response body if available (supertest supplies err.response)
140139 const body = err . response ? err . response . body : undefined ;
141140 results . push ( { name : testName , status : 'failed' , error : err . message , body } ) ;
142141 expect . fail ( err . message ) ;
@@ -157,7 +156,6 @@ describe('Auth API - Full Supertest Suite', function () {
157156 addedBy : adminId
158157 } ) ;
159158
160- // Expect 401 (missing token) or 403 depending on your verifyToken implementation.
161159 expect ( [ 401 , 403 ] ) . to . include ( res . status ) ;
162160 results . push ( { name : testName , status : 'passed' , note : `status=${ res . status } ` } ) ;
163161 } catch ( err ) {
@@ -173,7 +171,6 @@ describe('Auth API - Full Supertest Suite', function () {
173171 const res = await request ( app ) . post ( '/auth/refresh' ) . send ( { token : adminRefreshToken } ) ;
174172 expect ( res . status ) . to . equal ( 200 ) ;
175173 expect ( res . body ) . to . have . property ( 'accessToken' ) ;
176- // optionally capture the new refresh token if backend returns it
177174 results . push ( { name : testName , status : 'passed' } ) ;
178175 } catch ( err ) {
179176 console . error ( 'TEST ERROR - Refresh token:' , err ) ;
@@ -186,7 +183,6 @@ describe('Auth API - Full Supertest Suite', function () {
186183 it ( 'Viewer cannot call admin-only register — POST /auth/register (viewer token)' , async ( ) => {
187184 const testName = 'Viewer blocked from admin route' ;
188185 try {
189- // login viewer first (we created viewer earlier with unique email)
190186 const logRes = await request ( app ) . post ( '/auth/login' ) . send ( { email : viewerEmail , password : viewerPassword } ) ;
191187 expect ( logRes . status ) . to . equal ( 200 ) ;
192188 const viewerToken = logRes . body . accessToken ;
@@ -202,7 +198,6 @@ describe('Auth API - Full Supertest Suite', function () {
202198 permissions : [ ]
203199 } ) ;
204200
205- // Expect forbidden: 403 (if your role middleware uses 403), or other non-200
206201 expect ( res . status ) . to . equal ( 403 ) ;
207202 results . push ( { name : testName , status : 'passed' } ) ;
208203 } catch ( err ) {
0 commit comments