@@ -27,6 +27,9 @@ import userDataFixture from "../../fixtures/user/user";
27
27
import * as logService from "../../../services/logService" ;
28
28
import { testAcknowledgeOooRequest , createOooRequests3 } from "../../fixtures/oooRequest/oooRequest" ;
29
29
import { createRequest } from "../../../models/requests" ;
30
+ import * as requestModel from "../../../models/requests" ;
31
+ import * as oooRequestService from "../../../services/oooRequest" ;
32
+ import { NotFound , Conflict , BadRequest } from "http-errors" ;
30
33
31
34
describe ( "Test OOO Request Service" , function ( ) {
32
35
@@ -125,38 +128,37 @@ describe("Test OOO Request Service", function() {
125
128
} ) ;
126
129
127
130
it ( "should return INVALID_REQUEST_TYPE if request type is not OOO" , async function ( ) {
128
- const validationResponse = await validateOooAcknowledgeRequest (
129
- testOooRequest . id ,
131
+ await validateOooAcknowledgeRequest (
130
132
REQUEST_TYPE . ONBOARDING ,
131
133
testOooRequest . status
132
- ) ;
133
- expect ( validationResponse . error ) . to . be . not . undefined ;
134
- expect ( validationResponse . error ) . to . equal ( INVALID_REQUEST_TYPE ) ;
134
+ ) . catch ( ( error ) => {
135
+ expect ( error ) . to . be . not . undefined ;
136
+ expect ( error . message ) . to . equal ( INVALID_REQUEST_TYPE ) ;
137
+ } ) ;
135
138
} ) ;
136
139
137
140
it ( "should return REQUEST_ALREADY_APPROVED if request is already approved" , async function ( ) {
138
- const validationResponse = await validateOooAcknowledgeRequest (
139
- testOooRequest . id ,
141
+ await validateOooAcknowledgeRequest (
140
142
testOooRequest . type ,
141
143
REQUEST_STATE . APPROVED
142
- ) ;
143
- expect ( validationResponse . error ) . to . be . not . undefined ;
144
- expect ( validationResponse . error ) . to . equal ( REQUEST_ALREADY_APPROVED ) ;
144
+ ) . catch ( ( error ) => {
145
+ expect ( error ) . to . be . not . undefined ;
146
+ expect ( error . message ) . to . equal ( REQUEST_ALREADY_APPROVED ) ;
147
+ } ) ;
145
148
} ) ;
146
149
147
150
it ( "should return REQUEST_ALREADY_REJECTED if request is already rejected" , async function ( ) {
148
- const validationResponse = await validateOooAcknowledgeRequest (
149
- testOooRequest . id ,
151
+ await validateOooAcknowledgeRequest (
150
152
testOooRequest . type ,
151
153
REQUEST_STATE . REJECTED
152
- ) ;
153
- expect ( validationResponse . error ) . to . be . not . undefined ;
154
- expect ( validationResponse . error ) . to . equal ( REQUEST_ALREADY_REJECTED ) ;
154
+ ) . catch ( ( error ) => {
155
+ expect ( error ) . to . be . not . undefined ;
156
+ expect ( error . message ) . to . equal ( REQUEST_ALREADY_REJECTED ) ;
157
+ } ) ;
155
158
} ) ;
156
159
157
160
it ( "should return undefined when all validation checks passes" , async function ( ) {
158
161
const response = await validateOooAcknowledgeRequest (
159
- testOooRequest . id ,
160
162
testOooRequest . type ,
161
163
testOooRequest . status
162
164
) ;
@@ -183,54 +185,72 @@ describe("Test OOO Request Service", function() {
183
185
} ) ;
184
186
185
187
it ( "should return REQUEST_DOES_NOT_EXIST if invalid request id is passed" , async function ( ) {
186
- const invalidOOORequestId = "11111111111111111111" ;
187
- const response = await acknowledgeOooRequest (
188
- invalidOOORequestId ,
188
+ sinon . stub ( requestModel , "getRequestById" ) . throws ( new NotFound ( REQUEST_DOES_NOT_EXIST ) ) ;
189
+ await acknowledgeOooRequest (
190
+ "11111111111111111111" ,
189
191
testAcknowledgeOooRequest ,
190
192
testSuperUserId
191
- ) ;
192
- expect ( response . error ) . to . equal ( REQUEST_DOES_NOT_EXIST ) ;
193
+ ) . catch ( ( error ) => {
194
+ expect ( error ) . to . be . not . undefined ;
195
+ expect ( error . message ) . to . equal ( REQUEST_DOES_NOT_EXIST ) ;
196
+ } ) ;
197
+ } ) ;
198
+
199
+ it ( "should return REQUEST_ALREADY_APPROVED when status is approved" , async function ( ) {
200
+ sinon . stub ( requestModel , "getRequestById" ) . returns ( testOooRequest ) ;
201
+ sinon . stub ( oooRequestService , "validateOooAcknowledgeRequest" ) . throws ( new Conflict ( REQUEST_ALREADY_APPROVED ) ) ;
202
+ await acknowledgeOooRequest (
203
+ testOooRequest . id ,
204
+ testAcknowledgeOooRequest ,
205
+ testSuperUserId
206
+ ) . catch ( ( error ) => {
207
+ expect ( error ) . to . be . not . undefined ;
208
+ expect ( error . message ) . to . equal ( REQUEST_ALREADY_APPROVED ) ;
209
+ } ) ;
210
+ } ) ;
211
+
212
+ it ( "should throw error when approve or rejection fails" , async function ( ) {
213
+ sinon . stub ( requestModel , "getRequestById" ) . returns ( testOooRequest ) ;
214
+ sinon . stub ( oooRequestService , "validateOooAcknowledgeRequest" ) ;
215
+ sinon . stub ( requestModel , "updateRequest" ) . throws ( new BadRequest ( errorMessage ) ) ;
216
+ await acknowledgeOooRequest (
217
+ testOooRequest . id ,
218
+ testAcknowledgeOooRequest ,
219
+ testSuperUserId
220
+ ) . catch ( ( error ) => {
221
+ expect ( error ) . to . be . not . undefined ;
222
+ expect ( error . message ) . to . equal ( errorMessage ) ;
223
+ } ) ;
193
224
} ) ;
194
225
195
226
it ( "should approve OOO request" , async function ( ) {
227
+ sinon . stub ( requestModel , "getRequestById" ) . returns ( testOooRequest ) ;
228
+ sinon . stub ( oooRequestService , "validateOooAcknowledgeRequest" ) ;
229
+ sinon . stub ( requestModel , "updateRequest" ) . returns ( { ...testOooRequest , status : REQUEST_STATE . APPROVED } ) ;
196
230
const response = await acknowledgeOooRequest (
197
231
testOooRequest . id ,
198
232
testAcknowledgeOooRequest ,
199
233
testSuperUserId
200
234
) ;
201
- expect ( response ) . to . deep . include ( {
202
- message : REQUEST_APPROVED_SUCCESSFULLY ,
203
- data : {
204
- ...testAcknowledgeOooRequest ,
205
- id : testOooRequest . id ,
206
- lastModifiedBy : testSuperUserId ,
207
- updatedAt : response . data . updatedAt
208
- }
209
- } ) ;
235
+ expect ( response . message ) . to . equal ( REQUEST_APPROVED_SUCCESSFULLY ) ;
236
+ expect ( response . data . status ) . to . equal ( REQUEST_STATE . APPROVED ) ;
210
237
} ) ;
211
238
212
239
it ( "should reject OOO request" , async function ( ) {
240
+ sinon . stub ( requestModel , "getRequestById" ) . returns ( testOooRequest ) ;
241
+ sinon . stub ( oooRequestService , "validateOooAcknowledgeRequest" ) ;
242
+ sinon . stub ( requestModel , "updateRequest" ) . returns ( { ...testOooRequest , status : REQUEST_STATE . REJECTED } ) ;
213
243
const response = await acknowledgeOooRequest (
214
244
testOooRequest . id ,
215
245
{ ...testAcknowledgeOooRequest , status : REQUEST_STATE . REJECTED } ,
216
246
testSuperUserId
217
247
) ;
218
- expect ( response ) . to . deep . include ( {
219
- message : REQUEST_REJECTED_SUCCESSFULLY ,
220
- data : {
221
- ...testAcknowledgeOooRequest ,
222
- id : testOooRequest . id ,
223
- status : REQUEST_STATE . REJECTED ,
224
- lastModifiedBy : testSuperUserId ,
225
- updatedAt : response . data . updatedAt
226
- }
227
- } ) ;
248
+ expect ( response . message ) . to . equal ( REQUEST_REJECTED_SUCCESSFULLY ) ;
249
+ expect ( response . data . status ) . to . equal ( REQUEST_STATE . REJECTED ) ;
228
250
} ) ;
229
251
230
252
it ( "should throw error" , async function ( ) {
231
- sinon . stub ( logService , "addLog" ) . throws ( new Error ( errorMessage ) ) ;
232
- const createSpy = sinon . spy ( require ( "../../../services/oooRequest" ) , "acknowledgeOooRequest" ) ;
233
-
253
+ sinon . stub ( requestModel , "getRequestById" ) . throws ( new Error ( errorMessage ) ) ;
234
254
try {
235
255
await acknowledgeOooRequest (
236
256
testOooRequest . id ,
@@ -239,7 +259,6 @@ describe("Test OOO Request Service", function() {
239
259
) ;
240
260
} catch ( error ) {
241
261
expect ( error . message ) . to . equal ( errorMessage ) ;
242
- expect ( createSpy . calledOnce ) . to . be . true ;
243
262
}
244
263
} ) ;
245
264
} ) ;
0 commit comments