-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle book-seat concurrently fix bugs update db seat-model
- Loading branch information
1 parent
8b61b4e
commit 6fef321
Showing
11 changed files
with
481 additions
and
117 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 |
---|---|---|
@@ -1,56 +1,104 @@ | ||
import {sendPasswordResetEmail , sendVerificationOtpToEmail} from "../src/helper/sendOtp" | ||
jest.mock("../src/redis/redisClient", () => ({ | ||
setex: jest.fn(), | ||
})); | ||
|
||
jest.mock("../src/helper/mailTransporter", () => ({ | ||
__esModule: true, | ||
default: { | ||
sendMail: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock("../src/helper/sendOtp", () => ({ | ||
generateAndStoreOTPSecret: jest.fn(), | ||
})); | ||
|
||
import { | ||
sendPasswordResetEmail, | ||
sendVerificationOtpToEmail, | ||
generateAndStoreOTPSecret, | ||
} from "../src/helper/sendOtp"; | ||
import redisClient from "../src/redis/redisClient"; | ||
import transporter from "../src/helper/mailTransporter"; | ||
import dotenv from 'dotenv'; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
dotenv.config(); | ||
|
||
jest.mock("../src/redis/redisClient"); | ||
jest.mock("../src/helper/mailTransporter"); | ||
describe("sendVerificationOtpToEmail", () => { | ||
const mockEmail = "[email protected]"; | ||
const mockOtp = "123456"; | ||
|
||
describe("sendPasswordResetEmail", () => { | ||
it("should send a password reset email successfully", async () => { | ||
(redisClient.setex as jest.Mock).mockResolvedValue(true); | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
(transporter.sendMail as jest.Mock).mockResolvedValue({ messageId: "123" }); | ||
it("should generate an OTP, store it in Redis, and send an email successfully", async () => { | ||
(generateAndStoreOTPSecret as jest.Mock).mockResolvedValue(mockOtp); | ||
(transporter.sendMail as jest.Mock).mockResolvedValue({ | ||
messageId: "123", | ||
}); | ||
|
||
const result = await sendPasswordResetEmail("[email protected]"); | ||
await sendVerificationOtpToEmail(mockEmail); | ||
expect(generateAndStoreOTPSecret).toHaveBeenCalledWith(mockEmail); | ||
|
||
expect(redisClient.setex).toHaveBeenCalledWith( | ||
"passwordResetToken:[email protected]", | ||
600, | ||
expect.any(String) | ||
); | ||
expect(transporter.sendMail).toHaveBeenCalledWith({ | ||
from: process.env.EMAIL, | ||
to: "[email protected]", | ||
subject: "Reset your password for StealShow", | ||
html: expect.any(String), | ||
to: mockEmail, | ||
subject: "Verify your email for StealShow", | ||
text: `Your OTP for verification is: ${mockOtp}`, | ||
html: `<p>Your OTP for verification is: <b>${mockOtp}</b></p>`, | ||
}); | ||
expect(result).toEqual({ message: "Password reset email sent successfully." }); | ||
}); | ||
|
||
it("should throw an error if email sending fails", async () => { | ||
(generateAndStoreOTPSecret as jest.Mock).mockResolvedValue(mockOtp); | ||
(transporter.sendMail as jest.Mock).mockRejectedValue( | ||
new Error("Failed to send email") | ||
); | ||
|
||
await expect(sendVerificationOtpToEmail(mockEmail)).rejects.toThrow( | ||
"Failed to send OTP email." | ||
); | ||
|
||
expect(generateAndStoreOTPSecret).toHaveBeenCalledWith(mockEmail); | ||
expect(transporter.sendMail).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should throw an error if Redis storage fails", async () => { | ||
(generateAndStoreOTPSecret as jest.Mock).mockRejectedValue( | ||
new Error("Failed to store OTP in Redis") | ||
); | ||
|
||
await expect(sendVerificationOtpToEmail(mockEmail)).rejects.toThrow( | ||
"Failed to send OTP email." | ||
); | ||
|
||
expect(generateAndStoreOTPSecret).toHaveBeenCalledWith(mockEmail); | ||
expect(transporter.sendMail).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("sendVerificationOtpToEmail", () => { | ||
it("should send a verification OTP email successfully", async () => { | ||
describe("sendPasswordResetEmail", () => { | ||
it("should send a password reset email successfully", async () => { | ||
(redisClient.setex as jest.Mock).mockResolvedValue(true); | ||
|
||
(transporter.sendMail as jest.Mock).mockResolvedValue({ messageId: "123" }); | ||
|
||
const result = await sendVerificationOtpToEmail("[email protected]"); | ||
const result = await sendPasswordResetEmail("[email protected]"); | ||
|
||
expect(redisClient.setex).toHaveBeenCalledWith( | ||
"otpSecret:[email protected]", | ||
"passwordResetToken:[email protected]", | ||
600, | ||
expect.any(String) | ||
); | ||
expect(transporter.sendMail).toHaveBeenCalledWith({ | ||
from: process.env.EMAIL, | ||
to: "[email protected]", | ||
subject: "Verify your email for StealShow", | ||
text: expect.any(String), | ||
subject: "Reset your password for StealShow", | ||
html: expect.any(String), | ||
}); | ||
expect(result).toEqual({ message: "OTP sent successfully." }); | ||
expect(result).toEqual({ | ||
message: "Password reset email sent successfully.", | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.