Skip to content

Commit ae1bb30

Browse files
committed
feat: added tests for middlewares
1 parent 2ac202e commit ae1bb30

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { REQUEST_TYPE } from "../../../constants/requests";
2+
import { createOnboardingExtensionRequestValidator } from "../../../middlewares/validators/onboardingExtensionRequest";
3+
import sinon from "sinon";
4+
import { CreateOnboardingExtensionBody } from "../../../types/onboardingExtension";
5+
import { expect } from "chai";
6+
7+
describe("Onboarding Extension Request Validators", () => {
8+
let req: any;
9+
let res: any;
10+
let nextSpy: sinon.SinonSpy;
11+
beforeEach(function () {
12+
res = {
13+
boom: {
14+
badRequest: sinon.spy(),
15+
},
16+
};
17+
nextSpy = sinon.spy();
18+
});
19+
20+
describe("createOnboardingExtensionRequestValidator", () => {
21+
const requestBody:CreateOnboardingExtensionBody = {
22+
numberOfDays: 1,
23+
reason: "This is reason",
24+
userId: "22222",
25+
requestedBy: "1111",
26+
type: REQUEST_TYPE.ONBOARDING
27+
}
28+
it("should validate for a valid create request", async () => {
29+
req = {
30+
body: requestBody
31+
};
32+
res = {};
33+
34+
await createOnboardingExtensionRequestValidator(req as any, res as any, nextSpy);
35+
expect(nextSpy.calledOnce, "next should be called once");
36+
});
37+
38+
it("should not validate for an invalid request on wrong type", async () => {
39+
req = {
40+
body: { ...requestBody, type: REQUEST_TYPE.EXTENSION },
41+
};
42+
try {
43+
await createOnboardingExtensionRequestValidator(req as any, res as any, nextSpy);
44+
} catch (error) {
45+
expect(error.details[0].message).to.equal(`"type" must be [ONBOARDING]`);
46+
}
47+
});
48+
49+
it("should not validate for an invalid request on wrong numberOfDays", async () => {
50+
req = {
51+
body: { ...requestBody, numberOfDays: "2" },
52+
};
53+
try {
54+
await createOnboardingExtensionRequestValidator(req as any, res as any, nextSpy);
55+
} catch (error) {
56+
expect(error.details[0].message).to.equal(`numberOfDays must be a number`);
57+
}
58+
});
59+
60+
it("should not validate for an invalid request on wrong userId", async () => {
61+
req = {
62+
body: { ...requestBody, userId: undefined },
63+
};
64+
try {
65+
await createOnboardingExtensionRequestValidator(req as any, res as any, nextSpy);
66+
} catch (error) {
67+
expect(error.details[0].message).to.equal(`userId is required`);
68+
}
69+
});
70+
});
71+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import sinon from "sinon";
2+
import { skipAuthenticateForOnboardingExtensionRequest } from "../../../middlewares/skipAuthenticateForOnboardingExtension";
3+
import { REQUEST_TYPE } from "../../../constants/requests";
4+
import { assert } from "chai";
5+
6+
describe("skipAuthenticateForOnboardingExtensionRequest Middleware", () => {
7+
let req, res, next, authenticate: sinon.SinonSpy, verifyDiscordBot: sinon.SinonSpy;
8+
9+
beforeEach(() => {
10+
authenticate = sinon.spy();
11+
verifyDiscordBot = sinon.spy();
12+
req = {
13+
body:{},
14+
query:{},
15+
},
16+
res = {}
17+
});
18+
19+
it("should call authenticate when type is not onboarding", () => {
20+
req.body.type = REQUEST_TYPE.TASK
21+
const middleware = skipAuthenticateForOnboardingExtensionRequest(authenticate, verifyDiscordBot);
22+
middleware(req, res, next);
23+
24+
assert.isTrue(authenticate.calledOnce, "authenticate should be called once");
25+
assert.isTrue(verifyDiscordBot.notCalled, "verifyDiscordBot should not be called");
26+
});
27+
28+
it("should not call verifyDicordBot and authenticate when dev is not true and type is onboarding", async () => {
29+
req.query.dev = "false";
30+
req.body.type = REQUEST_TYPE.ONBOARDING;
31+
32+
const middleware = skipAuthenticateForOnboardingExtensionRequest(authenticate, verifyDiscordBot);
33+
middleware(req, res, next);
34+
35+
assert.isTrue(verifyDiscordBot.notCalled, "verifyDiscordBot should not be called");
36+
assert.isTrue(authenticate.notCalled, "authenticate should not be called");
37+
});
38+
39+
it("should call verifyDiscordBot when dev is true and type is onboarding", () => {
40+
req.query.dev = "true";
41+
req.body.type = REQUEST_TYPE.ONBOARDING;
42+
43+
const middleware = skipAuthenticateForOnboardingExtensionRequest(authenticate, verifyDiscordBot);
44+
middleware(req, res, next);
45+
46+
assert.isTrue(verifyDiscordBot.calledOnce, "verifyDiscordBot should be called once");
47+
assert.isTrue(authenticate.notCalled, "authenticate should not be called");
48+
});
49+
});

0 commit comments

Comments
 (0)