using System; using System.Collections.Generic; using System.IO; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using StellaOps.Scheduler.WebService.Options; using StellaOps.Scheduler.WebService.Runs; using StellaOps.Scheduler.ImpactIndex; namespace StellaOps.Scheduler.WebService.Tests; public sealed class SchedulerWebApplicationFactory : WebApplicationFactory { protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureAppConfiguration((_, configuration) => { var fixtureDirectory = GetFixtureDirectory(); configuration.AddInMemoryCollection(new[] { new KeyValuePair("Scheduler:Authority:Enabled", "false"), new KeyValuePair("Scheduler:Cartographer:Webhook:Enabled", "false"), new KeyValuePair("Scheduler:Events:GraphJobs:Enabled", "false"), new KeyValuePair("Scheduler:Events:Webhooks:Conselier:Enabled", "true"), new KeyValuePair("Scheduler:Events:Webhooks:Conselier:HmacSecret", "conselier-secret"), new KeyValuePair("Scheduler:Events:Webhooks:Conselier:RateLimitRequests", "20"), new KeyValuePair("Scheduler:Events:Webhooks:Conselier:RateLimitWindowSeconds", "60"), new KeyValuePair("Scheduler:Events:Webhooks:Excitor:Enabled", "true"), new KeyValuePair("Scheduler:Events:Webhooks:Excitor:HmacSecret", "excitor-secret"), new KeyValuePair("Scheduler:Events:Webhooks:Excitor:RateLimitRequests", "20"), new KeyValuePair("Scheduler:Events:Webhooks:Excitor:RateLimitWindowSeconds", "60"), new KeyValuePair("Scheduler:ImpactIndex:FixtureDirectory", fixtureDirectory) }); }); builder.ConfigureServices(services => { var fixtureDirectory = GetFixtureDirectory(); services.RemoveAll(); services.AddSingleton(new ImpactIndexStubOptions { FixtureDirectory = fixtureDirectory, SnapshotId = "tests/impact-index-stub" }); services.Configure(options => { options.Webhooks ??= new SchedulerInboundWebhooksOptions(); options.Webhooks.Conselier ??= SchedulerWebhookOptions.CreateDefault("conselier"); options.Webhooks.Excitor ??= SchedulerWebhookOptions.CreateDefault("excitor"); options.Webhooks.Conselier.HmacSecret = "conselier-secret"; options.Webhooks.Conselier.Enabled = true; options.Webhooks.Excitor.HmacSecret = "excitor-secret"; options.Webhooks.Excitor.Enabled = true; }); services.PostConfigure(options => { options.PollInterval = TimeSpan.FromMilliseconds(100); options.QueueLagInterval = TimeSpan.FromMilliseconds(200); options.HeartbeatInterval = TimeSpan.FromMilliseconds(150); }); }); } private static string GetFixtureDirectory() { var assemblyLocation = typeof(SchedulerWebApplicationFactory).Assembly.Location; var assemblyDirectory = Path.GetDirectoryName(assemblyLocation) ?? AppContext.BaseDirectory; var fixtureDirectory = Path.Combine(assemblyDirectory, "seed-data", "impact-index"); return Path.GetFullPath(fixtureDirectory); } }