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,139 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data.Jobs;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Jobs;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class JobApiTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task JobsMoveThroughPauseResumeAndDone()
|
||||
{
|
||||
var handler = new BlockingJobHandler(JobKind.DeepAnalyse);
|
||||
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<IJobHandler>(handler));
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var sequence = await CreateSequenceAsync(editor);
|
||||
|
||||
var job = await CreateAsync(editor, "deep-analyse", sequence);
|
||||
Assert.Equal(("queued", "user", "Harbour sunset"), (job.State, job.Priority, job.TargetLabel));
|
||||
await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
||||
await WaitForAsync(editor, job.Id, "running");
|
||||
|
||||
Assert.Equal("paused", (await ActAsync(editor, job.Id, "pause", HttpStatusCode.OK)).State);
|
||||
await WaitForAsync(editor, job.Id, "paused");
|
||||
await ActAsync(editor, job.Id, "pause", HttpStatusCode.Conflict);
|
||||
await ActAsync(editor, job.Id, "retry", HttpStatusCode.Conflict);
|
||||
|
||||
await ActAsync(editor, job.Id, "resume", HttpStatusCode.OK);
|
||||
await WaitForAsync(editor, job.Id, "running");
|
||||
handler.Release();
|
||||
var done = await WaitForAsync(editor, job.Id, "done");
|
||||
Assert.Equal((4L, 4L), (done.ProgressDone, done.ProgressTotal));
|
||||
await ActAsync(editor, job.Id, "cancel", HttpStatusCode.Conflict);
|
||||
|
||||
using var list = await editor.GetAsync("/api/v1/jobs?state=done&kind=deep-analyse");
|
||||
Assert.Equal(job.Id, (await list.ReadAsync(ProtocolJson.Default.PageJobDto)).Items.Single().Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FailedJobsRetryAndRunningJobsCancel()
|
||||
{
|
||||
var handler = new BlockingJobHandler(JobKind.Proxies) { FailWith = "decoder exploded" };
|
||||
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<IJobHandler>(handler));
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var sequence = await CreateSequenceAsync(editor);
|
||||
|
||||
var job = await CreateAsync(editor, "proxies", sequence);
|
||||
var failed = await WaitForAsync(editor, job.Id, "failed");
|
||||
Assert.Equal("decoder exploded", failed.Error);
|
||||
Assert.Contains("decoder exploded", failed.LogTail);
|
||||
|
||||
await ActAsync(editor, job.Id, "retry", HttpStatusCode.OK);
|
||||
await WaitForAsync(editor, job.Id, "running");
|
||||
Assert.Equal("cancelled", (await ActAsync(editor, job.Id, "cancel", HttpStatusCode.OK)).State);
|
||||
await Task.Delay(200);
|
||||
Assert.Equal("cancelled", (await GetAsync(editor, job.Id)).State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task WaitingJobsChangePriorityAndRightsAreChecked()
|
||||
{
|
||||
var handler = new BlockingJobHandler(JobKind.Thumbs);
|
||||
await using var server = await TestServer.StartAsync(context, o => o.Workers = 1, s => s.AddSingleton<IJobHandler>(handler));
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
|
||||
var sequence = await CreateSequenceAsync(editor);
|
||||
|
||||
var first = await CreateAsync(editor, "thumbs", sequence);
|
||||
await WaitForAsync(editor, first.Id, "running");
|
||||
var second = await CreateAsync(editor, "thumbs", sequence);
|
||||
using (var lower = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("background"), ProtocolJson.Default.JobPriorityRequest))
|
||||
Assert.Equal("background", (await lower.ReadAsync(ProtocolJson.Default.JobDto)).Priority);
|
||||
using (var bad = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("urgent"), ProtocolJson.Default.JobPriorityRequest))
|
||||
await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_priority");
|
||||
|
||||
using (var foreign = await viewer.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await foreign.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using (var create = await viewer.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("thumbs", [sequence], null), ProtocolJson.Default.CreateJobRequest))
|
||||
await create.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using (var unknown = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("teleport", [sequence], null), ProtocolJson.Default.CreateJobRequest))
|
||||
await unknown.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_job_kind");
|
||||
using (var unavailable = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("deep-analyse", [sequence], null), ProtocolJson.Default.CreateJobRequest))
|
||||
await unavailable.ExpectErrorAsync(HttpStatusCode.Conflict, "job_unavailable");
|
||||
using (var render = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("render", [sequence], "{}"), ProtocolJson.Default.CreateJobRequest))
|
||||
await render.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_parameters");
|
||||
|
||||
var admin = await server.AdminAsync();
|
||||
using (var removed = await admin.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await removed.ExpectAsync(HttpStatusCode.OK);
|
||||
handler.Release();
|
||||
await WaitForAsync(editor, first.Id, "done");
|
||||
}
|
||||
|
||||
public static async Task<Guid> CreateSequenceAsync(HttpClient client, string title = "Harbour sunset")
|
||||
{
|
||||
using var created = await client.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest(title, null), ProtocolJson.Default.CreateSequenceRequest);
|
||||
await created.ExpectAsync(HttpStatusCode.Created);
|
||||
return (await created.ReadAsync(ProtocolJson.Default.SequenceDto)).Id;
|
||||
}
|
||||
|
||||
public static async Task<JobDto> CreateAsync(HttpClient client, string kind, Guid target, string? parameters = null)
|
||||
{
|
||||
using var response = await client.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest(kind, [target], parameters), ProtocolJson.Default.CreateJobRequest);
|
||||
await response.ExpectAsync(HttpStatusCode.Accepted);
|
||||
return await response.ReadAsync(ProtocolJson.Default.JobDto);
|
||||
}
|
||||
|
||||
public static async Task<JobDto> GetAsync(HttpClient client, Guid id)
|
||||
{
|
||||
using var response = await client.GetAsync($"/api/v1/jobs/{id}");
|
||||
return await response.ReadAsync(ProtocolJson.Default.JobDto);
|
||||
}
|
||||
|
||||
public static async Task<JobDto> WaitForAsync(HttpClient client, Guid id, string state)
|
||||
{
|
||||
var deadline = DateTime.UtcNow.AddSeconds(15);
|
||||
while (true)
|
||||
{
|
||||
var job = await GetAsync(client, id);
|
||||
if (job.State == state) return job;
|
||||
if (DateTime.UtcNow > deadline) Assert.Fail($"Job {id} is {job.State}, expected {state}");
|
||||
await Task.Delay(50);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<JobDto> ActAsync(HttpClient client, Guid id, string action, HttpStatusCode expected)
|
||||
{
|
||||
using var response = await client.PostAsync($"/api/v1/jobs/{id}/{action}", null);
|
||||
if (expected != HttpStatusCode.OK)
|
||||
{
|
||||
await response.ExpectErrorAsync(expected, "invalid_state");
|
||||
return await GetAsync(client, id);
|
||||
}
|
||||
await response.ExpectAsync(expected);
|
||||
return await response.ReadAsync(ProtocolJson.Default.JobDto);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user