mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11:11:10 +00:00
Adds the unit tests for the theming feature
This commit is contained in:
1 parent
9c83e5dfa7
commit
baa6450406
47 files changed
+8234
-8
No files matched your search
+406
@@ -0,0 +1,406 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.Themes.Management.Commands.DeleteTheme;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Commands.DeleteTheme;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Themes;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Commands.DeleteTheme;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="DeleteThemeCommandHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DeleteThemeCommandHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeService _mockThemeService;
|
||||
private readonly ICurrentUserService _mockCurrentUserService;
|
||||
private readonly IAuthorizationService _mockAuthorizationService;
|
||||
private readonly IValidator<DeleteThemeCommand> _mockValidator;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly DeleteThemeCommandHandler _sut;
|
||||
private readonly DeleteThemeCommandFixture _deleteThemeCommandFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
private readonly Guid _userId;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DeleteThemeCommandHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public DeleteThemeCommandHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeService = Substitute.For<IThemeService>();
|
||||
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
_mockValidator = Substitute.For<IValidator<DeleteThemeCommand>>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
_userId = Guid.NewGuid();
|
||||
|
||||
_mockValidator.Validate(Arg.Any<DeleteThemeCommand>())
|
||||
.Returns([]);
|
||||
_mockCurrentUserService.UserId.Returns(_userId);
|
||||
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
|
||||
.Returns(true);
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
_mockThemeService.DeleteAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
_sut = new DeleteThemeCommandHandler(
|
||||
_mockUnitOfWork,
|
||||
_mockThemeService,
|
||||
_mockCurrentUserService,
|
||||
_mockAuthorizationService,
|
||||
_mockValidator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenValidationFails_ShouldReturnValidationErrorsWithoutAnyDownstreamCalls()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<DeleteThemeCommand>())
|
||||
.Returns([Errors.Themes.ThemeIdCannotBeEmpty]);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeIdCannotBeEmpty, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenUserIsNotAuthenticated_ShouldReturnNotAuthorizedError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
_mockCurrentUserService.UserId.Returns((Guid?)null);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenUserIsNotAdmin_ShouldReturnNotAuthorizedError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetThemeFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeDoesNotExist_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeIsAlreadyDeleted_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, isDeleted: true);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetAllThemesFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Uploaded, isDeleted: false);
|
||||
Error error = Error.Failure("Database.Error", "Failed to get all themes");
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenDeletingLastBundledTheme_ShouldReturnLastBundledThemeCannotBeDeletedError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
ThemeEntity userTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Uploaded, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, userTheme]));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.LastBundledThemeCannotBeDeleted, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.DidNotReceive().DeleteAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenDeletingCurrentThemeWithoutReplacement_ShouldReturnThemeCannotBeDeletedError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Uploaded, isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme]));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeCannotBeDeleted, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.DidNotReceive().DeleteAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenDeletingBundledTheme_ShouldSoftDeleteItAndRemoveStoredFiles()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Bundled, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity otherBundledTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, otherBundledTheme]));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == theme.Id && entity.IsDeleted && entity.IsCurrent == null && entity.UpdatedBy == _userId),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.Received(1).DeleteAsync(theme.ThemeId, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenDeletingUserTheme_ShouldHardDeleteItAndRemoveStoredFiles()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Uploaded, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity bundledTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, bundledTheme]));
|
||||
_mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockThemeRepository.Received(1).DeleteByIdAsync(theme.Id, Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.Received(1).DeleteAsync(theme.ThemeId, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenHardDeleteFails_ShouldReturnErrorWithoutRemovingStoredFiles()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Uploaded, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity bundledTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
Error error = Error.Failure("Database.Error", "Failed to delete theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, bundledTheme]));
|
||||
_mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.DidNotReceive().DeleteAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenDeletingCurrentBundledTheme_ShouldSwitchToDefaultReplacementThemeAndSoftDelete()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Bundled, isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity replacementTheme = _themeEntityFixture.Create(themeId: "default-theme", installSource: ThemeInstallSource.Bundled, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
_mockThemeService.DefaultThemeId.Returns(replacementTheme.ThemeId);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, replacementTheme]));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == replacementTheme.Id && entity.IsCurrent == true),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == theme.Id && entity.IsDeleted && entity.IsCurrent == null),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.Received(1).DeleteAsync(theme.ThemeId, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenNoDefaultThemeMatches_ShouldSwitchToAlphabeticallyFirstReplacementTheme()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Bundled, isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity alphaTheme = _themeEntityFixture.Create(themeId: "alpha", installSource: ThemeInstallSource.Bundled, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity betaTheme = _themeEntityFixture.Create(themeId: "beta", installSource: ThemeInstallSource.Bundled, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
_mockThemeService.DefaultThemeId.Returns("no-such-default");
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, betaTheme, alphaTheme]));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == alphaTheme.Id && entity.IsCurrent == true),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == theme.Id && entity.IsDeleted),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenFileDeletionFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Uploaded, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity bundledTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
Error error = Error.Failure("Theme.DeleteFailed", "Failed to delete theme files");
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, bundledTheme]));
|
||||
_mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockThemeService.DeleteAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.Themes.Management.Commands.DeleteTheme;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Commands.DeleteTheme;
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Commands.DeleteTheme;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="DeleteThemeCommandValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DeleteThemeCommandValidatorTests
|
||||
{
|
||||
private readonly DeleteThemeCommandValidator _validator = new();
|
||||
private readonly DeleteThemeCommandFixture _deleteThemeCommandFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
command = command with { ThemeId = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create(themeId: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsWhiteSpace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create(themeId: " ");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsValid_ShouldNotHaveAnyValidationErrors()
|
||||
{
|
||||
// Arrange
|
||||
DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
+302
@@ -0,0 +1,302 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.Themes.Management.Commands.InstallTheme;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Common.Infrastructure.Models.DTO.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Commands.InstallTheme;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Themes;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Commands.InstallTheme;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="InstallThemeCommandHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class InstallThemeCommandHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeService _mockThemeService;
|
||||
private readonly ICurrentUserService _mockCurrentUserService;
|
||||
private readonly IAuthorizationService _mockAuthorizationService;
|
||||
private readonly IValidator<InstallThemeCommand> _mockValidator;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly InstallThemeCommandHandler _sut;
|
||||
private readonly InstallThemeCommandFixture _installThemeCommandFixture = new();
|
||||
private readonly ThemeManifestDtoFixture _themeManifestDtoFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
private readonly Guid _userId;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="InstallThemeCommandHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public InstallThemeCommandHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeService = Substitute.For<IThemeService>();
|
||||
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
_mockValidator = Substitute.For<IValidator<InstallThemeCommand>>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
_userId = Guid.NewGuid();
|
||||
|
||||
_mockValidator.Validate(Arg.Any<InstallThemeCommand>())
|
||||
.Returns([]);
|
||||
_mockCurrentUserService.UserId.Returns(_userId);
|
||||
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
|
||||
.Returns(true);
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
|
||||
_sut = new InstallThemeCommandHandler(
|
||||
_mockUnitOfWork,
|
||||
_mockThemeService,
|
||||
_mockCurrentUserService,
|
||||
_mockAuthorizationService,
|
||||
_mockValidator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenValidationFails_ShouldReturnValidationErrorsWithoutAnyDownstreamCalls()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<InstallThemeCommand>())
|
||||
.Returns([Errors.Themes.ThemeArchiveCannotBeNull]);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeArchiveCannotBeNull, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().InstallAsync(Arg.Any<Stream>(), Arg.Any<CancellationToken>());
|
||||
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenUserIsNotAuthenticated_ShouldReturnNotAuthorizedError()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
_mockCurrentUserService.UserId.Returns((Guid?)null);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.DidNotReceive().InstallAsync(Arg.Any<Stream>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenUserIsNotAdmin_ShouldReturnNotAuthorizedError()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().InstallAsync(Arg.Any<Stream>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeInstallFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
Error error = Error.Failure("Theme.InstallFailed", "Failed to install theme");
|
||||
_mockThemeService.InstallAsync(command.Archive!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetThemeFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get theme");
|
||||
_mockThemeService.InstallAsync(command.Archive!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(manifest));
|
||||
_mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeDoesNotExist_ShouldInsertNewUploadedTheme()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
|
||||
_mockThemeService.InstallAsync(command.Archive!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(manifest));
|
||||
_mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
_mockThemeRepository.InsertAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Created);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(manifest.Id, result.Value.ThemeId);
|
||||
Assert.Equal(manifest.Name, result.Value.Name);
|
||||
Assert.Equal(manifest.Description, result.Value.Description);
|
||||
Assert.Equal(manifest.Author, result.Value.Author);
|
||||
Assert.Equal(manifest.Version, result.Value.Version);
|
||||
Assert.Equal(manifest.Preview, result.Value.PreviewPath);
|
||||
Assert.Equal(ThemeInstallSource.Uploaded, result.Value.InstallSource);
|
||||
Assert.Null(result.Value.IsCurrent);
|
||||
await _mockThemeRepository.Received(1).InsertAsync(
|
||||
Arg.Is<ThemeEntity>(theme =>
|
||||
theme.ThemeId == manifest.Id &&
|
||||
theme.Name == manifest.Name &&
|
||||
theme.Description == manifest.Description &&
|
||||
theme.Author == manifest.Author &&
|
||||
theme.Version == manifest.Version &&
|
||||
theme.PreviewPath == manifest.Preview &&
|
||||
theme.InstallSource == ThemeInstallSource.Uploaded &&
|
||||
theme.IsDeleted == false &&
|
||||
theme.CreatedBy == _userId &&
|
||||
theme.UpdatedBy == _userId),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenInsertFails_ShouldRollBackStoredThemeFiles()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to insert theme");
|
||||
_mockThemeService.InstallAsync(command.Archive!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(manifest));
|
||||
_mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
_mockThemeRepository.InsertAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockThemeService.Received(1).DeleteAsync(manifest.Id, Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeAlreadyExists_ShouldUpdateExistingThemeAndResurrectIt()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
|
||||
ThemeEntity existingTheme = _themeEntityFixture.Create(
|
||||
themeId: manifest.Id,
|
||||
installSource: ThemeInstallSource.Bundled,
|
||||
isCurrent: true, includeIsCurrent: true,
|
||||
isDeleted: true);
|
||||
_mockThemeService.InstallAsync(command.Archive!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(manifest));
|
||||
_mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(existingTheme));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(existingTheme.Id, result.Value.Id);
|
||||
Assert.Equal(manifest.Name, result.Value.Name);
|
||||
Assert.Equal(ThemeInstallSource.Bundled, result.Value.InstallSource);
|
||||
Assert.True(result.Value.IsCurrent);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(theme =>
|
||||
theme.Id == existingTheme.Id &&
|
||||
theme.ThemeId == manifest.Id &&
|
||||
theme.Name == manifest.Name &&
|
||||
theme.InstallSource == existingTheme.InstallSource &&
|
||||
theme.IsCurrent == existingTheme.IsCurrent &&
|
||||
theme.IsDeleted == false &&
|
||||
theme.CreatedOnUtc == existingTheme.CreatedOnUtc &&
|
||||
theme.CreatedBy == existingTheme.CreatedBy &&
|
||||
theme.UpdatedBy == _userId),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.DidNotReceive().InsertAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenUpdateFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
|
||||
ThemeEntity existingTheme = _themeEntityFixture.Create(themeId: manifest.Id, installSource: ThemeInstallSource.Bundled, isCurrent: null, isDeleted: false);
|
||||
Error error = Error.Failure("Database.Error", "Failed to update theme");
|
||||
_mockThemeService.InstallAsync(command.Archive!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(manifest));
|
||||
_mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(existingTheme));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.Themes.Management.Commands.InstallTheme;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Commands.InstallTheme;
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Commands.InstallTheme;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="InstallThemeCommandValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class InstallThemeCommandValidatorTests
|
||||
{
|
||||
private readonly InstallThemeCommandValidator _validator = new();
|
||||
private readonly InstallThemeCommandFixture _installThemeCommandFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenArchiveIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
command = command with { Archive = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeArchiveCannotBeNull);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenArchiveIsProvided_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create();
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.Themes.ThemeArchiveCannotBeNull);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenCommandIsValid_ShouldNotHaveAnyValidationErrors()
|
||||
{
|
||||
// Arrange
|
||||
InstallThemeCommand command = _installThemeCommandFixture.Create(new MemoryStream());
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
+296
@@ -0,0 +1,296 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.Themes.Management.Commands.SetCurrentTheme;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Commands.SetCurrentTheme;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Commands.SetCurrentTheme;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="SetCurrentThemeCommandHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class SetCurrentThemeCommandHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly ICurrentUserService _mockCurrentUserService;
|
||||
private readonly IAuthorizationService _mockAuthorizationService;
|
||||
private readonly IValidator<SetCurrentThemeCommand> _mockValidator;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly SetCurrentThemeCommandHandler _sut;
|
||||
private readonly SetCurrentThemeCommandFixture _setCurrentThemeCommandFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
private readonly Guid _userId;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SetCurrentThemeCommandHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public SetCurrentThemeCommandHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
_mockValidator = Substitute.For<IValidator<SetCurrentThemeCommand>>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
_userId = Guid.NewGuid();
|
||||
|
||||
_mockValidator.Validate(Arg.Any<SetCurrentThemeCommand>())
|
||||
.Returns([]);
|
||||
_mockCurrentUserService.UserId.Returns(_userId);
|
||||
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
|
||||
.Returns(true);
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
|
||||
_sut = new SetCurrentThemeCommandHandler(
|
||||
_mockUnitOfWork,
|
||||
_mockCurrentUserService,
|
||||
_mockAuthorizationService,
|
||||
_mockValidator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenValidationFails_ShouldReturnValidationErrorsWithoutAnyDownstreamCalls()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<SetCurrentThemeCommand>())
|
||||
.Returns([Errors.Themes.ThemeIdCannotBeEmpty]);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeIdCannotBeEmpty, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenUserIsNotAuthenticated_ShouldReturnNotAuthorizedError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
_mockCurrentUserService.UserId.Returns((Guid?)null);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenUserIsNotAdmin_ShouldReturnNotAuthorizedError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetThemeFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeDoesNotExist_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeIsDeleted_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, isDeleted: true);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeIsAlreadyCurrent_ShouldReturnResponseWithoutAnyPersistence()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(theme.Id, result.Value.Id);
|
||||
Assert.Equal(theme.ThemeId, result.Value.ThemeId);
|
||||
Assert.True(result.Value.IsCurrent);
|
||||
await _mockThemeRepository.DidNotReceive().GetCurrentAsync(Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetCurrentThemeFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
Error error = Error.Failure("Database.Error", "Failed to get current theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetCurrentAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenAnotherThemeIsCurrent_ShouldUnsetPreviousThemeAndActivateNewOne()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity currentTheme = _themeEntityFixture.Create(isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetCurrentAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(currentTheme));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(theme.Id, result.Value.Id);
|
||||
Assert.True(result.Value.IsCurrent);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == currentTheme.Id && entity.IsCurrent == null),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == theme.Id && entity.IsCurrent == true && entity.UpdatedBy == _userId),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenCurrentThemeIsTheSameTheme_ShouldNotUpdateCurrentThemeTwice()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetCurrentAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(theme.Id, result.Value.Id);
|
||||
Assert.True(result.Value.IsCurrent);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == theme.Id && entity.IsCurrent == true),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenNoThemeIsCurrentlySet_ShouldActivateThemeWithoutUnsettingAnything()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetCurrentAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(theme.Id, result.Value.Id);
|
||||
Assert.True(result.Value.IsCurrent);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == theme.Id && entity.IsCurrent == true && entity.UpdatedBy == _userId),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.Themes.Management.Commands.SetCurrentTheme;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Commands.SetCurrentTheme;
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Commands.SetCurrentTheme;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="SetCurrentThemeCommandValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class SetCurrentThemeCommandValidatorTests
|
||||
{
|
||||
private readonly SetCurrentThemeCommandValidator _validator = new();
|
||||
private readonly SetCurrentThemeCommandFixture _setCurrentThemeCommandFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
command = command with { ThemeId = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create(themeId: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsWhiteSpace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create(themeId: " ");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsValid_ShouldNotHaveAnyValidationErrors()
|
||||
{
|
||||
// Arrange
|
||||
SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetCurrentTheme;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetCurrentTheme;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetCurrentTheme;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetCurrentThemeQueryHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetCurrentThemeQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly GetCurrentThemeQueryHandler _sut;
|
||||
private readonly GetCurrentThemeQueryFixture _getCurrentThemeQueryFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetCurrentThemeQueryHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public GetCurrentThemeQueryHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
_sut = new GetCurrentThemeQueryHandler(_mockUnitOfWork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenCurrentThemeExists_ShouldReturnItsResponse()
|
||||
{
|
||||
// Arrange
|
||||
GetCurrentThemeQuery query = _getCurrentThemeQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(isCurrent: true, includeIsCurrent: true);
|
||||
_mockThemeRepository.GetCurrentAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(theme.Id, result.Value.Id);
|
||||
Assert.Equal(theme.ThemeId, result.Value.ThemeId);
|
||||
Assert.Equal(theme.Name, result.Value.Name);
|
||||
Assert.Equal(theme.Description, result.Value.Description);
|
||||
Assert.Equal(theme.Author, result.Value.Author);
|
||||
Assert.Equal(theme.Version, result.Value.Version);
|
||||
Assert.Equal(theme.PreviewPath, result.Value.PreviewPath);
|
||||
Assert.Equal(theme.InstallSource, result.Value.InstallSource);
|
||||
Assert.True(result.Value.IsCurrent);
|
||||
Assert.Equal(theme.InstalledAtUtc, result.Value.InstalledAtUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenNoCurrentThemeExists_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetCurrentThemeQuery query = _getCurrentThemeQueryFixture.Create();
|
||||
_mockThemeRepository.GetCurrentAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
GetCurrentThemeQuery query = _getCurrentThemeQueryFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get current theme");
|
||||
_mockThemeRepository.GetCurrentAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemeArchive;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemeArchive;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemeArchive;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemeArchiveQueryHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemeArchiveQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeService _mockThemeService;
|
||||
private readonly IValidator<GetThemeArchiveQuery> _mockValidator;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly GetThemeArchiveQueryHandler _sut;
|
||||
private readonly GetThemeArchiveQueryFixture _getThemeArchiveQueryFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetThemeArchiveQueryHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public GetThemeArchiveQueryHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeService = Substitute.For<IThemeService>();
|
||||
_mockValidator = Substitute.For<IValidator<GetThemeArchiveQuery>>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
|
||||
_mockValidator.Validate(Arg.Any<GetThemeArchiveQuery>())
|
||||
.Returns([]);
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
|
||||
_sut = new GetThemeArchiveQueryHandler(_mockUnitOfWork, _mockThemeService, _mockValidator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenValidationFails_ShouldReturnValidationErrorsWithoutAnyDownstreamCalls()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<GetThemeArchiveQuery>())
|
||||
.Returns([Errors.Themes.ThemeIdCannotBeEmpty]);
|
||||
|
||||
// Act
|
||||
Result<ThemeArchiveResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeIdCannotBeEmpty, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.DidNotReceive().BuildArchiveAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetThemeFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeArchiveResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().BuildArchiveAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeDoesNotExist_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ThemeArchiveResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().BuildArchiveAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeIsDeleted_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, isDeleted: true);
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
|
||||
// Act
|
||||
Result<ThemeArchiveResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().BuildArchiveAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenArchiveBuildsSuccessfully_ShouldReturnArchiveResponse()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, isDeleted: false);
|
||||
byte[] bytes = Encoding.UTF8.GetBytes("archive-content");
|
||||
string fileName = $"{theme.ThemeId}.zip";
|
||||
string contentType = "application/zip";
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.BuildArchiveAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(new ThemeArchiveDto(bytes, fileName, contentType)));
|
||||
|
||||
// Act
|
||||
Result<ThemeArchiveResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(bytes, result.Value.Bytes);
|
||||
Assert.Equal(fileName, result.Value.FileName);
|
||||
Assert.Equal(contentType, result.Value.ContentType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenArchiveBuildFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, isDeleted: false);
|
||||
Error error = Error.Failure("Theme.ArchiveBuildFailed", "Failed to build archive");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.BuildArchiveAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeArchiveResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemeArchive;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemeArchive;
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemeArchive;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemeArchiveQueryValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemeArchiveQueryValidatorTests
|
||||
{
|
||||
private readonly GetThemeArchiveQueryValidator _validator = new();
|
||||
private readonly GetThemeArchiveQueryFixture _getThemeArchiveQueryFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
query = query with { ThemeId = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create(themeId: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsWhiteSpace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create(themeId: " ");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsValid_ShouldNotHaveAnyValidationErrors()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeArchiveQuery query = _getThemeArchiveQueryFixture.Create();
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
+243
@@ -0,0 +1,243 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemeAsset;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemeAsset;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Themes;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemeAsset;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemeAssetQueryHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemeAssetQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeService _mockThemeService;
|
||||
private readonly IValidator<GetThemeAssetQuery> _mockValidator;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly GetThemeAssetQueryHandler _sut;
|
||||
private readonly GetThemeAssetQueryFixture _getThemeAssetQueryFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetThemeAssetQueryHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public GetThemeAssetQueryHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeService = Substitute.For<IThemeService>();
|
||||
_mockValidator = Substitute.For<IValidator<GetThemeAssetQuery>>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
|
||||
_mockValidator.Validate(Arg.Any<GetThemeAssetQuery>())
|
||||
.Returns([]);
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
|
||||
_sut = new GetThemeAssetQueryHandler(_mockUnitOfWork, _mockThemeService, _mockValidator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenValidationFails_ShouldReturnValidationErrorsWithoutAnyDownstreamCalls()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<GetThemeAssetQuery>())
|
||||
.Returns([Errors.Themes.ThemeIdCannotBeEmpty]);
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeIdCannotBeEmpty, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.DidNotReceive().GetAssetAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetThemeFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().GetAssetAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeDoesNotExist_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeIsDeleted_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, isDeleted: true);
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().GetAssetAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenAssetLoadsSuccessfully_ShouldReturnAssetResponse()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, isDeleted: false);
|
||||
byte[] bytes = Encoding.UTF8.GetBytes("asset-content");
|
||||
string contentType = "image/png";
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetAssetAsync(theme.ThemeId, query.AssetPath!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(new ThemeAssetDto(bytes, contentType)));
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(bytes, result.Value.Bytes);
|
||||
Assert.Equal(contentType, result.Value.ContentType);
|
||||
await _mockThemeService.DidNotReceive().RestoreBundledThemeAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenAssetLoadFailsForUserTheme_ShouldReturnAssetErrorWithoutRestoring()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Uploaded, isDeleted: false);
|
||||
Error error = Error.Failure("Theme.AssetMissing", "Asset missing");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetAssetAsync(theme.ThemeId, query.AssetPath!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().RestoreBundledThemeAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenAssetLoadFailsAndRestoreFails_ShouldReturnRestoreError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
Error assetError = Error.Failure("Theme.AssetMissing", "Asset missing");
|
||||
Error restoreError = Error.Failure("Theme.RestoreFailed", "Restore failed");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetAssetAsync(theme.ThemeId, query.AssetPath!, Arg.Any<CancellationToken>())
|
||||
.Returns(assetError);
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(restoreError);
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(restoreError, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenAssetLoadFailsAndRestoreSucceeds_ShouldRetryAndReturnAssetResponse()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
Error assetError = Error.Failure("Theme.AssetMissing", "Asset missing");
|
||||
byte[] restoredBytes = Encoding.UTF8.GetBytes("restored-asset");
|
||||
string contentType = "text/css";
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetAssetAsync(theme.ThemeId, query.AssetPath!, Arg.Any<CancellationToken>())
|
||||
.Returns(assetError, Result.From(new ThemeAssetDto(restoredBytes, contentType)));
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(restoredBytes, result.Value.Bytes);
|
||||
Assert.Equal(contentType, result.Value.ContentType);
|
||||
await _mockThemeService.Received(2).GetAssetAsync(theme.ThemeId, query.AssetPath!, Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.Received(1).RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRestoreSucceedsButRetryFails_ShouldReturnAssetError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
Error assetError = Error.Failure("Theme.AssetMissing", "Asset missing");
|
||||
Error retryError = Error.Failure("Theme.AssetMissingAgain", "Asset still missing");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetAssetAsync(theme.ThemeId, query.AssetPath!, Arg.Any<CancellationToken>())
|
||||
.Returns(assetError, retryError);
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
// Act
|
||||
Result<ThemeAssetResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(retryError, result.FirstError);
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemeAsset;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemeAsset;
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemeAsset;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemeAssetQueryValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemeAssetQueryValidatorTests
|
||||
{
|
||||
private readonly GetThemeAssetQueryValidator _validator = new();
|
||||
private readonly GetThemeAssetQueryFixture _getThemeAssetQueryFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
query = query with { ThemeId = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create(themeId: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsWhiteSpace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create(themeId: " ");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenAssetPathIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
query = query with { AssetPath = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeAssetPathCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenAssetPathIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create(assetPath: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeAssetPathCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenAssetPathIsWhiteSpace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create(assetPath: " ");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeAssetPathCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenQueryIsValid_ShouldNotHaveAnyValidationErrors()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeAssetQuery query = _getThemeAssetQueryFixture.Create();
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Themes;
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemeSettings;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemeSettings;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using NSubstitute;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemeSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemeSettingsQueryHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemeSettingsQueryHandlerTests
|
||||
{
|
||||
private readonly IThemeService _mockThemeService;
|
||||
private readonly GetThemeSettingsQueryHandler _sut;
|
||||
private readonly GetThemeSettingsQueryFixture _getThemeSettingsQueryFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetThemeSettingsQueryHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public GetThemeSettingsQueryHandlerTests()
|
||||
{
|
||||
_mockThemeService = Substitute.For<IThemeService>();
|
||||
_sut = new GetThemeSettingsQueryHandler(_mockThemeService);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenCalled_ShouldReturnThemeSettingsFromThemeService()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeSettingsQuery query = _getThemeSettingsQueryFixture.Create();
|
||||
long maxArchiveBytes = 50_000_000;
|
||||
bool allowThemeScripts = false;
|
||||
string defaultThemeId = "default-theme";
|
||||
_mockThemeService.MaxArchiveBytes.Returns(maxArchiveBytes);
|
||||
_mockThemeService.AllowThemeScripts.Returns(allowThemeScripts);
|
||||
_mockThemeService.DefaultThemeId.Returns(defaultThemeId);
|
||||
|
||||
// Act
|
||||
Result<ThemeSettingsResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(maxArchiveBytes, result.Value.MaxArchiveBytes);
|
||||
Assert.Equal(allowThemeScripts, result.Value.AllowThemeScripts);
|
||||
Assert.Equal(defaultThemeId, result.Value.DefaultThemeId);
|
||||
}
|
||||
}
|
||||
+333
@@ -0,0 +1,333 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemeTemplate;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemeTemplate;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Themes;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemeTemplate;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemeTemplateQueryHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemeTemplateQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeService _mockThemeService;
|
||||
private readonly IValidator<GetThemeTemplateQuery> _mockValidator;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly GetThemeTemplateQueryHandler _sut;
|
||||
private readonly GetThemeTemplateQueryFixture _getThemeTemplateQueryFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetThemeTemplateQueryHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public GetThemeTemplateQueryHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeService = Substitute.For<IThemeService>();
|
||||
_mockValidator = Substitute.For<IValidator<GetThemeTemplateQuery>>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
|
||||
_mockValidator.Validate(Arg.Any<GetThemeTemplateQuery>())
|
||||
.Returns([]);
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
|
||||
_sut = new GetThemeTemplateQueryHandler(_mockUnitOfWork, _mockThemeService, _mockValidator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenValidationFails_ShouldReturnValidationErrorsWithoutAnyDownstreamCalls()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<GetThemeTemplateQuery>())
|
||||
.Returns([Errors.Themes.ThemeIdCannotBeEmpty]);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeIdCannotBeEmpty, result.FirstError);
|
||||
await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.DidNotReceive().GetTemplateAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGetThemeFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().GetTemplateAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeDoesNotExist_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenThemeIsDeleted_ShouldReturnThemeNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, isDeleted: true);
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Themes.ThemeNotFound, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().GetTemplateAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenTemplateLoadsSuccessfully_ShouldReturnTemplateResponse()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, isDeleted: false);
|
||||
string template = "<html>Hello</html>";
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(template));
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(theme.Id, result.Value.Theme.Id);
|
||||
Assert.Equal(theme.ThemeId, result.Value.Theme.ThemeId);
|
||||
Assert.Equal(template, result.Value.Template);
|
||||
await _mockThemeService.DidNotReceive().RestoreBundledThemeAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenTemplateLoadFailsForUserTheme_ShouldReturnTemplateErrorWithoutRestoring()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Uploaded, isDeleted: false);
|
||||
Error error = Error.Failure("Theme.TemplateMissing", "Template missing");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockThemeService.DidNotReceive().RestoreBundledThemeAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenTemplateLoadFailsAndRestoreFails_ShouldReturnTemplateError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
Error templateError = Error.Failure("Theme.TemplateMissing", "Template missing");
|
||||
Error restoreError = Error.Failure("Theme.RestoreFailed", "Restore failed");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(templateError);
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(restoreError);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(templateError, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenTemplateLoadFailsAndRestoreSucceeds_ShouldRetryAndReturnTemplateResponse()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isCurrent: false, includeIsCurrent: true, isDeleted: false);
|
||||
Error templateError = Error.Failure("Theme.TemplateMissing", "Template missing");
|
||||
string restoredTemplate = "<html>Restored</html>";
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(templateError, Result.From(restoredTemplate));
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(restoredTemplate, result.Value.Template);
|
||||
await _mockThemeService.Received(2).GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>());
|
||||
await _mockThemeService.Received(1).RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRestoreSucceedsButRetryFails_ShouldReturnTemplateError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
Error templateError = Error.Failure("Theme.TemplateMissing", "Template missing");
|
||||
Error retryError = Error.Failure("Theme.TemplateMissingAgain", "Template still missing");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(templateError, retryError);
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(retryError, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRestoringCurrentBundledTheme_ShouldSwitchActiveThemeToDefaultTheme()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
ThemeEntity defaultTheme = _themeEntityFixture.Create(themeId: "default-theme", isDeleted: false);
|
||||
Error templateError = Error.Failure("Theme.TemplateMissing", "Template missing");
|
||||
string restoredTemplate = "<html>Restored</html>";
|
||||
_mockThemeService.DefaultThemeId.Returns(defaultTheme.ThemeId);
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetByThemeIdAsync(defaultTheme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(defaultTheme));
|
||||
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(templateError, Result.From(restoredTemplate));
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(restoredTemplate, result.Value.Template);
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == theme.Id && entity.IsCurrent == null),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockThemeRepository.Received(1).UpdateAsync(
|
||||
Arg.Is<ThemeEntity>(entity => entity.Id == defaultTheme.Id && entity.IsCurrent == true),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRestoringCurrentBundledThemeWithNoDefaultTheme_ShouldNotSwitchActiveTheme()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
Error templateError = Error.Failure("Theme.TemplateMissing", "Template missing");
|
||||
string restoredTemplate = "<html>Restored</html>";
|
||||
_mockThemeService.DefaultThemeId.Returns("missing-default");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetByThemeIdAsync("missing-default", Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(null));
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(templateError, Result.From(restoredTemplate));
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(restoredTemplate, result.Value.Template);
|
||||
await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenGettingDefaultThemeFails_ShouldNotSwitchActiveThemeButStillRestore()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
ThemeEntity theme = _themeEntityFixture.Create(themeId: query.ThemeId, installSource: ThemeInstallSource.Bundled, isCurrent: true, includeIsCurrent: true, isDeleted: false);
|
||||
Error templateError = Error.Failure("Theme.TemplateMissing", "Template missing");
|
||||
Error defaultError = Error.Failure("Database.Error", "Failed to get default theme");
|
||||
string restoredTemplate = "<html>Restored</html>";
|
||||
_mockThemeService.DefaultThemeId.Returns("default-theme");
|
||||
_mockThemeRepository.GetByThemeIdAsync(query.ThemeId!, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ThemeEntity?>(theme));
|
||||
_mockThemeRepository.GetByThemeIdAsync("default-theme", Arg.Any<CancellationToken>())
|
||||
.Returns(defaultError);
|
||||
_mockThemeService.GetTemplateAsync(theme.ThemeId, query.PageKey!, Arg.Any<CancellationToken>())
|
||||
.Returns(templateError, Result.From(restoredTemplate));
|
||||
_mockThemeService.RestoreBundledThemeAsync(theme.ThemeId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
|
||||
// Act
|
||||
Result<ThemeTemplateResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(restoredTemplate, result.Value.Template);
|
||||
await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemeTemplate;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemeTemplate;
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemeTemplate;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemeTemplateQueryValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemeTemplateQueryValidatorTests
|
||||
{
|
||||
private readonly GetThemeTemplateQueryValidator _validator = new();
|
||||
private readonly GetThemeTemplateQueryFixture _getThemeTemplateQueryFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
query = query with { ThemeId = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create(themeId: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenThemeIdIsWhiteSpace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create(themeId: " ");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenPageKeyIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
query = query with { PageKey = null };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.PageKeyCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenPageKeyIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create(pageKey: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.PageKeyCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenPageKeyIsWhiteSpace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create(pageKey: " ");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Themes.PageKeyCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenQueryIsValid_ShouldNotHaveAnyValidationErrors()
|
||||
{
|
||||
// Arrange
|
||||
GetThemeTemplateQuery query = _getThemeTemplateQueryFixture.Create();
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Core.Themes.Management.Queries.GetThemes;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemes;
|
||||
using Lumina.Contracts.Responses.Themes;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Themes;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Core.Themes.Management.Queries.GetThemes;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetThemesQueryHandler"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetThemesQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly GetThemesQueryHandler _sut;
|
||||
private readonly GetThemesQueryFixture _getThemesQueryFixture = new();
|
||||
private readonly ThemeEntityFixture _themeEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="GetThemesQueryHandlerTests"/> class.
|
||||
/// </summary>
|
||||
public GetThemesQueryHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
_sut = new GetThemesQueryHandler(_mockUnitOfWork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenCalled_ShouldReturnAllAvailableThemesOrderedBundledFirstThenByName()
|
||||
{
|
||||
// Arrange
|
||||
GetThemesQuery query = _getThemesQueryFixture.Create();
|
||||
ThemeEntity uploadedTheme = _themeEntityFixture.Create(themeId: "user-theme", name: "Zeta", installSource: ThemeInstallSource.Uploaded, isDeleted: false);
|
||||
ThemeEntity bundledLowerTheme = _themeEntityFixture.Create(themeId: "bundled-lower", name: "apple", installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
ThemeEntity bundledUpperTheme = _themeEntityFixture.Create(themeId: "bundled-upper", name: "Banana", installSource: ThemeInstallSource.Bundled, isDeleted: false);
|
||||
ThemeEntity deletedTheme = _themeEntityFixture.Create(themeId: "deleted-theme", name: "Deleted", installSource: ThemeInstallSource.Uploaded, isDeleted: true);
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([uploadedTheme, bundledLowerTheme, bundledUpperTheme, deletedTheme]));
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<ThemeResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(3, result.Value.Count);
|
||||
Assert.Equal(bundledLowerTheme.Id, result.Value[0].Id);
|
||||
Assert.Equal(bundledUpperTheme.Id, result.Value[1].Id);
|
||||
Assert.Equal(uploadedTheme.Id, result.Value[2].Id);
|
||||
Assert.Equal(ThemeInstallSource.Bundled, result.Value[0].InstallSource);
|
||||
Assert.Equal(ThemeInstallSource.Bundled, result.Value[1].InstallSource);
|
||||
Assert.Equal(ThemeInstallSource.Uploaded, result.Value[2].InstallSource);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenNoThemesExist_ShouldReturnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
GetThemesQuery query = _getThemesQueryFixture.Create();
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<ThemeEntity>>([]));
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<ThemeResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Empty(result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
GetThemesQuery query = _getThemesQueryFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get all themes");
|
||||
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<ThemeResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user