mirror of https://github.com/Squidex/squidex.git
7 changed files with 134 additions and 6 deletions
@ -0,0 +1,35 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
namespace Squidex.Infrastructure.Commands |
|||
{ |
|||
public sealed class ReadonlyCommandMiddleware : ICommandMiddleware |
|||
{ |
|||
private readonly IOptions<ReadonlyOptions> options; |
|||
|
|||
public ReadonlyCommandMiddleware(IOptions<ReadonlyOptions> options) |
|||
{ |
|||
Guard.NotNull(options, nameof(options)); |
|||
|
|||
this.options = options; |
|||
} |
|||
|
|||
public Task HandleAsync(CommandContext context, Func<Task> next) |
|||
{ |
|||
if (options.Value.IsReadonly) |
|||
{ |
|||
throw new DomainException("Application is in readonly mode at the moment."); |
|||
} |
|||
|
|||
return next(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
namespace Squidex.Infrastructure.Commands |
|||
{ |
|||
public sealed class ReadonlyOptions |
|||
{ |
|||
public bool IsReadonly { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,62 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Threading.Tasks; |
|||
using FakeItEasy; |
|||
using Microsoft.Extensions.Options; |
|||
using Squidex.Infrastructure.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Infrastructure.Commands |
|||
{ |
|||
public class ReadonlyCommandMiddlewareTests |
|||
{ |
|||
private readonly ICommand command = A.Dummy<ICommand>(); |
|||
private readonly ICommandBus commandBus = A.Dummy<ICommandBus>(); |
|||
private readonly ReadonlyOptions options = new ReadonlyOptions(); |
|||
private readonly ReadonlyCommandMiddleware sut; |
|||
|
|||
public ReadonlyCommandMiddlewareTests() |
|||
{ |
|||
sut = new ReadonlyCommandMiddleware(Options.Create(options)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_throw_exception_if_readonly() |
|||
{ |
|||
var context = new CommandContext(command, commandBus); |
|||
|
|||
options.IsReadonly = true; |
|||
|
|||
await Assert.ThrowsAsync<DomainException>(() => MakeCallAsync(context)); |
|||
|
|||
Assert.False(context.IsCompleted); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_not_throw_exception_if_not_readonly() |
|||
{ |
|||
var context = new CommandContext(command, commandBus); |
|||
|
|||
options.IsReadonly = false; |
|||
|
|||
await MakeCallAsync(context); |
|||
|
|||
Assert.True(context.IsCompleted); |
|||
} |
|||
|
|||
private async Task MakeCallAsync(CommandContext context) |
|||
{ |
|||
await sut.HandleAsync(context, () => |
|||
{ |
|||
context.Complete(true); |
|||
|
|||
return TaskHelper.Done; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue