mirror of https://github.com/abpframework/abp.git
committed by
GitHub
23 changed files with 763 additions and 8 deletions
@ -0,0 +1,17 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.test.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net10.0</TargetFramework> |
|||
<RootNamespace /> |
|||
<Nullable>enable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.Components.Server\Volo.Abp.AspNetCore.Components.Server.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,42 @@ |
|||
using System.Linq; |
|||
using Microsoft.Extensions.Options; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.Auditing; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Testing; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Server; |
|||
|
|||
public abstract class AbpAspNetCoreComponentsServerModule_Tests<TStartupModule> : AbpIntegratedTest<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
private const string ComponentHubTypeFullName = "Microsoft.AspNetCore.Components.Server.ComponentHub"; |
|||
|
|||
private readonly AbpAspNetCoreAuditingOptions _auditingOptions; |
|||
|
|||
protected AbpAspNetCoreComponentsServerModule_Tests() |
|||
{ |
|||
_auditingOptions = GetRequiredService<IOptions<AbpAspNetCoreAuditingOptions>>().Value; |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Only_Ignore_ComponentHub_For_Hub_Auditing() |
|||
{ |
|||
_auditingOptions.IgnoredHubTypes.Single().FullName.ShouldBe(ComponentHubTypeFullName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Ignore_Blazor_Urls_For_Auditing() |
|||
{ |
|||
_auditingOptions.IgnoredUrls.ShouldContain("/_blazor"); |
|||
} |
|||
} |
|||
|
|||
public class AbpAspNetCoreComponentsServerModule_BlazorServer_Tests : AbpAspNetCoreComponentsServerModule_Tests<AbpAspNetCoreComponentsServerTestModule> |
|||
{ |
|||
} |
|||
|
|||
public class AbpAspNetCoreComponentsServerModule_BlazorWebApp_Tests : AbpAspNetCoreComponentsServerModule_Tests<AbpAspNetCoreComponentsServerWebAppTestModule> |
|||
{ |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Server; |
|||
|
|||
[DependsOn(typeof(AbpAspNetCoreComponentsServerModule))] |
|||
public class AbpAspNetCoreComponentsServerTestModule : AbpModule |
|||
{ |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using Volo.Abp.AspNetCore.Components.Web; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Server; |
|||
|
|||
[DependsOn(typeof(AbpAspNetCoreComponentsServerModule))] |
|||
public class AbpAspNetCoreComponentsServerWebAppTestModule : AbpModule |
|||
{ |
|||
public override void PreConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
PreConfigure<AbpAspNetCoreComponentsWebOptions>(options => |
|||
{ |
|||
options.IsBlazorWebApp = true; |
|||
}); |
|||
} |
|||
} |
|||
@ -0,0 +1,242 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.SignalR; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using NSubstitute; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.Auditing; |
|||
using Volo.Abp.AspNetCore.SignalR.Auditing.TestApp; |
|||
using Volo.Abp.Auditing; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.SignalR.Auditing; |
|||
|
|||
[Collection("AbpAspNetCoreSignalR")] |
|||
public class AbpAuditHubFilter_Tests : AbpAspNetCoreTestBase<Program> |
|||
{ |
|||
private IAuditingStore _auditingStore; |
|||
|
|||
public AbpAuditHubFilter_Tests() |
|||
{ |
|||
_auditingStore = GetRequiredService<IAuditingStore>(); |
|||
} |
|||
|
|||
protected override void ConfigureServices(IServiceCollection services) |
|||
{ |
|||
services.Configure<AbpAspNetCoreAuditingOptions>(options => |
|||
{ |
|||
options.IgnoredHubTypes.Add(typeof(IgnoredAuditHub)); |
|||
}); |
|||
|
|||
_auditingStore = Substitute.For<IAuditingStore>(); |
|||
services.Replace(ServiceDescriptor.Singleton(_auditingStore)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Begin_An_Audit_Scope_For_Regular_Hubs() |
|||
{ |
|||
(await HasAuditScopeInHubMethodAsync(new RegularAuditHub())).ShouldBeTrue(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Begin_An_Audit_Scope_For_Ignored_Hubs() |
|||
{ |
|||
(await HasAuditScopeInHubMethodAsync(new IgnoredAuditHub())).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Begin_An_Audit_Scope_For_Hubs_Derived_From_Ignored_Hubs() |
|||
{ |
|||
(await HasAuditScopeInHubMethodAsync(new DerivedIgnoredAuditHub())).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Save_The_Hub_Audit_Log_After_A_Regular_Hub_Method_Completes() |
|||
{ |
|||
var noteId = await CreateNoteAsync("old"); |
|||
using var scope = GetRequiredService<IServiceScopeFactory>().CreateScope(); |
|||
var noteService = scope.ServiceProvider.GetRequiredService<AuditedNoteService>(); |
|||
|
|||
var invocationTask = InvokeHubMethodAsync(scope.ServiceProvider, new RegularAuditHub(), async () => |
|||
{ |
|||
await noteService.UpdateTextAsync(noteId, "new"); |
|||
}); |
|||
|
|||
await Task.Delay(100); |
|||
invocationTask.IsCompleted.ShouldBeFalse(); |
|||
await _auditingStore.DidNotReceive().SaveAsync(Arg.Any<AuditLogInfo>()); |
|||
|
|||
GetRequiredService<AuditedWorkSignal>().Complete(); |
|||
await invocationTask; |
|||
|
|||
await ShouldHaveSavedTheNoteUpdateAsync(noteId); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Save_The_Audit_Log_Of_A_Service_That_Completes_After_An_Ignored_Hub_Method_Returns() |
|||
{ |
|||
var noteId = await CreateNoteAsync("old"); |
|||
using var scope = GetRequiredService<IServiceScopeFactory>().CreateScope(); |
|||
|
|||
var serviceTask = await StartServiceInIgnoredHubMethodAsync(scope.ServiceProvider, noteId); |
|||
await _auditingStore.DidNotReceive().SaveAsync(Arg.Any<AuditLogInfo>()); |
|||
|
|||
GetRequiredService<AuditedWorkSignal>().Complete(); |
|||
await serviceTask; |
|||
|
|||
await ShouldHaveSavedTheNoteUpdateAsync(noteId); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Save_The_Entity_Changes_Saved_By_The_Unit_Of_Work_Of_A_Service_That_Completes_After_An_Ignored_Hub_Method_Returns() |
|||
{ |
|||
var noteId = await CreateNoteAsync("old"); |
|||
using var scope = GetRequiredService<IServiceScopeFactory>().CreateScope(); |
|||
|
|||
var serviceTask = await StartServiceInIgnoredHubMethodAsync( |
|||
scope.ServiceProvider, |
|||
noteService => noteService.UpdateTextWithoutSavingAsync(noteId, "new")); |
|||
await _auditingStore.DidNotReceive().SaveAsync(Arg.Any<AuditLogInfo>()); |
|||
|
|||
GetRequiredService<AuditedWorkSignal>().Complete(); |
|||
await serviceTask; |
|||
|
|||
await ShouldHaveSavedTheNoteUpdateAsync(noteId, nameof(AuditedNoteService.UpdateTextWithoutSavingAsync)); |
|||
(await GetRequiredService<IRepository<AuditedNote, Guid>>().GetAsync(noteId)).Text.ShouldBe("new"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Save_The_Audit_Log_Of_A_Service_Called_By_An_Ignored_Hub_Over_A_Non_WebSocket_Transport() |
|||
{ |
|||
var noteId = await CreateNoteAsync("old"); |
|||
using var scope = GetRequiredService<IServiceScopeFactory>().CreateScope(); |
|||
|
|||
// Server-Sent Events keep the connection on a single GET request.
|
|||
var httpContextAccessor = scope.ServiceProvider.GetRequiredService<IHttpContextAccessor>(); |
|||
var connectionRequest = new DefaultHttpContext { RequestServices = scope.ServiceProvider }; |
|||
connectionRequest.Request.Method = HttpMethods.Get; |
|||
connectionRequest.Request.Path = "/signalr-hubs/ignored"; |
|||
httpContextAccessor.HttpContext = connectionRequest; |
|||
|
|||
try |
|||
{ |
|||
var serviceTask = await StartServiceInIgnoredHubMethodAsync(scope.ServiceProvider, noteId); |
|||
|
|||
GetRequiredService<AuditedWorkSignal>().Complete(); |
|||
await serviceTask; |
|||
} |
|||
finally |
|||
{ |
|||
httpContextAccessor.HttpContext = null; |
|||
} |
|||
|
|||
await ShouldHaveSavedTheNoteUpdateAsync(noteId); |
|||
await _auditingStore.Received(1).SaveAsync(Arg.Is<AuditLogInfo>(x => x.HttpMethod == null)); |
|||
} |
|||
|
|||
private Task<Task> StartServiceInIgnoredHubMethodAsync(IServiceProvider serviceProvider, Guid noteId) |
|||
{ |
|||
return StartServiceInIgnoredHubMethodAsync(serviceProvider, noteService => noteService.UpdateTextAsync(noteId, "new")); |
|||
} |
|||
|
|||
private async Task<Task> StartServiceInIgnoredHubMethodAsync(IServiceProvider serviceProvider, Func<AuditedNoteService, Task> serviceCall) |
|||
{ |
|||
var noteService = serviceProvider.GetRequiredService<AuditedNoteService>(); |
|||
|
|||
Task serviceTask = null; |
|||
await InvokeHubMethodAsync(serviceProvider, new IgnoredAuditHub(), () => |
|||
{ |
|||
// Like Blazor's ComponentHub, return before the started work completes.
|
|||
serviceTask = serviceCall(noteService); |
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
return serviceTask; |
|||
} |
|||
|
|||
private async Task<Guid> CreateNoteAsync(string text) |
|||
{ |
|||
var noteId = Guid.NewGuid(); |
|||
using (var uow = GetRequiredService<IUnitOfWorkManager>().Begin(requiresNew: true)) |
|||
{ |
|||
await GetRequiredService<IRepository<AuditedNote, Guid>>().InsertAsync(new AuditedNote(noteId, text)); |
|||
await uow.CompleteAsync(); |
|||
} |
|||
|
|||
_auditingStore.ClearReceivedCalls(); |
|||
return noteId; |
|||
} |
|||
|
|||
private async Task ShouldHaveSavedTheNoteUpdateAsync(Guid noteId, string methodName = nameof(AuditedNoteService.UpdateTextAsync)) |
|||
{ |
|||
await _auditingStore.Received(1).SaveAsync(Arg.Any<AuditLogInfo>()); |
|||
await _auditingStore.Received(1).SaveAsync(Arg.Is<AuditLogInfo>(x => |
|||
x.Actions.Count == 1 && |
|||
x.Actions[0].MethodName == methodName && |
|||
x.EntityChanges.Count == 1 && |
|||
x.EntityChanges[0].ChangeType == EntityChangeType.Updated && |
|||
x.EntityChanges[0].EntityTypeFullName == typeof(AuditedNote).FullName && |
|||
x.EntityChanges[0].EntityId == noteId.ToString() && |
|||
x.EntityChanges[0].PropertyChanges.Any(p => |
|||
p.PropertyName == nameof(AuditedNote.Text) && |
|||
p.OriginalValue == "\"old\"" && |
|||
p.NewValue == "\"new\""))); |
|||
} |
|||
|
|||
private async Task<bool> HasAuditScopeInHubMethodAsync(Hub hub) |
|||
{ |
|||
using var scope = GetRequiredService<IServiceScopeFactory>().CreateScope(); |
|||
var auditingManager = scope.ServiceProvider.GetRequiredService<IAuditingManager>(); |
|||
|
|||
var hasAuditScope = false; |
|||
await InvokeHubMethodAsync(scope.ServiceProvider, hub, () => |
|||
{ |
|||
hasAuditScope = auditingManager.Current != null; |
|||
return Task.CompletedTask; |
|||
}); |
|||
|
|||
return hasAuditScope; |
|||
} |
|||
|
|||
private static async Task InvokeHubMethodAsync(IServiceProvider serviceProvider, Hub hub, Func<Task> hubMethod) |
|||
{ |
|||
var invocationContext = new HubInvocationContext( |
|||
new TestHubCallerContext(), |
|||
serviceProvider, |
|||
hub, |
|||
typeof(RegularAuditHub).GetMethod(nameof(RegularAuditHub.Ping)), |
|||
Array.Empty<object>()); |
|||
|
|||
// AbpAspNetCoreSignalRModule also registers the hub context accessor filter before the audit filter.
|
|||
await new AbpHubContextAccessorHubFilter().InvokeMethodAsync(invocationContext, context => |
|||
new AbpAuditHubFilter().InvokeMethodAsync(context, async _ => |
|||
{ |
|||
await hubMethod(); |
|||
return null; |
|||
})); |
|||
} |
|||
|
|||
[DisableConventionalRegistration] |
|||
public class RegularAuditHub : Hub |
|||
{ |
|||
public void Ping() |
|||
{ |
|||
} |
|||
} |
|||
|
|||
[DisableConventionalRegistration] |
|||
public class IgnoredAuditHub : Hub |
|||
{ |
|||
} |
|||
|
|||
[DisableConventionalRegistration] |
|||
public class DerivedIgnoredAuditHub : IgnoredAuditHub |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
using System.Linq; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.SignalR.Auditing; |
|||
|
|||
[Collection("AbpAspNetCoreSignalR")] |
|||
public class AbpSignalRAuditingMiddleware_Tests : AbpAspNetCoreTestBase<Program> |
|||
{ |
|||
[Theory] |
|||
[InlineData("/signalr-hubs/regular-abp/negotiate?negotiateVersion=1")] |
|||
[InlineData("/custom-route-hub/negotiate?negotiateVersion=1")] |
|||
public async Task Should_Not_Begin_An_Audit_Scope_For_The_Requests_Of_The_Auto_Mapped_Hubs(string url) |
|||
{ |
|||
var response = await Client.PostAsync(url, null); |
|||
|
|||
response.StatusCode.ShouldBe(HttpStatusCode.OK); |
|||
IsAuditScopeStarted(response).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Begin_An_Audit_Scope_For_The_Requests_Of_The_Ignored_Hub_Types() |
|||
{ |
|||
var response = await Client.PostAsync("/ignored-auditing-hub/negotiate?negotiateVersion=1", null); |
|||
|
|||
response.StatusCode.ShouldBe(HttpStatusCode.OK); |
|||
IsAuditScopeStarted(response).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Begin_An_Audit_Scope_For_The_Requests_Of_The_Other_Manually_Mapped_Hubs() |
|||
{ |
|||
var response = await Client.PostAsync("/manually-mapped-hub/negotiate?negotiateVersion=1", null); |
|||
|
|||
response.StatusCode.ShouldBe(HttpStatusCode.OK); |
|||
IsAuditScopeStarted(response).ShouldBeTrue(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Begin_An_Audit_Scope_For_The_Other_Requests() |
|||
{ |
|||
var response = await Client.GetAsync("/not-a-hub"); |
|||
|
|||
IsAuditScopeStarted(response).ShouldBeTrue(); |
|||
} |
|||
|
|||
private static bool IsAuditScopeStarted(HttpResponseMessage response) |
|||
{ |
|||
return bool.Parse(response.Headers.GetValues(AbpAspNetCoreSignalRTestModule.AuditScopeStartedHeaderName).Single()); |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.AspNetCore.SignalR.Auditing.TestApp; |
|||
|
|||
public class AuditedNote : Entity<Guid> |
|||
{ |
|||
public string Text { get; set; } |
|||
|
|||
protected AuditedNote() |
|||
{ |
|||
} |
|||
|
|||
public AuditedNote(Guid id, string text) |
|||
: base(id) |
|||
{ |
|||
Text = text; |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Auditing; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.AspNetCore.SignalR.Auditing.TestApp; |
|||
|
|||
[Audited] |
|||
public class AuditedNoteService : ITransientDependency |
|||
{ |
|||
private readonly IRepository<AuditedNote, Guid> _noteRepository; |
|||
private readonly AuditedWorkSignal _signal; |
|||
|
|||
public AuditedNoteService(IRepository<AuditedNote, Guid> noteRepository, AuditedWorkSignal signal) |
|||
{ |
|||
_noteRepository = noteRepository; |
|||
_signal = signal; |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task UpdateTextAsync(Guid id, string text) |
|||
{ |
|||
await _signal.Task; |
|||
|
|||
var note = await _noteRepository.GetAsync(id); |
|||
note.Text = text; |
|||
await _noteRepository.UpdateAsync(note, autoSave: true); |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task UpdateTextWithoutSavingAsync(Guid id, string text) |
|||
{ |
|||
await _signal.Task; |
|||
|
|||
// The change is saved when the unit of work completes.
|
|||
var note = await _noteRepository.GetAsync(id); |
|||
note.Text = text; |
|||
} |
|||
} |
|||
|
|||
public class AuditedWorkSignal : ISingletonDependency |
|||
{ |
|||
private readonly TaskCompletionSource _completionSource = new(TaskCreationOptions.RunContinuationsAsynchronously); |
|||
|
|||
public Task Task => _completionSource.Task; |
|||
|
|||
public void Complete() => _completionSource.TrySetResult(); |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace Volo.Abp.AspNetCore.SignalR.Auditing.TestApp; |
|||
|
|||
public class SignalRAuditingTestDbContext : AbpDbContext<SignalRAuditingTestDbContext> |
|||
{ |
|||
public DbSet<AuditedNote> AuditedNotes { get; set; } |
|||
|
|||
public SignalRAuditingTestDbContext(DbContextOptions<SignalRAuditingTestDbContext> options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
using System.Collections.Generic; |
|||
using System.Security.Claims; |
|||
using System.Threading; |
|||
using Microsoft.AspNetCore.Http.Features; |
|||
using Microsoft.AspNetCore.SignalR; |
|||
|
|||
namespace Volo.Abp.AspNetCore.SignalR.Auditing.TestApp; |
|||
|
|||
public class TestHubCallerContext : HubCallerContext |
|||
{ |
|||
public override string ConnectionId => "test-connection"; |
|||
|
|||
public override string UserIdentifier => null; |
|||
|
|||
public override ClaimsPrincipal User => null; |
|||
|
|||
public override IDictionary<object, object> Items { get; } = new Dictionary<object, object>(); |
|||
|
|||
public override IFeatureCollection Features { get; } = new FeatureCollection(); |
|||
|
|||
public override CancellationToken ConnectionAborted => CancellationToken.None; |
|||
|
|||
public override void Abort() |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using Microsoft.AspNetCore.SignalR; |
|||
|
|||
namespace Volo.Abp.AspNetCore.SignalR.SampleHubs; |
|||
|
|||
[DisableAutoHubMap] |
|||
public class IgnoredAuditingHub : Hub |
|||
{ |
|||
} |
|||
@ -0,0 +1,146 @@ |
|||
using System.Linq; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.SignalR; |
|||
using Microsoft.AspNetCore.TestHost; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Shouldly; |
|||
using Volo.Abp.Auditing; |
|||
using Volo.Abp.Modularity; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Auditing; |
|||
|
|||
public class AbpAuditingMiddleware_HubRequest_Tests |
|||
{ |
|||
[Fact] |
|||
public async Task Should_Begin_An_Audit_Scope_For_Hub_Requests_By_Default() |
|||
{ |
|||
(await IsAuditScopeStartedForHubRequestAsync<NativeSignalRAuditingTestModule>()).ShouldBeTrue(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Begin_An_Audit_Scope_For_The_Requests_Of_An_Ignored_Hub_Type() |
|||
{ |
|||
(await IsAuditScopeStartedForHubRequestAsync<IgnoredHubTypeTestModule>()).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Not_Begin_An_Audit_Scope_For_The_Requests_Of_A_Hub_Derived_From_An_Ignored_Hub_Type() |
|||
{ |
|||
(await IsAuditScopeStartedForHubRequestAsync<IgnoredBaseHubTypeTestModule>()).ShouldBeFalse(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Begin_An_Audit_Scope_For_The_Requests_Of_The_Other_Hubs() |
|||
{ |
|||
(await IsAuditScopeStartedForHubRequestAsync<IgnoredOtherHubTypeTestModule>()).ShouldBeTrue(); |
|||
} |
|||
|
|||
private static async Task<bool> IsAuditScopeStartedForHubRequestAsync<TModule>() |
|||
where TModule : IAbpModule |
|||
{ |
|||
var builder = WebApplication.CreateBuilder(); |
|||
builder.WebHost.UseTestServer(); |
|||
builder.Host.UseAutofac(); |
|||
await builder.AddApplicationAsync<TModule>(); |
|||
|
|||
await using var app = builder.Build(); |
|||
await app.InitializeApplicationAsync(); |
|||
await app.StartAsync(); |
|||
|
|||
try |
|||
{ |
|||
using var client = ((IHost)app).GetTestClient(); |
|||
var response = await client.PostAsync("/native-hub/negotiate?negotiateVersion=1", null); |
|||
|
|||
response.StatusCode.ShouldBe(HttpStatusCode.OK); |
|||
return bool.Parse(response.Headers.GetValues(NativeSignalRAuditingTestModule.AuditScopeStartedHeaderName).Single()); |
|||
} |
|||
finally |
|||
{ |
|||
await app.StopAsync(); |
|||
} |
|||
} |
|||
|
|||
public class NativeTestHubBase : Hub |
|||
{ |
|||
} |
|||
|
|||
public class NativeTestHub : NativeTestHubBase |
|||
{ |
|||
} |
|||
|
|||
public class OtherNativeTestHub : Hub |
|||
{ |
|||
} |
|||
|
|||
[DependsOn(typeof(AbpAspNetCoreTestModule))] |
|||
public class NativeSignalRAuditingTestModule : AbpModule |
|||
{ |
|||
public const string AuditScopeStartedHeaderName = "X-Audit-Scope-Started"; |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddSignalR(); |
|||
} |
|||
|
|||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|||
{ |
|||
var app = context.GetApplicationBuilder(); |
|||
|
|||
app.UseRouting(); |
|||
app.UseAuditing(); |
|||
app.Use(async (httpContext, next) => |
|||
{ |
|||
httpContext.Response.Headers[AuditScopeStartedHeaderName] = |
|||
(httpContext.RequestServices.GetRequiredService<IAuditingManager>().Current != null).ToString(); |
|||
|
|||
await next(); |
|||
}); |
|||
app.UseEndpoints(endpoints => |
|||
{ |
|||
endpoints.MapHub<NativeTestHub>("/native-hub"); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
[DependsOn(typeof(NativeSignalRAuditingTestModule))] |
|||
public class IgnoredHubTypeTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
Configure<AbpAspNetCoreAuditingOptions>(options => |
|||
{ |
|||
options.IgnoredHubTypes.Add(typeof(NativeTestHub)); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
[DependsOn(typeof(NativeSignalRAuditingTestModule))] |
|||
public class IgnoredBaseHubTypeTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
Configure<AbpAspNetCoreAuditingOptions>(options => |
|||
{ |
|||
options.IgnoredHubTypes.Add(typeof(NativeTestHubBase)); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
[DependsOn(typeof(NativeSignalRAuditingTestModule))] |
|||
public class IgnoredOtherHubTypeTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
Configure<AbpAspNetCoreAuditingOptions>(options => |
|||
{ |
|||
options.IgnoredHubTypes.Add(typeof(OtherNativeTestHub)); |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue