129 lines
3.7 KiB
C#
129 lines
3.7 KiB
C#
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;
|
|
|
|
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 readonly 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();
|
|
var config = builder.Configuration;
|
|
builder.Services.AddSignalR();
|
|
builder.Services.AddAuthService(
|
|
config,
|
|
consumers => consumers.Add(new(0, "mock", "123", "client.mock")) // Set mock consumer
|
|
);
|
|
|
|
// 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();
|
|
|
|
// 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();
|
|
await _app.DisposeAsync();
|
|
Console.WriteLine("Test server stopped.");
|
|
}
|
|
|
|
while (_disposableAsync.Count > 0)
|
|
await _disposableAsync.Dequeue().DisposeAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReceiveMessage_ShouldCallOnMessageReceived()
|
|
{
|
|
// Arrange
|
|
string rcv_issuer = string.Empty;
|
|
string rcv_audience = string.Empty;
|
|
string rcv_key = string.Empty;
|
|
|
|
// Receiver client
|
|
var provider_receiver = Build(opt =>
|
|
{
|
|
opt.Url = _hubUrl;
|
|
opt.OnPublicKeyReceived += (client, issuer, audience, key, logger) =>
|
|
{
|
|
rcv_issuer = issuer;
|
|
rcv_audience = audience;
|
|
rcv_key = key;
|
|
};
|
|
});
|
|
var client_receiver = provider_receiver.GetRequiredService<IAuthClient>();
|
|
await client_receiver.StartAsync();
|
|
|
|
string issuer = "issuer";
|
|
string audience = "audience";
|
|
string key = "key";
|
|
|
|
// Act
|
|
await _notifier.UpdateKeyAsync(issuer, audience, key);
|
|
|
|
// delay fort getting answer
|
|
await Task.Delay(2000);
|
|
|
|
// Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(rcv_issuer, Is.EqualTo(issuer));
|
|
Assert.That(rcv_audience, Is.EqualTo(audience));
|
|
Assert.That(rcv_key, Is.EqualTo(key));
|
|
});
|
|
}
|
|
} |