diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/DeleteThemeRequestMappingTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/DeleteThemeRequestMappingTests.cs
new file mode 100644
index 00000000..6dcb16ce
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/DeleteThemeRequestMappingTests.cs
@@ -0,0 +1,80 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Mapping.Themes;
+using Lumina.Application.Core.Themes.Management.Commands.DeleteTheme;
+using Lumina.Contracts.Fixtures.Core.Requests.Themes;
+using Lumina.Contracts.Requests.Themes;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+#endregion
+
+namespace Lumina.Application.UnitTests.Common.Mapping.Themes;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class DeleteThemeRequestMappingTests
+{
+ private readonly DeleteThemeRequestFixture _deleteThemeRequestFixture = new();
+
+ [Fact]
+ public void ToCommand_WhenMappingValidRequest_ShouldMapCorrectly()
+ {
+ // Arrange
+ DeleteThemeRequest request = _deleteThemeRequestFixture.Create();
+
+ // Act
+ DeleteThemeCommand result = request.ToCommand();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(request.ThemeId, result.ThemeId);
+ }
+
+ [Theory]
+ [InlineData("default-theme")]
+ [InlineData("my-uploaded-theme")]
+ [InlineData("T-123")]
+ public void ToCommand_WhenMappingDifferentThemeIds_ShouldMapCorrectly(string themeId)
+ {
+ // Arrange
+ DeleteThemeRequest request = new(themeId);
+
+ // Act
+ DeleteThemeCommand result = request.ToCommand();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(themeId, result.ThemeId);
+ }
+
+ [Fact]
+ public void ToCommand_WhenThemeIdIsNull_ShouldMapNull()
+ {
+ // Arrange
+ DeleteThemeRequest request = new(null);
+
+ // Act
+ DeleteThemeCommand result = request.ToCommand();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Null(result.ThemeId);
+ }
+
+ [Fact]
+ public void ToCommand_WhenMappingMultipleRequests_ShouldMapAllCorrectly()
+ {
+ // Arrange
+ List requests = _deleteThemeRequestFixture.CreateMany();
+
+ // Act
+ List results = [.. requests.Select(r => r.ToCommand())];
+
+ // Assert
+ Assert.Equal(requests.Count, results.Count);
+ for (int i = 0; i < requests.Count; i++)
+ Assert.Equal(requests[i].ThemeId, results[i].ThemeId);
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeArchiveRequestMappingTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeArchiveRequestMappingTests.cs
new file mode 100644
index 00000000..8c758111
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeArchiveRequestMappingTests.cs
@@ -0,0 +1,80 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Mapping.Themes;
+using Lumina.Application.Core.Themes.Management.Queries.GetThemeArchive;
+using Lumina.Contracts.Fixtures.Core.Requests.Themes;
+using Lumina.Contracts.Requests.Themes;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+#endregion
+
+namespace Lumina.Application.UnitTests.Common.Mapping.Themes;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class GetThemeArchiveRequestMappingTests
+{
+ private readonly GetThemeArchiveRequestFixture _getThemeArchiveRequestFixture = new();
+
+ [Fact]
+ public void ToQuery_WhenMappingValidRequest_ShouldMapCorrectly()
+ {
+ // Arrange
+ GetThemeArchiveRequest request = _getThemeArchiveRequestFixture.Create();
+
+ // Act
+ GetThemeArchiveQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(request.ThemeId, result.ThemeId);
+ }
+
+ [Theory]
+ [InlineData("default-theme")]
+ [InlineData("my-uploaded-theme")]
+ [InlineData("T-123")]
+ public void ToQuery_WhenMappingDifferentThemeIds_ShouldMapCorrectly(string themeId)
+ {
+ // Arrange
+ GetThemeArchiveRequest request = new(themeId);
+
+ // Act
+ GetThemeArchiveQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(themeId, result.ThemeId);
+ }
+
+ [Fact]
+ public void ToQuery_WhenThemeIdIsNull_ShouldMapNull()
+ {
+ // Arrange
+ GetThemeArchiveRequest request = new(null);
+
+ // Act
+ GetThemeArchiveQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Null(result.ThemeId);
+ }
+
+ [Fact]
+ public void ToQuery_WhenMappingMultipleRequests_ShouldMapAllCorrectly()
+ {
+ // Arrange
+ List requests = _getThemeArchiveRequestFixture.CreateMany();
+
+ // Act
+ List results = [.. requests.Select(r => r.ToQuery())];
+
+ // Assert
+ Assert.Equal(requests.Count, results.Count);
+ for (int i = 0; i < requests.Count; i++)
+ Assert.Equal(requests[i].ThemeId, results[i].ThemeId);
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeAssetRequestMappingTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeAssetRequestMappingTests.cs
new file mode 100644
index 00000000..d6775a90
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeAssetRequestMappingTests.cs
@@ -0,0 +1,86 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Mapping.Themes;
+using Lumina.Application.Core.Themes.Management.Queries.GetThemeAsset;
+using Lumina.Contracts.Fixtures.Core.Requests.Themes;
+using Lumina.Contracts.Requests.Themes;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+#endregion
+
+namespace Lumina.Application.UnitTests.Common.Mapping.Themes;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class GetThemeAssetRequestMappingTests
+{
+ private readonly GetThemeAssetRequestFixture _getThemeAssetRequestFixture = new();
+
+ [Fact]
+ public void ToQuery_WhenMappingValidRequest_ShouldMapCorrectly()
+ {
+ // Arrange
+ GetThemeAssetRequest request = _getThemeAssetRequestFixture.Create();
+
+ // Act
+ GetThemeAssetQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(request.ThemeId, result.ThemeId);
+ Assert.Equal(request.AssetPath, result.AssetPath);
+ }
+
+ [Theory]
+ [InlineData("default-theme", "assets/logo.png")]
+ [InlineData("my-theme", "css/main.css")]
+ [InlineData("T-123", "js/app.js")]
+ public void ToQuery_WhenMappingDifferentThemeIdsAndAssetPaths_ShouldMapCorrectly(string themeId, string assetPath)
+ {
+ // Arrange
+ GetThemeAssetRequest request = new(themeId, assetPath);
+
+ // Act
+ GetThemeAssetQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(themeId, result.ThemeId);
+ Assert.Equal(assetPath, result.AssetPath);
+ }
+
+ [Fact]
+ public void ToQuery_WhenFieldsAreNull_ShouldMapNull()
+ {
+ // Arrange
+ GetThemeAssetRequest request = new(null, null);
+
+ // Act
+ GetThemeAssetQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Null(result.ThemeId);
+ Assert.Null(result.AssetPath);
+ }
+
+ [Fact]
+ public void ToQuery_WhenMappingMultipleRequests_ShouldMapAllCorrectly()
+ {
+ // Arrange
+ List requests = _getThemeAssetRequestFixture.CreateMany();
+
+ // Act
+ List results = [.. requests.Select(r => r.ToQuery())];
+
+ // Assert
+ Assert.Equal(requests.Count, results.Count);
+ for (int i = 0; i < requests.Count; i++)
+ {
+ Assert.Equal(requests[i].ThemeId, results[i].ThemeId);
+ Assert.Equal(requests[i].AssetPath, results[i].AssetPath);
+ }
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeTemplateRequestMappingTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeTemplateRequestMappingTests.cs
new file mode 100644
index 00000000..ff5f7043
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/GetThemeTemplateRequestMappingTests.cs
@@ -0,0 +1,86 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Mapping.Themes;
+using Lumina.Application.Core.Themes.Management.Queries.GetThemeTemplate;
+using Lumina.Contracts.Fixtures.Core.Requests.Themes;
+using Lumina.Contracts.Requests.Themes;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+#endregion
+
+namespace Lumina.Application.UnitTests.Common.Mapping.Themes;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class GetThemeTemplateRequestMappingTests
+{
+ private readonly GetThemeTemplateRequestFixture _getThemeTemplateRequestFixture = new();
+
+ [Fact]
+ public void ToQuery_WhenMappingValidRequest_ShouldMapCorrectly()
+ {
+ // Arrange
+ GetThemeTemplateRequest request = _getThemeTemplateRequestFixture.Create();
+
+ // Act
+ GetThemeTemplateQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(request.ThemeId, result.ThemeId);
+ Assert.Equal(request.PageKey, result.PageKey);
+ }
+
+ [Theory]
+ [InlineData("default-theme", "home")]
+ [InlineData("my-theme", "library")]
+ [InlineData("T-123", "settings")]
+ public void ToQuery_WhenMappingDifferentThemeIdsAndPageKeys_ShouldMapCorrectly(string themeId, string pageKey)
+ {
+ // Arrange
+ GetThemeTemplateRequest request = new(themeId, pageKey);
+
+ // Act
+ GetThemeTemplateQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(themeId, result.ThemeId);
+ Assert.Equal(pageKey, result.PageKey);
+ }
+
+ [Fact]
+ public void ToQuery_WhenFieldsAreNull_ShouldMapNull()
+ {
+ // Arrange
+ GetThemeTemplateRequest request = new(null, null);
+
+ // Act
+ GetThemeTemplateQuery result = request.ToQuery();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Null(result.ThemeId);
+ Assert.Null(result.PageKey);
+ }
+
+ [Fact]
+ public void ToQuery_WhenMappingMultipleRequests_ShouldMapAllCorrectly()
+ {
+ // Arrange
+ List requests = _getThemeTemplateRequestFixture.CreateMany();
+
+ // Act
+ List results = [.. requests.Select(r => r.ToQuery())];
+
+ // Assert
+ Assert.Equal(requests.Count, results.Count);
+ for (int i = 0; i < requests.Count; i++)
+ {
+ Assert.Equal(requests[i].ThemeId, results[i].ThemeId);
+ Assert.Equal(requests[i].PageKey, results[i].PageKey);
+ }
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/SetCurrentThemeRequestMappingTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/SetCurrentThemeRequestMappingTests.cs
new file mode 100644
index 00000000..cdde81c5
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/SetCurrentThemeRequestMappingTests.cs
@@ -0,0 +1,80 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.Mapping.Themes;
+using Lumina.Application.Core.Themes.Management.Commands.SetCurrentTheme;
+using Lumina.Contracts.Fixtures.Core.Requests.Themes;
+using Lumina.Contracts.Requests.Themes;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+#endregion
+
+namespace Lumina.Application.UnitTests.Common.Mapping.Themes;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class SetCurrentThemeRequestMappingTests
+{
+ private readonly SetCurrentThemeRequestFixture _setCurrentThemeRequestFixture = new();
+
+ [Fact]
+ public void ToCommand_WhenMappingValidRequest_ShouldMapCorrectly()
+ {
+ // Arrange
+ SetCurrentThemeRequest request = _setCurrentThemeRequestFixture.Create();
+
+ // Act
+ SetCurrentThemeCommand result = request.ToCommand();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(request.ThemeId, result.ThemeId);
+ }
+
+ [Theory]
+ [InlineData("default-theme")]
+ [InlineData("my-uploaded-theme")]
+ [InlineData("T-123")]
+ public void ToCommand_WhenMappingDifferentThemeIds_ShouldMapCorrectly(string themeId)
+ {
+ // Arrange
+ SetCurrentThemeRequest request = new(themeId);
+
+ // Act
+ SetCurrentThemeCommand result = request.ToCommand();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(themeId, result.ThemeId);
+ }
+
+ [Fact]
+ public void ToCommand_WhenThemeIdIsNull_ShouldMapNull()
+ {
+ // Arrange
+ SetCurrentThemeRequest request = new(null);
+
+ // Act
+ SetCurrentThemeCommand result = request.ToCommand();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Null(result.ThemeId);
+ }
+
+ [Fact]
+ public void ToCommand_WhenMappingMultipleRequests_ShouldMapAllCorrectly()
+ {
+ // Arrange
+ List requests = _setCurrentThemeRequestFixture.CreateMany();
+
+ // Act
+ List results = [.. requests.Select(r => r.ToCommand())];
+
+ // Assert
+ Assert.Equal(requests.Count, results.Count);
+ for (int i = 0; i < requests.Count; i++)
+ Assert.Equal(requests[i].ThemeId, results[i].ThemeId);
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/ThemeEntityMappingTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/ThemeEntityMappingTests.cs
new file mode 100644
index 00000000..17f390ee
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Mapping/Themes/ThemeEntityMappingTests.cs
@@ -0,0 +1,93 @@
+#region ========================================================================= USING =====================================================================================
+using Lumina.Application.Common.DataAccess.Entities.Themes;
+using Lumina.Application.Common.Mapping.Themes;
+using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
+using Lumina.Contracts.Responses.Themes;
+using Lumina.Domain.SharedKernel.Common.Enums.Themes;
+using System;
+using System.Diagnostics.CodeAnalysis;
+#endregion
+
+namespace Lumina.Application.UnitTests.Common.Mapping.Themes;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class ThemeEntityMappingTests
+{
+ private readonly ThemeEntityFixture _themeEntityFixture = new();
+
+ [Fact]
+ public void ToResponse_WhenMappingValidThemeEntity_ShouldMapAllFields()
+ {
+ // Arrange
+ ThemeEntity theme = _themeEntityFixture.Create(
+ themeId: "my-theme",
+ name: "My Theme",
+ description: "A nice theme",
+ author: "Jane Doe",
+ version: "1.2.3",
+ previewPath: "preview.png",
+ installSource: ThemeInstallSource.Bundled,
+ isCurrent: true,
+ isDeleted: false);
+
+ // Act
+ ThemeResponse result = theme.ToResponse();
+
+ // Assert
+ Assert.NotNull(result);
+ Assert.Equal(theme.Id, result.Id);
+ Assert.Equal(theme.ThemeId, result.ThemeId);
+ Assert.Equal(theme.Name, result.Name);
+ Assert.Equal(theme.Description, result.Description);
+ Assert.Equal(theme.Author, result.Author);
+ Assert.Equal(theme.Version, result.Version);
+ Assert.Equal(theme.PreviewPath, result.PreviewPath);
+ Assert.Equal(theme.InstallSource, result.InstallSource);
+ Assert.Equal(theme.IsCurrent, result.IsCurrent);
+ Assert.Equal(theme.InstalledAtUtc, result.InstalledAtUtc);
+ }
+
+ [Fact]
+ public void ToResponse_WhenPreviewPathIsNull_ShouldMapNullPreviewPath()
+ {
+ // Arrange
+ ThemeEntity theme = _themeEntityFixture.Create();
+ theme.PreviewPath = null;
+
+ // Act
+ ThemeResponse result = theme.ToResponse();
+
+ // Assert
+ Assert.Null(result.PreviewPath);
+ }
+
+ [Fact]
+ public void ToResponse_WhenIsCurrentIsNull_ShouldMapNullIsCurrent()
+ {
+ // Arrange
+ ThemeEntity theme = _themeEntityFixture.Create();
+ theme.IsCurrent = null;
+
+ // Act
+ ThemeResponse result = theme.ToResponse();
+
+ // Assert
+ Assert.Null(result.IsCurrent);
+ }
+
+ [Fact]
+ public void ToResponse_WhenMappingUploadedTheme_ShouldMapInstallSourceAsUploaded()
+ {
+ // Arrange
+ ThemeEntity theme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Uploaded, isCurrent: null);
+
+ // Act
+ ThemeResponse result = theme.ToResponse();
+
+ // Assert
+ Assert.Equal(ThemeInstallSource.Uploaded, result.InstallSource);
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/DeleteTheme/DeleteThemeCommandHandlerTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/DeleteTheme/DeleteThemeCommandHandlerTests.cs
new file mode 100644
index 00000000..f0d763af
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/DeleteTheme/DeleteThemeCommandHandlerTests.cs
@@ -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;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class DeleteThemeCommandHandlerTests
+{
+ private readonly IUnitOfWork _mockUnitOfWork;
+ private readonly IThemeService _mockThemeService;
+ private readonly ICurrentUserService _mockCurrentUserService;
+ private readonly IAuthorizationService _mockAuthorizationService;
+ private readonly IValidator _mockValidator;
+ private readonly IThemeRepository _mockThemeRepository;
+ private readonly DeleteThemeCommandHandler _sut;
+ private readonly DeleteThemeCommandFixture _deleteThemeCommandFixture = new();
+ private readonly ThemeEntityFixture _themeEntityFixture = new();
+ private readonly Guid _userId;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public DeleteThemeCommandHandlerTests()
+ {
+ _mockUnitOfWork = Substitute.For();
+ _mockThemeService = Substitute.For();
+ _mockCurrentUserService = Substitute.For();
+ _mockAuthorizationService = Substitute.For();
+ _mockValidator = Substitute.For>();
+ _mockThemeRepository = Substitute.For();
+ _userId = Guid.NewGuid();
+
+ _mockValidator.Validate(Arg.Any())
+ .Returns([]);
+ _mockCurrentUserService.UserId.Returns(_userId);
+ _mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any())
+ .Returns(true);
+ _mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
+ _mockThemeService.DeleteAsync(Arg.Any(), Arg.Any())
+ .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())
+ .Returns([Errors.Themes.ThemeIdCannotBeEmpty]);
+
+ // Act
+ Result 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(), Arg.Any());
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenUserIsNotAuthenticated_ShouldReturnNotAuthorizedError()
+ {
+ // Arrange
+ DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
+ _mockCurrentUserService.UserId.Returns((Guid?)null);
+
+ // Act
+ Result 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(), Arg.Any(), Arg.Any());
+ await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any(), Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenUserIsNotAdmin_ShouldReturnNotAuthorizedError()
+ {
+ // Arrange
+ DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
+ _mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any())
+ .Returns(false);
+
+ // Act
+ Result 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(), Arg.Any());
+ }
+
+ [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())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenThemeDoesNotExist_ShouldReturnThemeNotFoundError()
+ {
+ // Arrange
+ DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
+ _mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any())
+ .Returns(Result.From(null));
+
+ // Act
+ Result 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());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+
+ // Act
+ Result 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());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme, userTheme]));
+
+ // Act
+ Result 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(), Arg.Any());
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ await _mockThemeService.DidNotReceive().DeleteAsync(Arg.Any(), Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme]));
+
+ // Act
+ Result 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(), Arg.Any());
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ await _mockThemeService.DidNotReceive().DeleteAsync(Arg.Any(), Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme, otherBundledTheme]));
+ _mockThemeRepository.UpdateAsync(Arg.Any(), Arg.Any())
+ .Returns(Result.Updated);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.False(result.IsFailure);
+ await _mockThemeRepository.Received(1).UpdateAsync(
+ Arg.Is(entity => entity.Id == theme.Id && entity.IsDeleted && entity.IsCurrent == null && entity.UpdatedBy == _userId),
+ Arg.Any());
+ await _mockThemeRepository.DidNotReceive().DeleteByIdAsync(Arg.Any(), Arg.Any());
+ await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any());
+ await _mockThemeService.Received(1).DeleteAsync(theme.ThemeId, Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme, bundledTheme]));
+ _mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any())
+ .Returns(Result.Deleted);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.False(result.IsFailure);
+ await _mockThemeRepository.Received(1).DeleteByIdAsync(theme.Id, Arg.Any());
+ await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any(), Arg.Any());
+ await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any());
+ await _mockThemeService.Received(1).DeleteAsync(theme.ThemeId, Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme, bundledTheme]));
+ _mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ await _mockThemeService.DidNotReceive().DeleteAsync(Arg.Any(), Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme, replacementTheme]));
+ _mockThemeRepository.UpdateAsync(Arg.Any(), Arg.Any())
+ .Returns(Result.Updated);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.False(result.IsFailure);
+ await _mockThemeRepository.Received(1).UpdateAsync(
+ Arg.Is(entity => entity.Id == replacementTheme.Id && entity.IsCurrent == true),
+ Arg.Any());
+ await _mockThemeRepository.Received(1).UpdateAsync(
+ Arg.Is(entity => entity.Id == theme.Id && entity.IsDeleted && entity.IsCurrent == null),
+ Arg.Any());
+ await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any());
+ await _mockThemeService.Received(1).DeleteAsync(theme.ThemeId, Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme, betaTheme, alphaTheme]));
+ _mockThemeRepository.UpdateAsync(Arg.Any(), Arg.Any())
+ .Returns(Result.Updated);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.False(result.IsFailure);
+ await _mockThemeRepository.Received(1).UpdateAsync(
+ Arg.Is(entity => entity.Id == alphaTheme.Id && entity.IsCurrent == true),
+ Arg.Any());
+ await _mockThemeRepository.Received(1).UpdateAsync(
+ Arg.Is(entity => entity.Id == theme.Id && entity.IsDeleted),
+ Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+ _mockThemeRepository.GetAllAsync(Arg.Any())
+ .Returns(Result.From>([theme, bundledTheme]));
+ _mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any())
+ .Returns(Result.Deleted);
+ _mockThemeService.DeleteAsync(theme.ThemeId, Arg.Any())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any());
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/DeleteTheme/DeleteThemeCommandValidatorTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/DeleteTheme/DeleteThemeCommandValidatorTests.cs
new file mode 100644
index 00000000..533e428b
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/DeleteTheme/DeleteThemeCommandValidatorTests.cs
@@ -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;
+
+///
+/// Contains unit tests for the class.
+///
+[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 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 result = _validator.TestValidate(command);
+
+ // Assert
+ result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
+ }
+
+ [Fact]
+ public void Validate_WhenThemeIdIsWhiteSpace_ShouldHaveValidationError()
+ {
+ // Arrange
+ DeleteThemeCommand command = _deleteThemeCommandFixture.Create(themeId: " ");
+
+ // Act
+ List result = _validator.TestValidate(command);
+
+ // Assert
+ result.ShouldHaveValidationError(Errors.Themes.ThemeIdCannotBeEmpty);
+ }
+
+ [Fact]
+ public void Validate_WhenThemeIdIsValid_ShouldNotHaveAnyValidationErrors()
+ {
+ // Arrange
+ DeleteThemeCommand command = _deleteThemeCommandFixture.Create();
+
+ // Act
+ List result = _validator.TestValidate(command);
+
+ // Assert
+ result.ShouldNotHaveAnyValidationErrors();
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandHandlerTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandHandlerTests.cs
new file mode 100644
index 00000000..833c6414
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandHandlerTests.cs
@@ -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;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class InstallThemeCommandHandlerTests
+{
+ private readonly IUnitOfWork _mockUnitOfWork;
+ private readonly IThemeService _mockThemeService;
+ private readonly ICurrentUserService _mockCurrentUserService;
+ private readonly IAuthorizationService _mockAuthorizationService;
+ private readonly IValidator _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;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public InstallThemeCommandHandlerTests()
+ {
+ _mockUnitOfWork = Substitute.For();
+ _mockThemeService = Substitute.For();
+ _mockCurrentUserService = Substitute.For();
+ _mockAuthorizationService = Substitute.For();
+ _mockValidator = Substitute.For>();
+ _mockThemeRepository = Substitute.For();
+ _userId = Guid.NewGuid();
+
+ _mockValidator.Validate(Arg.Any())
+ .Returns([]);
+ _mockCurrentUserService.UserId.Returns(_userId);
+ _mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any())
+ .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())
+ .Returns([Errors.Themes.ThemeArchiveCannotBeNull]);
+
+ // Act
+ Result 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(), Arg.Any());
+ await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any(), Arg.Any(), Arg.Any());
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenUserIsNotAuthenticated_ShouldReturnNotAuthorizedError()
+ {
+ // Arrange
+ InstallThemeCommand command = _installThemeCommandFixture.Create();
+ _mockCurrentUserService.UserId.Returns((Guid?)null);
+
+ // Act
+ Result 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(), Arg.Any(), Arg.Any());
+ await _mockThemeService.DidNotReceive().InstallAsync(Arg.Any(), Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenUserIsNotAdmin_ShouldReturnNotAuthorizedError()
+ {
+ // Arrange
+ InstallThemeCommand command = _installThemeCommandFixture.Create();
+ _mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any())
+ .Returns(false);
+
+ // Act
+ Result 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(), Arg.Any());
+ }
+
+ [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())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any(), Arg.Any());
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(manifest));
+ _mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenThemeDoesNotExist_ShouldInsertNewUploadedTheme()
+ {
+ // Arrange
+ InstallThemeCommand command = _installThemeCommandFixture.Create();
+ ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
+ _mockThemeService.InstallAsync(command.Archive!, Arg.Any())
+ .Returns(Result.From(manifest));
+ _mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any())
+ .Returns(Result.From(null));
+ _mockThemeRepository.InsertAsync(Arg.Any(), Arg.Any())
+ .Returns(Result.Created);
+
+ // Act
+ Result 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(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());
+ await _mockThemeRepository.DidNotReceive().UpdateAsync(Arg.Any(), Arg.Any());
+ await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(manifest));
+ _mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any())
+ .Returns(Result.From(null));
+ _mockThemeRepository.InsertAsync(Arg.Any(), Arg.Any())
+ .Returns(error);
+
+ // Act
+ Result 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());
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(manifest));
+ _mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any())
+ .Returns(Result.From(existingTheme));
+ _mockThemeRepository.UpdateAsync(Arg.Any(), Arg.Any())
+ .Returns(Result.Updated);
+
+ // Act
+ Result 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(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());
+ await _mockThemeRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any());
+ await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any());
+ }
+
+ [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())
+ .Returns(Result.From(manifest));
+ _mockThemeRepository.GetByThemeIdAsync(manifest.Id, Arg.Any())
+ .Returns(Result.From(existingTheme));
+ _mockThemeRepository.UpdateAsync(Arg.Any(), Arg.Any())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandValidatorTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandValidatorTests.cs
new file mode 100644
index 00000000..7ccea36e
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/InstallTheme/InstallThemeCommandValidatorTests.cs
@@ -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;
+
+///
+/// Contains unit tests for the class.
+///
+[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 result = _validator.TestValidate(command);
+
+ // Assert
+ result.ShouldHaveValidationError(Errors.Themes.ThemeArchiveCannotBeNull);
+ }
+
+ [Fact]
+ public void Validate_WhenArchiveIsProvided_ShouldNotHaveValidationError()
+ {
+ // Arrange
+ InstallThemeCommand command = _installThemeCommandFixture.Create();
+
+ // Act
+ List 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 result = _validator.TestValidate(command);
+
+ // Assert
+ result.ShouldNotHaveAnyValidationErrors();
+ }
+}
diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/SetCurrentTheme/SetCurrentThemeCommandHandlerTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/SetCurrentTheme/SetCurrentThemeCommandHandlerTests.cs
new file mode 100644
index 00000000..f63854be
--- /dev/null
+++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/Themes/Management/Commands/SetCurrentTheme/SetCurrentThemeCommandHandlerTests.cs
@@ -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;
+
+///
+/// Contains unit tests for the class.
+///
+[ExcludeFromCodeCoverage]
+public class SetCurrentThemeCommandHandlerTests
+{
+ private readonly IUnitOfWork _mockUnitOfWork;
+ private readonly ICurrentUserService _mockCurrentUserService;
+ private readonly IAuthorizationService _mockAuthorizationService;
+ private readonly IValidator _mockValidator;
+ private readonly IThemeRepository _mockThemeRepository;
+ private readonly SetCurrentThemeCommandHandler _sut;
+ private readonly SetCurrentThemeCommandFixture _setCurrentThemeCommandFixture = new();
+ private readonly ThemeEntityFixture _themeEntityFixture = new();
+ private readonly Guid _userId;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public SetCurrentThemeCommandHandlerTests()
+ {
+ _mockUnitOfWork = Substitute.For();
+ _mockCurrentUserService = Substitute.For();
+ _mockAuthorizationService = Substitute.For();
+ _mockValidator = Substitute.For>();
+ _mockThemeRepository = Substitute.For();
+ _userId = Guid.NewGuid();
+
+ _mockValidator.Validate(Arg.Any())
+ .Returns([]);
+ _mockCurrentUserService.UserId.Returns(_userId);
+ _mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any())
+ .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())
+ .Returns([Errors.Themes.ThemeIdCannotBeEmpty]);
+
+ // Act
+ Result 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(), Arg.Any());
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenUserIsNotAuthenticated_ShouldReturnNotAuthorizedError()
+ {
+ // Arrange
+ SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
+ _mockCurrentUserService.UserId.Returns((Guid?)null);
+
+ // Act
+ Result 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(), Arg.Any(), Arg.Any());
+ await _mockThemeRepository.DidNotReceive().GetByThemeIdAsync(Arg.Any(), Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenUserIsNotAdmin_ShouldReturnNotAuthorizedError()
+ {
+ // Arrange
+ SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
+ _mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any())
+ .Returns(false);
+
+ // Act
+ Result 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(), Arg.Any());
+ }
+
+ [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())
+ .Returns(error);
+
+ // Act
+ Result result = await _sut.HandleAsync(command, CancellationToken.None);
+
+ // Assert
+ Assert.True(result.IsFailure);
+ Assert.Equal(error, result.FirstError);
+ await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
+ }
+
+ [Fact]
+ public async Task HandleAsync_WhenThemeDoesNotExist_ShouldReturnThemeNotFoundError()
+ {
+ // Arrange
+ SetCurrentThemeCommand command = _setCurrentThemeCommandFixture.Create();
+ _mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any())
+ .Returns(Result.From(null));
+
+ // Act
+ Result 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());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+
+ // Act
+ Result 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());
+ }
+
+ [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())
+ .Returns(Result.From(theme));
+
+ // Act
+ Result 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