From a341528aa65f36b8cd986faf25030fe65a5583d9 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Mon, 17 Feb 2020 13:39:17 +0300 Subject: [PATCH] Updated AuditingMidware exception check and new test --- .../AspNetCore/Auditing/AbpAuditingMiddleware.cs | 5 +++++ .../AspNetCore/Mvc/Auditing/AuditTestController.cs | 5 +++++ .../Mvc/Auditing/AuditTestController_Tests.cs | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs index be84495336..f59c5b6b3a 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; @@ -34,6 +35,10 @@ namespace Volo.Abp.AspNetCore.Auditing try { await next(context); + if (_auditingManager.Current.Log.Exceptions.Any()) + { + hasError = true; + } } catch (Exception) { 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 9b4892cea4..b6bfe203db 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 @@ -26,5 +26,10 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing { throw new UserFriendlyException("Exception occurred!"); } + [Route("audit-fail-object")] + public object AuditFailForGetRequestsReturningObject() + { + throw new UserFriendlyException("Exception occurred!"); + } } } 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 733fab3f49..f0371ef51a 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 @@ -34,7 +34,7 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing _options.IsEnabledForGetRequests = true; _options.AlwaysLogOnException = false; await GetResponseAsync("api/audit-test/audit-success"); - await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope + await _auditingStore.Received().SaveAsync(Arg.Any()); } [Fact] @@ -49,7 +49,17 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing } catch { } - await _auditingStore.Received().SaveAsync(Arg.Any()); //Won't work, save happens out of scope + await _auditingStore.Received().SaveAsync(Arg.Any()); + } + [Fact] + public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Returns_Object() + { + _options.IsEnabled = true; + _options.AlwaysLogOnException = true; + + await GetResponseAsync("api/audit-test/audit-fail-object", System.Net.HttpStatusCode.Forbidden); + + await _auditingStore.Received().SaveAsync(Arg.Any()); } } }