feat(AuthHubTests): Erstellt, um Hub und Melder zu testen
This commit is contained in:
parent
0cce082cb7
commit
6694e4b626
128
DigitalData.Auth.Tests/API/AuthHubTests.cs
Normal file
128
DigitalData.Auth.Tests/API/AuthHubTests.cs
Normal file
@ -0,0 +1,128 @@
|
||||
using DigitalData.Auth.Abstractions;
|
||||
using DigitalData.Auth.Client;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using DigitalData.Auth.API.Services;
|
||||
using DigitalData.Auth.API.Services.Contracts;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace DigitalData.Auth.Tests.API;
|
||||
|
||||
// TODO: The test checks if the services are working. Performance measurement is ignored. Update it to measure performance as well.
|
||||
[TestFixture]
|
||||
public class AuthHubTests
|
||||
{
|
||||
private string _hubUrl;
|
||||
|
||||
private Func<Action<ClientParams>, ServiceProvider> Build;
|
||||
|
||||
private WebApplication? _app = null;
|
||||
|
||||
private readonly Queue<IAsyncDisposable> _disposableAsync = new();
|
||||
|
||||
private INotifier _notifier;
|
||||
|
||||
private static int AvailablePort
|
||||
{
|
||||
get
|
||||
{
|
||||
using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
int port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
|
||||
listener.Stop();
|
||||
return port;
|
||||
}
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
Build = options =>
|
||||
{
|
||||
var provider = new ServiceCollection()
|
||||
.AddAuthHubClient(options)
|
||||
.BuildServiceProvider();
|
||||
|
||||
_disposableAsync.Enqueue(provider);
|
||||
|
||||
return provider;
|
||||
};
|
||||
|
||||
// Create and run test server
|
||||
// Create builder and add SignalR service
|
||||
var builder = WebApplication.CreateBuilder();
|
||||
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string?>()
|
||||
{
|
||||
{ "Consumers", null }
|
||||
});
|
||||
var config = builder.Configuration;
|
||||
builder.Services.AddSignalR();
|
||||
builder.Services.AddAuthService(config);
|
||||
|
||||
// Listen AvailablePort and map hub
|
||||
var _app = builder.Build();
|
||||
var url = $"http://localhost:{AvailablePort}";
|
||||
var hubRoute = "/auth-hub";
|
||||
_hubUrl = url + hubRoute;
|
||||
_app.Urls.Add(url);
|
||||
_app.MapHub<Auth.API.Hubs.AuthHub>(hubRoute);
|
||||
_app.Start();
|
||||
|
||||
_disposableAsync.Enqueue(_app);
|
||||
|
||||
// Create notifier by app services
|
||||
_notifier = _app.Services.GetRequiredService<INotifier>();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public async Task TearDown()
|
||||
{
|
||||
// Stop test server
|
||||
if (_app is not null)
|
||||
{
|
||||
await _app.StopAsync();
|
||||
Console.WriteLine("Test server stopped.");
|
||||
}
|
||||
|
||||
while (_disposableAsync.Count > 0)
|
||||
await _disposableAsync.Dequeue().DisposeAsync();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReceiveMessage_ShouldCallOnMessageReceived()
|
||||
{
|
||||
// Arrange
|
||||
string rcv_name = string.Empty;
|
||||
string rcv_value = string.Empty;
|
||||
|
||||
// Receiver client
|
||||
var provider_receiver = Build(opt =>
|
||||
{
|
||||
opt.Url = _hubUrl;
|
||||
opt.Events.OnMessageReceived = (name, value, logger) =>
|
||||
{
|
||||
rcv_name = name;
|
||||
rcv_value = value;
|
||||
};
|
||||
});
|
||||
var client_receiver = provider_receiver.GetRequiredService<IAuthClient>();
|
||||
await client_receiver.StartAsync();
|
||||
|
||||
string name = "name";
|
||||
string value = "value";
|
||||
|
||||
// Act
|
||||
await _notifier.UpdateKeyAsync(name, value);
|
||||
|
||||
// delay fort getting answer
|
||||
await Task.Delay(2000);
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(rcv_name, Is.EqualTo(name));
|
||||
Assert.That(rcv_value, Is.EqualTo(value));
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -9,8 +9,21 @@
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="API\appsettings.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="API\appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user