@@ -361,7 +361,7 @@ public async Task AddCommentToPost_Failure(int postId, int userId, string conten
361361 public async Task GetCommentsForPost_Success ( )
362362 {
363363 // Arrange
364- int postId = 6 ;
364+ int postId = 5 ;
365365
366366 // Act
367367 var response = await _client . GetAsync ( $ "/posts/{ postId } /comments") ;
@@ -463,5 +463,101 @@ public async Task DeleteComment_SuccessAndThenNotFound()
463463 Assert . That ( deleteAgainResponse . StatusCode , Is . EqualTo ( HttpStatusCode . NotFound ) ) ;
464464 }
465465
466+
467+ [ Test ]
468+ public async Task GetPostsByUser_Success ( )
469+ {
470+ // Arrange
471+ int userId = 1 ; // CHeck if this user still has posts if this test fails (manuially)
472+
473+ // Act
474+ var response = await _client . GetAsync ( $ "/posts/user/{ userId } ") ;
475+ var contentString = await response . Content . ReadAsStringAsync ( ) ;
476+ var message = JsonNode . Parse ( contentString ) ;
477+ Console . WriteLine ( message ) ;
478+ // Assert status
479+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
480+
481+
482+ Assert . That ( message ? [ "message" ] ? . GetValue < string > ( ) , Is . EqualTo ( "Success" ) ) ;
483+ var data = message ? [ "data" ] ? . AsArray ( ) ;
484+ Assert . That ( data , Is . Not . Null ) ;
485+ Assert . That ( data ! . Count , Is . GreaterThan ( 0 ) , "Expected user 1 to have at least one post." ) ;
486+
487+ // Assert that ALL posts in the response belong to the correct user
488+ foreach ( var post in data ! )
489+ {
490+ Assert . That ( post ? [ "user" ] ? [ "id" ] ? . GetValue < int > ( ) , Is . EqualTo ( userId ) ) ;
491+ }
492+
493+ var firstPost = data . First ( ) ;
494+ Assert . That ( firstPost ? [ "id" ] ? . GetValue < int > ( ) , Is . GreaterThan ( 0 ) ) ;
495+ Assert . That ( firstPost ? [ "content" ] ? . GetValue < string > ( ) , Is . Not . Null ) ;
496+ }
497+
498+ [ Test ]
499+ public async Task GetPostsByUser_NotFound ( )
500+ {
501+ // Arrange
502+ int nonExistentUserId = 99999 ;
503+
504+ // Act
505+ var response = await _client . GetAsync ( $ "/posts/user/{ nonExistentUserId } ") ;
506+ var contentString = await response . Content . ReadAsStringAsync ( ) ;
507+ var message = JsonNode . Parse ( contentString ) ;
508+
509+ // Assert
510+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . NotFound ) ) ;
511+ Assert . That ( message ? [ "message" ] ? . GetValue < string > ( ) , Is . EqualTo ( "No posts found for this user" ) ) ;
512+ Assert . That ( message ? [ "data" ] , Is . Null ) ;
513+ }
514+
515+ [ Test ]
516+ public async Task GetCommentsByUser_Success ( )
517+ {
518+ // Arrange
519+ int userId = 1 ; // Check if this user still has comments if this fails
520+
521+ // Act
522+ var response = await _client . GetAsync ( $ "/comments/user/{ userId } ") ;
523+ var contentString = await response . Content . ReadAsStringAsync ( ) ;
524+ var message = JsonNode . Parse ( contentString ) ;
525+ Console . WriteLine ( message ) ;
526+ // Assert status
527+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . OK ) ) ;
528+
529+ Assert . That ( message ? [ "message" ] ? . GetValue < string > ( ) , Is . EqualTo ( "Success" ) ) ;
530+ var data = message ? [ "data" ] ? . AsArray ( ) ;
531+ Assert . That ( data , Is . Not . Null ) ;
532+ Assert . That ( data ! . Count , Is . GreaterThan ( 0 ) , "Expected user 1 to have at least one comment." ) ;
533+
534+ // Assert that ALL comments in the response belong to the correct user
535+ foreach ( var comment in data ! )
536+ {
537+ Assert . That ( comment ? [ "user" ] ? [ "id" ] ? . GetValue < int > ( ) , Is . EqualTo ( userId ) ) ;
538+ }
539+
540+ var firstComment = data . First ( ) ;
541+ Assert . That ( firstComment ? [ "id" ] ? . GetValue < int > ( ) , Is . GreaterThan ( 0 ) ) ;
542+ Assert . That ( firstComment ? [ "content" ] ? . GetValue < string > ( ) , Is . Not . Null . Or . Empty ) ;
543+ }
544+
545+ [ Test ]
546+ public async Task GetCommentsByUser_NotFound ( )
547+ {
548+ // Arrange
549+ int nonExistentUserId = 99999 ;
550+
551+ // Act
552+ var response = await _client . GetAsync ( $ "/comments/user/{ nonExistentUserId } ") ;
553+ var contentString = await response . Content . ReadAsStringAsync ( ) ;
554+ var message = JsonNode . Parse ( contentString ) ;
555+
556+ // Assert
557+ Assert . That ( response . StatusCode , Is . EqualTo ( HttpStatusCode . NotFound ) ) ;
558+ Assert . That ( message ? [ "message" ] ? . GetValue < string > ( ) , Is . EqualTo ( "No comments found for this user" ) ) ;
559+ Assert . That ( message ? [ "data" ] , Is . Null ) ;
560+ }
561+
466562 }
467563}
0 commit comments