diff --git a/DigitalData.Auth.Tests/API/AuthHubTests.cs b/DigitalData.Auth.Tests/API/AuthHubTests.cs new file mode 100644 index 0000000..e6ef12a --- /dev/null +++ b/DigitalData.Auth.Tests/API/AuthHubTests.cs @@ -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, ServiceProvider> Build; + + private WebApplication? _app = null; + + private readonly Queue _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() + { + { "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(hubRoute); + _app.Start(); + + _disposableAsync.Enqueue(_app); + + // Create notifier by app services + _notifier = _app.Services.GetRequiredService(); + } + + [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(); + 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)); + }); + } +} \ No newline at end of file diff --git a/DigitalData.Auth.Tests/DigitalData.Auth.Tests.csproj b/DigitalData.Auth.Tests/DigitalData.Auth.Tests.csproj index cb455f7..b22eaa5 100644 --- a/DigitalData.Auth.Tests/DigitalData.Auth.Tests.csproj +++ b/DigitalData.Auth.Tests/DigitalData.Auth.Tests.csproj @@ -9,8 +9,21 @@ true + + + + + + + PreserveNewest + true + PreserveNewest + + + +