@@ -2,17 +2,18 @@ import { expect } from "chai";
22import cleanDb from "../../utils/cleanDb" ;
33import * as impersonationModel from "../../../models/impersonationRequests" ;
44import { 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" ;
66import addUser from "../../utils/addUser" ;
77import userDataFixture from "../../fixtures/user/user" ;
88import sinon from "sinon" ;
9- import { CreateImpersonationRequestModelDto } from "../../../types/impersonationRequest" ;
9+
1010
1111
1212describe ( "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