128 lines
3.6 KiB
C#
128 lines
3.6 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;
|
|
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));
|
|
});
|
|
}
|
|
} |