-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea36a8b
commit bc47bdf
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |