// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using FakeItEasy; using Squidex.Assets; using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Domain.Apps.Entities.TestHelpers; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Validation; using Xunit; namespace Squidex.Domain.Apps.Entities.Apps.DomainObject { public class AppCommandMiddlewareTests : HandlerTestBase { private readonly IDomainObjectFactory domainObjectFactory = A.Fake(); private readonly IContextProvider contextProvider = A.Fake(); private readonly IAppImageStore appImageStore = A.Fake(); private readonly IAssetThumbnailGenerator assetThumbnailGenerator = A.Fake(); private readonly NamedId appId = NamedId.Of(DomainId.NewGuid(), "my-app"); private readonly Context requestContext; private readonly AppCommandMiddleware sut; public sealed class MyCommand : SquidexCommand { } protected override DomainId Id { get => appId.Id; } public AppCommandMiddlewareTests() { requestContext = Context.Anonymous(Mocks.App(appId)); A.CallTo(() => contextProvider.Context) .Returns(requestContext); sut = new AppCommandMiddleware(domainObjectFactory, appImageStore, assetThumbnailGenerator, contextProvider); } [Fact] public async Task Should_replace_context_app_with_domain_object_actual() { var actual = A.Fake(); await HandleAsync(new UpdateApp(), actual); Assert.Same(actual, requestContext.App); } [Fact] public async Task Should_upload_image_to_store() { var file = new NoopAssetFile(); A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(A._, file.MimeType, default)) .Returns(new ImageInfo(100, 100, ImageOrientation.None, ImageFormat.PNG)); await HandleAsync(new UploadAppImage { File = file }, None.Value); A.CallTo(() => appImageStore.UploadAsync(appId.Id, A._, A._)) .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(() => assetThumbnailGenerator.GetImageInfoAsync(A._, file.MimeType, default)) .Returns(Task.FromResult(null)); await Assert.ThrowsAsync(() => HandleAsync(sut, command)); } 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); } } }