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:
16 files changed
+1646
No files matched your search
@@ -0,0 +1,89 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Jobs;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Tests.Jobs;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Postgres;
|
||||
|
||||
/// <summary>The server on PostgreSQL (<c>TITANO_TEST_PG</c>), each test in a private schema.</summary>
|
||||
[Category("server")]
|
||||
[RequiresEnvironment("TITANO_TEST_PG")]
|
||||
public sealed class PostgresServerTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task SignInUploadSyncAndJobsWorkOnPostgres()
|
||||
{
|
||||
await using var schema = await Schema.CreateAsync(context);
|
||||
var handler = new BlockingJobHandler(JobKind.Thumbs);
|
||||
await using var server = await TestServer.StartAsync(context, o =>
|
||||
{
|
||||
o.Database = DatabaseKind.Postgres;
|
||||
o.DbConnectionString = schema.ConnectionString;
|
||||
o.ChunkSizeBytes = 64 * 1024;
|
||||
}, s => s.AddSingleton<IJobHandler>(handler));
|
||||
Assert.Equal(Dialect.Postgres, server.Database.Dialect);
|
||||
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor", quotaBytes: 10_000_000);
|
||||
var data = Uploads.Photo(150_000, 71);
|
||||
var frame = await Uploads.UploadAsync(editor, data, sessionKey: "pg");
|
||||
using (var manifest = await editor.GetAsync($"/api/v1/sequences/{frame.SequenceId}/manifest"))
|
||||
Assert.Equal(Uploads.Sha(data), (await manifest.ReadAsync(ProtocolJson.Default.SequenceManifest)).Frames.Single().Sha256);
|
||||
using (var me = await editor.GetAsync("/api/v1/auth/me")) Assert.Equal(data.Length, (await me.ReadAsync(ProtocolJson.Default.UserDto)).UsedBytes);
|
||||
|
||||
using (var stale = await editor.PatchAsJsonAsync($"/api/v1/sequences/{frame.SequenceId}", new UpdateSequenceRequest(null, ["pg"], null, null, "{}", 99),
|
||||
ProtocolJson.Default.UpdateSequenceRequest))
|
||||
await stale.ExpectErrorAsync(HttpStatusCode.Conflict, "settings_conflict");
|
||||
|
||||
// Uploading queued a background thumbnail job for the sequence; it runs on the server's engine.
|
||||
using var queued = await editor.GetAsync("/api/v1/jobs?kind=thumbs");
|
||||
var job = (await queued.ReadAsync(ProtocolJson.Default.PageJobDto)).Items.Single();
|
||||
await JobApiTests.WaitForAsync(editor, job.Id, "running");
|
||||
handler.Release();
|
||||
await JobApiTests.WaitForAsync(editor, job.Id, "done");
|
||||
|
||||
var admin = await server.AdminAsync();
|
||||
using var dashboard = await admin.GetAsync("/api/v1/server/dashboard");
|
||||
Assert.True((await dashboard.ReadAsync(ProtocolJson.Default.DashboardDto)).Database.StartsWith("PostgreSQL ", StringComparison.Ordinal));
|
||||
using var health = await server.Client.GetAsync("/health");
|
||||
Assert.Equal("ok", (await health.ReadAsync(ProtocolJson.Default.HealthStatus)).Checks["database"]);
|
||||
}
|
||||
|
||||
/// <summary>A throw-away schema on the test server, selected through <c>Search Path</c>.</summary>
|
||||
private sealed class Schema : IAsyncDisposable
|
||||
{
|
||||
private readonly IDatabase _admin;
|
||||
private readonly string _name;
|
||||
|
||||
private Schema(IDatabase admin, string name, string connectionString)
|
||||
{
|
||||
_admin = admin;
|
||||
_name = name;
|
||||
ConnectionString = connectionString;
|
||||
}
|
||||
|
||||
public string ConnectionString { get; }
|
||||
|
||||
public static async Task<Schema> CreateAsync(TestContext context)
|
||||
{
|
||||
var baseConnection = Environment.GetEnvironmentVariable("TITANO_TEST_PG")!;
|
||||
var admin = DatabaseFactory.Open(DatabaseOptions.Postgres(baseConnection));
|
||||
var name = "titano_s_" + Guid.NewGuid().ToString("N")[..16];
|
||||
await using (var session = await admin.OpenAsync(context.Cancellation))
|
||||
await session.ExecuteScriptAsync($"CREATE SCHEMA {name}", context.Cancellation);
|
||||
return new Schema(admin, name, baseConnection.TrimEnd(';', ' ') + $";Search Path={name}");
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await using (var session = await _admin.OpenAsync(CancellationToken.None))
|
||||
await session.ExecuteScriptAsync($"DROP SCHEMA IF EXISTS {_name} CASCADE", CancellationToken.None);
|
||||
await _admin.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user