Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

teste de login #21

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions auth/src/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Request, Response } from "express";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import User from "./models/user";
import { login } from "./controller/login";
import { publishAuthenticationEvent } from "./controller/publishAuthEvent";

jest.mock("./models/user");
jest.mock("bcrypt");
jest.mock("jsonwebtoken");
jest.mock("./controller/publishAuthEvent");

describe("login function", () => {
let req: Partial<Request>;
let res: Partial<Response>;

beforeEach(() => {
req = {
body: {
email: "[email protected]",
password: "Mauagendar$up3r$3nh4!",
},
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
});

afterEach(() => {
jest.clearAllMocks();
});

it("should log in a user with correct credentials", async () => {
const user = new User({
email: "[email protected]",
password: await bcrypt.hash("Mauagendar$up3r$3nh4!", undefined),
});

(User.findOne as jest.Mock).mockResolvedValue(user);
(bcrypt.compare as jest.Mock).mockResolvedValue(true);
(jwt.sign as jest.Mock).mockReturnValue("token");

await login(req as Request, res as Response);

expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
message: "User logged in!",
token: "token",
});
expect(User.findOne).toHaveBeenCalledWith({ where: { email: req.body.email } });
expect(bcrypt.compare).toHaveBeenCalledWith(req.body.password, user.password);
expect(jwt.sign).toHaveBeenCalledWith(
{
email: user.email,
user_id: user.id,
},
undefined,
{ expiresIn: "1h" }
);
expect(publishAuthenticationEvent).toHaveBeenCalledWith("token");
});

it("should return an error if the user is not found", async () => {
(User.findOne as jest.Mock).mockResolvedValue(null);

await login(req as Request, res as Response);

expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: "User not found" });
expect(bcrypt.compare).not.toHaveBeenCalled();
expect(jwt.sign).not.toHaveBeenCalled();
expect(publishAuthenticationEvent).not.toHaveBeenCalled();
});

it("should return an error if the password is incorrect", async () => {
const user = new User({
email: "[email protected]",
password: await bcrypt.hash("Mauagendar$up3r$3nh4!", 10),
});

(User.findOne as jest.Mock).mockResolvedValue(user);
(bcrypt.compare as jest.Mock).mockResolvedValue(false);

await login(req as Request, res as Response);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ message: "Invalid credentials" });
expect(bcrypt.compare).toHaveBeenCalledWith(req.body.password, user.password);
expect(jwt.sign).not.toHaveBeenCalled();
expect(publishAuthenticationEvent).not.toHaveBeenCalled();
});
});
Loading