From 2823596497cecaa5c91ac2e568b5524607da7ff4 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 30 Dec 2023 21:27:54 +0800 Subject: [PATCH] Add unit tests. --- .../Middleware/AbpMiddlewareBase.cs | 2 +- .../Volo/Abp/DisableAbpFeaturesAttribute.cs | 2 +- .../MvcDisableAbpFeaturesAttribute_Tests.cs | 74 +++++++++++++++++++ .../DynamicProxy/AbpInterceptionTestBase.cs | 31 ++++++++ .../AlwaysExceptionAsyncInterceptor.cs | 11 +++ .../DisableInterceptionTargetClass.cs | 41 ++++++++++ 6 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs create mode 100644 framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs create mode 100644 framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs index f41578da33..52c9990a4f 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs @@ -12,7 +12,7 @@ public abstract class AbpMiddlewareBase : IMiddleware var endpoint = context.GetEndpoint(); var controllerActionDescriptor = endpoint?.Metadata.GetMetadata(); var disableAbpFeaturesAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute(); - return Task.FromResult(disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMiddlewares); + return Task.FromResult(disableAbpFeaturesAttribute != null && disableAbpFeaturesAttribute.DisableMiddleware); } public abstract Task InvokeAsync(HttpContext context, RequestDelegate next); diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs index 69f80446a6..28c92b7463 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DisableAbpFeaturesAttribute.cs @@ -15,7 +15,7 @@ public class DisableAbpFeaturesAttribute : Attribute /// The framework middleware will skip the class. /// This will cause the all features that depend on middleware to not work. /// - public bool DisableMiddlewares { get; set; } = true; + public bool DisableMiddleware { get; set; } = true; /// /// The framework will not remove all built-in filters for the class. diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs new file mode 100644 index 0000000000..7ff181c645 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Filters/MvcDisableAbpFeaturesAttribute_Tests.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Filters; +using Shouldly; +using Volo.Abp.AspNetCore.Mvc.Auditing; +using Volo.Abp.AspNetCore.Mvc.ExceptionHandling; +using Volo.Abp.AspNetCore.Mvc.Features; +using Volo.Abp.AspNetCore.Mvc.GlobalFeatures; +using Volo.Abp.AspNetCore.Mvc.Response; +using Volo.Abp.AspNetCore.Mvc.Uow; +using Volo.Abp.AspNetCore.Mvc.Validation; +using Xunit; + +namespace Volo.Abp.AspNetCore.Mvc.Filters; + +[Route("api/enabled-features-test")] +public class EnabledAbpFeaturesController : AbpController, IRemoteService +{ + [HttpGet] + public Task> GetAsync() + { + var filters = HttpContext.GetEndpoint().Metadata.GetMetadata() + .FilterDescriptors.Where(x => x.Filter is ServiceFilterAttribute) + .Select(x => x.Filter.As().ServiceType.FullName).ToList(); + + return Task.FromResult(filters); + } +} + +[Route("api/disabled-features-test")] +[DisableAbpFeatures] +public class DisabledAbpFeaturesController : AbpController, IRemoteService +{ + [HttpGet] + public Task> GetAsync() + { + var filters = HttpContext.GetEndpoint().Metadata.GetMetadata() + .FilterDescriptors.Where(x => x.Filter is ServiceFilterAttribute) + .Select(x => x.Filter.As().ServiceType.FullName).ToList(); + + return Task.FromResult(filters); + } +} + +public class MvcDisableAbpFeaturesAttribute_Tests : AspNetCoreMvcTestBase +{ + [Fact] + public async Task Should_Disable_MVC_Filters() + { + var filters = await GetResponseAsObjectAsync>("/api/enabled-features-test"); + filters.ShouldContain(typeof(GlobalFeatureActionFilter).FullName); + filters.ShouldContain(typeof(AbpAuditActionFilter).FullName); + filters.ShouldContain(typeof(AbpNoContentActionFilter).FullName); + filters.ShouldContain(typeof(AbpFeatureActionFilter).FullName); + filters.ShouldContain(typeof(AbpValidationActionFilter).FullName); + filters.ShouldContain(typeof(AbpUowActionFilter).FullName); + filters.ShouldContain(typeof(AbpExceptionFilter).FullName); + + filters = await GetResponseAsObjectAsync>("/api/disabled-features-test"); + filters.ShouldNotContain(typeof(GlobalFeatureActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpAuditActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpNoContentActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpFeatureActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpValidationActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpUowActionFilter).FullName); + filters.ShouldNotContain(typeof(AbpExceptionFilter).FullName); + } + +} diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs index e244237598..4cce6a0c78 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AbpInterceptionTestBase.cs @@ -15,22 +15,34 @@ public abstract class AbpInterceptionTestBase : AbpAsyncIntegrat services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.OnRegistered(registration => { if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType) { registration.Interceptors.Add(); registration.Interceptors.Add(); + registration.Interceptors.Add(); } if (typeof(CachedTestObject) == registration.ImplementationType) { registration.Interceptors.Add(); } + + if (typeof(DisableInterceptionTargetClass) == registration.ImplementationType) + { + registration.Interceptors.Add(); + registration.Interceptors.Add(); + registration.Interceptors.Add(); + registration.Interceptors.Add(); + } }); return Task.CompletedTask; @@ -96,4 +108,23 @@ public abstract class AbpInterceptionTestBase : AbpAsyncIntegrat (await target.GetValueAsync(43)).ShouldBe(42); //First run, cached previous value (await target.GetValueAsync(44)).ShouldBe(42); //First run, cached previous value } + + [Fact] + public async Task Should_Disable_Interceptors() + { + //Arrange + + var target = ServiceProvider.GetService(); + + //Act + + await target.DoItAsync(); + + //Assert + + target.Logs.Count.ShouldBe(3); + target.Logs[0].ShouldBe("EnterDoItAsync"); + target.Logs[1].ShouldBe("MiddleDoItAsync"); + target.Logs[2].ShouldBe("ExitDoItAsync"); + } } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs new file mode 100644 index 0000000000..2b9845a6ea --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/AlwaysExceptionAsyncInterceptor.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.DynamicProxy; + +public class AlwaysExceptionAsyncInterceptor : AbpInterceptor +{ + public override Task InterceptAsync(IAbpMethodInvocation invocation) + { + throw new AbpException("This interceptor should not be executed!"); + } +} diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs new file mode 100644 index 0000000000..caf17b5b9d --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DynamicProxy/DisableInterceptionTargetClass.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.TestBase.Logging; + +namespace Volo.Abp.DynamicProxy; + +[DisableAbpFeatures] +public class DisableInterceptionTargetClass : ICanLogOnObject +{ + public List Logs { get; } = new List(); + + public virtual void DoIt() + { + Logs.Add("ExecutingDoIt"); + } + + public virtual int GetValue() + { + Logs.Add("ExecutingGetValue"); + return 42; + } + + public virtual async Task GetValueAsync() + { + Logs.Add("EnterGetValueAsync"); + await Task.Delay(5); + Logs.Add("MiddleGetValueAsync"); + await Task.Delay(5); + Logs.Add("ExitGetValueAsync"); + return 42; + } + + public virtual async Task DoItAsync() + { + Logs.Add("EnterDoItAsync"); + await Task.Delay(5); + Logs.Add("MiddleDoItAsync"); + await Task.Delay(5); + Logs.Add("ExitDoItAsync"); + } +}