|
| 1 | +import request from "supertest"; |
| 2 | +import app from "../app"; |
| 3 | +import User from "../models/User"; |
| 4 | + |
| 5 | +jest.mock("../models/User.js"); |
| 6 | + |
| 7 | +// Mockeamos connectMongoose para evitar conexiones reales |
| 8 | +jest.mock("../lib/connectMongoose.js", () => { |
| 9 | + return jest.fn().mockResolvedValue({ |
| 10 | + name: "mockConnection", |
| 11 | + close: jest.fn(), |
| 12 | + }); |
| 13 | +}); |
| 14 | + |
| 15 | +// Mockeamos session para evitar problemas de sesión |
| 16 | +jest.mock("express-session", () => { |
| 17 | + return () => (req, res, next) => { |
| 18 | + req.session = { |
| 19 | + userId: null, |
| 20 | + regenerate: jest.fn((cb) => cb(null)), |
| 21 | + }; |
| 22 | + next(); |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +// Mockeamos sessionManager para simplificar los tests |
| 27 | +jest.mock("../lib/sessionManager", () => ({ |
| 28 | + middleware: (req, res, next) => { |
| 29 | + req.session = { |
| 30 | + userId: null, |
| 31 | + regenerate: jest.fn((cb) => cb(null)), |
| 32 | + }; |
| 33 | + next(); |
| 34 | + }, |
| 35 | + useSessionInViews: (req, res, next) => { |
| 36 | + res.locals.session = req.session || {}; |
| 37 | + next(); |
| 38 | + }, |
| 39 | + guard: (req, res, next) => next(), |
| 40 | +})); |
| 41 | + |
1 | 42 | describe("loginController", () => { |
2 | | - it.todo("debe devolver un 200 al visitar la página de login"); |
| 43 | + // Limpiamos mocks antes de cada test |
| 44 | + beforeEach(() => { |
| 45 | + jest.clearAllMocks(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("debe devolver un 200 al visitar la página de login", async () => { |
| 49 | + const response = await request(app).get("/login"); |
| 50 | + |
| 51 | + expect(response.status).toBe(200); |
| 52 | + }); |
| 53 | + |
| 54 | + it("debe mostrar formulario de login sin errores inicialmente", async () => { |
| 55 | + const response = await request(app).get("/login"); |
| 56 | + |
| 57 | + expect(response.text).toContain("form"); |
| 58 | + expect(response.text).not.toContain("Invalid credentials"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("debe redirigir a home tras login exitoso", async () => { |
| 62 | + // Mock de usuario existente con contraseña correcta |
| 63 | + const mockUser = { |
| 64 | + id: "user123", |
| 65 | + email: "test@example.com", |
| 66 | + comparePassword: jest.fn().mockResolvedValue(true), |
| 67 | + addLoginRecord: jest.fn(), |
| 68 | + save: jest.fn().mockResolvedValue(true), |
| 69 | + }; |
| 70 | + |
| 71 | + User.findOne = jest.fn().mockResolvedValue(mockUser); |
| 72 | + |
| 73 | + const response = await request(app).post("/login").type("form").send({ |
| 74 | + email: "test@example.com", |
| 75 | + password: "password123", |
| 76 | + }); |
| 77 | + |
| 78 | + expect(response.status).toBe(302); |
| 79 | + expect(response.headers.location).toBe("/"); |
| 80 | + expect(User.findOne).toHaveBeenCalledWith({ email: "test@example.com" }); |
| 81 | + expect(mockUser.comparePassword).toHaveBeenCalledWith("password123"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("debe mostrar error con credenciales inválidas", async () => { |
| 85 | + // Mock de usuario existente con contraseña incorrecta |
| 86 | + const mockUser = { |
| 87 | + email: "test@example.com", |
| 88 | + comparePassword: jest.fn().mockResolvedValue(false), |
| 89 | + }; |
| 90 | + |
| 91 | + User.findOne = jest.fn().mockResolvedValue(mockUser); |
| 92 | + |
| 93 | + const response = await request(app).post("/login").type("form").send({ |
| 94 | + email: "test@example.com", |
| 95 | + password: "wrongpassword", |
| 96 | + }); |
| 97 | + |
| 98 | + expect(response.status).toBe(200); |
| 99 | + expect(response.text).toContain("Invalid credentials"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("debe mostrar error cuando el usuario no existe", async () => { |
| 103 | + // Mock de usuario no existente |
| 104 | + User.findOne = jest.fn().mockResolvedValue(null); |
| 105 | + |
| 106 | + const response = await request(app).post("/login").type("form").send({ |
| 107 | + email: "nonexistent@example.com", |
| 108 | + password: "anypassword", |
| 109 | + }); |
| 110 | + |
| 111 | + expect(response.status).toBe(200); |
| 112 | + expect(response.text).toContain("Invalid credentials"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("debe redirigir a la URL especificada después del login exitoso", async () => { |
| 116 | + // Mock de usuario existente con contraseña correcta |
| 117 | + const mockUser = { |
| 118 | + id: "user123", |
| 119 | + email: "test@example.com", |
| 120 | + comparePassword: jest.fn().mockResolvedValue(true), |
| 121 | + addLoginRecord: jest.fn(), |
| 122 | + save: jest.fn().mockResolvedValue(true), |
| 123 | + }; |
| 124 | + |
| 125 | + User.findOne = jest.fn().mockResolvedValue(mockUser); |
| 126 | + |
| 127 | + const response = await request(app).post("/login?redir=/agents/new").type("form").send({ |
| 128 | + email: "test@example.com", |
| 129 | + password: "password123", |
| 130 | + }); |
| 131 | + |
| 132 | + expect(response.status).toBe(302); |
| 133 | + expect(response.headers.location).toBe("/agents/new"); |
| 134 | + }); |
| 135 | + it("debe cerrar sesión y redirigir a home al hacer logout", async () => { |
| 136 | + const response = await request(app).get("/logout"); |
| 137 | + |
| 138 | + expect(response.status).toBe(302); |
| 139 | + expect(response.headers.location).toBe("/"); |
| 140 | + }); |
3 | 141 | }); |
0 commit comments