mirror of https://github.com/abpframework/abp.git
Browse Source
Allow to write audit logs on exceptions even if disabled for GET requestspull/2822/head
committed by
GitHub
6 changed files with 114 additions and 13 deletions
@ -0,0 +1,29 @@ |
|||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.Auditing; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Auditing |
||||
|
{ |
||||
|
[Route("api/audit-test")] |
||||
|
public class AuditTestController : AbpController |
||||
|
{ |
||||
|
private readonly AbpAuditingOptions _options; |
||||
|
|
||||
|
public AuditTestController(IOptions<AbpAuditingOptions> options) |
||||
|
{ |
||||
|
_options = options.Value; |
||||
|
} |
||||
|
|
||||
|
[Route("audit-success")] |
||||
|
public IActionResult AuditSuccessForGetRequests() |
||||
|
{ |
||||
|
return Ok(); |
||||
|
} |
||||
|
|
||||
|
[Route("audit-fail")] |
||||
|
public IActionResult AuditFailForGetRequests() |
||||
|
{ |
||||
|
throw new UserFriendlyException("Exception occurred!"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,48 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.DependencyInjection.Extensions; |
||||
|
using Microsoft.Extensions.Hosting; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using NSubstitute; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Auditing; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Auditing |
||||
|
{ |
||||
|
public class AuditTestController_Tests : AspNetCoreMvcTestBase |
||||
|
{ |
||||
|
private readonly AbpAuditingOptions _options; |
||||
|
private IAuditingStore _auditingStore; |
||||
|
|
||||
|
public AuditTestController_Tests() |
||||
|
{ |
||||
|
_options = ServiceProvider.GetRequiredService<IOptions<AbpAuditingOptions>>().Value; |
||||
|
_auditingStore = ServiceProvider.GetRequiredService<IAuditingStore>(); |
||||
|
} |
||||
|
|
||||
|
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) |
||||
|
{ |
||||
|
_auditingStore = Substitute.For<IAuditingStore>(); |
||||
|
services.Replace(ServiceDescriptor.Singleton(_auditingStore)); |
||||
|
base.ConfigureServices(context, services); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_GetRequests() |
||||
|
{ |
||||
|
_options.IsEnabledForGetRequests = true; |
||||
|
_options.AlwaysLogOnException = false; |
||||
|
await GetResponseAsync("api/audit-test/audit-success"); |
||||
|
//await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); //Won't work, save happens out of scope
|
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() |
||||
|
{ |
||||
|
_options.IsEnabled = true; |
||||
|
_options.AlwaysLogOnException = true; |
||||
|
await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.BadRequest); |
||||
|
//await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); //Won't work, save happens out of scope
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue