@@ -14,6 +14,10 @@ beforeAll((done) => {
14
14
if ( err ) return console . log ( 'Failed to connect to DB' , err ) ;
15
15
done ( ) ;
16
16
} ) ;
17
+
18
+ jest . mock ( '@src/utils/sendEmail' , ( ) => ( {
19
+ sendEmailVerificationEmail : jest . fn ( ) . mockResolvedValue ( 'Sending Email Success' ) ,
20
+ } ) ) ;
17
21
} ) ;
18
22
19
23
afterAll ( async ( ) => {
@@ -34,7 +38,7 @@ afterEach(async () => {
34
38
35
39
describe ( 'Post' , ( ) => {
36
40
/**
37
- * Testing get all post endpoint
41
+ * Testing get all posts endpoint
38
42
*/
39
43
describe ( 'GET /api/v1/feed/posts' , ( ) => {
40
44
describe ( 'given no post in db' , ( ) => {
@@ -88,7 +92,7 @@ describe('Post', () => {
88
92
} ) ;
89
93
90
94
/**
91
- * Testing get timeline post endpoint
95
+ * Testing get timeline posts endpoint
92
96
*/
93
97
describe ( 'GET /api/v1/feed/posts/timeline' , ( ) => {
94
98
describe ( 'given the user is not logged in' , ( ) => {
@@ -211,6 +215,217 @@ describe('Post', () => {
211
215
} ) ;
212
216
} ) ;
213
217
218
+ /**
219
+ * Testing get user posts endpoint
220
+ */
221
+ describe ( 'GET /api/v1/feed/posts/user-posts' , ( ) => {
222
+ describe ( 'given the user is not logged in' , ( ) => {
223
+ it ( 'should return a 401 status with a json message - Auth Failed' , async ( ) => {
224
+ const response = await request ( app ) . get ( '/api/v1/feed/posts/user-posts' ) ;
225
+
226
+ expect ( response . body ) . toMatchObject ( {
227
+ data : null ,
228
+ success : false ,
229
+ error : true ,
230
+ message : expect . any ( String ) ,
231
+ status : 401 ,
232
+ stack : expect . any ( String ) ,
233
+ } ) ;
234
+ } ) ;
235
+ } ) ;
236
+
237
+ describe ( 'given no post in db' , ( ) => {
238
+ it ( 'should return a 200 status with a json contain empty array' , async ( ) => {
239
+ try {
240
+ const authUser = new User ( {
241
+ ...userPayload ,
242
+ } ) ;
243
+
244
+ await authUser . save ( ) ;
245
+
246
+ const authResponse = await request ( app ) . post ( '/api/v1/auth/login' ) . send ( {
247
+ email : userPayload . email ,
248
+ password : userPayload . password ,
249
+ } ) ;
250
+
251
+ const token = ( authResponse && authResponse ?. body ?. data ?. accessToken ) || '' ;
252
+
253
+ if ( token ) {
254
+ const response = await request ( app )
255
+ . get ( '/api/v1/feed/posts/user-posts' )
256
+ . set ( 'Authorization' , `Bearer ${ token } ` )
257
+ . send ( { postId : validMongooseObjectId , commentId : validMongooseObjectId } ) ;
258
+
259
+ expect ( response ?. body ?. data ?. posts ?. length ) . toBe ( 0 ) ;
260
+ expect ( response . body ) . toMatchObject ( {
261
+ success : true ,
262
+ error : false ,
263
+ message : expect . any ( String ) ,
264
+ status : 200 ,
265
+ } ) ;
266
+ expect ( response ?. body ?. message ) . toMatch ( 'No post found for user' ) ;
267
+ }
268
+ } catch ( error ) {
269
+ console . log ( error ) ;
270
+ }
271
+ } ) ;
272
+ } ) ;
273
+
274
+ describe ( 'given the user is logged in and has 3 post in DB' , ( ) => {
275
+ it ( 'should return a 200 status with a json contain array of 3 posts' , async ( ) => {
276
+ try {
277
+ const authUser = new User ( {
278
+ ...userPayload ,
279
+ } ) ;
280
+
281
+ const testUser = new User ( {
282
+ ...userPayload ,
283
+ email : 'test@gmail.com' ,
284
+ } ) ;
285
+
286
+ const authPost = { ...postPayload , author : authUser . _id } ;
287
+ const testUserPost = { ...postPayload , author : testUser . _id } ;
288
+
289
+ await Post . insertMany ( [ authPost , authPost , testUserPost , authPost ] ) ;
290
+
291
+ await authUser . save ( ) ;
292
+
293
+ const authResponse = await request ( app ) . post ( '/api/v1/auth/login' ) . send ( {
294
+ email : userPayload . email ,
295
+ password : userPayload . password ,
296
+ } ) ;
297
+
298
+ const token = ( authResponse && authResponse ?. body ?. data ?. accessToken ) || '' ;
299
+
300
+ if ( token ) {
301
+ const response = await request ( app )
302
+ . get ( '/api/v1/feed/posts/user-posts' )
303
+ . set ( 'Authorization' , `Bearer ${ token } ` ) ;
304
+
305
+ expect ( response ?. body ?. data ?. posts ?. length ) . toBe ( 3 ) ;
306
+ expect ( response . body ) . toMatchObject ( {
307
+ success : true ,
308
+ error : false ,
309
+ message : expect . any ( String ) ,
310
+ status : 200 ,
311
+ } ) ;
312
+ expect ( response ?. body ?. message ) . toMatch ( 'found all posts for user' ) ;
313
+ }
314
+ } catch ( error ) {
315
+ console . log ( error ) ;
316
+ }
317
+ } ) ;
318
+ } ) ;
319
+
320
+ describe ( 'given added 3 posts in db' , ( ) => {
321
+ it ( 'should return a 200 status with a json contain array of 3 posts' , async ( ) => {
322
+ const user = new User ( userPayload ) ;
323
+ await user . save ( ) ;
324
+ const post = { ...postPayload , author : user . _id } ;
325
+ await Post . insertMany ( [ post , post , post ] ) ;
326
+
327
+ await request ( app )
328
+ . get ( '/api/v1/feed/posts' )
329
+ . expect ( 'Content-Type' , / j s o n / )
330
+ . then ( ( response ) => {
331
+ expect ( response . body ) . toMatchObject ( {
332
+ success : true ,
333
+ error : false ,
334
+ message : 'Successful Found posts' ,
335
+ status : 200 ,
336
+ data : {
337
+ posts : expect . any ( Array ) ,
338
+ totalDocs : expect . any ( Number ) ,
339
+ } ,
340
+ } ) ;
341
+ } )
342
+ . catch ( ( error ) => {
343
+ console . log ( error ) ;
344
+ } ) ;
345
+ } ) ;
346
+ } ) ;
347
+ } ) ;
348
+
349
+ /**
350
+ * Testing get single post endpoint
351
+ */
352
+ describe ( 'GET /api/v1/feed/posts/:postId' , ( ) => {
353
+ describe ( 'given post id is not valid ' , ( ) => {
354
+ it ( 'should return a 422 status with validation message' , async ( ) => {
355
+ // postId not vaild
356
+ try {
357
+ const response = await request ( app ) . get ( `/api/v1/feed/posts/notvaild` ) ;
358
+ expect ( response . body ) . toMatchObject ( {
359
+ data : null ,
360
+ error : true ,
361
+ status : 422 ,
362
+ message : expect . any ( String ) ,
363
+ stack : expect . any ( String ) ,
364
+ } ) ;
365
+ 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 / ) ;
366
+ } catch ( error ) {
367
+ console . log ( error ) ;
368
+ }
369
+ } ) ;
370
+ } ) ;
371
+
372
+ describe ( 'given the post does not exist' , ( ) => {
373
+ it ( 'should return a 400 status' , async ( ) => {
374
+ try {
375
+ const response = await request ( app )
376
+ . get ( `/api/v1/feed/posts/${ validMongooseObjectId } ` )
377
+ . set ( 'Accept' , 'application/json' )
378
+ . expect ( 'Content-Type' , / j s o n / ) ;
379
+ expect ( response . body ) . toMatchObject ( {
380
+ data : null ,
381
+ success : false ,
382
+ error : true ,
383
+ message : 'Bad Request' ,
384
+ status : 400 ,
385
+ stack : expect . any ( String ) ,
386
+ } ) ;
387
+ } catch ( error ) {
388
+ console . log ( error ) ;
389
+ }
390
+ } ) ;
391
+ } ) ;
392
+
393
+ describe ( 'given the post does exist' , ( ) => {
394
+ it ( 'should return a 200 status and the product' , async ( ) => {
395
+ try {
396
+ const user = new User ( {
397
+ ...userPayload ,
398
+ email : ( adminEmails && adminEmails [ 0 ] ) || userPayload . email ,
399
+ role : authorizationRoles . admin ,
400
+ } ) ;
401
+
402
+ await user . save ( ) ;
403
+
404
+ const post = new Post ( {
405
+ ...postPayload ,
406
+ author : user . _id ,
407
+ } ) ;
408
+ await post . save ( ) ;
409
+ const response = await request ( app )
410
+ . get ( `/api/v1/feed/posts/${ post ?. _id } ` )
411
+ . set ( 'Accept' , 'application/json' )
412
+ . expect ( 'Content-Type' , / j s o n / ) ;
413
+
414
+ expect ( response ?. body ?. data ?. post ) . toMatchObject ( postPayload ) ;
415
+
416
+ expect ( response . body ) . toMatchObject ( {
417
+ success : true ,
418
+ error : false ,
419
+ message : expect . any ( String ) ,
420
+ status : 200 ,
421
+ } ) ;
422
+ } catch ( error ) {
423
+ console . log ( error ) ;
424
+ }
425
+ } ) ;
426
+ } ) ;
427
+ } ) ;
428
+
214
429
/**
215
430
* Testing like/un-like post endpoint
216
431
*/
0 commit comments