From 9d8c8ac975670d9bfa39527f71bd5b8dbdba5001 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 24 Mar 2019 20:14:35 +0100 Subject: [PATCH] Added missing files. --- .../ApiCostsAttributeTests.cs | 22 ++++ .../ApiExceptionFilterAttributeTests.cs | 122 ++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 tests/Squidex.Web.Tests/ApiCostsAttributeTests.cs create mode 100644 tests/Squidex.Web.Tests/ApiExceptionFilterAttributeTests.cs diff --git a/tests/Squidex.Web.Tests/ApiCostsAttributeTests.cs b/tests/Squidex.Web.Tests/ApiCostsAttributeTests.cs new file mode 100644 index 000000000..91f9c4a16 --- /dev/null +++ b/tests/Squidex.Web.Tests/ApiCostsAttributeTests.cs @@ -0,0 +1,22 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using Xunit; + +namespace Squidex.Web +{ + public class ApiCostsAttributeTests + { + [Fact] + public void Should_assign_weight() + { + var sut = new ApiCostsAttribute(10.5); + + Assert.Equal(10.5, sut.Weight); + } + } +} diff --git a/tests/Squidex.Web.Tests/ApiExceptionFilterAttributeTests.cs b/tests/Squidex.Web.Tests/ApiExceptionFilterAttributeTests.cs new file mode 100644 index 000000000..51a15b1d5 --- /dev/null +++ b/tests/Squidex.Web.Tests/ApiExceptionFilterAttributeTests.cs @@ -0,0 +1,122 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System; +using System.Collections.Generic; +using System.Security; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Routing; +using Squidex.Infrastructure; +using Xunit; + +namespace Squidex.Web +{ + public class ApiExceptionFilterAttributeTests + { + private readonly ApiExceptionFilterAttribute sut = new ApiExceptionFilterAttribute(); + + [Fact] + public void Should_generate_404_for_DomainObjectNotFoundException() + { + var context = E(new DomainObjectNotFoundException("1", typeof(object))); + + sut.OnException(context); + + Assert.IsType(context.Result); + } + + [Fact] + public void Should_generate_400_for_ValidationException() + { + var ex = new ValidationException("NotAllowed", + new ValidationError("Error1"), + new ValidationError("Error2", "P"), + new ValidationError("Error3", "P1", "P2")); + + var context = E(ex); + + sut.OnException(context); + + var result = context.Result as ObjectResult; + + Assert.Equal(400, result.StatusCode); + Assert.Equal(400, (result.Value as ErrorDto)?.StatusCode); + + Assert.Equal(ex.Summary, (result.Value as ErrorDto).Message); + + Assert.Equal(new string[] { "Error1", "P: Error2", "P1, P2: Error3" }, (result.Value as ErrorDto).Details); + } + + [Fact] + public void Should_generate_400_for_DomainException() + { + var context = E(new DomainException("NotAllowed")); + + sut.OnException(context); + + Validate(400, context); + } + + [Fact] + public void Should_generate_412_for_DomainObjectVersionException() + { + var context = E(new DomainObjectVersionException("1", typeof(object), 1, 2)); + + sut.OnException(context); + + Validate(412, context); + } + + [Fact] + public void Should_generate_403_for_DomainForbiddenException() + { + var context = E(new DomainForbiddenException("Forbidden")); + + sut.OnException(context); + + Validate(403, context); + } + + [Fact] + public void Should_generate_403_for_SecurityException() + { + var context = E(new SecurityException("Forbidden")); + + sut.OnException(context); + + Validate(403, context); + } + + private static ExceptionContext E(Exception exception) + { + var httpContext = new DefaultHttpContext(); + + var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor + { + FilterDescriptors = new List() + }); + + return new ExceptionContext(actionContext, new List()) + { + Exception = exception + }; + } + + private static void Validate(int statusCode, ExceptionContext context) + { + var result = context.Result as ObjectResult; + + Assert.Equal(statusCode, result.StatusCode); + Assert.Equal(statusCode, (result.Value as ErrorDto)?.StatusCode); + + Assert.Equal(context.Exception.Message, (result.Value as ErrorDto).Message); + } + } +}