From 60c5c15fdf2886a5350e7d2b020160e977bb6ba4 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Sat, 16 May 2020 19:35:45 +0200 Subject: [PATCH] Migrated ValidationFilterAttribute into netcore --- .../Filters/ValidationFilterAttributeTests.cs | 68 +++++++++++++++++++ .../Filters/ValidationFilterAttribute.cs | 21 ++++++ .../Umbraco.Web.BackOffice.csproj | 6 ++ .../Filters/ValidationFilterAttribute.cs | 1 + 4 files changed, 96 insertions(+) create mode 100644 src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/ValidationFilterAttributeTests.cs create mode 100644 src/Umbraco.Web.BackOffice/Filters/ValidationFilterAttribute.cs diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/ValidationFilterAttributeTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/ValidationFilterAttributeTests.cs new file mode 100644 index 0000000000..51521c48fa --- /dev/null +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Web.BackOffice/Filters/ValidationFilterAttributeTests.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Net; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.Routing; +using Microsoft.AspNetCore.Routing; +using Moq; +using NUnit.Framework; +using Umbraco.Web.BackOffice.Filters; + +namespace Umbraco.Tests.UnitTests.Umbraco.Web.BackOffice.Filters +{ + [TestFixture] + public class ValidationFilterAttributeTests + { + [Test] + public void Does_Not_Set_Result_When_No_Errors_In_Model_State() + { + // Arrange + var context = CreateContext(); + var attribute = new ValidationFilterAttribute(); + + // Act + attribute.OnActionExecuting(context); + + // Assert + Assert.IsNull(context.Result); + } + + [Test] + public void Returns_Bad_Request_When_Errors_In_Model_State() + { + // Arrange + var context = CreateContext(withError: true); + var attribute = new ValidationFilterAttribute(); + + // Act + attribute.OnActionExecuting(context); + + // Assert + var typedResult = context.Result as BadRequestObjectResult; + Assert.IsNotNull(typedResult); + } + + private static ActionExecutingContext CreateContext(bool withError = false) + { + var httpContext = new DefaultHttpContext(); + + var modelState = new ModelStateDictionary(); + if (withError) + { + modelState.AddModelError(string.Empty, "Error"); + } + + var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor(), modelState); + + return new ActionExecutingContext( + actionContext, + new List(), + new Dictionary(), + new Mock().Object); + } + } +} diff --git a/src/Umbraco.Web.BackOffice/Filters/ValidationFilterAttribute.cs b/src/Umbraco.Web.BackOffice/Filters/ValidationFilterAttribute.cs new file mode 100644 index 0000000000..360cc88e0a --- /dev/null +++ b/src/Umbraco.Web.BackOffice/Filters/ValidationFilterAttribute.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace Umbraco.Web.BackOffice.Filters +{ + /// + /// An action filter used to do basic validation against the model and return a result + /// straight away if it fails. + /// + internal sealed class ValidationFilterAttribute : ActionFilterAttribute + { + public override void OnActionExecuting(ActionExecutingContext context) + { + var modelState = context.ModelState; + if (!context.ModelState.IsValid) + { + context.Result = new BadRequestObjectResult(modelState); + } + } + } +} diff --git a/src/Umbraco.Web.BackOffice/Umbraco.Web.BackOffice.csproj b/src/Umbraco.Web.BackOffice/Umbraco.Web.BackOffice.csproj index 5b031c095e..3f6dd6969c 100644 --- a/src/Umbraco.Web.BackOffice/Umbraco.Web.BackOffice.csproj +++ b/src/Umbraco.Web.BackOffice/Umbraco.Web.BackOffice.csproj @@ -14,6 +14,12 @@ + + + <_Parameter1>Umbraco.Tests.UnitTests + + + diff --git a/src/Umbraco.Web/WebApi/Filters/ValidationFilterAttribute.cs b/src/Umbraco.Web/WebApi/Filters/ValidationFilterAttribute.cs index f56d9c28c4..d4e6af93ac 100644 --- a/src/Umbraco.Web/WebApi/Filters/ValidationFilterAttribute.cs +++ b/src/Umbraco.Web/WebApi/Filters/ValidationFilterAttribute.cs @@ -13,6 +13,7 @@ namespace Umbraco.Web.WebApi.Filters /// An action filter used to do basic validation against the model and return a result /// straight away if it fails. /// + /// Migrated to .NET core internal sealed class ValidationFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext)