@@ -3,18 +3,32 @@ const express = require('express');
33const request = require ( 'supertest' ) ;
44const session = require ( 'express-session' ) ;
55const passport = require ( 'passport' ) ;
6+
67const User = require ( '../backend/models/User' ) ;
78const authRoutes = require ( '../backend/routes/auth' ) ;
89
9- // Setup Express app for testing
10+ // Create test app
1011function createTestApp ( ) {
1112 const app = express ( ) ;
13+
1214 app . use ( express . json ( ) ) ;
13- app . use ( session ( { secret : 'test' , resave : false , saveUninitialized : false } ) ) ;
15+
16+ app . use (
17+ session ( {
18+ secret : 'test-secret' ,
19+ resave : false ,
20+ saveUninitialized : false ,
21+ } )
22+ ) ;
23+
1424 app . use ( passport . initialize ( ) ) ;
1525 app . use ( passport . session ( ) ) ;
26+
27+ // Load passport config AFTER initializing passport
1628 require ( '../backend/config/passportConfig' ) ;
29+
1730 app . use ( '/auth' , authRoutes ) ;
31+
1832 return app ;
1933}
2034
@@ -27,76 +41,107 @@ describe('Auth Routes', () => {
2741 } ) ;
2842
2943 afterAll ( async ( ) => {
30- await mongoose . connection . db . dropDatabase ( ) ;
31- await mongoose . disconnect ( ) ;
44+ if ( mongoose . connection . readyState === 1 ) {
45+ await mongoose . connection . db . dropDatabase ( ) ;
46+ await mongoose . disconnect ( ) ;
47+ }
3248 } ) ;
3349
3450 afterEach ( async ( ) => {
3551 await User . deleteMany ( { } ) ;
3652 } ) ;
3753
54+ // ---------------- SIGNUP ----------------
3855 it ( 'should sign up a new user' , async ( ) => {
3956 const res = await request ( app )
4057 . post ( '/auth/signup' )
41- . send ( { username : 'testuser' , email : 'test@example.com' , password : 'password123' } ) ;
58+ . send ( {
59+ username : 'testuser' ,
60+ email : 'test@example.com' ,
61+ password : 'password123' ,
62+ } ) ;
63+
4264 expect ( res . status ) . toBe ( 201 ) ;
4365 expect ( res . body . message ) . toBe ( 'User created successfully' ) ;
66+
4467 const user = await User . findOne ( { email : 'test@example.com' } ) ;
4568 expect ( user ) . toBeTruthy ( ) ;
4669 } ) ;
4770
4871 it ( 'should not sign up a user with existing email' , async ( ) => {
49- await new User ( { username : 'testuser' , email : 'test@example.com' , password : 'password123' } ) . save ( ) ;
50- const res = await request ( app )
51- . post ( '/auth/signup' )
52- . send ( { username : 'testuser2' , email : 'test@example.com' , password : 'password456' } ) ;
53- expect ( res . status ) . toBe ( 400 ) ;
54- expect ( res . body . message ) . toBe ( 'User already exists' ) ;
55- } ) ;
72+ await User . create ( {
73+ username : 'testuser' ,
74+ email : 'test@example.com' ,
75+ password : 'password123' ,
76+ } ) ;
5677
57- it ( 'should not sign up a user with existing username' , async ( ) => {
58- await new User ( { username : 'testuser' , email : 'test@example.com' , password : 'password123' } ) . save ( ) ;
5978 const res = await request ( app )
6079 . post ( '/auth/signup' )
61- . send ( { username : 'testuser' , email : 'test2@example.com' , password : 'password456' } ) ;
80+ . send ( {
81+ username : 'testuser2' ,
82+ email : 'test@example.com' ,
83+ password : 'password456' ,
84+ } ) ;
85+
6286 expect ( res . status ) . toBe ( 400 ) ;
6387 expect ( res . body . message ) . toBe ( 'User already exists' ) ;
6488 } ) ;
6589
90+ // ---------------- LOGIN ----------------
6691 it ( 'should login a user with correct credentials' , async ( ) => {
67- await request ( app )
68- . post ( '/auth/signup' )
69- . send ( { username : 'testuser' , email : 'test@example.com' , password : 'password123' } ) ;
92+ await User . create ( {
93+ username : 'testuser' ,
94+ email : 'test@example.com' ,
95+ password : 'password123' ,
96+ } ) ;
97+
7098 const agent = request . agent ( app ) ;
71- const res = await agent
72- . post ( '/auth/login' )
73- . send ( { email : 'test@example.com' , password : 'password123' } ) ;
99+
100+ const res = await agent . post ( '/auth/login' ) . send ( {
101+ email : 'test@example.com' ,
102+ password : 'password123' ,
103+ } ) ;
104+
74105 expect ( res . status ) . toBe ( 200 ) ;
75106 expect ( res . body . message ) . toBe ( 'Login successful' ) ;
76107 expect ( res . body . user . email ) . toBe ( 'test@example.com' ) ;
77108 } ) ;
78109
79110 it ( 'should not login a user with wrong password' , async ( ) => {
80- await request ( app )
81- . post ( '/auth/signup' )
82- . send ( { username : 'testuser' , email : 'test@example.com' , password : 'password123' } ) ;
111+ await User . create ( {
112+ username : 'testuser' ,
113+ email : 'test@example.com' ,
114+ password : 'password123' ,
115+ } ) ;
116+
83117 const agent = request . agent ( app ) ;
84- const res = await agent
85- . post ( '/auth/login' )
86- . send ( { email : 'test@example.com' , password : 'wrongpassword' } ) ;
118+
119+ const res = await agent . post ( '/auth/login' ) . send ( {
120+ email : 'test@example.com' ,
121+ password : 'wrongpassword' ,
122+ } ) ;
123+
87124 expect ( res . status ) . toBe ( 401 ) ;
88125 } ) ;
89126
127+ // ---------------- LOGOUT ----------------
90128 it ( 'should logout a logged-in user' , async ( ) => {
91- await request ( app )
92- . post ( '/auth/signup' )
93- . send ( { username : 'testuser' , email : 'test@example.com' , password : 'password123' } ) ;
94129 const agent = request . agent ( app ) ;
95- await agent
96- . post ( '/auth/login' )
97- . send ( { email : 'test@example.com' , password : 'password123' } ) ;
130+
131+ await agent . post ( '/auth/signup' ) . send ( {
132+ username : 'testuser' ,
133+ email : 'test@example.com' ,
134+ password : 'password123' ,
135+ } ) ;
136+
137+ await agent . post ( '/auth/login' ) . send ( {
138+ email : 'test@example.com' ,
139+ password : 'password123' ,
140+ } ) ;
141+
98142 const res = await agent . get ( '/auth/logout' ) ;
143+
99144 expect ( res . status ) . toBe ( 200 ) ;
100145 expect ( res . body . message ) . toBe ( 'Logged out successfully' ) ;
101146 } ) ;
102- } ) ;
147+ } ) ;
0 commit comments