Skip to content

Commit b83ca14

Browse files
test/ unit tests for get Impersonation requests (#2443)
* added unit tests for fetch logic * fixed types and small comments * fixed comments and variable types * added impersonation requests array * removed update model function from tests * removed page based pagination from tests * added new validator tests for new route --------- Co-authored-by: Achintya Chatterjee <[email protected]>
1 parent 8c27d4c commit b83ca14

File tree

2 files changed

+234
-3
lines changed

2 files changed

+234
-3
lines changed

test/unit/middlewares/impersonationRequests.test.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import chai from "chai";
22
import sinon from "sinon";
33
import {
4-
createImpersonationRequestValidator
4+
createImpersonationRequestValidator,
5+
getImpersonationRequestByIdValidator,
6+
getImpersonationRequestsValidator
57
} from "../../../middlewares/validators/impersonationRequests";
68
import {
79
CreateImpersonationRequest,
810
CreateImpersonationRequestBody,
911
ImpersonationRequestResponse,
12+
GetImpersonationControllerRequest,
13+
GetImpersonationRequestByIdRequest,
1014
} from "../../../types/impersonationRequest";
1115
import { Request, Response } from "express";
16+
import { getImpersonationRequestById } from "../../../models/impersonationRequests";
1217

1318
const { expect } = chai;
1419

@@ -75,4 +80,101 @@ describe("Impersonation Request Validators", function () {
7580
expect(nextSpy.called).to.be.false;
7681
});
7782
});
83+
84+
describe("getImpersonationRequestByIdValidator", function () {
85+
it("should validate for a valid get by id impersonation request", async function () {
86+
req = {
87+
params:{
88+
id:"cuJ7lKT1DFybHNwaMJHu",
89+
dev:"true"
90+
}
91+
}
92+
await getImpersonationRequestByIdValidator(
93+
req as GetImpersonationRequestByIdRequest,
94+
res as ImpersonationRequestResponse,
95+
nextSpy
96+
);
97+
expect(nextSpy.calledOnce).to.be.true;
98+
})
99+
100+
it("should not validate for a request without dev flag", async function (){
101+
req = {
102+
params:{
103+
id:"192sjsj/dhid"
104+
}
105+
}
106+
await getImpersonationRequestByIdValidator(
107+
req as GetImpersonationRequestByIdRequest,
108+
res as ImpersonationRequestResponse,
109+
nextSpy
110+
);
111+
112+
expect(res.boom.badRequest.calledOnce).to.be.true;
113+
expect(nextSpy.called).to.be.false;
114+
})
115+
116+
it("should not validate for a request with missing id", async function(){
117+
req = {
118+
params:{
119+
id:"",
120+
dev:"true"
121+
}
122+
}
123+
await getImpersonationRequestByIdValidator(
124+
req as GetImpersonationRequestByIdRequest,
125+
res as ImpersonationRequestResponse,
126+
nextSpy
127+
);
128+
129+
expect(res.boom.badRequest.calledOnce).to.be.true;
130+
expect(nextSpy.called).to.be.false;
131+
})
132+
})
133+
134+
describe("getImpersonationRequestsValidator", function () {
135+
it("should validate for a valid get impersonation requests query", async function () {
136+
req = {
137+
query: {
138+
dev: "true",
139+
status: "APPROVED",
140+
},
141+
};
142+
await getImpersonationRequestsValidator(
143+
req as GetImpersonationControllerRequest,
144+
res as ImpersonationRequestResponse,
145+
nextSpy
146+
);
147+
expect(nextSpy.calledOnce).to.be.true;
148+
});
149+
150+
it("should validate a request with partial query", async function () {
151+
req = {
152+
query: {
153+
dev: "true",
154+
},
155+
};
156+
await getImpersonationRequestsValidator(
157+
req as GetImpersonationControllerRequest,
158+
res as ImpersonationRequestResponse,
159+
nextSpy
160+
);
161+
expect(nextSpy.calledOnce).to.be.true;
162+
});
163+
164+
it("should not validate for an invalid status in query", async function () {
165+
req = {
166+
query: {
167+
dev: "true",
168+
status: "INVALID_STATUS",
169+
},
170+
};
171+
await getImpersonationRequestsValidator(
172+
req as GetImpersonationControllerRequest,
173+
res as ImpersonationRequestResponse,
174+
nextSpy
175+
);
176+
expect(res.boom.badRequest.calledOnce).to.be.true;
177+
expect(nextSpy.called).to.be.false;
178+
});
179+
});
78180
});

