From 5d2b9af1c24977502c0f390e5cd9f65d10adb6ec Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 17 Mar 2021 21:48:19 +0800 Subject: [PATCH] Get the correct type of page model. Resolve #8106 --- .../Mvc/Auditing/AbpAuditPageFilter.cs | 2 +- .../Mvc/Auditing/AuditTestController.cs | 6 ++++++ .../Mvc/Auditing/AuditTestController_Tests.cs | 14 +++++++++++++- .../Mvc/Auditing/AuditTestPage.cshtml.cs | 7 ++++++- .../Mvc/Auditing/AuditTestPage_Tests.cs | 18 +++++++++++++++--- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs index cc12120280..4cfecd5a3d 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs @@ -83,7 +83,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing auditLog = auditLogScope.Log; auditLogAction = auditingHelper.CreateAuditLogAction( auditLog, - context.HandlerMethod.GetType(), + context.HandlerMethod.MethodInfo.DeclaringType, context.HandlerMethod.MethodInfo, context.HandlerArguments ); diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs index 49dfef9db8..94e57403f5 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs @@ -14,6 +14,12 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing _options = options.Value; } + [HttpGet] + public IActionResult Get() + { + return Ok(); + } + [Route("audit-success")] public IActionResult AuditSuccessForGetRequests() { diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs index 3f934d5fe8..9d3da08095 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; @@ -28,6 +29,17 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing base.ConfigureServices(context, services); } + [Fact] + public async Task Should_Get_Correct_ServiceName_And_MethodName() + { + _options.IsEnabledForGetRequests = true; + _options.AlwaysLogOnException = false; + await GetResponseAsync("/api/audit-test/"); + await _auditingStore.Received().SaveAsync(Arg.Is(x => + x.Actions.Any(a => a.ServiceName == typeof(AuditTestController).FullName) && + x.Actions.Any(a => a.MethodName == nameof(AuditTestController.Get)))); + } + [Fact] public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_GetRequests() { @@ -51,7 +63,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing await _auditingStore.Received().SaveAsync(Arg.Any()); } - + [Fact] public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Returns_Object() { diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs index 6425d6585b..7dc1967a2e 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs @@ -15,6 +15,11 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing _options = options.Value; } + public void OnGet() + { + + } + public IActionResult OnGetAuditSuccessForGetRequests() { return new OkResult(); @@ -30,4 +35,4 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing throw new UserFriendlyException("Exception occurred!"); } } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs index 7d5298e253..311abe5aa5 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; @@ -27,6 +28,17 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing base.ConfigureServices(context, services); } + [Fact] + public async Task Should_Get_Correct_ServiceName_And_MethodName() + { + _options.IsEnabledForGetRequests = true; + _options.AlwaysLogOnException = false; + await GetResponseAsync("/Auditing/AuditTestPage"); + await _auditingStore.Received().SaveAsync(Arg.Is(x => + x.Actions.Any(a => a.ServiceName == typeof(AuditTestPage).FullName) && + x.Actions.Any(a => a.MethodName == nameof(AuditTestPage.OnGet)))); + } + [Fact] public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_GetRequests() { @@ -50,7 +62,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing await _auditingStore.Received().SaveAsync(Arg.Any()); } - + [Fact] public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Returns_Object() { @@ -62,4 +74,4 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing await _auditingStore.Received().SaveAsync(Arg.Any()); } } -} \ No newline at end of file +}