Skip to content

Commit

Permalink
feat: added tests for handling edges cases and fixed existing test
Browse files Browse the repository at this point in the history
  • Loading branch information
pankajjs committed Dec 29, 2024
1 parent bc7c34c commit da10ae7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
40 changes: 35 additions & 5 deletions test/integration/onboardingExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ chai.use(chaiHttp);
describe("/requests Onboarding Extension", () => {
describe("POST /requests", () => {
let testUserId: string;
const testUserDiscordId = "654321";
let testUserIdForInvalidDiscordJoinedDate: string;
let testUserDiscordIdForInvalidDiscordJoinedDate: string = "54321";
const testUserDiscordId: string = "654321";
const extensionRequest = {
state: REQUEST_STATE.APPROVED,
type: REQUEST_TYPE.ONBOARDING,

Check failure on line 29 in test/integration/onboardingExtension.test.ts

View workflow job for this annotation

GitHub Actions / build (20.11.x)

Property 'ONBOARDING' does not exist on type '{ OOO: string; EXTENSION: string; TASK: string; ALL: string; }'.
Expand All @@ -38,12 +40,26 @@ describe("/requests Onboarding Extension", () => {

beforeEach(async () => {
testUserId = await addUser({...userData[6], discordId: testUserDiscordId, discordJoinedAt: "2023-04-06T01:47:34.488000+00:00"});
testUserIdForInvalidDiscordJoinedDate = await addUser({...userData[1], discordId: testUserDiscordIdForInvalidDiscordJoinedDate, discordJoinedAt: "2023-04-06T01"});
})
afterEach(async ()=>{
sinon.restore();
await cleanDb();
})

it("should not called verifyDiscordBot and return 401 response when extension type is not onboarding", (done)=> {
chai.request(app)
.post(`${postEndpoint}?dev=true`)
.send({...body, type: REQUEST_TYPE.OOO})
.end((err, res)=>{
if(err) return done(err);
expect(res.statusCode).to.equal(401);
expect(res.body.error).to.equal("Unauthorized");
expect(res.body.message).to.equal("Unauthenticated User");
done();
})
})

it("should return Feature not implemented when dev is not true", (done) => {
chai.request(app)
.post(`${postEndpoint}`)
Expand Down Expand Up @@ -140,7 +156,21 @@ describe("/requests Onboarding Extension", () => {
done();
})
})


it("should return 500 response when discordJoinedAt date string is invalid", (done) => {
createUserStatusWithState(testUserIdForInvalidDiscordJoinedDate, userStatusModel, userState.ONBOARDING);
chai.request(app)
.post(`${postEndpoint}?dev=true`)
.set("authorization", `Bearer ${botToken}`)
.send({...body, userId: testUserDiscordIdForInvalidDiscordJoinedDate})
.end((err, res)=>{
if (err) return done(err);
expect(res.statusCode).to.equal(500);
expect(res.body.message).to.equal("An internal server error occurred");
done();
})
})

it("should return 404 response when user does not exist", (done) => {
chai.request(app)
.post(`${postEndpoint}?dev=true`)
Expand All @@ -155,16 +185,16 @@ describe("/requests Onboarding Extension", () => {
})
})

it("should return 401 response when user's status is not onboarding", (done)=> {
it("should return 403 response when user's status is not onboarding", (done)=> {
createUserStatusWithState(testUserId, userStatusModel, userState.ACTIVE);
chai.request(app)
.post(`${postEndpoint}?dev=true`)
.set("authorization", `Bearer ${botToken}`)
.send(body)
.end((err, res) => {
if (err) return done(err);
expect(res.statusCode).to.equal(401);
expect(res.body.error).to.equal("Unauthorized");
expect(res.statusCode).to.equal(403);
expect(res.body.error).to.equal("Forbidden");
expect(res.body.message).to.equal(UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST);
done();
})
Expand Down
45 changes: 45 additions & 0 deletions test/unit/utils/requests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { convertDateStringToMilliseconds, getNewDeadline } from "../../../utils/requests"
import { convertDaysToMilliseconds } from "../../../utils/time";
import {expect} from "chai";

describe("Test getNewDeadline", () => {
const currentDate = Date.now();
const millisecondsInTwoDays = convertDaysToMilliseconds(2);
let oldEndsOn = currentDate - millisecondsInTwoDays;
const numberOfDaysInMillisecond = convertDaysToMilliseconds(5);

it("should return correct new deadline when old deadline has been missed", () => {
const res = getNewDeadline(currentDate, oldEndsOn, numberOfDaysInMillisecond);
expect(res).to.equal(currentDate + numberOfDaysInMillisecond);
})

it("shoudl return correct new deadline when old deadline has not been missed", () => {
oldEndsOn += millisecondsInTwoDays;
const res = getNewDeadline(currentDate, oldEndsOn, numberOfDaysInMillisecond);
expect(res).to.equal(oldEndsOn + numberOfDaysInMillisecond);
})
})

describe("Test convertDateStringInMilliseconds", () => {
const validDateString = "2024-10-17T16:10:52.668000+00:00";
const invalidDateString = "2024-10-17T16";
const emptyDateString = "";

it("should return isDate equal false for invalid date string", () => {
const res = convertDateStringToMilliseconds(invalidDateString);
expect(res.isDate).to.equal(false);
expect(res.milliseconds).to.equal(undefined);
})

it("should return isDate equal false for empty date string", () => {
const res = convertDateStringToMilliseconds(emptyDateString);
expect(res.isDate).to.equal(false);
expect(res.milliseconds).to.equal(undefined);
})

it("should return isDate equal true for valid date string", () => {
const res = convertDateStringToMilliseconds(validDateString);
expect(res.isDate).to.equal(true);
expect(res.milliseconds).to.equal(Date.parse(validDateString));
})
})

0 comments on commit da10ae7

Please sign in to comment.