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
84 lines
3.4 KiB
C#
84 lines
3.4 KiB
C#
using System.Net;
|
|
using System.Net.WebSockets;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Titano.Core.Model;
|
|
using Titano.Data.Jobs;
|
|
using Titano.Protocol;
|
|
using Titano.Protocol.Client;
|
|
using Titano.Server.Tests.Jobs;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Server.Tests.Events;
|
|
|
|
[Category("server")]
|
|
public sealed class EventTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public async Task AWebSocketClientReceivesJobProgressAndStates()
|
|
{
|
|
var handler = new BlockingJobHandler(JobKind.DeepAnalyse);
|
|
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<IJobHandler>(handler));
|
|
var (editor, _, token) = await server.UserAsync("alberto", "editor");
|
|
var sequence = await JobApiTests.CreateSequenceAsync(editor);
|
|
|
|
using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "tests") { Token = token };
|
|
using var listening = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
|
listening.CancelAfter(TimeSpan.FromSeconds(20));
|
|
var received = new List<ServerEvent>();
|
|
var connected = new TaskCompletionSource();
|
|
var reader = Task.Run(async () =>
|
|
{
|
|
var events = client.EventsAsync(listening.Token).GetAsyncEnumerator(listening.Token);
|
|
var first = events.MoveNextAsync();
|
|
connected.TrySetResult();
|
|
try
|
|
{
|
|
if (!await first) return;
|
|
do
|
|
{
|
|
received.Add(events.Current);
|
|
if (events.Current is JobStateEvent { State: "done" }) return;
|
|
}
|
|
while (await events.MoveNextAsync());
|
|
}
|
|
finally
|
|
{
|
|
await events.DisposeAsync();
|
|
}
|
|
});
|
|
await connected.Task;
|
|
await WaitForSubscriberAsync(server);
|
|
|
|
var job = await JobApiTests.CreateAsync(editor, "deep-analyse", sequence);
|
|
await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
|
handler.Release();
|
|
await reader.WaitAsync(TimeSpan.FromSeconds(20));
|
|
|
|
Assert.Contains(received, e => e is JobProgressEvent p && p.JobId == job.Id && p.Total == 4);
|
|
Assert.Contains(received, e => e is JobStateEvent { State: "running" } s && s.JobId == job.Id);
|
|
Assert.Contains(received, e => e is JobStateEvent { State: "done" } s && s.JobId == job.Id);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheEventSocketNeedsAValidToken()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context);
|
|
var ws = new UriBuilder(new Uri(server.HttpBase, "/api/v1/events")) { Scheme = "ws" }.Uri;
|
|
using (var anonymous = new ClientWebSocket())
|
|
{
|
|
var error = await Assert.ThrowsAsync<WebSocketException>(() => anonymous.ConnectAsync(ws, context.Cancellation));
|
|
Assert.Contains("401", error.Message);
|
|
}
|
|
var admin = await server.AdminAsync();
|
|
using var plain = await admin.GetAsync("/api/v1/events");
|
|
await plain.ExpectErrorAsync(HttpStatusCode.BadRequest, "websocket_required");
|
|
}
|
|
|
|
private static async Task WaitForSubscriberAsync(TestServer server)
|
|
{
|
|
var hub = server.Services.GetRequiredService<Titano.Server.Events.EventHub>();
|
|
for (var i = 0; i < 200 && hub.SubscriberCount == 0; i++) await Task.Delay(25);
|
|
Assert.Equal(1, hub.SubscriberCount);
|
|
}
|
|
}
|