@@ -195,4 +195,294 @@ describe('Admin', () => {
195
195
} ) ;
196
196
} ) ;
197
197
} ) ;
198
+
199
+ /**
200
+ * Testing admin delete one comment in post endpoint
201
+ */
202
+ describe ( 'DELETE /api/v1/admin/feed/posts/comment' , ( ) => {
203
+ describe ( 'given the user is not logged in' , ( ) => {
204
+ it ( 'should return a 401 status with a json message - Auth Failed' , async ( ) => {
205
+ request ( app )
206
+ . delete ( '/api/v1/admin/feed/posts/comment' )
207
+ . expect ( 401 )
208
+ . then ( ( response ) =>
209
+ expect ( response . body ) . toMatchObject ( {
210
+ data : null ,
211
+ success : false ,
212
+ error : true ,
213
+ message : expect . any ( String ) ,
214
+ status : 401 ,
215
+ stack : expect . any ( String ) ,
216
+ } )
217
+ ) ;
218
+ } ) ;
219
+ } ) ;
220
+
221
+ describe ( 'given any of the flowing filed is missing (postId,commentId)' , ( ) => {
222
+ it ( 'should return a 422 status with validation message' , async ( ) => {
223
+ const newUser = new User ( {
224
+ ...userPayload ,
225
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
226
+ role : authorizationRoles . admin ,
227
+ } ) ;
228
+ await newUser . save ( ) ;
229
+
230
+ const authResponse = await request ( app )
231
+ . post ( '/api/v1/auth/login' )
232
+ . send ( {
233
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
234
+ password : userPayload . password ,
235
+ } ) ;
236
+
237
+ const token = ( authResponse && authResponse ?. body ?. data ?. accessToken ) || '' ;
238
+
239
+ if ( token ) {
240
+ // postId is missing
241
+ await request ( app )
242
+ . delete ( '/api/v1/admin/feed/posts/comment' )
243
+ . set ( 'Authorization' , `Bearer ${ token } ` )
244
+ . send ( { } )
245
+ . then ( ( response ) => {
246
+ expect ( response . body ) . toMatchObject ( {
247
+ data : null ,
248
+ error : true ,
249
+ status : 422 ,
250
+ message : expect . any ( String ) ,
251
+ stack : expect . any ( String ) ,
252
+ } ) ;
253
+ expect ( response ?. body ?. message ) . toMatch ( / p o s t I d / ) ;
254
+ } )
255
+ . catch ( ( error ) => {
256
+ console . log ( error ) ;
257
+ } ) ;
258
+
259
+ // commentId is missing
260
+ await request ( app )
261
+ . delete ( '/api/v1/admin/feed/posts/comment' )
262
+ . set ( 'Authorization' , `Bearer ${ token } ` )
263
+ . send ( { comment : 'comment' , postId : validMongooseObjectId } )
264
+ . then ( ( response ) => {
265
+ expect ( response . body ) . toMatchObject ( {
266
+ data : null ,
267
+ error : true ,
268
+ status : 422 ,
269
+ message : expect . any ( String ) ,
270
+ stack : expect . any ( String ) ,
271
+ } ) ;
272
+ expect ( response ?. body ?. message ) . toMatch ( / c o m m e n t I d / ) ;
273
+ } )
274
+ . catch ( ( error ) => {
275
+ console . log ( error ) ;
276
+ } ) ;
277
+ }
278
+ } ) ;
279
+ } ) ;
280
+
281
+ describe ( 'given post id or comment id is not valid ' , ( ) => {
282
+ it ( 'should return a 422 status with validation message' , async ( ) => {
283
+ const newUser = new User ( {
284
+ ...userPayload ,
285
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
286
+ role : authorizationRoles . admin ,
287
+ } ) ;
288
+ await newUser . save ( ) ;
289
+
290
+ const authResponse = await request ( app )
291
+ . post ( '/api/v1/auth/login' )
292
+ . send ( {
293
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
294
+ password : userPayload . password ,
295
+ } ) ;
296
+
297
+ const token = ( authResponse && authResponse ?. body ?. data ?. accessToken ) || '' ;
298
+
299
+ if ( token ) {
300
+ // postId not vaild
301
+ await request ( app )
302
+ . delete ( '/api/v1/admin/feed/posts/comment' )
303
+ . set ( 'Authorization' , `Bearer ${ token } ` )
304
+ . send ( { postId : 'notvaild' } )
305
+ . then ( ( response ) => {
306
+ expect ( response . body ) . toMatchObject ( {
307
+ data : null ,
308
+ error : true ,
309
+ status : 422 ,
310
+ message : expect . any ( String ) ,
311
+ stack : expect . any ( String ) ,
312
+ } ) ;
313
+ expect ( response ?. body ?. message ) . toMatch ( / f a i l s t o m a t c h t h e v a l i d m o n g o i d p a t t e r n / ) ;
314
+ } )
315
+ . catch ( ( error ) => {
316
+ console . log ( error ) ;
317
+ } ) ;
318
+
319
+ // commentId not vaild
320
+ await request ( app )
321
+ . delete ( '/api/v1/admin/feed/posts/comment' )
322
+ . set ( 'Authorization' , `Bearer ${ token } ` )
323
+ . send ( { postId : validMongooseObjectId , commentId : 'notcorrectid' } )
324
+ . then ( ( response ) => {
325
+ expect ( response . body ) . toMatchObject ( {
326
+ data : null ,
327
+ error : true ,
328
+ status : 422 ,
329
+ message : expect . any ( String ) ,
330
+ stack : expect . any ( String ) ,
331
+ } ) ;
332
+ expect ( response ?. body ?. message ) . toMatch ( / f a i l s t o m a t c h t h e v a l i d m o n g o i d p a t t e r n / ) ;
333
+ } )
334
+ . catch ( ( error ) => {
335
+ console . log ( error ) ;
336
+ } ) ;
337
+ }
338
+ } ) ;
339
+ } ) ;
340
+
341
+ describe ( 'given the post does not exist' , ( ) => {
342
+ it ( 'should return a 400 status with a json message - bad request' , async ( ) => {
343
+ const newUser = new User ( {
344
+ ...userPayload ,
345
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
346
+ role : authorizationRoles . admin ,
347
+ } ) ;
348
+ await newUser . save ( ) ;
349
+
350
+ const authResponse = await request ( app )
351
+ . post ( '/api/v1/auth/login' )
352
+ . send ( {
353
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
354
+ password : userPayload . password ,
355
+ } ) ;
356
+
357
+ const token = ( authResponse && authResponse ?. body ?. data ?. accessToken ) || '' ;
358
+
359
+ if ( token ) {
360
+ await request ( app )
361
+ . delete ( '/api/v1/admin/feed/posts/comment' )
362
+ . set ( 'Authorization' , `Bearer ${ token } ` )
363
+ . send ( { postId : validMongooseObjectId , commentId : validMongooseObjectId } )
364
+ . then ( ( response ) => {
365
+ expect ( response . body ) . toMatchObject ( {
366
+ data : null ,
367
+ success : false ,
368
+ error : true ,
369
+ message : expect . any ( String ) ,
370
+ status : 400 ,
371
+ stack : expect . any ( String ) ,
372
+ } ) ;
373
+ } )
374
+ . catch ( ( error ) => {
375
+ console . log ( error ) ;
376
+ } ) ;
377
+ }
378
+ } ) ;
379
+ } ) ;
380
+
381
+ describe ( 'given the user is logged in and authorized and the given post does exist in DB but not the comment does not exist' , ( ) => {
382
+ it ( 'should return a 403 status with a json message - Auth Failed' , async ( ) => {
383
+ const user = new User ( {
384
+ ...userPayload ,
385
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
386
+ role : authorizationRoles . admin ,
387
+ } ) ;
388
+
389
+ await user . save ( ) ;
390
+
391
+ const post = new Post ( { ...postPayload , author : user . _id } ) ;
392
+ await post . save ( ) ;
393
+
394
+ const authResponse = await request ( app )
395
+ . post ( '/api/v1/auth/login' )
396
+ . send ( {
397
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
398
+ password : userPayload . password ,
399
+ } ) ;
400
+
401
+ const token = ( authResponse && authResponse ?. body ?. data ?. accessToken ) || '' ;
402
+
403
+ if ( token ) {
404
+ await request ( app )
405
+ . delete ( '/api/v1/admin/feed/posts/comment' )
406
+ . set ( 'Authorization' , `Bearer ${ token } ` )
407
+ . send ( { postId : post ?. _id , commentId : validMongooseObjectId } )
408
+ . expect ( 'Content-Type' , / j s o n / )
409
+ . then ( ( response ) => {
410
+ expect ( response . body ) . toMatchObject ( {
411
+ data : null ,
412
+ success : false ,
413
+ error : true ,
414
+ message : expect . any ( String ) ,
415
+ status : 403 ,
416
+ stack : expect . any ( String ) ,
417
+ } ) ;
418
+ } )
419
+ . catch ( ( error ) => {
420
+ console . log ( error ) ;
421
+ } ) ;
422
+ }
423
+ } ) ;
424
+ } ) ;
425
+
426
+ describe ( 'given the user is logged in and authorized and the post and comment does exist' , ( ) => {
427
+ it ( 'should delete the comment and return a 200 status with the updated post' , async ( ) => {
428
+ const user = new User ( {
429
+ ...userPayload ,
430
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
431
+ role : authorizationRoles . admin ,
432
+ } ) ;
433
+
434
+ await user . save ( ) ;
435
+
436
+ const comment = 'comment' ;
437
+
438
+ const post = new Post ( {
439
+ ...postPayload ,
440
+ author : user . _id ,
441
+ comments : [
442
+ {
443
+ user : user ?. _id ,
444
+ comment,
445
+ } ,
446
+ {
447
+ user : user ?. _id ,
448
+ comment,
449
+ } ,
450
+ ] ,
451
+ } ) ;
452
+
453
+ await post . save ( ) ;
454
+
455
+ const authResponse = await request ( app )
456
+ . post ( '/api/v1/auth/login' )
457
+ . send ( {
458
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
459
+ password : userPayload . password ,
460
+ } ) ;
461
+
462
+ const token = ( authResponse && authResponse ?. body ?. data ?. accessToken ) || '' ;
463
+
464
+ if ( post && token ) {
465
+ await request ( app )
466
+ . delete ( '/api/v1/admin/feed/posts/comment' )
467
+ . set ( 'Authorization' , `Bearer ${ token } ` )
468
+ . send ( { postId : post ?. _id , commentId : post ?. comments [ 0 ] ?. _id } )
469
+ . expect ( 'Content-Type' , / j s o n / )
470
+ . then ( ( response ) => {
471
+ expect ( response . body ) . toMatchObject ( {
472
+ success : true ,
473
+ error : false ,
474
+ message : expect . any ( String ) ,
475
+ status : 200 ,
476
+ } ) ;
477
+
478
+ expect ( response ?. body ?. message ) . toMatch ( 'Successfully delete comment' ) ;
479
+ expect ( response ?. body ?. data ?. post ?. comments ?. length ) . toBe ( 1 ) ;
480
+ } )
481
+ . catch ( ( error ) => {
482
+ console . log ( error ) ;
483
+ } ) ;
484
+ }
485
+ } ) ;
486
+ } ) ;
487
+ } ) ;
198
488
} ) ;
0 commit comments