Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions src/Web.Api.Tests/Features/Users/AdminUsersControllerTests.cs
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()
Copy link
Member

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

Copy link
Contributor Author

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:

  1. Compilation errors: Tests were trying to use array indexing [0] on IReadOnlyCollection<UserDto> which doesn't support indexing. Fixed by using .First() method.

  2. StyleCop violations: Fixed trailing whitespace, UTF-8 BOM requirements, and string literal issues.

All tests are now passing successfully (commit 832b8d6).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

{
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);
}
}
11 changes: 8 additions & 3 deletions src/Web.Api/Features/Users/AdminUsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Web.Api.Features.Users.Models;
using Web.Api.Features.Users.SearchUsersForAdmin;
using Web.Api.Setup.Attributes;

namespace Web.Api.Features.Users;
Expand All @@ -35,19 +36,23 @@ public AdminUsersController(

[HttpGet("")]
public async Task<Pageable<UserDto>> All(
[FromQuery] PageModel pageParams = null)
[FromQuery] SearchUsersForAdminQueryParams queryParams = null)
{
await _auth.HasRoleOrFailAsync(Role.Admin);

pageParams ??= PageModel.Default;
queryParams ??= new SearchUsersForAdminQueryParams();

var emailFilter = queryParams.Email?.Trim().ToLowerInvariant();
return await _context.Users
.AsNoTracking()
.Include(x => x.UserRoles)
.Include(x => x.Salaries)
.Where(x => x.DeletedAt == null)
.When(queryParams.HasEmailFilter(), x => x.Email != null && x.Email.ToLower().Contains(emailFilter))
.When(queryParams.HasUnsubscribeFilter(), x => x.UnsubscribeMeFromAll == queryParams.UnsubscribeMeFromAll.Value)
.OrderBy(x => x.CreatedAt)
.Select(UserDto.Transformation)
.AsPaginatedAsync(pageParams);
.AsPaginatedAsync(queryParams);
}

[HttpGet("{id:long}")]
Expand Down
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;
}