|
1 |
| -import request from "supertest"; |
2 |
| -import app from "../app"; |
3 |
| -import { guard, middleware } from "../lib/sessionManager"; |
4 |
| - |
5 |
| -// Podemos mockear funciones internas |
6 |
| -jest.mock("../lib/sessionManager", () => ({ |
7 |
| - guard: jest.fn((req, res, next) => { |
8 |
| - // Simular que el usuario no esta autenticado |
9 |
| - res.redirect(`/login?redir=${req.url}`); |
10 |
| - |
11 |
| - // Todos los middlewares llaman a next(); |
12 |
| - next(); |
13 |
| - }), |
14 |
| - middleware: jest.fn((req, res, next) => next()), |
15 |
| - useSessionInViews: jest.fn((req, res, next) => next()), |
16 |
| -})); |
17 |
| - |
18 |
| -describe("AgentsController", () => { |
19 |
| - it("Debería redirigir a la vista de login si no existe una sesión", async () => { |
20 |
| - expect.assertions(2); |
21 |
| - // Hacer una request a nuestra app |
22 |
| - const response = await request(app) |
23 |
| - // en la ruta /agents/new |
24 |
| - .get("/agents/new"); |
25 |
| - |
26 |
| - // Esperamos una redirección a login |
27 |
| - expect(response.status).toBe(302); |
28 |
| - expect(response.headers.location).toBe("/login?redir=/agents/new"); |
| 1 | +import User from "./User"; |
| 2 | + |
| 3 | +describe("User model & schema", () => { |
| 4 | + it("Debería hashear correctamente la contraseña", async () => { |
| 5 | + // Crear un password plano |
| 6 | + const clearPassword = "supersegura"; |
| 7 | + |
| 8 | + // Crear su hash |
| 9 | + const hash = await User.hashPassword(clearPassword); |
| 10 | + |
| 11 | + // Verificar que son diferentes |
| 12 | + expect(hash).not.toBe(clearPassword); |
| 13 | + expect(hash.length).toBeGreaterThan(1); |
| 14 | + expect(hash).not.toHaveLength(0); |
29 | 15 | });
|
30 | 16 |
|
31 |
| - it("debería llamar al guard antes de permitir el acceso a la ruta", async () => { |
32 |
| - expect.assertions(1); |
33 |
| - const response = await request(app).get("/agents/new"); |
| 17 | + it.todo("Debería generar el hash utilizando la utilidad bcrypt"); |
| 18 | + |
| 19 | + it("Debería comparar correctamente la contraseña", async () => { |
| 20 | + expect.assertions(2); |
| 21 | + // Al crear un usuario, crear su hash y compararlo con el password plano, deberia ser true. |
| 22 | + // Crear un password plano |
| 23 | + const clearPassword = "supersegura"; |
| 24 | + |
| 25 | + // Crear su hash |
| 26 | + const hash = await User.hashPassword(clearPassword); |
| 27 | + |
| 28 | + const user = new User({ |
| 29 | + email: "demo@example.com", |
| 30 | + password: hash, |
| 31 | + }); |
| 32 | + |
| 33 | + const matchPassword = await user.comparePassword(clearPassword); |
| 34 | + expect(matchPassword).toBeTrue(); |
34 | 35 |
|
35 |
| - // El middleware de guard se debe llamar |
36 |
| - expect(guard).toHaveBeenCalled(); |
| 36 | + const notMatchPassword = await user.comparePassword("errorPassword"); |
| 37 | + expect(notMatchPassword).toBeFalse(); |
37 | 38 | });
|
38 | 39 | });
|
0 commit comments