Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add FluentValidationFilterTests
support8c361d9e0c26
diff --git a/src/Tests/Grand.Infrastructure.Tests/Validators/FluentValidationFilterTests.cs b/src/Tests/Grand.Infrastructure.Tests/Validators/FluentValidationFilterTests.csnew file mode 100644index 000000000..acba61a4e--- /dev/null+++ b/src/Tests/Grand.Infrastructure.Tests/Validators/FluentValidationFilterTests.cs@@ -0,0 +1,110 @@+using FluentValidation;+using Grand.Infrastructure.Tests.Validators;+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.Routing;+using Microsoft.Extensions.DependencyInjection;+using Microsoft.VisualStudio.TestTools.UnitTesting;++namespace Grand.Infrastructure.Validators.Tests+{+ [TestClass()]+ public class FluentValidationFilterTests+ {+ [TestMethod()]+ public async Task OnActionExecutionAsyncTest_ValidModel()+ {++ //arrange+ var services = new ServiceCollection();+ services.AddScoped<IValidator<SourceTest>, SourceTestValidator>();+ var serviceProvider = services.BuildServiceProvider();++ var actionFilter = new FluentValidationFilter(serviceProvider);++ var httpContext = new DefaultHttpContext();+ httpContext.Request.Method = "POST";++ var actionContext = new ActionContext(httpContext,+ new RouteData(),+ new ActionDescriptor(),+ new ModelStateDictionary());++ var actionExecutedContext = new ActionExecutedContext(actionContext,+ new List<IFilterMetadata>(),+ controller: null);++ var actionExecutingContext = new ActionExecutingContext(actionContext,+ new List<IFilterMetadata>(),+ new Dictionary<string, object>(),+ controller: null);++ var source = new SourceTest {+ FirstName = "ABCD",+ LastName = "ABCD",++ };+ actionExecutingContext.ActionArguments["model"] = source;++ //act+ var next = new ActionExecutionDelegate(() => Task.FromResult(CreateActionExecutedContext(actionExecutingContext)));+ await actionFilter.OnActionExecutionAsync(actionExecutingContext, next);++ //assert+ Assert.IsTrue(actionExecutingContext.ModelState.IsValid);++ }+ [TestMethod()]+ public async Task OnActionExecutionAsyncTest_NotValidModel()+ {++ //arrange+ var services = new ServiceCollection();+ services.AddScoped<IValidator<SourceTest>, SourceTestValidator>();+ var serviceProvider = services.BuildServiceProvider();++ var actionFilter = new FluentValidationFilter(serviceProvider);++ var httpContext = new DefaultHttpContext();+ httpContext.Request.Method = "POST";++ var actionContext = new ActionContext(httpContext,+ new RouteData(),+ new ActionDescriptor(),+ new ModelStateDictionary());++ var actionExecutedContext = new ActionExecutedContext(actionContext,+ new List<IFilterMetadata>(),+ controller: null);++ var actionExecutingContext = new ActionExecutingContext(actionContext,+ new List<IFilterMetadata>(),+ new Dictionary<string, object>(),+ controller: null);++ var source = new SourceTest {+ FirstName = "ABCD",+ };+ actionExecutingContext.ActionArguments["model"] = source;++ //act+ var next = new ActionExecutionDelegate(() => Task.FromResult(CreateActionExecutedContext(actionExecutingContext)));+ await actionFilter.OnActionExecutionAsync(actionExecutingContext, next);++ //assert+ Assert.IsFalse(actionExecutingContext.ModelState.IsValid);+ }++++ private static ActionExecutedContext CreateActionExecutedContext(ActionExecutingContext context)+ {+ return new ActionExecutedContext(context, context.Filters, context.Controller) {+ Result = context.Result,+ };+ }+ }+}\ No newline at end of filediff --git a/src/Tests/Grand.Infrastructure.Tests/Validators/SourceTest.cs b/src/Tests/Grand.Infrastructure.Tests/Validators/SourceTest.csnew file mode 100644index 000000000..5f6d5b69b--- /dev/null+++ b/src/Tests/Grand.Infrastructure.Tests/Validators/SourceTest.cs@@ -0,0 +1,8 @@+namespace Grand.Infrastructure.Tests.Validators+{+ public class SourceTest+ {+ public string FirstName { get; set; }+ public string LastName { get; set; }+ }+}diff --git a/src/Tests/Grand.Infrastructure.Tests/Validators/SourceTestValidator.cs b/src/Tests/Grand.Infrastructure.Tests/Validators/SourceTestValidator.csnew file mode 100644index 000000000..1f0e38411--- /dev/null+++ b/src/Tests/Grand.Infrastructure.Tests/Validators/SourceTestValidator.cs@@ -0,0 +1,12 @@+using FluentValidation;++namespace Grand.Infrastructure.Tests.Validators+{+ public class SourceTestValidator : AbstractValidator<SourceTest>+ {+ public SourceTestValidator()+ {+ RuleFor(person => person.LastName).NotNull();+ }+ }+}