Migrated ValidationFilterAttribute into netcore

This commit is contained in:
Andy Butland
2020-05-16 19:35:45 +02:00
parent b2e12f400c
commit 60c5c15fdf
4 changed files with 96 additions and 0 deletions

View File

@@ -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<IFilterMetadata>(),
new Dictionary<string, object>(),
new Mock<Controller>().Object);
}
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Umbraco.Web.BackOffice.Filters
{
/// <summary>
/// An action filter used to do basic validation against the model and return a result
/// straight away if it fails.
/// </summary>
internal sealed class ValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
var modelState = context.ModelState;
if (!context.ModelState.IsValid)
{
context.Result = new BadRequestObjectResult(modelState);
}
}
}
}

View File

@@ -14,6 +14,12 @@
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Umbraco.Tests.UnitTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Umbraco.Configuration\Umbraco.Configuration.csproj" />
<ProjectReference Include="..\Umbraco.Core\Umbraco.Core.csproj" />

View File

@@ -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.
/// </summary>
/// Migrated to .NET core
internal sealed class ValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)