diff --git a/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardApp.cs b/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardApp.cs index 38f044e95..f5b9d7366 100644 --- a/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardApp.cs +++ b/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardApp.cs @@ -28,7 +28,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards } else if (await appProvider.GetAppAsync(command.Name) != null) { - error(new ValidationError($"An app with name '{command.Name}' already exists.", nameof(command.Name))); + error(new ValidationError($"An app with the same name already exists.", nameof(command.Name))); } }); } @@ -47,7 +47,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards { if (appPlans.GetPlan(command.PlanId) == null) { - error(new ValidationError($"Plan with id '{command.PlanId}' is not available.", nameof(command.PlanId))); + error(new ValidationError("A plan with this id does not exist.", nameof(command.PlanId))); } if (!string.IsNullOrWhiteSpace(command.PlanId) && plan != null && !plan.Owner.Equals(command.Actor)) diff --git a/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppClients.cs b/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppClients.cs index f9b0e5cea..3a1dd36e0 100644 --- a/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppClients.cs +++ b/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppClients.cs @@ -25,7 +25,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards } else if (clients.ContainsKey(command.Id)) { - error(new ValidationError($"A client with id '{command.Id}' has already been added.")); + error(new ValidationError($"A client with the same id already exists.")); } }); } diff --git a/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppPattern.cs b/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppPattern.cs index 6a71461ec..1a7f9e2c2 100644 --- a/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppPattern.cs +++ b/src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardAppPattern.cs @@ -20,19 +20,24 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards Validate.It(() => "Cannot add pattern.", error => { + if (command.PatternId == Guid.Empty) + { + error(new ValidationError("Id is required.", nameof(command.PatternId))); + } + if (string.IsNullOrWhiteSpace(command.Name)) { - error(new ValidationError("Pattern name can not be empty.", nameof(command.Name))); + error(new ValidationError("Name is required.", nameof(command.Name))); } if (patterns.Values.Any(x => x.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase))) { - error(new ValidationError("Pattern name is already assigned.", nameof(command.Name))); + error(new ValidationError("An pattern with the same name already exists.")); } if (string.IsNullOrWhiteSpace(command.Pattern)) { - error(new ValidationError("Pattern can not be empty.", nameof(command.Pattern))); + error(new ValidationError("Pattern is required.", nameof(command.Pattern))); } else if (!command.Pattern.IsValidRegex()) { @@ -41,7 +46,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards if (patterns.Values.Any(x => x.Pattern == command.Pattern)) { - error(new ValidationError("Pattern already exists.", nameof(command.Pattern))); + error(new ValidationError("This pattern already exists but with another name.")); } }); } @@ -69,17 +74,17 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards { if (string.IsNullOrWhiteSpace(command.Name)) { - error(new ValidationError("Pattern name can not be empty.", nameof(command.Name))); + error(new ValidationError("Name is required.", nameof(command.Name))); } if (patterns.Any(x => x.Key != command.PatternId && x.Value.Name.Equals(command.Name, StringComparison.OrdinalIgnoreCase))) { - error(new ValidationError("Pattern name is already assigned.", nameof(command.Name))); + error(new ValidationError("An pattern with the same name already exists.")); } if (string.IsNullOrWhiteSpace(command.Pattern)) { - error(new ValidationError("Pattern can not be empty.", nameof(command.Pattern))); + error(new ValidationError("Pattern is required.", nameof(command.Pattern))); } else if (!command.Pattern.IsValidRegex()) { @@ -88,7 +93,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards if (patterns.Any(x => x.Key != command.PatternId && x.Value.Pattern == command.Pattern)) { - error(new ValidationError("Pattern already exists.", nameof(command.Pattern))); + error(new ValidationError("This pattern already exists but with another name.")); } }); } diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppClientsTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppClientsTests.cs index ef67886ee..a7f2b73ff 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppClientsTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppClientsTests.cs @@ -36,7 +36,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards var clients_1 = clients_0.Add("android", "secret"); ValidationAssert.Throws(() => GuardAppClients.CanAttach(clients_1, command), - new ValidationError("A client with id 'android' has already been added.")); + new ValidationError("A client with the same id already exists.")); } [Fact] diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppPatternsTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppPatternsTests.cs index 506025bf7..1f7a6e208 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppPatternsTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppPatternsTests.cs @@ -8,13 +8,13 @@ using System; using Squidex.Domain.Apps.Core.Apps; using Squidex.Domain.Apps.Entities.Apps.Commands; -using Squidex.Domain.Apps.Entities.Apps.Guards; +using Squidex.Domain.Apps.Entities.TestHelpers; using Squidex.Infrastructure; using Xunit; #pragma warning disable SA1310 // Field names must not contain underscore -namespace Squidex.Domain.Apps.Write.Apps.Guards +namespace Squidex.Domain.Apps.Entities.Apps.Guards { public class GuardAppPatternsTests { @@ -26,15 +26,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards { var command = new AddPattern { PatternId = patternId, Name = string.Empty, Pattern = ".*" }; - Assert.Throws(() => GuardAppPattern.CanAdd(patterns_0, command)); - } - - [Fact] - public void CanAdd_should_throw_exception_if_id_empty_guid() - { - var command = new AddPattern { Name = string.Empty, Pattern = ".*" }; - - Assert.Throws(() => GuardAppPattern.CanAdd(patterns_0, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanAdd(patterns_0, command), + new ValidationError("Name is required.", "Name")); } [Fact] @@ -42,7 +35,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards { var command = new AddPattern { PatternId = patternId, Name = "any", Pattern = string.Empty }; - Assert.Throws(() => GuardAppPattern.CanAdd(patterns_0, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanAdd(patterns_0, command), + new ValidationError("Pattern is required.", "Pattern")); } [Fact] @@ -50,7 +44,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards { var command = new AddPattern { PatternId = patternId, Name = "any", Pattern = "[0-9{1}" }; - Assert.Throws(() => GuardAppPattern.CanAdd(patterns_0, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanAdd(patterns_0, command), + new ValidationError("Pattern is not a valid regular expression.", "Pattern")); } [Fact] @@ -60,7 +55,19 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards var command = new AddPattern { PatternId = patternId, Name = "any", Pattern = ".*" }; - Assert.Throws(() => GuardAppPattern.CanAdd(patterns_1, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanAdd(patterns_1, command), + new ValidationError("An pattern with the same name already exists.")); + } + + [Fact] + public void CanAdd_should_throw_exception_if_pattern_exists() + { + var patterns_1 = patterns_0.Add(Guid.NewGuid(), "any", "[a-z]", "Message"); + + var command = new AddPattern { PatternId = patternId, Name = "other", Pattern = "[a-z]" }; + + ValidationAssert.Throws(() => GuardAppPattern.CanAdd(patterns_1, command), + new ValidationError("This pattern already exists but with another name.")); } [Fact] @@ -96,7 +103,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards var command = new UpdatePattern { PatternId = patternId, Name = string.Empty, Pattern = ".*" }; - Assert.Throws(() => GuardAppPattern.CanUpdate(patterns_1, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanUpdate(patterns_1, command), + new ValidationError("Name is required.", "Name")); } [Fact] @@ -106,7 +114,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards var command = new UpdatePattern { PatternId = patternId, Name = "any", Pattern = string.Empty }; - Assert.Throws(() => GuardAppPattern.CanUpdate(patterns_1, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanUpdate(patterns_1, command), + new ValidationError("Pattern is required.", "Pattern")); } [Fact] @@ -116,7 +125,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards var command = new UpdatePattern { PatternId = patternId, Name = "any", Pattern = "[0-9{1}" }; - Assert.Throws(() => GuardAppPattern.CanUpdate(patterns_1, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanUpdate(patterns_1, command), + new ValidationError("Pattern is not a valid regular expression.", "Pattern")); } [Fact] @@ -130,7 +140,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards var command = new UpdatePattern { PatternId = id2, Name = "Pattern1", Pattern = "[0-4]" }; - Assert.Throws(() => GuardAppPattern.CanUpdate(patterns_2, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanUpdate(patterns_2, command), + new ValidationError("An pattern with the same name already exists.")); } [Fact] @@ -144,7 +155,8 @@ namespace Squidex.Domain.Apps.Write.Apps.Guards var command = new UpdatePattern { PatternId = id2, Name = "Pattern2", Pattern = "[0-5]" }; - Assert.Throws(() => GuardAppPattern.CanUpdate(patterns_2, command)); + ValidationAssert.Throws(() => GuardAppPattern.CanUpdate(patterns_2, command), + new ValidationError("This pattern already exists but with another name.")); } [Fact] diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppTests.cs index 622364340..f153ab49a 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppTests.cs @@ -44,7 +44,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards var command = new CreateApp { Name = "new-app" }; return ValidationAssert.ThrowsAsync(() => GuardApp.CanCreate(command, apps), - new ValidationError("An app with name 'new-app' already exists.", "Name")); + new ValidationError("An app with the same name already exists.", "Name")); } [Fact] @@ -86,7 +86,7 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards AppPlan plan = null; ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), - new ValidationError("Plan with id 'free' is not available.", "PlanId")); + new ValidationError("A plan with this id does not exist.", "PlanId")); } [Fact] diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/ValidationAssert.cs b/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/ValidationAssert.cs index ca6a28ce4..1b3d62108 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/ValidationAssert.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/ValidationAssert.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Threading.Tasks; using Squidex.Infrastructure; using Xunit; +using Xunit.Sdk; namespace Squidex.Domain.Apps.Entities.TestHelpers { @@ -27,6 +28,10 @@ namespace Squidex.Domain.Apps.Entities.TestHelpers { ex.Errors.ToArray().ShouldBeEquivalent(errors); } + catch (XunitException) + { + throw; + } catch (Exception ex) { Assert.True(false, $"Excepted {typeof(ValidationException)}, but got {ex.GetType()}"); @@ -45,6 +50,10 @@ namespace Squidex.Domain.Apps.Entities.TestHelpers { ex.Errors.ToArray().ShouldBeEquivalent(errors); } + catch (XunitException) + { + throw; + } catch (Exception ex) { Assert.True(false, $"Excepted {typeof(ValidationException)}, but got {ex.GetType()}");