11using BugLab . Business . Queries . Bugs ;
22using BugLab . Data . Entities ;
3+ using BugLab . Shared . Responses ;
34using BugLab . Tests . Helpers ;
45using FluentAssertions ;
56using Microsoft . EntityFrameworkCore ;
@@ -14,17 +15,51 @@ public class GetBugsHandlerTests
1415 {
1516 private GetBugsHandler _sut ;
1617
18+ private bool AssignedToOrCreatedBy ( BugResponse bug , string userId )
19+ {
20+ if ( bug . CreatedBy . Id == userId ) return true ;
21+
22+ var assignedToId = bug . AssignedTo ? . Id ;
23+ return assignedToId != null && assignedToId == userId ;
24+ }
25+
1726 [ Fact ]
1827 public async Task GetBugs_ShouldNot_ReturnDeletedBugs ( )
1928 {
2029 using var context = await DbContextHelpers . CreateAsync ( ) ;
2130 _sut = new ( context ) ;
2231
23- var deletedBug = context . Bugs . IgnoreQueryFilters ( ) . FirstOrDefault ( x => x . Deleted != null ) ;
32+ var deletedBug = context . Bugs . IgnoreQueryFilters ( ) . FirstOrDefault ( x => x . Deleted . HasValue ) ;
2433 var bugs = await _sut . Handle ( new GetBugsQuery ( DbContextHelpers . CurrentUserId ) , default ) ;
2534
2635 bugs . Should ( ) . NotBeNullOrEmpty ( ) ;
2736 bugs . Should ( ) . NotContain ( x => x . Title == deletedBug . Title ) ;
2837 }
38+
39+ [ Fact ]
40+ public async Task GetBugs_WithUserId_ReturnsUsersBugs ( )
41+ {
42+ using var context = await DbContextHelpers . CreateAsync ( ) ;
43+ _sut = new ( context ) ;
44+
45+ var bugs = await _sut . Handle ( new GetBugsQuery ( DbContextHelpers . CurrentUserId ) , default ) ;
46+
47+ bugs . Should ( ) . NotBeNullOrEmpty ( ) ;
48+ bugs . Should ( ) . Match ( b => b . All ( b => AssignedToOrCreatedBy ( b , DbContextHelpers . CurrentUserId ) ) ) ;
49+ }
50+
51+ [ Fact ]
52+ public async Task GetBugs_WithProjectId_ReturnsBugsInProject ( )
53+ {
54+ using var context = await DbContextHelpers . CreateAsync ( ) ;
55+ _sut = new ( context ) ;
56+
57+ var request = new GetBugsQuery ( DbContextHelpers . CurrentUserId ) ;
58+ request . ProjectId = context . ProjectUsers . AsNoTracking ( ) . First ( x => x . UserId == DbContextHelpers . CurrentUserId ) . ProjectId ;
59+ var bugs = await _sut . Handle ( request , default ) ;
60+
61+ bugs . Should ( ) . NotBeNullOrEmpty ( ) ;
62+ bugs . Should ( ) . Match ( b => b . All ( b => b . ProjectId == request . ProjectId ) ) ;
63+ }
2964 }
3065}
0 commit comments