|
1 | | -using System; |
2 | | -using Microsoft.Extensions.DependencyInjection; |
3 | | -using Microsoft.Extensions.DependencyInjection.Extensions; |
4 | | -using Microsoft.Extensions.Hosting; |
5 | | -using Microsoft.Extensions.Options; |
6 | | -using NSubstitute; |
7 | | -using System.Threading.Tasks; |
8 | | -using Volo.Abp.Auditing; |
9 | | -using Xunit; |
10 | | - |
11 | | -namespace Volo.Abp.AspNetCore.Mvc.Auditing |
12 | | -{ |
13 | | - public class AuditTestController_Tests : AspNetCoreMvcTestBase |
14 | | - { |
15 | | - private readonly AbpAuditingOptions _options; |
16 | | - private IAuditingStore _auditingStore; |
17 | | - |
18 | | - public AuditTestController_Tests() |
19 | | - { |
20 | | - _options = ServiceProvider.GetRequiredService<IOptions<AbpAuditingOptions>>().Value; |
21 | | - _auditingStore = ServiceProvider.GetRequiredService<IAuditingStore>(); |
22 | | - } |
23 | | - |
24 | | - protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) |
25 | | - { |
26 | | - _auditingStore = Substitute.For<IAuditingStore>(); |
27 | | - services.Replace(ServiceDescriptor.Singleton(_auditingStore)); |
28 | | - base.ConfigureServices(context, services); |
29 | | - } |
30 | | - |
31 | | - [Fact] |
32 | | - public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_GetRequests() |
33 | | - { |
34 | | - _options.IsEnabledForGetRequests = true; |
35 | | - _options.AlwaysLogOnException = false; |
36 | | - await GetResponseAsync("api/audit-test/audit-success"); |
37 | | - await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
38 | | - } |
39 | | - |
40 | | - [Fact] |
41 | | - public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() |
42 | | - { |
43 | | - _options.IsEnabled = true; |
44 | | - _options.AlwaysLogOnException = true; |
45 | | - |
46 | | - try |
47 | | - { |
48 | | - await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.Forbidden); |
49 | | - } |
50 | | - catch { } |
51 | | - |
52 | | - await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
53 | | - } |
54 | | - [Fact] |
55 | | - public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Returns_Object() |
56 | | - { |
57 | | - _options.IsEnabled = true; |
58 | | - _options.AlwaysLogOnException = true; |
59 | | - |
60 | | - await GetResponseAsync("api/audit-test/audit-fail-object", System.Net.HttpStatusCode.Forbidden); |
61 | | - |
62 | | - await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
63 | | - } |
64 | | - } |
65 | | -} |
| 1 | +using System; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | +using NSubstitute; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Volo.Abp.Auditing; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Volo.Abp.AspNetCore.Mvc.Auditing |
| 12 | +{ |
| 13 | + public class AuditTestController_Tests : AspNetCoreMvcTestBase |
| 14 | + { |
| 15 | + private readonly AbpAuditingOptions _options; |
| 16 | + private IAuditingStore _auditingStore; |
| 17 | + |
| 18 | + public AuditTestController_Tests() |
| 19 | + { |
| 20 | + _options = ServiceProvider.GetRequiredService<IOptions<AbpAuditingOptions>>().Value; |
| 21 | + _auditingStore = ServiceProvider.GetRequiredService<IAuditingStore>(); |
| 22 | + } |
| 23 | + |
| 24 | + protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) |
| 25 | + { |
| 26 | + _auditingStore = Substitute.For<IAuditingStore>(); |
| 27 | + services.Replace(ServiceDescriptor.Singleton(_auditingStore)); |
| 28 | + base.ConfigureServices(context, services); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task Should_Trigger_Middleware_And_AuditLog_Success_For_GetRequests() |
| 33 | + { |
| 34 | + _options.IsEnabledForGetRequests = true; |
| 35 | + _options.AlwaysLogOnException = false; |
| 36 | + await GetResponseAsync("api/audit-test/audit-success"); |
| 37 | + await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task Should_Trigger_Middleware_And_AuditLog_Exception_Always() |
| 42 | + { |
| 43 | + _options.IsEnabled = true; |
| 44 | + _options.AlwaysLogOnException = true; |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + await GetResponseAsync("api/audit-test/audit-fail", System.Net.HttpStatusCode.Forbidden); |
| 49 | + } |
| 50 | + catch { } |
| 51 | + |
| 52 | + await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
| 53 | + } |
| 54 | + [Fact] |
| 55 | + public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Returns_Object() |
| 56 | + { |
| 57 | + _options.IsEnabled = true; |
| 58 | + _options.AlwaysLogOnException = true; |
| 59 | + |
| 60 | + await GetResponseAsync("api/audit-test/audit-fail-object", System.Net.HttpStatusCode.Forbidden); |
| 61 | + |
| 62 | + await _auditingStore.Received().SaveAsync(Arg.Any<AuditLogInfo>()); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments