// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using Squidex.Assets; using Squidex.Domain.Apps.Core.Apps; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Domain.Apps.Entities.TestHelpers; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Validation; namespace Squidex.Domain.Apps.Entities.Apps.DomainObject; public class AppCommandMiddlewareTests : HandlerTestBase { private readonly IDomainObjectFactory domainObjectFactory = A.Fake(); private readonly IAppImageStore appImageStore = A.Fake(); private readonly IAssetThumbnailGenerator assetGenerator = A.Fake(); private readonly AppCommandMiddleware sut; public sealed class MyCommand : SquidexCommand { } protected override DomainId Id { get => AppId.Id; } public AppCommandMiddlewareTests() { sut = new AppCommandMiddleware(domainObjectFactory, appImageStore, assetGenerator, ApiContextProvider); } [Fact] public async Task Should_replace_context_app_with_domain_object_result() { var replaced = new App(); await HandleAsync(new UpdateApp(), replaced); Assert.Same(replaced, ApiContext.App); } [Fact] public async Task Should_upload_image_to_store() { var file = new NoopAssetFile(); A.CallTo(() => assetGenerator.GetImageInfoAsync(A._, file.MimeType, CancellationToken)) .Returns(new ImageInfo(ImageFormat.PNG, 100, 100, ImageOrientation.None, false)); await HandleAsync(new UploadAppImage { File = file }, None.Value); A.CallTo(() => appImageStore.UploadAsync(AppId.Id, A._, CancellationToken)) .MustHaveHappened(); } [Fact] public async Task Should_throw_exception_if_file_to_upload_is_not_an_image() { var file = new NoopAssetFile(); var command = new UploadAppImage { File = file }; A.CallTo(() => assetGenerator.GetImageInfoAsync(A._, file.MimeType, CancellationToken)) .Returns(Task.FromResult(null)); await Assert.ThrowsAsync(() => HandleAsync(sut, command, CancellationToken)); } private Task HandleAsync(AppCommand command, object actual) { command.AppId = AppId; var domainObject = A.Fake(); A.CallTo(() => domainObject.ExecuteAsync(A._, A._)) .Returns(new CommandResult(command.AggregateId, 1, 0, actual)); A.CallTo(() => domainObjectFactory.Create(command.AggregateId)) .Returns(domainObject); return HandleAsync(sut, command, CancellationToken); } }