-
Notifications
You must be signed in to change notification settings - Fork 4
Add email and unsubscribe filters to admin users search endpoint #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6b7c308
Initial plan for issue
Copilot ac86658
Add email and unsubscribe filters to admin users endpoint
Copilot 19dc5c4
Add defensive null check and additional test coverage
Copilot 832b8d6
Fix test compilation and style issues in AdminUsersControllerTests
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/Web.Api.Tests/Features/Users/AdminUsersControllerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Domain.Enums; | ||
| using TestUtils.Auth; | ||
| using TestUtils.Db; | ||
| using TestUtils.Fakes; | ||
| using Web.Api.Features.Users; | ||
| using Web.Api.Features.Users.SearchUsersForAdmin; | ||
| using Xunit; | ||
|
|
||
| namespace Web.Api.Tests.Features.Users; | ||
|
|
||
| public class AdminUsersControllerTests | ||
| { | ||
| [Fact] | ||
| public async Task All_WithEmailFilter_ReturnsMatchingUsers() | ||
| { | ||
| await using var context = new InMemoryDatabaseContext(); | ||
| var admin = await new UserFake(Role.Admin).PleaseAsync(context); | ||
| var user1 = await new UserFake(Role.Interviewer, userName: "john.doe@example.com").PleaseAsync(context); | ||
| var user2 = await new UserFake(Role.Interviewer, userName: "jane.smith@example.com").PleaseAsync(context); | ||
|
|
||
| var controller = new AdminUsersController( | ||
| new FakeAuth(admin), | ||
| context); | ||
|
|
||
| var queryParams = new SearchUsersForAdminQueryParams | ||
| { | ||
| Email = "john" | ||
| }; | ||
|
|
||
| context.ChangeTracker.Clear(); | ||
| var result = await controller.All(queryParams); | ||
|
|
||
| Assert.Single(result.Results); | ||
| Assert.Equal(user1.Id, result.Results.First().Id); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task All_WithUnsubscribeFilter_ReturnsMatchingUsers() | ||
| { | ||
| await using var context = new InMemoryDatabaseContext(); | ||
| var admin = await new UserFake(Role.Admin).PleaseAsync(context); | ||
| var user1 = await new UserFake(Role.Interviewer).WithUnsubscribeMeFromAll(true).PleaseAsync(context); | ||
| var user2 = await new UserFake(Role.Interviewer).WithUnsubscribeMeFromAll(false).PleaseAsync(context); | ||
|
|
||
| var controller = new AdminUsersController( | ||
| new FakeAuth(admin), | ||
| context); | ||
|
|
||
| var queryParams = new SearchUsersForAdminQueryParams | ||
| { | ||
| UnsubscribeMeFromAll = true | ||
| }; | ||
|
|
||
| context.ChangeTracker.Clear(); | ||
| var result = await controller.All(queryParams); | ||
|
|
||
| Assert.Single(result.Results); | ||
| Assert.Equal(user1.Id, result.Results.First().Id); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task All_WithBothFilters_ReturnsMatchingUsers() | ||
| { | ||
| await using var context = new InMemoryDatabaseContext(); | ||
| var admin = await new UserFake(Role.Admin).PleaseAsync(context); | ||
| var user1 = await new UserFake(Role.Interviewer, userName: "john.doe@example.com").WithUnsubscribeMeFromAll(true).PleaseAsync(context); | ||
| var user2 = await new UserFake(Role.Interviewer, userName: "john.smith@example.com").WithUnsubscribeMeFromAll(false).PleaseAsync(context); | ||
| var user3 = await new UserFake(Role.Interviewer, userName: "jane.doe@example.com").WithUnsubscribeMeFromAll(true).PleaseAsync(context); | ||
|
|
||
| var controller = new AdminUsersController( | ||
| new FakeAuth(admin), | ||
| context); | ||
|
|
||
| var queryParams = new SearchUsersForAdminQueryParams | ||
| { | ||
| Email = "john", | ||
| UnsubscribeMeFromAll = true | ||
| }; | ||
|
|
||
| context.ChangeTracker.Clear(); | ||
| var result = await controller.All(queryParams); | ||
|
|
||
| Assert.Single(result.Results); | ||
| Assert.Equal(user1.Id, result.Results.First().Id); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task All_WithNoFilters_ReturnsAllActiveUsers() | ||
| { | ||
| await using var context = new InMemoryDatabaseContext(); | ||
| var admin = await new UserFake(Role.Admin).PleaseAsync(context); | ||
| var user1 = await new UserFake(Role.Interviewer).PleaseAsync(context); | ||
| var user2 = await new UserFake(Role.Interviewer).PleaseAsync(context); | ||
|
|
||
| var controller = new AdminUsersController( | ||
| new FakeAuth(admin), | ||
| context); | ||
|
|
||
| var queryParams = new SearchUsersForAdminQueryParams(); | ||
|
|
||
| context.ChangeTracker.Clear(); | ||
| var result = await controller.All(queryParams); | ||
|
|
||
| // Should return all 3 users (admin + 2 test users) | ||
| Assert.Equal(3, result.Results.Count); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task All_WithEmptyEmailFilter_ReturnsAllActiveUsers() | ||
| { | ||
| await using var context = new InMemoryDatabaseContext(); | ||
| var admin = await new UserFake(Role.Admin).PleaseAsync(context); | ||
| var user1 = await new UserFake(Role.Interviewer).PleaseAsync(context); | ||
|
|
||
| var controller = new AdminUsersController( | ||
| new FakeAuth(admin), | ||
| context); | ||
|
|
||
| var queryParams = new SearchUsersForAdminQueryParams | ||
| { | ||
| Email = string.Empty // Empty string should not filter | ||
| }; | ||
|
|
||
| context.ChangeTracker.Clear(); | ||
| var result = await controller.All(queryParams); | ||
|
|
||
| // Should return all users since empty email filter is ignored | ||
| Assert.Equal(2, result.Results.Count); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Web.Api/Features/Users/SearchUsersForAdmin/SearchUsersForAdminQueryParams.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using Domain.ValueObjects.Pagination; | ||
|
|
||
| namespace Web.Api.Features.Users.SearchUsersForAdmin; | ||
|
|
||
| public record SearchUsersForAdminQueryParams : PageModel | ||
| { | ||
| public string Email { get; init; } = string.Empty; | ||
|
|
||
| public bool? UnsubscribeMeFromAll { get; init; } | ||
|
|
||
| public bool HasEmailFilter() | ||
| => !string.IsNullOrWhiteSpace(Email); | ||
|
|
||
| public bool HasUnsubscribeFilter() | ||
| => UnsubscribeMeFromAll.HasValue; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test are failing. Please analyze and fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the test failures. The issues were:
Compilation errors: Tests were trying to use array indexing
[0]onIReadOnlyCollection<UserDto>which doesn't support indexing. Fixed by using.First()method.StyleCop violations: Fixed trailing whitespace, UTF-8 BOM requirements, and string literal issues.
All tests are now passing successfully (commit 832b8d6).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice