test(server): auth, uploads, ranges, sync, jobs, render nodes, events

Covers bootstrap admin, sign-in failure, lockout and rate limit, token
revocation and expiry, 401 versus 403, CSRF on cookie sessions, the error
shape and API header, chunked upload happy path, resume, hash
short-circuit, damaged chunk, quota, Range on originals, thumbnails and
exports, path traversal, settings_version conflicts, job state
transitions with a fake handler, render node claim, heartbeat timeout and
reassignment, output upload, WebSocket progress events, the discovery
beacon, the Windows service command line and status rules, and a
PostgreSQL run (TITANO_TEST_PG).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 12:28:47 +00:00
1 parent c78747053e
commit 80b91d0585
16 files changed
+1646

No files matched your search

@@ -0,0 +1,41 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Titano.Protocol;
using Titano.Server.Api;
using Titano.Server.Hosting;
using Titano.Testing;
namespace Titano.Server.Tests.Hosting;
[Category("server")]
public sealed class DiscoveryTests(TestContext context)
{
[Test]
public async Task TheBeaconTravelsOverUdpAndParses()
{
await using var server = await TestServer.StartAsync(context, o => o.ServerName = "Studio NAS");
var service = server.Services.GetRequiredService<DiscoveryBeaconService>();
using var listener = new UdpClient(new IPEndPoint(IPAddress.Loopback, 0));
using var sender = new UdpClient(AddressFamily.InterNetwork);
await service.SendOnceAsync(sender, [(IPEndPoint)listener.Client.LocalEndPoint!], context.Cancellation);
var datagram = await listener.ReceiveAsync(context.Cancellation).AsTask().WaitAsync(TimeSpan.FromSeconds(5));
var beacon = Assert.NotNull(DiscoveryBeacon.TryParse(datagram.Buffer));
Assert.Equal("Studio NAS", beacon.Name);
Assert.Equal(SystemEndpoints.Version, beacon.Version);
Assert.Equal(server.HttpBase.Port, beacon.HttpPort);
Assert.Equal(server.HttpsBase!.Port, beacon.HttpsPort);
using var info = await server.Client.GetAsync("/api/v1/server/info");
Assert.Equal((await info.ReadAsync(ProtocolJson.Default.ServerInfo)).TlsFingerprint, beacon.TlsFingerprint);
}
[Test]
public void ForeignDatagramsAreIgnored()
{
Assert.Null(DiscoveryBeacon.TryParse(Encoding.UTF8.GetBytes("HELLO {}")));
Assert.Null(DiscoveryBeacon.TryParse(Encoding.UTF8.GetBytes("TITANO1 {not json")));
Assert.Null(DiscoveryBeacon.TryParse(new byte[4096]));
}
}
@@ -0,0 +1,67 @@
using Titano.Data;
using Titano.Server.Configuration;
using Titano.Server.Hosting;
using Titano.Testing;
namespace Titano.Server.Tests.Hosting;
[Category("server")]
public sealed class StartupTests(TestContext context)
{
[Test]
public void TheServerRefusesToStartWithoutTheArchiveModule()
{
var root = context.CreateTempDirectory("bare");
var options = new ServerOptions
{
DataPath = Path.Combine(root, "data"),
ArchivePath = Path.Combine(root, "archive"),
HttpPort = 0,
HttpsPort = 0,
Database = DatabaseKind.Sqlite,
Discovery = false,
};
var error = Assert.Throws<InvalidOperationException>(() => ServerHost.Build(options, []));
Assert.Contains("IArchiveIngest", error.Message);
Assert.Contains("IFrameAssetLocator", error.Message);
}
[Test]
public void TitanoDbAcceptsAConnectionString()
{
var root = context.CreateTempDirectory("env");
var options = ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "Host=db.lan;Port=6543;Database=archive;Username=titano;Password=secret",
});
Assert.Equal(DatabaseKind.Postgres, options.Database);
var database = options.ToDatabaseOptions();
Assert.Equal(Dialect.Postgres, database.Dialect);
Assert.Equal(6543, Titano.Data.Postgres.PostgresSettings.Parse(database.ConnectionString).Port);
}
[Test]
public void PartsBuildAQuotedPostgresConnectionString()
{
var options = new ServerOptions { DbHost = "postgres", DbName = "titano", DbUser = "titano", DbPassword = "it's;secret", DbSslMode = "disable" };
var settings = Titano.Data.Postgres.PostgresSettings.Parse(options.ToDatabaseOptions().ConnectionString);
Assert.Equal(("postgres", "titano", "it's;secret"), (settings.Host, settings.Database, settings.Password));
Assert.Equal(Titano.Data.Postgres.SslMode.Disable, settings.SslMode);
}
[Test]
public void EnvironmentVariablesLockTheirSettings()
{
var root = context.CreateTempDirectory("locked");
var options = ServerOptions.Load(new Dictionary<string, string?>
{
["TITANO_DATA_PATH"] = root,
["TITANO_DB"] = "postgres",
["TITANO_HTTP_PORT"] = "9090",
["TITANO_DISCOVERY"] = "false",
});
Assert.SequenceEqual(["Database", "HttpPort", "Discovery"], options.Locked);
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "many" }));
}
}
@@ -0,0 +1,55 @@
using Titano.Server.Hosting;
using Titano.Server.Hosting.Windows;
using Titano.Testing;
namespace Titano.Server.Tests.Hosting;
public sealed class WindowsServiceTests
{
[Test]
public void CommandSwitchesAreParsedAndTheRestPassedOn()
{
Assert.Equal(ServerCommand.Run, ServerCommandLine.Parse([]).Command);
var service = ServerCommandLine.Parse(["--service", "--urls", "x"]);
Assert.Equal(ServerCommand.Service, service.Command);
Assert.SequenceEqual(["--urls", "x"], service.HostArguments);
Assert.Equal(ServerCommand.InstallService, ServerCommandLine.Parse(["--install-service"]).Command);
Assert.Equal(ServerCommand.UninstallService, ServerCommandLine.Parse(["--uninstall-service"]).Command);
Assert.Equal(ServerCommand.HealthCheck, ServerCommandLine.Parse(["--healthcheck"]).Command);
Assert.Equal(ServerCommand.Help, ServerCommandLine.Parse(["/?"]).Command);
Assert.NotNull(ServerCommandLine.Parse(["--install-service", "--uninstall-service"]).Error);
Assert.Null(ServerCommandLine.Parse(["--service", "--service"]).Error);
}
[Test]
public void StatusReportsFollowTheServiceControlManagerRules()
{
var tracker = new ServiceStatusTracker();
var start = tracker.StartPending(60_000);
Assert.Equal((ServiceState.StartPending, 0u, 1u, 60_000u), (start.State, start.ControlsAccepted, start.CheckPoint, start.WaitHintMilliseconds));
Assert.Equal(2u, tracker.StartPending(60_000).CheckPoint);
var running = tracker.Running();
Assert.Equal((ServiceState.Running, ServiceStatusTracker.AcceptStop | ServiceStatusTracker.AcceptShutdown, 0u), (running.State, running.ControlsAccepted, running.CheckPoint));
Assert.Throws<InvalidOperationException>(() => tracker.Running());
Assert.Equal(1u, tracker.StopPending(35_000).CheckPoint);
Assert.Equal(2u, tracker.StopPending(35_000).CheckPoint);
var stopped = tracker.Stopped(0);
Assert.Equal((ServiceState.Stopped, 0u, 0u), (stopped.State, stopped.ControlsAccepted, stopped.CheckPoint));
Assert.Throws<InvalidOperationException>(() => tracker.StopPending(1));
Assert.Equal(1064u, new ServiceStatusTracker().Stopped(1064).ExitCode);
}
[Test]
[WindowsOnly]
public void TheDispatcherDeclinesAConsoleProcess()
{
if (!OperatingSystem.IsWindows()) return;
var built = false;
Assert.False(WindowsServiceHost.Run(() =>
{
built = true;
throw new InvalidOperationException("never built outside the SCM");
}));
Assert.False(built);
}
}