diff --git a/src/Squidex.Write/Schemas/SchemaCommandHandler.cs b/src/Squidex.Write/Schemas/SchemaCommandHandler.cs index 396c673ea..b80e61281 100644 --- a/src/Squidex.Write/Schemas/SchemaCommandHandler.cs +++ b/src/Squidex.Write/Schemas/SchemaCommandHandler.cs @@ -21,15 +21,15 @@ namespace Squidex.Write.Schemas private readonly ISchemaProvider schemaProvider; public SchemaCommandHandler( - ISchemaProvider schemaProvider, IDomainObjectFactory domainObjectFactory, - IDomainObjectRepository domainObjectRepository) + IDomainObjectRepository domainObjectRepository, + ISchemaProvider schemaProvider) : base(domainObjectFactory, domainObjectRepository) { this.schemaProvider = schemaProvider; } - public async Task On(CreateSchema command, CommandContext context) + protected async Task On(CreateSchema command, CommandContext context) { if (await schemaProvider.FindSchemaIdByNameAsync(command.AppId, command.Name) != null) { @@ -37,10 +37,15 @@ namespace Squidex.Write.Schemas throw new ValidationException("Cannot create a new schema", error); } - await CreateAsync(command, s => s.Create(command)); + await CreateAsync(command, s => + { + s.Create(command); + + context.Succeed(command.Name); + }); } - public Task On(AddField command, CommandContext context) + protected Task On(AddField command, CommandContext context) { return UpdateAsync(command, s => { @@ -50,42 +55,42 @@ namespace Squidex.Write.Schemas }); } - public Task On(DeleteSchema command, CommandContext context) + protected Task On(DeleteSchema command, CommandContext context) { return UpdateAsync(command, s => s.Delete(command)); } - public Task On(DeleteField command, CommandContext context) + protected Task On(DeleteField command, CommandContext context) { return UpdateAsync(command, s => s.DeleteField(command)); } - public Task On(DisableField command, CommandContext context) + protected Task On(DisableField command, CommandContext context) { return UpdateAsync(command, s => s.DisableField(command)); } - public Task On(EnableField command, CommandContext context) + protected Task On(EnableField command, CommandContext context) { return UpdateAsync(command, s => s.EnableField(command)); } - public Task On(HideField command, CommandContext context) + protected Task On(HideField command, CommandContext context) { return UpdateAsync(command, s => s.HideField(command)); } - public Task On(ShowField command, CommandContext context) + protected Task On(ShowField command, CommandContext context) { return UpdateAsync(command, s => s.ShowField(command)); } - public Task On(UpdateSchema command, CommandContext context) + protected Task On(UpdateSchema command, CommandContext context) { return UpdateAsync(command, s => s.Update(command)); } - public Task On(UpdateField command) + protected Task On(UpdateField command, CommandContext context) { return UpdateAsync(command, s => { s.UpdateField(command); }); } diff --git a/tests/Postman-Collection.json b/tests/Postman-Collection.json deleted file mode 100644 index 5aa8d36a1..000000000 --- a/tests/Postman-Collection.json +++ /dev/null @@ -1,145 +0,0 @@ -{ - "variables": [], - "info": { - "name": "Squidex", - "_postman_id": "3534653d-3285-a837-6257-a9454368e761", - "description": "", - "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" - }, - "item": [ - { - "name": "Apps", - "description": "", - "item": [ - { - "name": "{{url}}/api/apps", - "request": { - "url": "{{url}}/api/apps", - "method": "POST", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "description": "" - } - ], - "body": { - "mode": "raw", - "raw": "{\n\t\"name\": \"my-app\"\n}" - }, - "description": "" - }, - "response": [] - }, - { - "name": "{{url}}/api/apps", - "request": { - "url": "{{url}}/api/apps", - "method": "GET", - "header": [], - "body": { - "mode": "formdata", - "formdata": [] - }, - "description": "" - }, - "response": [] - } - ] - }, - { - "name": "Schemas", - "description": "", - "item": [ - { - "name": "{{url}}/api/schemas", - "request": { - "url": "{{url}}/api/schemas", - "method": "GET", - "header": [], - "body": { - "mode": "formdata", - "formdata": [] - }, - "description": "Get schemas" - }, - "response": [] - }, - { - "name": "{{url}}/api/schemas/users", - "request": { - "url": "{{url}}/api/schemas/users", - "method": "GET", - "header": [], - "body": { - "mode": "formdata", - "formdata": [] - }, - "description": "Get schema" - }, - "response": [] - }, - { - "name": "{{url}}/api/schemas/users", - "request": { - "url": "{{url}}/api/schemas/users", - "method": "PUT", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "description": "" - } - ], - "body": { - "mode": "raw", - "raw": "{\n \"label\": \"Users\",\n \"hints\": \"All users in our system\"\n}" - }, - "description": "Update user" - }, - "response": [] - }, - { - "name": "{{url}}/api/schemas", - "request": { - "url": "{{url}}/api/schemas", - "method": "POST", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "description": "" - } - ], - "body": { - "mode": "raw", - "raw": "{\n \"name\": \"users\"\n}" - }, - "description": "Create schema" - }, - "response": [] - }, - { - "name": "{{url}}/api/schemas/users/fields", - "request": { - "url": "{{url}}/api/schemas/users/fields", - "method": "POST", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "description": "" - } - ], - "body": { - "mode": "raw", - "raw": "{\n \"type\": \"number\",\n \"name\": \"age\",\n \"properties\": {\n \"minValue\": 1\n }\n}" - }, - "description": "Creates a field" - }, - "response": [] - } - ] - } - ] -} \ No newline at end of file diff --git a/tests/Squidex.Write.Tests/Apps/AppCommandHandlerTests.cs b/tests/Squidex.Write.Tests/Apps/AppCommandHandlerTests.cs index 1a6b39952..5bf908088 100644 --- a/tests/Squidex.Write.Tests/Apps/AppCommandHandlerTests.cs +++ b/tests/Squidex.Write.Tests/Apps/AppCommandHandlerTests.cs @@ -113,6 +113,22 @@ namespace Squidex.Write.Tests.Apps }, false); } + [Fact] + public async Task AssignContributor_should_throw_if_null_user_not_found() + { + CreateApp(); + + var command = new AssignContributor { AggregateId = Id, ContributorId = null }; + var context = new CommandContext(command); + + userRepository.Setup(x => x.FindUserByIdAsync(command.ContributorId)).Returns(Task.FromResult(null)); + + await TestUpdate(app, async _ => + { + await Assert.ThrowsAsync(() => sut.HandleAsync(context)); + }, false); + } + [Fact] public async Task AssignContributor_should_assign_if_user_found() { diff --git a/tests/Squidex.Write.Tests/Schemas/SchemaCommandHandlerTests.cs b/tests/Squidex.Write.Tests/Schemas/SchemaCommandHandlerTests.cs new file mode 100644 index 000000000..99e7de6a6 --- /dev/null +++ b/tests/Squidex.Write.Tests/Schemas/SchemaCommandHandlerTests.cs @@ -0,0 +1,226 @@ +// ========================================================================== +// SchemaCommandHandlerTests.cs +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex Group +// All rights reserved. +// ========================================================================== + +using System; +using System.Reflection; +using System.Threading.Tasks; +using Moq; +using Squidex.Core.Schemas; +using Squidex.Infrastructure; +using Squidex.Infrastructure.CQRS.Commands; +using Squidex.Read.Schemas.Services; +using Squidex.Write.Schemas; +using Squidex.Write.Schemas.Commands; +using Squidex.Write.Tests.Utils; +using Xunit; +// ReSharper disable ConvertToConstant.Local + +namespace Squidex.Write.Tests.Schemas +{ + public class SchemaCommandHandlerTests : HandlerTestBase + { + private readonly Mock schemaProvider = new Mock(); + private readonly SchemaCommandHandler sut; + private readonly SchemaDomainObject schema; + private readonly FieldRegistry registry = new FieldRegistry(); + private readonly Guid appId = Guid.NewGuid(); + private readonly string fieldName = "age"; + private readonly string schemaName = "users"; + + static SchemaCommandHandlerTests() + { + TypeNameRegistry.Map(typeof(Schema).GetTypeInfo().Assembly); + } + + public SchemaCommandHandlerTests() + { + schema = new SchemaDomainObject(Id, 0, registry); + + sut = new SchemaCommandHandler( + DomainObjectFactory.Object, + DomainObjectRepository.Object, + schemaProvider.Object); + } + + [Fact] + public async Task Create_should_throw_if_a_name_with_same_name_already_exists() + { + var command = new CreateSchema { Name = schemaName, AppId = appId, AggregateId = Id }; + var context = new CommandContext(command); + + schemaProvider.Setup(x => x.FindSchemaIdByNameAsync(appId, schemaName)).Returns(Task.FromResult(Guid.NewGuid())).Verifiable(); + + await TestCreate(schema, async _ => + { + await Assert.ThrowsAsync(async () => await sut.HandleAsync(context)); + }, false); + + schemaProvider.VerifyAll(); + } + + [Fact] + public async Task Create_should_create_schema_if_name_is_free() + { + var command = new CreateSchema { Name = schemaName, AppId = appId, AggregateId = Id }; + var context = new CommandContext(command); + + schemaProvider.Setup(x => x.FindSchemaIdByNameAsync(Id, schemaName)).Returns(Task.FromResult(null)).Verifiable(); + + await TestCreate(schema, async _ => + { + await sut.HandleAsync(context); + }); + + Assert.Equal(command.Name, context.Result()); + } + + [Fact] + public async Task UpdateSchema_should_update_domain_object() + { + CreateSchema(); + + var command = new UpdateSchema { AggregateId = Id, Properties = new SchemaProperties() }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + [Fact] + public async Task DeleteSchema_should_update_domain_object() + { + CreateSchema(); + + var command = new DeleteSchema { AggregateId = Id }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + [Fact] + public async Task AddField_should_update_domain_object() + { + CreateSchema(); + + var command = new AddField { AggregateId = Id, Name = fieldName, Properties = new NumberFieldProperties() }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + + Assert.Equal(1, context.Result()); + } + + [Fact] + public async Task UpdateField_should_update_domain_object() + { + CreateSchema(); + CreateField(); + + var command = new UpdateField { AggregateId = Id, FieldId = 1, Properties = new NumberFieldProperties() }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + [Fact] + public async Task HideField_should_update_domain_object() + { + CreateSchema(); + CreateField(); + + var command = new HideField { AggregateId = Id, FieldId = 1 }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + [Fact] + public async Task ShowField_should_update_domain_object() + { + CreateSchema(); + CreateField(); + + var command = new ShowField { AggregateId = Id, FieldId = 1 }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + [Fact] + public async Task DisableField_should_update_domain_object() + { + CreateSchema(); + CreateField(); + + var command = new DisableField { AggregateId = Id, FieldId = 1 }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + [Fact] + public async Task EnableField_should_update_domain_object() + { + CreateSchema(); + CreateField(); + + var command = new EnableField { AggregateId = Id, FieldId = 1 }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + [Fact] + public async Task DeleteField_should_update_domain_object() + { + CreateSchema(); + CreateField(); + + var command = new DeleteField { AggregateId = Id, FieldId = 1 }; + var context = new CommandContext(command); + + await TestUpdate(schema, async _ => + { + await sut.HandleAsync(context); + }); + } + + private void CreateSchema() + { + schema.Create(new CreateSchema { Name = schemaName }); + } + + private void CreateField() + { + schema.AddField(new AddField { Name = fieldName, Properties = new NumberFieldProperties() }); + } + } +} diff --git a/tests/Squidex.Write.Tests/Schemas/SchemaDomainObjectTests.cs b/tests/Squidex.Write.Tests/Schemas/SchemaDomainObjectTests.cs index 94bf85a27..bdbd0f8f8 100644 --- a/tests/Squidex.Write.Tests/Schemas/SchemaDomainObjectTests.cs +++ b/tests/Squidex.Write.Tests/Schemas/SchemaDomainObjectTests.cs @@ -29,7 +29,7 @@ namespace Squidex.Write.Tests.Schemas private readonly string appName = "schema"; private readonly FieldRegistry registry = new FieldRegistry(); private readonly SchemaDomainObject sut; - + public SchemaDomainObjectTests() { sut = new SchemaDomainObject(Guid.NewGuid(), 0, registry);