test/unit/models/impersonationRequests.test.ts

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import { expect } from "chai";
22
import cleanDb from "../../utils/cleanDb";
33
import * as impersonationModel from "../../../models/impersonationRequests";
44
import { impersonationRequestsBodyData } from "../../fixtures/impersonation-requests/impersonationRequests";
5-
import { REQUEST_STATE, ERROR_WHILE_CREATING_REQUEST, REQUEST_ALREADY_PENDING, IMPERSONATION_NOT_COMPLETED } from "../../../constants/requests";
5+
import { REQUEST_STATE, ERROR_WHILE_CREATING_REQUEST } from "../../../constants/requests";
66
import addUser from "../../utils/addUser";
77
import userDataFixture from "../../fixtures/user/user";
88
import sinon from "sinon";
9-
import { CreateImpersonationRequestModelDto } from "../../../types/impersonationRequest";
9+
1010

1111

1212
describe("models/impersonationRequests", () => {
1313
let impersonationRequest;
1414
let mockRequestBody = impersonationRequestsBodyData[0];
1515
let testUserId:string;
16+
let impersonationRequests=[];
1617
const userData = userDataFixture();
1718

1819
beforeEach(async () => {
@@ -77,4 +78,132 @@ describe("models/impersonationRequests", () => {
7778
}
7879
})
7980
});
81+
82+
describe("getImpersonationRequestById", () => {
83+
it("should return the impersonation request by id", async () => {
84+
const impersonationRequest = await impersonationModel.createImpersonationRequest(impersonationRequestsBodyData[0]);
85+
const request = await impersonationModel.getImpersonationRequestById(impersonationRequest.id);
86+
expect(request).to.not.be.null;
87+
expect(request.id).to.equal(impersonationRequest.id);
88+
});
89+
90+
it("should return null if the request does not exist", async () => {
91+
const request = await impersonationModel.getImpersonationRequestById("nonexistentId");
92+
expect(request).to.be.null;
93+
});
94+
});
95+
96+
describe("getImpersonationRequests", () => {
97+
beforeEach(async () => {
98+
impersonationRequests = [];
99+
impersonationRequests = await Promise.all(
100+
impersonationRequestsBodyData.slice(0, 5).map((data) =>
101+
impersonationModel.createImpersonationRequest(data)
102+
)
103+
);
104+
});
105+
106+
afterEach(async () => {
107+
sinon.restore();
108+
await cleanDb();
109+
});
110+
111+
it("should return a list of impersonation requests", async () => {
112+
const requests = await impersonationModel.getImpersonationRequests({});
113+
expect(requests).to.not.be.null;
114+
expect(requests.allRequests.length).to.be.greaterThan(0);
115+
expect(requests.allRequests.length).to.be.equal(impersonationRequests.length);
116+
});
117+
118+
it("Should return a list of all the requests with specified status - APPROVED", async () => {
119+
await cleanDb();
120+
await Promise.all(
121+
impersonationRequestsBodyData.slice(0, 5).map((data) =>
122+
impersonationModel.createImpersonationRequest({...data,status:"APPROVED"})
123+
)
124+
);
125+
const query = { status: REQUEST_STATE.APPROVED };
126+
const result = await impersonationModel.getImpersonationRequests(query);
127+
expect(result).to.not.be.null;
128+
expect(result.allRequests.every(r => r.status === REQUEST_STATE.APPROVED)).to.be.true;
129+
});
130+
131+
it("Should return a list of all the requests with specified status - PENDING", async () => {
132+
const query = { status: REQUEST_STATE.PENDING };
133+
const result = await impersonationModel.getImpersonationRequests(query);
134+
expect(result).to.not.be.null;
135+
expect(result.allRequests.every(r => r.status === REQUEST_STATE.PENDING)).to.be.true;
136+
});
137+
138+
it("Should return a list of all the requests with specified status - REJECTED", async () => {
139+
await cleanDb();
140+
await Promise.all(
141+
impersonationRequestsBodyData.slice(0, 5).map((data) =>
142+
impersonationModel.createImpersonationRequest({...data,status:"REJECTED"})
143+
)
144+
);
145+
const query = { status: REQUEST_STATE.REJECTED };
146+
const result = await impersonationModel.getImpersonationRequests(query);
147+
expect(result).to.not.be.null;
148+
expect(result.allRequests.every(r => r.status === REQUEST_STATE.REJECTED)).to.be.true;
149+
});
150+
151+
it("should filter requests by createdBy", async () => {
152+
const query = { createdBy: impersonationRequests[0].createdBy };
153+
const result = await impersonationModel.getImpersonationRequests(query);
154+
expect(result.allRequests.length).to.be.equal(1);
155+
expect(result.allRequests.every(r => r.createdBy === impersonationRequests[0].createdBy)).to.be.true;
156+
});
157+
158+
it("should return requests by size", async () => {
159+
const query = { size: 2 };
160+
const result = await impersonationModel.getImpersonationRequests(query);
161+
expect(result.allRequests.length).to.be.equal(2);
162+
});
163+
164+
it("should filter requests by createdFor", async () => {
165+
const query = { createdFor: impersonationRequests[0].createdFor };
166+
const result = await impersonationModel.getImpersonationRequests(query);
167+
expect(result.allRequests.length).to.be.equal(1);
168+
expect(result.allRequests.every(r => r.createdFor === impersonationRequests[0].createdFor)).to.be.true;
169+
});
170+
171+
172+
it("Should return null if no data is found", async () => {
173+
await cleanDb();
174+
const query = { status: REQUEST_STATE.PENDING };
175+
const impersonationRequestData = await impersonationModel.getImpersonationRequests(query);
176+
expect(impersonationRequestData).to.be.equal(null);
177+
});
178+
179+
it("should support pagination", async () => {
180+
const query = { size: 2 };
181+
const result = await impersonationModel.getImpersonationRequests(query);
182+
expect(result.allRequests.length).to.be.at.most(2);
183+
expect(result.next).to.exist;
184+
expect(result.prev).to.be.null;
185+
expect(result.count).to.be.equal(2);
186+
});
187+
188+
189+
it("should return the next doc of results using next cursor", async () => {
190+
const first = await impersonationModel.getImpersonationRequests({ size: 2 });
191+
expect(first.next).to.exist;
192+
const next = await impersonationModel.getImpersonationRequests({ size: 2, next: first.next });
193+
expect(next.allRequests.length).to.be.at.most(2);
194+
expect(next.next).to.not.equal(null);
195+
expect(next.prev).to.not.equal(null);
196+
expect(next.allRequests[0].id).to.not.equal(first.allRequests[0].id);
197+
});
198+
199+
it("should return the previous doc of results using prev cursor", async () => {
200+
const firstPage = await impersonationModel.getImpersonationRequests({ size: 2 });
201+
const nextPage = await impersonationModel.getImpersonationRequests({ size: 2, next: firstPage.next });
202+
if (nextPage.prev) {
203+
const prevPage = await impersonationModel.getImpersonationRequests({ size: 2, prev: nextPage.prev });
204+
expect(prevPage.allRequests.length).to.be.at.most(2);
205+
expect(prevPage.allRequests[0].id).to.equal(firstPage.allRequests[0].id);
206+
}
207+
});
208+
});
80209
});

0 commit comments

Comments
 (0)