@@ -15,6 +15,7 @@ import config from '../../../config/index.js';
1515 * Unit tests
1616 */
1717describe ( 'Home integration tests:' , ( ) => {
18+ let app ; // Express app instance for fresh (unauthenticated) requests (#3472)
1819 let agent ;
1920 let HomeService ;
2021 let adminToken ;
@@ -40,7 +41,8 @@ describe('Home integration tests:', () => {
4041 config . organizations . enabled = false ;
4142 const init = await bootstrap ( ) ;
4243 HomeService = ( await import ( path . resolve ( './modules/home/services/home.service.js' ) ) ) . default ;
43- agent = request . agent ( init . app ) ;
44+ app = init . app ;
45+ agent = request . agent ( app ) ;
4446
4547 // Create admin user and sign JWT for health endpoint test
4648 const User = mongoose . model ( 'User' ) ;
@@ -72,24 +74,28 @@ describe('Home integration tests:', () => {
7274 }
7375 } ) ;
7476
77+ // Public ("logged out") route tests. Use a fresh `request(app)` per test
78+ // instead of the shared `agent` so no cookie state from this block leaks
79+ // into later describes (#3472). Auth-required cases still use explicit
80+ // `set('Cookie', 'TOKEN=...')` headers derived from JWTs.
7581 describe ( 'Logout' , ( ) => {
7682 test ( 'should be able to get releases' , async ( ) => {
77- const result = await agent . get ( '/api/home/releases' ) . expect ( 200 ) ;
83+ const result = await request ( app ) . get ( '/api/home/releases' ) . expect ( 200 ) ;
7884 expect ( result . body . type ) . toBe ( 'success' ) ;
7985 expect ( result . body . message ) . toBe ( 'releases' ) ;
8086 expect ( result . body . data ) . toBeInstanceOf ( Array ) ;
8187 } ) ;
8288
8389 test ( 'should be able to get changelogs' , async ( ) => {
84- const result = await agent . get ( '/api/home/changelogs' ) . expect ( 200 ) ;
90+ const result = await request ( app ) . get ( '/api/home/changelogs' ) . expect ( 200 ) ;
8591 expect ( result . body . type ) . toBe ( 'success' ) ;
8692 expect ( result . body . message ) . toBe ( 'changelogs' ) ;
8793 expect ( result . body . data ) . toBeInstanceOf ( Array ) ;
8894 } ) ;
8995
9096 test ( 'should be able to get team members' , async ( ) => {
9197 try {
92- const result = await agent . get ( '/api/home/team' ) . expect ( 200 ) ;
98+ const result = await request ( app ) . get ( '/api/home/team' ) . expect ( 200 ) ;
9399 expect ( result . body . type ) . toBe ( 'success' ) ;
94100 expect ( result . body . message ) . toBe ( 'team list' ) ;
95101 expect ( result . body . data ) . toBeInstanceOf ( Array ) ;
@@ -101,7 +107,7 @@ describe('Home integration tests:', () => {
101107
102108 test ( 'should be able to get an existing page' , async ( ) => {
103109 try {
104- const result = await agent . get ( '/api/home/pages/terms' ) . expect ( 200 ) ;
110+ const result = await request ( app ) . get ( '/api/home/pages/terms' ) . expect ( 200 ) ;
105111 expect ( result . body . type ) . toBe ( 'success' ) ;
106112 expect ( result . body . message ) . toBe ( 'page' ) ;
107113 expect ( result . body . data [ 0 ] . title ) . toBe ( 'Terms' ) ;
@@ -115,7 +121,7 @@ describe('Home integration tests:', () => {
115121
116122 test ( 'should be able to catch error of unknown page' , async ( ) => {
117123 try {
118- const result = await agent . get ( '/api/home/pages/test' ) . expect ( 404 ) ;
124+ const result = await request ( app ) . get ( '/api/home/pages/test' ) . expect ( 404 ) ;
119125 expect ( result . body . type ) . toBe ( 'error' ) ;
120126 expect ( result . body . message ) . toBe ( 'Not Found' ) ;
121127 expect ( result . body . description ) . toBe ( 'No page with that name has been found' ) ;
@@ -127,15 +133,15 @@ describe('Home integration tests:', () => {
127133
128134 test ( 'should return empty releases gracefully when GitHub API fails' , async ( ) => {
129135 axios . get . mockRejectedValueOnce ( new Error ( 'GitHub API unavailable' ) ) ;
130- const result = await agent . get ( '/api/home/releases' ) . expect ( 200 ) ;
136+ const result = await request ( app ) . get ( '/api/home/releases' ) . expect ( 200 ) ;
131137 expect ( result . body . type ) . toBe ( 'success' ) ;
132138 expect ( result . body . message ) . toBe ( 'releases' ) ;
133139 expect ( result . body . data ) . toEqual ( [ ] ) ;
134140 } ) ;
135141
136142 test ( 'should return empty changelogs gracefully when GitHub API fails' , async ( ) => {
137143 axios . get . mockRejectedValueOnce ( new Error ( 'GitHub API unavailable' ) ) ;
138- const result = await agent . get ( '/api/home/changelogs' ) . expect ( 200 ) ;
144+ const result = await request ( app ) . get ( '/api/home/changelogs' ) . expect ( 200 ) ;
139145 expect ( result . body . type ) . toBe ( 'success' ) ;
140146 expect ( result . body . message ) . toBe ( 'changelogs' ) ;
141147 expect ( result . body . data ) . toEqual ( [ ] ) ;
@@ -146,7 +152,7 @@ describe('Home integration tests:', () => {
146152 axios . get . mockClear ( ) ;
147153 // Temporarily set a fake token to cover the token-truthy branch in home.service releases()
148154 config . repos = originalRepos . map ( ( repo ) => ( { ...repo , token : 'fake-test-token' } ) ) ;
149- const result = await agent . get ( '/api/home/releases' ) . expect ( 200 ) ;
155+ const result = await request ( app ) . get ( '/api/home/releases' ) . expect ( 200 ) ;
150156 expect ( result . body . type ) . toBe ( 'success' ) ;
151157 const releaseCalls = axios . get . mock . calls . filter ( ( [ url ] ) => url . includes ( '/releases' ) ) ;
152158 expect ( releaseCalls . length ) . toBeGreaterThan ( 0 ) ;
@@ -160,7 +166,7 @@ describe('Home integration tests:', () => {
160166 const originalRepos = config . repos ;
161167 axios . get . mockClear ( ) ;
162168 config . repos = originalRepos . map ( ( repo ) => ( { ...repo , token : 'fake-test-token' } ) ) ;
163- const result = await agent . get ( '/api/home/changelogs' ) . expect ( 200 ) ;
169+ const result = await request ( app ) . get ( '/api/home/changelogs' ) . expect ( 200 ) ;
164170 expect ( result . body . type ) . toBe ( 'success' ) ;
165171 const changelogCalls = axios . get . mock . calls . filter ( ( [ url ] ) => url . includes ( '/contents/' ) ) ;
166172 expect ( changelogCalls . length ) . toBeGreaterThan ( 0 ) ;
@@ -171,15 +177,15 @@ describe('Home integration tests:', () => {
171177 } ) ;
172178
173179 test ( 'should return minimal health status without auth' , async ( ) => {
174- const result = await agent . get ( '/api/health' ) . expect ( 200 ) ;
180+ const result = await request ( app ) . get ( '/api/health' ) . expect ( 200 ) ;
175181 expect ( result . body . type ) . toBe ( 'success' ) ;
176182 expect ( result . body . data . status ) . toBe ( 'ok' ) ;
177183 expect ( result . body . data . db ) . toBeUndefined ( ) ;
178184 expect ( result . body . data . memory ) . toBeUndefined ( ) ;
179185 } ) ;
180186
181187 test ( 'should return detailed health status for admin' , async ( ) => {
182- const result = await agent . get ( '/api/health' ) . set ( 'Cookie' , `TOKEN=${ adminToken } ` ) . expect ( 200 ) ;
188+ const result = await request ( app ) . get ( '/api/health' ) . set ( 'Cookie' , `TOKEN=${ adminToken } ` ) . expect ( 200 ) ;
183189 expect ( result . body . type ) . toBe ( 'success' ) ;
184190 expect ( result . body . data . status ) . toBe ( 'ok' ) ;
185191 expect ( result . body . data . db ) . toBe ( 'connected' ) ;
@@ -196,7 +202,7 @@ describe('Home integration tests:', () => {
196202 version : '0.0.0' ,
197203 memory : process . memoryUsage ( ) ,
198204 } ) ;
199- const result = await agent . get ( '/api/health' ) . expect ( 503 ) ;
205+ const result = await request ( app ) . get ( '/api/health' ) . expect ( 503 ) ;
200206 expect ( result . body . type ) . toBe ( 'error' ) ;
201207 expect ( result . body . message ) . toBe ( 'Service Unavailable' ) ;
202208 } ) ;
0 commit comments