|
|
|
@ -27,51 +27,71 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
private readonly ClaimsPrincipal user = new ClaimsPrincipal(); |
|
|
|
private readonly Instant dueTimeInPast = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromHours(1)); |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanCreate_should_throw_exception_if_data_is_null() |
|
|
|
public GuardContentTests() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task CanCreate_should_throw_exception_if_data_is_null() |
|
|
|
{ |
|
|
|
var command = new CreateContent(); |
|
|
|
|
|
|
|
ValidationAssert.Throws(() => GuardContent.CanCreate(schema, command), |
|
|
|
await ValidationAssert.ThrowsAsync(() => GuardContent.CanCreate(schema, contentWorkflow, command), |
|
|
|
new ValidationError("Data is required.", "Data")); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanCreate_should_throw_exception_if_singleton() |
|
|
|
public async Task CanCreate_should_throw_exception_if_singleton() |
|
|
|
{ |
|
|
|
SetupSingleton(true); |
|
|
|
|
|
|
|
var command = new CreateContent { Data = new NamedContentData() }; |
|
|
|
|
|
|
|
Assert.Throws<DomainException>(() => GuardContent.CanCreate(schema, command)); |
|
|
|
await Assert.ThrowsAsync<DomainException>(() => GuardContent.CanCreate(schema, contentWorkflow, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanCreate_should_not_throw_exception_if_singleton_and_id_is_schema_id() |
|
|
|
public async Task CanCreate_should_not_throw_exception_if_singleton_and_id_is_schema_id() |
|
|
|
{ |
|
|
|
SetupSingleton(true); |
|
|
|
|
|
|
|
var command = new CreateContent { Data = new NamedContentData(), ContentId = schema.Id }; |
|
|
|
|
|
|
|
GuardContent.CanCreate(schema, command); |
|
|
|
await GuardContent.CanCreate(schema, contentWorkflow, command); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void CanCreate_should_not_throw_exception_if_data_is_not_null() |
|
|
|
public async Task CanCreate_should_throw_exception_publish_not_allowed() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
SetupCanCreatePublish(false); |
|
|
|
|
|
|
|
var command = new CreateContent { Data = new NamedContentData(), Publish = true }; |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<DomainException>(() => GuardContent.CanCreate(schema, contentWorkflow, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task CanCreate_should_not_throw_exception_publishing_allowed() |
|
|
|
{ |
|
|
|
SetupCanCreatePublish(true); |
|
|
|
|
|
|
|
var command = new CreateContent { Data = new NamedContentData(), Publish = true }; |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<DomainException>(() => GuardContent.CanCreate(schema, contentWorkflow, command)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task CanCreate_should_not_throw_exception_if_data_is_not_null() |
|
|
|
{ |
|
|
|
var command = new CreateContent { Data = new NamedContentData() }; |
|
|
|
|
|
|
|
GuardContent.CanCreate(schema, command); |
|
|
|
await GuardContent.CanCreate(schema, contentWorkflow, command); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task CanUpdate_should_throw_exception_if_data_is_null() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
SetupCanUpdate(true); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
@ -84,7 +104,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanUpdate_should_throw_exception_if_workflow_blocks_it() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
SetupCanUpdate(false); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
@ -96,7 +115,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanUpdate_should_not_throw_exception_if_data_is_not_null() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
SetupCanUpdate(true); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
@ -108,7 +126,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanPatch_should_throw_exception_if_data_is_null() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
SetupCanUpdate(true); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
@ -121,7 +138,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanPatch_should_throw_exception_if_workflow_blocks_it() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
SetupCanUpdate(false); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
@ -133,7 +149,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanPatch_should_not_throw_exception_if_data_is_not_null() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
SetupCanUpdate(true); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
@ -145,8 +160,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanChangeStatus_should_throw_exception_if_publishing_without_pending_changes() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Published, false); |
|
|
|
var command = new ChangeContentStatus { Status = Status.Published }; |
|
|
|
|
|
|
|
@ -179,8 +192,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanChangeStatus_should_throw_exception_if_due_date_in_past() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
var command = new ChangeContentStatus { Status = Status.Published, DueTime = dueTimeInPast, User = user }; |
|
|
|
|
|
|
|
@ -194,8 +205,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanChangeStatus_should_throw_exception_if_status_flow_not_valid() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
var command = new ChangeContentStatus { Status = Status.Published, User = user }; |
|
|
|
|
|
|
|
@ -209,8 +218,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public async Task CanChangeStatus_should_not_throw_exception_if_status_flow_valid() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
|
|
|
|
var content = CreateContent(Status.Draft, false); |
|
|
|
var command = new ChangeContentStatus { Status = Status.Published, User = user }; |
|
|
|
|
|
|
|
@ -231,8 +238,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public void CanDiscardChanges_should_not_throw_exception_if_pending() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
|
|
|
|
var command = new DiscardChanges(); |
|
|
|
|
|
|
|
GuardContent.CanDiscardChanges(true, command); |
|
|
|
@ -251,8 +256,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
[Fact] |
|
|
|
public void CanDelete_should_not_throw_exception() |
|
|
|
{ |
|
|
|
SetupSingleton(false); |
|
|
|
|
|
|
|
var command = new DeleteContent(); |
|
|
|
|
|
|
|
GuardContent.CanDelete(schema, command); |
|
|
|
@ -264,6 +267,12 @@ namespace Squidex.Domain.Apps.Entities.Contents.Guard |
|
|
|
.Returns(canUpdate); |
|
|
|
} |
|
|
|
|
|
|
|
private void SetupCanCreatePublish(bool canCreate) |
|
|
|
{ |
|
|
|
A.CallTo(() => contentWorkflow.CanPublishOnCreateAsync(schema, A<NamedContentData>.Ignored, user)) |
|
|
|
.Returns(canCreate); |
|
|
|
} |
|
|
|
|
|
|
|
private void SetupSingleton(bool isSingleton) |
|
|
|
{ |
|
|
|
A.CallTo(() => schema.SchemaDef) |
|
|
|
|