Skip to content

Commit

Permalink
fix: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaskh3 committed Oct 13, 2024
1 parent 1ffe7f1 commit 75e80f4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
3 changes: 2 additions & 1 deletion constants/subscription-validator.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
export const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
export const phoneNumberRegex = /^[+]{1}(?:[0-9\-\\(\\)\\/.]\s?){6,15}[0-9]{1}$/;
3 changes: 2 additions & 1 deletion middlewares/devFlag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextFunction, Request, Response } from "express";
import { NextFunction } from "express";
import { CustomRequest, CustomResponse } from "../types/global";

export const devFlagMiddleware = (req: CustomRequest, res: CustomResponse, next: NextFunction) => {
Expand All @@ -9,6 +9,7 @@ export const devFlagMiddleware = (req: CustomRequest, res: CustomResponse, next:
}
next();
} catch (err) {
logger.error("Error occurred in devFlagMiddleware:", err.message);
next(err);
}
};
11 changes: 9 additions & 2 deletions middlewares/validators/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { NextFunction } from "express";
import { CustomRequest, CustomResponse } from "../../types/global";
import { emailRegex } from "../../constants/subscription-validator";
import { emailRegex, phoneNumberRegex } from "../../constants/subscription-validator"; // Removed phoneNumberRegex import
import Joi from 'joi';

export const validateSubscribe = (req: CustomRequest, res: CustomResponse, next: NextFunction) => {

if(req.body.email){
req.body.email = req.body.email.trim();
}
if (req.body.phoneNumber) {
req.body.phoneNumber = req.body.phoneNumber.trim();
}
const subscribeSchema = Joi.object({
phoneNumber: Joi.string().allow('').optional(),
phoneNumber: Joi.string().allow('').optional().regex(phoneNumberRegex),
email: Joi.string().required().regex(emailRegex)
});
const { error } = subscribeSchema.validate(req.body);
Expand Down
42 changes: 36 additions & 6 deletions test/unit/middlewares/subscription-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("Middleware | Validators | Subscription", function () {
email: "[email protected]",
};

await validateSubscribe(req, res, nextSpy);
validateSubscribe(req, res, nextSpy);

expect(nextSpy.calledOnce).to.be.equal(true);
expect(res.status.called).to.be.equal(false);
Expand All @@ -32,7 +32,7 @@ describe("Middleware | Validators | Subscription", function () {
email: "[email protected]",
};

await validateSubscribe(req, res, nextSpy);
validateSubscribe(req, res, nextSpy);
expect(nextSpy.calledOnce).to.be.equal(true);
expect(res.status.called).to.be.equal(false);
expect(res.json.called).to.be.equal(false);
Expand All @@ -43,7 +43,7 @@ describe("Middleware | Validators | Subscription", function () {
phoneNumber: "+911234567890",
};

await validateSubscribe(req, res, nextSpy);
validateSubscribe(req, res, nextSpy);

expect(nextSpy.called).to.be.equal(false);
expect(res.status.calledOnceWith(400)).to.be.equal(true);
Expand All @@ -54,7 +54,7 @@ describe("Middleware | Validators | Subscription", function () {
it("should return a 400 error when both phoneNumber and email are missing", async function () {
req.body = {};

await validateSubscribe(req, res, nextSpy);
validateSubscribe(req, res, nextSpy);
expect(nextSpy.called).to.be.equal(false);
expect(res.status.calledOnceWith(400)).to.be.equal(true);
expect(res.json.calledOnce).to.be.equal(true);
Expand All @@ -67,7 +67,7 @@ describe("Middleware | Validators | Subscription", function () {
email: "invalid-email",
};

await validateSubscribe(req, res, nextSpy);
validateSubscribe(req, res, nextSpy);

expect(nextSpy.called).to.be.equal(false);
expect(res.status.calledOnceWith(400)).to.be.equal(true);
Expand All @@ -83,9 +83,39 @@ describe("Middleware | Validators | Subscription", function () {
email: "[email protected]",
};

await validateSubscribe(req, res, nextSpy);
validateSubscribe(req, res, nextSpy);
expect(nextSpy.calledOnce).to.be.equal(true);
expect(res.status.called).to.be.equal(false);
expect(res.json.called).to.be.equal(false);
});

it("should trim and validate phoneNumber if it contains leading or trailing spaces", async function () {
req.body = {
phoneNumber: " +911234567890 ",
email: "[email protected]",
};

validateSubscribe(req, res, nextSpy);

expect(nextSpy.calledOnce).to.be.equal(true);
expect(res.status.called).to.be.equal(false);
expect(res.json.called).to.be.equal(false);
expect(req.body.phoneNumber).to.equal("+911234567890");
});

it("should return a 400 error when phoneNumber is in incorrect format", async function () {
req.body = {
phoneNumber: "invalid-number",
email: "[email protected]",
};

validateSubscribe(req, res, nextSpy);

expect(nextSpy.called).to.be.equal(false);
expect(res.status.calledOnceWith(400)).to.be.equal(true);
expect(res.json.calledOnce).to.be.equal(true);
expect(res.json.firstCall.args[0])
.to.have.property("error")
.that.includes('"phoneNumber" with value "invalid-number" fails to match the required pattern');
});
});

0 comments on commit 75e80f4

Please sign in to comment.