using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using StellaOps.Scanner.WebService.Options; using StellaOps.Scanner.WebService.Services; namespace StellaOps.Scanner.WebService.Tests; public sealed class PlatformEventPublisherRegistrationTests { [Fact] public void NullPublisherRegisteredWhenEventsDisabled() { using var factory = new ScannerApplicationFactory(configuration => { configuration["scanner:events:enabled"] = "false"; configuration["scanner:events:dsn"] = string.Empty; }); using var scope = factory.Services.CreateScope(); var publisher = scope.ServiceProvider.GetRequiredService(); Assert.IsType(publisher); } [Fact] public void RedisPublisherRegisteredWhenEventsEnabled() { var originalEnabled = Environment.GetEnvironmentVariable("SCANNER__EVENTS__ENABLED"); var originalDriver = Environment.GetEnvironmentVariable("SCANNER__EVENTS__DRIVER"); var originalDsn = Environment.GetEnvironmentVariable("SCANNER__EVENTS__DSN"); var originalStream = Environment.GetEnvironmentVariable("SCANNER__EVENTS__STREAM"); var originalTimeout = Environment.GetEnvironmentVariable("SCANNER__EVENTS__PUBLISHTIMEOUTSECONDS"); var originalMax = Environment.GetEnvironmentVariable("SCANNER__EVENTS__MAXSTREAMLENGTH"); Environment.SetEnvironmentVariable("SCANNER__EVENTS__ENABLED", "true"); Environment.SetEnvironmentVariable("SCANNER__EVENTS__DRIVER", "redis"); Environment.SetEnvironmentVariable("SCANNER__EVENTS__DSN", "localhost:6379"); Environment.SetEnvironmentVariable("SCANNER__EVENTS__STREAM", "stella.events.tests"); Environment.SetEnvironmentVariable("SCANNER__EVENTS__PUBLISHTIMEOUTSECONDS", "1"); Environment.SetEnvironmentVariable("SCANNER__EVENTS__MAXSTREAMLENGTH", "100"); try { using var factory = new ScannerApplicationFactory(configuration => { configuration["scanner:events:enabled"] = "true"; configuration["scanner:events:driver"] = "redis"; configuration["scanner:events:dsn"] = "localhost:6379"; configuration["scanner:events:stream"] = "stella.events.tests"; configuration["scanner:events:publishTimeoutSeconds"] = "1"; configuration["scanner:events:maxStreamLength"] = "100"; }); using var scope = factory.Services.CreateScope(); var options = scope.ServiceProvider.GetRequiredService>().Value; Assert.True(options.Events.Enabled); Assert.Equal("redis", options.Events.Driver); var publisher = scope.ServiceProvider.GetRequiredService(); Assert.IsType(publisher); } finally { Environment.SetEnvironmentVariable("SCANNER__EVENTS__ENABLED", originalEnabled); Environment.SetEnvironmentVariable("SCANNER__EVENTS__DRIVER", originalDriver); Environment.SetEnvironmentVariable("SCANNER__EVENTS__DSN", originalDsn); Environment.SetEnvironmentVariable("SCANNER__EVENTS__STREAM", originalStream); Environment.SetEnvironmentVariable("SCANNER__EVENTS__PUBLISHTIMEOUTSECONDS", originalTimeout); Environment.SetEnvironmentVariable("SCANNER__EVENTS__MAXSTREAMLENGTH", originalMax); } } }