68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using Heimdall.Application.Common;
|
|||
|
|
using Heimdall.Application.Common.Persistence;
|
||
|
|
using Heimdall.Application.Users.Commands.AssignSites;
|
||
|
|
using Heimdall.Domain.Entities;
|
||
|
|
using Microsoft.AspNetCore.Identity;
|
||
|
|
using NSubstitute;
|
||
|
|
|
||
|
|
namespace Heimdall.Application.Tests.Handlers.Users;
|
||
|
|
|
||
|
|
public class AssignSitesHandlerTests
|
||
|
|
{
|
||
|
|
private readonly AppUser _user = new()
|
||
|
|
{
|
||
|
|
Id = Guid.NewGuid(),
|
||
|
|
UserName = "test@test.com",
|
||
|
|
Email = "test@test.com"
|
||
|
|
};
|
||
|
|
|
||
|
|
private readonly UserManager<AppUser> _userManager;
|
||
|
|
private readonly IUserRepository _repository;
|
||
|
|
private readonly AssignSitesHandler _handler;
|
||
|
|
|
||
|
|
public AssignSitesHandlerTests()
|
||
|
|
{
|
||
|
|
var store = Substitute.For<IUserStore<AppUser>>();
|
||
|
|
_userManager = Substitute.For<UserManager<AppUser>>(store, null!, null!, null!, null!, null!, null!, null!, null!);
|
||
|
|
_repository = Substitute.For<IUserRepository>();
|
||
|
|
_handler = new AssignSitesHandler(_userManager, _repository);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Assign_sites_should_return_success()
|
||
|
|
{
|
||
|
|
_userManager.FindByIdAsync(_user.Id.ToString()).Returns(_user);
|
||
|
|
var siteIds = new List<Guid> { Guid.NewGuid(), Guid.NewGuid() };
|
||
|
|
|
||
|
|
var result = await _handler.Handle(
|
||
|
|
new AssignSitesCommand(_user.Id, siteIds), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeTrue();
|
||
|
|
await _repository.Received(1).SetUserSitesAsync(_user.Id, siteIds, Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Assign_sites_nonexistent_user_should_return_not_found()
|
||
|
|
{
|
||
|
|
_userManager.FindByIdAsync(Arg.Any<string>()).Returns((AppUser?)null);
|
||
|
|
|
||
|
|
var result = await _handler.Handle(
|
||
|
|
new AssignSitesCommand(Guid.NewGuid(), new List<Guid>()), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeFalse();
|
||
|
|
result.StatusCode.ShouldBe(404);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Assign_empty_sites_should_return_success()
|
||
|
|
{
|
||
|
|
_userManager.FindByIdAsync(_user.Id.ToString()).Returns(_user);
|
||
|
|
var emptySiteIds = new List<Guid>();
|
||
|
|
|
||
|
|
var result = await _handler.Handle(
|
||
|
|
new AssignSitesCommand(_user.Id, emptySiteIds), CancellationToken.None);
|
||
|
|
|
||
|
|
result.IsSuccess.ShouldBeTrue();
|
||
|
|
await _repository.Received(1).SetUserSitesAsync(_user.Id, emptySiteIds, Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
}
|