6ff8665ab3
RestartWarningSeconds on Scripts (0 for existing ones) and RestartDueUtc on Jobs, for SQLite, PostgreSQL and SQL Server. A sequence step's Restart On Completion carries the warning to the agent, and a Scheduled step restart waits like Requested and Restarting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
5.1 KiB
C#
125 lines
5.1 KiB
C#
using Backend.Auth;
|
|
using Backend.Data;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.AspNetCore.TestHost;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NATS.NKeys;
|
|
|
|
namespace Backend.Tests;
|
|
|
|
/// <summary>
|
|
/// Boots the backend with an isolated SQLite database and a freshly generated
|
|
/// account signing key. NATS itself is not required for these tests.
|
|
/// </summary>
|
|
public class BackendTestFixture : WebApplicationFactory<Program>
|
|
{
|
|
public KeyPair AccountKey { get; } = KeyPair.CreatePair(PrefixByte.Account);
|
|
public KeyPair SigningKey { get; } = KeyPair.CreatePair(PrefixByte.Account);
|
|
public string DatabasePath { get; } =
|
|
Path.Combine(Path.GetTempPath(), $"backend-tests-{Guid.NewGuid():N}.db");
|
|
public string DataPath { get; } =
|
|
Path.Combine(Path.GetTempPath(), $"backend-tests-data-{Guid.NewGuid():N}");
|
|
|
|
/// <summary>Settings a test needs on top of these; applied last.</summary>
|
|
public Dictionary<string, string?> Settings { get; } = [];
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
{
|
|
builder.UseSetting("DATABASE_PATH", DatabasePath);
|
|
// Own data root per fixture (the agent catalog keeps package copies there);
|
|
// tests drive catalog syncs themselves.
|
|
builder.UseSetting("DATA_PATH", DataPath);
|
|
builder.UseSetting("AGENT_CATALOG_SYNC", "0");
|
|
builder.UseSetting("S3_EMBEDDED", "0");
|
|
builder.UseSetting("NATS_ACCOUNT_SIGNING_SEED", SigningKey.GetSeed());
|
|
builder.UseSetting("NATS_ACCOUNT_PUBLIC_KEY", AccountKey.GetPublicKey());
|
|
builder.UseSetting("NATS_URL", string.Empty);
|
|
builder.UseSetting("RATE_LIMIT_PER_MINUTE", "10000");
|
|
// Tests drive schedule sweeps manually; the background worker would
|
|
// race them (fire-and-advance between seeding steps).
|
|
builder.UseSetting("SCHEDULE_WORKER", "0");
|
|
// TestServer never terminates TLS; skip chain generation (and its
|
|
// file writes, which would race across parallel fixtures).
|
|
builder.UseSetting("TLS_SELF_SIGNED", "0");
|
|
builder.UseEnvironment("Development");
|
|
foreach (var (name, value) in Settings)
|
|
{
|
|
builder.UseSetting(name, value);
|
|
}
|
|
|
|
// Sign-in methods that reach outside the process get test doubles (OIDC uses a real loopback provider).
|
|
builder.ConfigureTestServices(services =>
|
|
{
|
|
services.AddSingleton(Directory);
|
|
services.AddSingleton<ILdapDirectory>(Directory);
|
|
services.AddSingleton<ILocalLogon>(Windows);
|
|
services.AddSingleton<ILocalAccounts>(Windows);
|
|
services.AddSingleton<Backend.Jobs.IScriptCommandSender>(Scripts);
|
|
services.AddSingleton<Backend.Jobs.IAgentSettingsSender>(AgentSettings);
|
|
services.AddSingleton<Backend.Jobs.IRestartCommandSender>(Restarts);
|
|
services.AddSingleton<Backend.DeviceFiles.IDeviceFileClient>(DeviceFiles);
|
|
services.AddSingleton<Backend.Maintenance.IBackupObjectStore>(BackupStore);
|
|
services.AddSingleton<Backend.Screen.IScreenAgentClient>(ScreenAgents);
|
|
});
|
|
}
|
|
|
|
/// <summary>Stands in for the S3 store database backups go to; unconfigured until a test says otherwise.</summary>
|
|
public FakeBackupObjectStore BackupStore { get; } = new();
|
|
|
|
/// <summary>Stands in for devices' Remote View: echoes the browser's bytes back.</summary>
|
|
public FakeScreenAgents ScreenAgents { get; } = new();
|
|
|
|
/// <summary>Stands in for NATS: refuses every script command, as an unconfigured hub does, until told otherwise.</summary>
|
|
public RecordingScriptSender Scripts { get; } = new();
|
|
|
|
/// <summary>Stands in for NATS for restart cancellations: records each one.</summary>
|
|
public RecordingRestartSender Restarts { get; } = new();
|
|
|
|
/// <summary>Stands in for NATS for Agent Settings pushes: records each one.</summary>
|
|
public RecordingSettingsSender AgentSettings { get; } = new();
|
|
|
|
/// <summary>Stands in for a device's agent.</summary>
|
|
public FakeDeviceFileClient DeviceFiles { get; } = new();
|
|
|
|
public FakeDirectory Directory { get; } = new();
|
|
|
|
public FakeWindowsAccounts Windows { get; } = new();
|
|
|
|
public async Task<T> WithDbAsync<T>(Func<AppDbContext, Task<T>> action)
|
|
{
|
|
using var scope = Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
return await action(db);
|
|
}
|
|
|
|
public Task WithDbAsync(Func<AppDbContext, Task> action)
|
|
=> WithDbAsync(async db =>
|
|
{
|
|
await action(db);
|
|
return true;
|
|
});
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
if (disposing)
|
|
{
|
|
AccountKey.Dispose();
|
|
SigningKey.Dispose();
|
|
try
|
|
{
|
|
File.Delete(DatabasePath);
|
|
if (System.IO.Directory.Exists(DataPath))
|
|
{
|
|
System.IO.Directory.Delete(DataPath, recursive: true);
|
|
}
|
|
}
|
|
catch (IOException)
|
|
{
|
|
// Best effort.
|
|
}
|
|
}
|
|
}
|
|
}
|