Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

71 lines
2.4 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using FakeItEasy;
using Squidex.Domain.Apps.Entities;
using Squidex.Domain.Apps.Entities.Contents.Commands;
using Squidex.Domain.Apps.Entities.TestHelpers;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands;
using Xunit;
namespace Squidex.Web.CommandMiddlewares
{
public class EnrichWithAppIdCommandMiddlewareTests
{
private readonly IContextProvider contextProvider = A.Fake<IContextProvider>();
private readonly NamedId<DomainId> appId = NamedId.Of(DomainId.NewGuid(), "my-app");
private readonly Context requestContext;
private readonly EnrichWithAppIdCommandMiddleware sut;
public EnrichWithAppIdCommandMiddlewareTests()
{
requestContext = Context.Anonymous(Mocks.App(appId));
A.CallTo(() => contextProvider.Context)
.Returns(requestContext);
sut = new EnrichWithAppIdCommandMiddleware(contextProvider);
}
[Fact]
public async Task Should_throw_exception_if_app_not_found()
{
A.CallTo(() => contextProvider.Context)
.Returns(Context.Anonymous(null!));
await Assert.ThrowsAsync<InvalidOperationException>(() => HandleAsync(new CreateContent()));
}
[Fact]
public async Task Should_assign_app_id_and_name_to_app_command()
{
var context = await HandleAsync(new CreateContent());
Assert.Equal(appId, ((IAppCommand)context.Command).AppId);
}
[Fact]
public async Task Should_not_override_app_id_and_name()
{
var customId = NamedId.Of(DomainId.NewGuid(), "other-app");
var context = await HandleAsync(new CreateContent { AppId = customId });
Assert.Equal(customId, ((IAppCommand)context.Command).AppId);
}
private async Task<CommandContext> HandleAsync(ICommand command)
{
var commandContext = new CommandContext(command, A.Fake<ICommandBus>());
await sut.HandleAsync(commandContext);
return commandContext;
}
}
}