Skip to content

Commit

Permalink
feat: add test for send-email API
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaskh3 committed Sep 8, 2024
1 parent b534a6e commit 8c8f6b4
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions test/integration/subscription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ const chaiHttp = require("chai-http");
chai.use(chaiHttp);
const nodemailer = require("nodemailer");
const nodemailerMock = require("nodemailer-mock");

const userData = require("../fixtures/user/user")();
const { expect } = chai;
let userId = "";

const superUser = userData[4];
let superUserAuthToken = "";
describe("/subscription email notifications", function () {
let jwt;

Expand Down Expand Up @@ -70,7 +71,9 @@ describe("/subscription email notifications", function () {
});

describe("/send-email endpoint", function () {
beforeEach(function () {
beforeEach(async function () {
const superUserId = await addUser(superUser);
superUserAuthToken = authService.generateAuthToken({ userId: superUserId });
sinon.stub(nodemailerMock, "createTransport").callsFake(nodemailerMock.createTransport);
});

Expand All @@ -79,6 +82,21 @@ describe("/subscription email notifications", function () {
nodemailerMock.mock.reset();
});

it("Should return 401 if the super user is not logged in", function (done) {
chai
.request(app)
.post("/subscription")
.end((err, res) => {
if (err) {
return done();
}
expect(res).to.have.status(401);
expect(res.body).to.be.a("object");
expect(res.body.message).to.equal("Unauthenticated User");
return done();
});
});

it("should handle errors if sending email fails", function (done) {
sinon.stub(nodemailer, "createTransport").callsFake(() => {
throw new Error("Transport error");
Expand All @@ -87,9 +105,9 @@ describe("/subscription email notifications", function () {
chai
.request(app)
.get("/subscription/send-email")
.set("Cookie", `${cookieName}=${superUserAuthToken}`)
.end((err, res) => {
if (err) return done(err);

expect(res).to.have.status(500);
expect(res.body).to.have.property("message", "Failed to send email");
expect(res.body).to.have.property("error");
Expand Down

0 comments on commit 8c8f6b4

Please sign in to comment.