Skip to content

Commit

Permalink
handle book-seat concurrently fix bugs update db seat-model
Browse files Browse the repository at this point in the history
  • Loading branch information
priyanshuxkumar committed Oct 4, 2024
1 parent 8b61b4e commit 6fef321
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 117 deletions.
102 changes: 75 additions & 27 deletions __tests__/email.test.ts
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.",
});
});
});
});
Loading

0 comments on commit 6fef321

Please sign in to comment.