1
1
import request from "supertest" ;
2
- import app from "../app" ;
2
+ import app from "../app.js " ;
3
3
import User from "../models/User" ;
4
4
5
+ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
6
jest . mock ( "../models/User.js" ) ;
6
7
7
- // Mockeamos connectMongoose para evitar conexiones reales
8
+ //We mock connectMongoose to avoid real connections++++++++++++
8
9
jest . mock ( "../lib/connectMongoose.js" , ( ) => {
9
10
return jest . fn ( ) . mockResolvedValue ( {
10
11
name : "mockConnection" ,
11
12
close : jest . fn ( ) ,
12
13
} ) ;
13
14
} ) ;
14
15
15
- // Mockeamos session para evitar problemas de sesión
16
+ //We mock session to avoid session issues++++++++++++++++++++++
16
17
jest . mock ( "express-session" , ( ) => {
17
18
return ( ) => ( req , res , next ) => {
18
19
req . session = {
@@ -23,7 +24,7 @@ jest.mock("express-session", () => {
23
24
} ;
24
25
} ) ;
25
26
26
- // Mockeamos sessionManager para simplificar los tests
27
+ //We mock sessionManager to simplify the tests+++++++++++++++++
27
28
jest . mock ( "../lib/sessionManager" , ( ) => ( {
28
29
middleware : ( req , res , next ) => {
29
30
req . session = {
@@ -39,27 +40,31 @@ jest.mock("../lib/sessionManager", () => ({
39
40
guard : ( req , res , next ) => next ( ) ,
40
41
} ) ) ;
41
42
43
+ //#######################################################################################################
42
44
describe ( "loginController" , ( ) => {
43
- // Limpiamos mocks antes de cada test
45
+ // Clear mocks before each test
44
46
beforeEach ( ( ) => {
45
47
jest . clearAllMocks ( ) ;
46
48
} ) ;
47
49
48
- it ( "debe devolver un 200 al visitar la página de login" , async ( ) => {
50
+ //=========================================================================
51
+ it ( "should return 200 when visiting the login page" , async ( ) => {
49
52
const response = await request ( app ) . get ( "/login" ) ;
50
53
51
54
expect ( response . status ) . toBe ( 200 ) ;
52
55
} ) ;
53
56
54
- it ( "debe mostrar formulario de login sin errores inicialmente" , async ( ) => {
57
+ //=========================================================================
58
+ it ( "should show login form without errors initially" , async ( ) => {
55
59
const response = await request ( app ) . get ( "/login" ) ;
56
60
57
61
expect ( response . text ) . toContain ( "form" ) ;
58
62
expect ( response . text ) . not . toContain ( "Invalid credentials" ) ;
59
63
} ) ;
60
64
61
- it ( "debe redirigir a home tras login exitoso" , async ( ) => {
62
- // Mock de usuario existente con contraseña correcta
65
+ //=========================================================================
66
+ it ( "should redirect to home after successful login" , async ( ) => {
67
+ // Mock an existing user with correct password
63
68
const mockUser = {
64
69
id : "user123" ,
65
70
email : "test@example.com" ,
@@ -81,8 +86,9 @@ describe("loginController", () => {
81
86
expect ( mockUser . comparePassword ) . toHaveBeenCalledWith ( "password123" ) ;
82
87
} ) ;
83
88
84
- it ( "debe mostrar error con credenciales inválidas" , async ( ) => {
85
- // Mock de usuario existente con contraseña incorrecta
89
+ //=========================================================================
90
+ it ( "should show error with invalid credentials" , async ( ) => {
91
+ // Mock an existing user with incorrect password
86
92
const mockUser = {
87
93
email : "test@example.com" ,
88
94
comparePassword : jest . fn ( ) . mockResolvedValue ( false ) ,
@@ -99,8 +105,9 @@ describe("loginController", () => {
99
105
expect ( response . text ) . toContain ( "Invalid credentials" ) ;
100
106
} ) ;
101
107
102
- it ( "debe mostrar error cuando el usuario no existe" , async ( ) => {
103
- // Mock de usuario no existente
108
+ //=========================================================================
109
+ it ( "should show error when user does not exist" , async ( ) => {
110
+ // Mock non-existent user
104
111
User . findOne = jest . fn ( ) . mockResolvedValue ( null ) ;
105
112
106
113
const response = await request ( app ) . post ( "/login" ) . type ( "form" ) . send ( {
@@ -112,8 +119,9 @@ describe("loginController", () => {
112
119
expect ( response . text ) . toContain ( "Invalid credentials" ) ;
113
120
} ) ;
114
121
115
- it ( "debe redirigir a la URL especificada después del login exitoso" , async ( ) => {
116
- // Mock de usuario existente con contraseña correcta
122
+ //=========================================================================
123
+ it ( "should redirect to specified URL after successful login" , async ( ) => {
124
+ // Mock an existing user with correct password
117
125
const mockUser = {
118
126
id : "user123" ,
119
127
email : "test@example.com" ,
@@ -132,10 +140,35 @@ describe("loginController", () => {
132
140
expect ( response . status ) . toBe ( 302 ) ;
133
141
expect ( response . headers . location ) . toBe ( "/agents/new" ) ;
134
142
} ) ;
135
- it ( "debe cerrar sesión y redirigir a home al hacer logout" , async ( ) => {
143
+
144
+ //=========================================================================
145
+ it ( "should logout and redirect to home" , async ( ) => {
136
146
const response = await request ( app ) . get ( "/logout" ) ;
137
147
138
148
expect ( response . status ) . toBe ( 302 ) ;
139
149
expect ( response . headers . location ) . toBe ( "/" ) ;
140
150
} ) ;
141
151
} ) ;
152
+
153
+ //#######################################################################################################
154
+ describe . only ( "TDD Exercise" , ( ) => {
155
+ //=========================================================================
156
+ it ( "should record a new login upon successful login" , async ( ) => {
157
+ const mockUser = {
158
+ id : "user123" ,
159
+ email : "test@example.com" ,
160
+ comparePassword : jest . fn ( ) . mockResolvedValue ( true ) ,
161
+ addLoginRecord : jest . fn ( ) ,
162
+ save : jest . fn ( ) . mockResolvedValue ( true ) ,
163
+ } ;
164
+ User . findOne = jest . fn ( ) . mockResolvedValue ( mockUser ) ;
165
+
166
+ await request ( app ) . post ( "/login" ) . type ( "form" ) . send ( {
167
+ email : "test@example.com" ,
168
+ password : "password123" ,
169
+ } ) ;
170
+
171
+ expect ( mockUser . addLoginRecord ) . toHaveBeenCalled ( ) ;
172
+ expect ( mockUser . save ) . toHaveBeenCalled ( ) ;
173
+ } ) ;
174
+ } ) ;
0 commit comments