11import request from "supertest" ;
2- import app from "../app" ;
2+ import app from "../app.js " ;
33import User from "../models/User" ;
44
5+ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56jest . mock ( "../models/User.js" ) ;
67
7- // Mockeamos connectMongoose para evitar conexiones reales
8+ //We mock connectMongoose to avoid real connections++++++++++++
89jest . mock ( "../lib/connectMongoose.js" , ( ) => {
910 return jest . fn ( ) . mockResolvedValue ( {
1011 name : "mockConnection" ,
1112 close : jest . fn ( ) ,
1213 } ) ;
1314} ) ;
1415
15- // Mockeamos session para evitar problemas de sesión
16+ //We mock session to avoid session issues++++++++++++++++++++++
1617jest . mock ( "express-session" , ( ) => {
1718 return ( ) => ( req , res , next ) => {
1819 req . session = {
@@ -23,7 +24,7 @@ jest.mock("express-session", () => {
2324 } ;
2425} ) ;
2526
26- // Mockeamos sessionManager para simplificar los tests
27+ //We mock sessionManager to simplify the tests+++++++++++++++++
2728jest . mock ( "../lib/sessionManager" , ( ) => ( {
2829 middleware : ( req , res , next ) => {
2930 req . session = {
@@ -39,27 +40,31 @@ jest.mock("../lib/sessionManager", () => ({
3940 guard : ( req , res , next ) => next ( ) ,
4041} ) ) ;
4142
43+ //#######################################################################################################
4244describe ( "loginController" , ( ) => {
43- // Limpiamos mocks antes de cada test
45+ // Clear mocks before each test
4446 beforeEach ( ( ) => {
4547 jest . clearAllMocks ( ) ;
4648 } ) ;
4749
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 ( ) => {
4952 const response = await request ( app ) . get ( "/login" ) ;
5053
5154 expect ( response . status ) . toBe ( 200 ) ;
5255 } ) ;
5356
54- it ( "debe mostrar formulario de login sin errores inicialmente" , async ( ) => {
57+ //=========================================================================
58+ it ( "should show login form without errors initially" , async ( ) => {
5559 const response = await request ( app ) . get ( "/login" ) ;
5660
5761 expect ( response . text ) . toContain ( "form" ) ;
5862 expect ( response . text ) . not . toContain ( "Invalid credentials" ) ;
5963 } ) ;
6064
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
6368 const mockUser = {
6469 id : "user123" ,
6570 email : "test@example.com" ,
@@ -81,8 +86,9 @@ describe("loginController", () => {
8186 expect ( mockUser . comparePassword ) . toHaveBeenCalledWith ( "password123" ) ;
8287 } ) ;
8388
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
8692 const mockUser = {
8793 email : "test@example.com" ,
8894 comparePassword : jest . fn ( ) . mockResolvedValue ( false ) ,
@@ -99,8 +105,9 @@ describe("loginController", () => {
99105 expect ( response . text ) . toContain ( "Invalid credentials" ) ;
100106 } ) ;
101107
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
104111 User . findOne = jest . fn ( ) . mockResolvedValue ( null ) ;
105112
106113 const response = await request ( app ) . post ( "/login" ) . type ( "form" ) . send ( {
@@ -112,8 +119,9 @@ describe("loginController", () => {
112119 expect ( response . text ) . toContain ( "Invalid credentials" ) ;
113120 } ) ;
114121
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
117125 const mockUser = {
118126 id : "user123" ,
119127 email : "test@example.com" ,
@@ -132,10 +140,35 @@ describe("loginController", () => {
132140 expect ( response . status ) . toBe ( 302 ) ;
133141 expect ( response . headers . location ) . toBe ( "/agents/new" ) ;
134142 } ) ;
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 ( ) => {
136146 const response = await request ( app ) . get ( "/logout" ) ;
137147
138148 expect ( response . status ) . toBe ( 302 ) ;
139149 expect ( response . headers . location ) . toBe ( "/" ) ;
140150 } ) ;
141151} ) ;
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