-
Notifications
You must be signed in to change notification settings - Fork 84
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
2670193
commit cdb5bee
Showing
6 changed files
with
154 additions
and
131 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
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
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
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
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
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 |
---|---|---|
@@ -1,139 +1,153 @@ | ||
// @ts-nocheck | ||
|
||
import { AppDataSource } from "../data-source"; | ||
import AppDataSource from "../data-source"; | ||
import { User } from "../models"; | ||
import { hashPassword, generateNumericOTP, comparePassword } from "../utils"; | ||
import { Sendmail } from "../utils/mail"; | ||
import jwt from "jsonwebtoken"; | ||
import config from "../config"; | ||
import { Conflict, HttpError } from "../middleware"; | ||
import { AuthService } from "../services"; | ||
|
||
jest.mock("../data-source"); | ||
jest.mock("../data-source", () => { | ||
return { | ||
AppDataSource: { | ||
manager: {}, | ||
initialize: jest.fn().mockResolvedValue(true), | ||
}, | ||
}; | ||
}); | ||
jest.mock("../models"); | ||
jest.mock("../utils"); | ||
jest.mock("../utils/mail"); | ||
jest.mock("jsonwebtoken"); | ||
|
||
describe("AuthService", () => { | ||
let authService: AuthService; | ||
let mockManager; | ||
|
||
beforeEach(() => { | ||
authService = new AuthService(); | ||
|
||
mockManager = { | ||
save: jest.fn(), | ||
}; | ||
|
||
// Assign the mock manager to the AppDataSource.manager | ||
AppDataSource.manager = mockManager; | ||
}); | ||
|
||
// describe("signUp", () => { | ||
// it("should sign up a new user", async () => { | ||
// const payload = { | ||
// firstName: "John", | ||
// lastName: "Doe", | ||
// email: "[email protected]", | ||
// password: "password123", | ||
// phone: "1234567890", | ||
// }; | ||
|
||
// const hashedPassword = "hashedPassword"; | ||
// const otp = "123456"; | ||
// const mailSent = "mailSent"; | ||
// const createdUser = { | ||
// id: 1, | ||
// name: "John Doe", | ||
// email: "[email protected]", | ||
// password: hashedPassword, | ||
// profile: { | ||
// phone: "1234567890", | ||
// first_name: "John", | ||
// last_name: "Doe", | ||
// avatarUrl: "", | ||
// }, | ||
// otp: parseInt(otp), | ||
// otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), | ||
// }; | ||
// const token = "access_token"; | ||
|
||
// (User.findOne as jest.Mock).mockResolvedValue(null); | ||
// (hashPassword as jest.Mock).mockResolvedValue(hashedPassword); | ||
// (generateNumericOTP as jest.Mock).mockReturnValue(otp); | ||
// (AppDataSource.manager.save as jest.Mock).mockResolvedValue(createdUser); | ||
// (jwt.sign as jest.Mock).mockReturnValue(token); | ||
// (Sendmail as jest.Mock).mockResolvedValue(mailSent); | ||
|
||
// const result = await authService.signUp(payload); | ||
|
||
// expect(result).toEqual({ | ||
// mailSent, | ||
// newUser: { | ||
// id: 1, | ||
// name: "John Doe", | ||
// email: "[email protected]", | ||
// profile: { | ||
// phone: "1234567890", | ||
// first_name: "John", | ||
// last_name: "Doe", | ||
// avatarUrl: "", | ||
// }, | ||
// otp: parseInt(otp), | ||
// otp_expires_at: expect.any(Date), | ||
// }, | ||
// access_token: token, | ||
// }); | ||
// }); | ||
|
||
// it("should throw a Conflict error if the user already exists", async () => { | ||
// const payload = { | ||
// firstName: "John", | ||
// lastName: "Doe", | ||
// email: "[email protected]", | ||
// password: "password123", | ||
// phone: "1234567890", | ||
// }; | ||
|
||
// (User.findOne as jest.Mock).mockResolvedValue({}); | ||
|
||
// await expect(authService.signUp(payload)).rejects.toThrow(Conflict); | ||
// }); | ||
// }); | ||
|
||
// describe("verifyEmail", () => { | ||
// it("should verify email with correct OTP", async () => { | ||
// const token = "validToken"; | ||
// const otp = 123456; | ||
// const user = { | ||
// id: 1, | ||
// email: "[email protected]", | ||
// otp, | ||
// otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), | ||
// isverified: false, | ||
// }; | ||
|
||
// (jwt.verify as jest.Mock).mockReturnValue({ userId: 1 }); | ||
// (User.findOne as jest.Mock).mockResolvedValue(user); | ||
// (AppDataSource.manager.save as jest.Mock).mockResolvedValue(user); | ||
|
||
// const result = await authService.verifyEmail(token, otp); | ||
|
||
// expect(result).toEqual({ message: "Email successfully verified" }); | ||
// }); | ||
|
||
// it("should throw an error for invalid OTP", async () => { | ||
// const token = "validToken"; | ||
// const otp = 123456; | ||
// const user = { | ||
// id: 1, | ||
// email: "[email protected]", | ||
// otp: 654321, | ||
// otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), | ||
// isverified: false, | ||
// }; | ||
|
||
// (jwt.verify as jest.Mock).mockReturnValue({ userId: 1 }); | ||
// (User.findOne as jest.Mock).mockResolvedValue(user); | ||
|
||
// await expect(authService.verifyEmail(token, otp)).rejects.toThrow( | ||
// HttpError | ||
// ); | ||
// }); | ||
// }); | ||
describe("signUp", () => { | ||
it("should sign up a new user", async () => { | ||
const payload = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "[email protected]", | ||
password: "password123", | ||
phone: "1234567890", | ||
}; | ||
|
||
const hashedPassword = "hashedPassword"; | ||
const otp = "123456"; | ||
const mailSent = "mailSent"; | ||
const createdUser = { | ||
id: 1, | ||
name: "John Doe", | ||
email: "[email protected]", | ||
password: hashedPassword, | ||
profile: { | ||
phone: "1234567890", | ||
first_name: "John", | ||
last_name: "Doe", | ||
avatarUrl: "", | ||
}, | ||
otp: parseInt(otp), | ||
otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), | ||
}; | ||
const token = "access_token"; | ||
|
||
(User.findOne as jest.Mock).mockResolvedValue(null); | ||
(hashPassword as jest.Mock).mockResolvedValue(hashedPassword); | ||
(generateNumericOTP as jest.Mock).mockReturnValue(otp); | ||
mockManager.save.mockResolvedValue(createdUser); | ||
(jwt.sign as jest.Mock).mockReturnValue(token); | ||
(Sendmail as jest.Mock).mockResolvedValue(mailSent); | ||
|
||
const result = await authService.signUp(payload); | ||
|
||
expect(result).toEqual({ | ||
mailSent, | ||
newUser: { | ||
id: 1, | ||
name: "John Doe", | ||
email: "[email protected]", | ||
profile: { | ||
phone: "1234567890", | ||
first_name: "John", | ||
last_name: "Doe", | ||
avatarUrl: "", | ||
}, | ||
otp: parseInt(otp), | ||
otp_expires_at: expect.any(Date), | ||
}, | ||
access_token: token, | ||
}); | ||
}); | ||
|
||
it("should throw a Conflict error if the user already exists", async () => { | ||
const payload = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "[email protected]", | ||
password: "password123", | ||
phone: "1234567890", | ||
}; | ||
|
||
(User.findOne as jest.Mock).mockResolvedValue({}); | ||
|
||
await expect(authService.signUp(payload)).rejects.toThrow(Conflict); | ||
}); | ||
}); | ||
|
||
describe("verifyEmail", () => { | ||
it("should verify email with correct OTP", async () => { | ||
const token = "validToken"; | ||
const otp = 123456; | ||
const user = { | ||
id: 1, | ||
email: "[email protected]", | ||
otp, | ||
otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), | ||
isverified: false, | ||
}; | ||
|
||
(jwt.verify as jest.Mock).mockReturnValue({ userId: 1 }); | ||
(User.findOne as jest.Mock).mockResolvedValue(user); | ||
mockManager.save.mockResolvedValue(user); | ||
|
||
const result = await authService.verifyEmail(token, otp); | ||
|
||
expect(result).toEqual({ message: "Email successfully verified" }); | ||
}); | ||
|
||
it("should throw an error for invalid OTP", async () => { | ||
const token = "validToken"; | ||
const otp = 123456; | ||
const user = { | ||
id: 1, | ||
email: "[email protected]", | ||
otp: 654321, | ||
otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), | ||
isverified: false, | ||
}; | ||
|
||
(jwt.verify as jest.Mock).mockReturnValue({ userId: 1 }); | ||
(User.findOne as jest.Mock).mockResolvedValue(user); | ||
|
||
await expect(authService.verifyEmail(token, otp)).rejects.toThrow( | ||
HttpError | ||
); | ||
}); | ||
}); | ||
|
||
describe("login", () => { | ||
it("should login user with correct credentials", async () => { | ||
|