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,90 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Api;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class AdminTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task TheDashboardSummarisesTheServer()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ServerName = "Studio NAS");
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
await Uploads.UploadAsync(editor, Uploads.Photo(4000, 61));
|
||||
var admin = await server.AdminAsync();
|
||||
using var response = await admin.GetAsync("/api/v1/server/dashboard");
|
||||
await response.ExpectAsync(HttpStatusCode.OK);
|
||||
var dashboard = await response.ReadAsync(ProtocolJson.Default.DashboardDto);
|
||||
Assert.Equal(("Studio NAS", 2, 1L, 1L), (dashboard.ServerName, dashboard.Users, dashboard.Archive.Sequences, dashboard.Archive.Photos));
|
||||
Assert.True(dashboard.Database.StartsWith("SQLite ", StringComparison.Ordinal), dashboard.Database);
|
||||
Assert.Equal(1L, dashboard.Uploads.Single().Frames);
|
||||
Assert.Contains(dashboard.Activity, a => a.Action == "user_created");
|
||||
Assert.True(dashboard.Archive.CapacityBytes > 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SettingsAreSavedAndRestartOnlyValuesFlagged()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using var read = await admin.GetAsync("/api/v1/server/settings");
|
||||
var settings = await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto);
|
||||
Assert.False(settings.RestartRequired);
|
||||
|
||||
var edited = settings with { ServerName = "Rooftop", Discovery = true, HttpPort = 18080, HttpsPort = 18443 };
|
||||
using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto))
|
||||
Assert.True((await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).RestartRequired);
|
||||
// Name and discovery apply at once; ports wait for the restart.
|
||||
Assert.Equal(("Rooftop", true, 0), (server.Options.ServerName, server.Options.Discovery, server.Options.HttpPort));
|
||||
using (var again = await admin.GetAsync("/api/v1/server/settings"))
|
||||
Assert.Equal(18080, (await again.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).HttpPort);
|
||||
var file = JsonNode.Parse(await File.ReadAllTextAsync(server.Options.SettingsFile))!.AsObject();
|
||||
Assert.Equal(("Rooftop", 18080), (file["ServerName"]!.GetValue<string>(), file["HttpPort"]!.GetValue<int>()));
|
||||
Assert.False(file.ContainsKey("AdminPassword"));
|
||||
|
||||
using var invalid = await admin.PutAsJsonAsync("/api/v1/server/settings", edited with { Language = "tlh" }, ProtocolJson.Default.ServerSettingsDto);
|
||||
Assert.Equal("Language", (await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LogsAreReadableButOnlyByName()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using var tail = await admin.GetAsync("/api/v1/logs?lines=20");
|
||||
var lines = await tail.ReadAsync(ProtocolJson.Default.LogTailDto);
|
||||
Assert.NotEmpty(lines.Lines);
|
||||
Assert.True(lines.Lines.Count <= 20);
|
||||
using var files = await admin.GetAsync("/api/v1/logs/files");
|
||||
var name = (await files.ReadAsync(ProtocolJson.Default.ListLogFileDto)).First().Name;
|
||||
using (var download = await admin.GetAsync($"/api/v1/logs/files/{name}")) await download.ExpectAsync(HttpStatusCode.OK);
|
||||
using var traversal = await admin.GetAsync("/api/v1/logs/files/..%2Fserver.json");
|
||||
await traversal.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ArchiveSettingsValidateTemplatesAndJobsNeedHandlers()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using var read = await admin.GetAsync("/api/v1/archive/settings");
|
||||
var archive = await read.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto);
|
||||
Assert.Equal(Path.GetFullPath(server.Options.ArchivePath), archive.RootPath);
|
||||
|
||||
using (var bad = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { FolderTemplate = "{year/{date" }, ProtocolJson.Default.ArchiveSettingsDto))
|
||||
Assert.Equal("folderTemplate", (await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details);
|
||||
using (var good = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { Name = "Studio", ProxyLongEdge = 2560 }, ProtocolJson.Default.ArchiveSettingsDto))
|
||||
{
|
||||
var saved = await good.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto);
|
||||
Assert.Equal(("Studio", 2560), (saved.Name, saved.ProxyLongEdge));
|
||||
}
|
||||
using var scan = await admin.PostAsync("/api/v1/archive/scan", null);
|
||||
await scan.ExpectErrorAsync(HttpStatusCode.Conflict, "job_unavailable");
|
||||
using var stats = await admin.GetAsync("/api/v1/archive/stats");
|
||||
await stats.ExpectAsync(HttpStatusCode.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Api;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class SequenceTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task ConcurrentSettingsEditsConflict()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (alberto, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var (giulia, _, _) = await server.UserAsync("giulia", "editor");
|
||||
using var created = await alberto.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest("Harbour sunset", null), ProtocolJson.Default.CreateSequenceRequest);
|
||||
await created.ExpectAsync(HttpStatusCode.Created);
|
||||
var sequence = await created.ReadAsync(ProtocolJson.Default.SequenceDto);
|
||||
Assert.Equal(0, sequence.SettingsVersion);
|
||||
|
||||
using (var first = await alberto.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}",
|
||||
new UpdateSequenceRequest(null, ["sunset", "harbour"], 4, null, """{"deflicker":{"strength":0.8}}""", 0), ProtocolJson.Default.UpdateSequenceRequest))
|
||||
{
|
||||
await first.ExpectAsync(HttpStatusCode.OK);
|
||||
var updated = await first.ReadAsync(ProtocolJson.Default.SequenceDto);
|
||||
Assert.Equal(1, updated.SettingsVersion);
|
||||
Assert.SequenceEqual(["harbour", "sunset"], updated.Tags);
|
||||
Assert.Equal(4, updated.Rating);
|
||||
}
|
||||
|
||||
using (var stale = await giulia.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}",
|
||||
new UpdateSequenceRequest(null, null, null, null, """{"deflicker":{"strength":0.2}}""", 0), ProtocolJson.Default.UpdateSequenceRequest))
|
||||
{
|
||||
var error = await stale.ExpectErrorAsync(HttpStatusCode.Conflict, "settings_conflict");
|
||||
Assert.Equal("1", error.Details);
|
||||
}
|
||||
|
||||
// The ETag of a fresh read works as If-Match.
|
||||
using var read = await giulia.GetAsync($"/api/v1/sequences/{sequence.Id}");
|
||||
var etag = Assert.NotNull(read.Headers.ETag);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Patch, $"/api/v1/sequences/{sequence.Id}")
|
||||
{
|
||||
Content = JsonContent.Create(new UpdateSequenceRequest(null, null, null, null, """{"deflicker":{"strength":0.2}}""", null), ProtocolJson.Default.UpdateSequenceRequest),
|
||||
};
|
||||
request.Headers.IfMatch.Add(etag);
|
||||
using (var retried = await giulia.SendAsync(request))
|
||||
{
|
||||
await retried.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Equal(2, (await retried.ReadAsync(ProtocolJson.Default.SequenceDto)).SettingsVersion);
|
||||
}
|
||||
|
||||
using var notModified = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/sequences/{sequence.Id}");
|
||||
notModified.Headers.IfNoneMatch.Add(etag);
|
||||
using (var changed = await giulia.SendAsync(notModified)) await changed.ExpectAsync(HttpStatusCode.OK);
|
||||
|
||||
using var invalid = await giulia.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}",
|
||||
new UpdateSequenceRequest(null, null, null, null, "{ broken", null), ProtocolJson.Default.UpdateSequenceRequest);
|
||||
await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_settings");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheManifestCarriesHashesAndAnEtag()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var a = Uploads.Photo(2000, 31);
|
||||
var b = Uploads.Photo(2000, 32);
|
||||
var first = await Uploads.UploadAsync(editor, a, "a.CR3", sessionKey: "k");
|
||||
await Uploads.UploadAsync(editor, b, "b.CR3", sessionKey: "k");
|
||||
|
||||
using var response = await editor.GetAsync($"/api/v1/sequences/{first.SequenceId}/manifest");
|
||||
await response.ExpectAsync(HttpStatusCode.OK);
|
||||
var manifest = await response.ReadAsync(ProtocolJson.Default.SequenceManifest);
|
||||
Assert.SequenceEqual([Uploads.Sha(a), Uploads.Sha(b)], manifest.Frames.Select(f => f.Sha256));
|
||||
Assert.Equal(2, manifest.FramesVersion);
|
||||
|
||||
using var cached = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/sequences/{first.SequenceId}/manifest");
|
||||
cached.Headers.IfNoneMatch.Add(response.Headers.ETag!);
|
||||
using (var unchanged = await editor.SendAsync(cached)) await unchanged.ExpectAsync(HttpStatusCode.NotModified);
|
||||
|
||||
await Uploads.UploadAsync(editor, Uploads.Photo(2000, 33), "c.CR3", sessionKey: "k");
|
||||
using var again = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/sequences/{first.SequenceId}/manifest");
|
||||
again.Headers.IfNoneMatch.Add(response.Headers.ETag!);
|
||||
using var changed = await editor.SendAsync(again);
|
||||
await changed.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Count(3, (await changed.ReadAsync(ProtocolJson.Default.SequenceManifest)).Frames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ListingFiltersAndPagesWithACursor()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
using var created = await editor.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest($"Shoot {i}", null), ProtocolJson.Default.CreateSequenceRequest);
|
||||
await created.ExpectAsync(HttpStatusCode.Created);
|
||||
}
|
||||
var seen = new List<string>();
|
||||
string? cursor = null;
|
||||
long? total = null;
|
||||
do
|
||||
{
|
||||
using var page = await editor.GetAsync($"/api/v1/sequences?sort=title&limit=2{(cursor is null ? string.Empty : "&cursor=" + Uri.EscapeDataString(cursor))}");
|
||||
await page.ExpectAsync(HttpStatusCode.OK);
|
||||
var items = await page.ReadAsync(ProtocolJson.Default.PageSequenceDto);
|
||||
total ??= items.Total;
|
||||
seen.AddRange(items.Items.Select(s => s.DisplayTitle));
|
||||
cursor = items.NextCursor;
|
||||
}
|
||||
while (cursor is not null);
|
||||
Assert.Equal(5L, total);
|
||||
Assert.SequenceEqual(["Shoot 0", "Shoot 1", "Shoot 2", "Shoot 3", "Shoot 4"], seen);
|
||||
|
||||
using (var search = await editor.GetAsync("/api/v1/sequences?q=shoot%203")) Assert.Count(1, (await search.ReadAsync(ProtocolJson.Default.PageSequenceDto)).Items);
|
||||
using (var badCursor = await editor.GetAsync("/api/v1/sequences?cursor=bm9wZQ")) await badCursor.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_cursor");
|
||||
using var badStatus = await editor.GetAsync("/api/v1/sequences?status=shiny");
|
||||
await badStatus.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_filter");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeletingPlansFirstAndMovesTheFolderToTrash()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var (other, _, _) = await server.UserAsync("giulia", "editor");
|
||||
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 34));
|
||||
var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!;
|
||||
var folder = Path.Combine(server.Options.ArchivePath, sequence.Folder);
|
||||
|
||||
using (var notOwner = await other.DeleteAsync($"/api/v1/sequences/{sequence.Id}")) await notOwner.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using (var plan = await editor.DeleteAsync($"/api/v1/sequences/{sequence.Id}?dryRun=true"))
|
||||
{
|
||||
var dryRun = await plan.ReadAsync(ProtocolJson.Default.SequenceDeletePlan);
|
||||
Assert.True(dryRun.DryRun);
|
||||
Assert.Equal(1, dryRun.FrameCount);
|
||||
Assert.Equal($".titano/trash/{sequence.Id:N}", dryRun.TrashFolder);
|
||||
}
|
||||
Assert.True(Directory.Exists(folder), "a dry run changes nothing");
|
||||
Assert.NotNull(await server.Database.Sequences.GetAsync(sequence.Id));
|
||||
|
||||
using (var deleted = await editor.DeleteAsync($"/api/v1/sequences/{sequence.Id}")) await deleted.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.False(Directory.Exists(folder));
|
||||
Assert.True(Directory.Exists(Path.Combine(server.Options.TrashPath, sequence.Id.ToString("N"))), "the files are kept in the trash");
|
||||
Assert.Null(await server.Database.Sequences.GetAsync(sequence.Id));
|
||||
using var gone = await editor.GetAsync($"/api/v1/sequences/{sequence.Id}");
|
||||
await gone.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FramesAndStatsArePaged()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var first = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 40), "f0.jpg", sessionKey: "p");
|
||||
for (var i = 1; i < 4; i++) await Uploads.UploadAsync(editor, Uploads.Photo(1000 + i, 40 + i), $"f{i}.jpg", sessionKey: "p");
|
||||
var id = first.SequenceId;
|
||||
using var page1 = await editor.GetAsync($"/api/v1/sequences/{id}/frames?limit=2");
|
||||
var frames = await page1.ReadAsync(ProtocolJson.Default.PageFrameDto);
|
||||
Assert.SequenceEqual([0, 1], frames.Items.Select(f => f.Index));
|
||||
using var page2 = await editor.GetAsync($"/api/v1/sequences/{id}/frames?limit=2&cursor={Uri.EscapeDataString(frames.NextCursor!)}");
|
||||
Assert.SequenceEqual([2, 3], (await page2.ReadAsync(ProtocolJson.Default.PageFrameDto)).Items.Select(f => f.Index));
|
||||
|
||||
using var stats = await editor.GetAsync($"/api/v1/sequences/{id}/stats");
|
||||
var dto = await stats.ReadAsync(ProtocolJson.Default.SequenceStatsDto);
|
||||
Assert.Equal(4, dto.FrameCount);
|
||||
Assert.Equal(4L, dto.ByFormat.Sum(f => f.Count));
|
||||
using var analysis = await editor.GetAsync($"/api/v1/sequences/{id}/analysis");
|
||||
Assert.Equal("analysis", (await analysis.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Api;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class UserTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task AdminsManageAccountsAndTemporaryPasswords()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using var created = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("lucia", "Lucia", "editor", 2_000_000_000_000, null, AllowRenderNode: false),
|
||||
ProtocolJson.Default.CreateUserRequest);
|
||||
await created.ExpectAsync(HttpStatusCode.Created);
|
||||
var account = await created.ReadAsync(ProtocolJson.Default.TemporaryPasswordResponse);
|
||||
Assert.True(account.User.MustChangePassword);
|
||||
Assert.Equal(14, account.TemporaryPassword.Length);
|
||||
Assert.Equal(0L, account.User.Permissions & (long)Permissions.RenderNode);
|
||||
|
||||
using (var taken = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("LUCIA", null, "viewer", 0, null, false), ProtocolJson.Default.CreateUserRequest))
|
||||
await taken.ExpectErrorAsync(HttpStatusCode.Conflict, "username_taken");
|
||||
using (var badName = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("lu cia", null, "viewer", 0, null, false), ProtocolJson.Default.CreateUserRequest))
|
||||
await badName.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_username");
|
||||
using (var badRole = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("lucio", null, "owner", 0, null, false), ProtocolJson.Default.CreateUserRequest))
|
||||
await badRole.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_role");
|
||||
|
||||
using (var promoted = await admin.PatchAsJsonAsync($"/api/v1/users/{account.User.Id}", new UpdateUserRequest(null, "uploader", 5, null, true), ProtocolJson.Default.UpdateUserRequest))
|
||||
{
|
||||
var user = await promoted.ReadAsync(ProtocolJson.Default.UserDto);
|
||||
Assert.Equal(("uploader", 5L), (user.Role, user.QuotaBytes));
|
||||
Assert.Equal((long)(RolePermissions.Default(UserRole.Uploader) | Permissions.RenderNode), user.Permissions);
|
||||
}
|
||||
|
||||
using var reset = await admin.PostAsJsonAsync($"/api/v1/users/{account.User.Id}/reset-password", new ResetPasswordRequest(null), ProtocolJson.Default.ResetPasswordRequest);
|
||||
var temporary = await reset.ReadAsync(ProtocolJson.Default.TemporaryPasswordResponse);
|
||||
var login = await server.LoginAsync("lucia", temporary.TemporaryPassword);
|
||||
Assert.True(login.MustChangePassword);
|
||||
|
||||
using var list = await admin.GetAsync("/api/v1/users");
|
||||
var rows = await list.ReadAsync(ProtocolJson.Default.ListUserSummaryDto);
|
||||
var lucia = rows.Single(r => r.User.Username == "lucia");
|
||||
Assert.Equal("tests", lucia.LastClient);
|
||||
Assert.NotNull(lucia.LastSeen);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheLastAdministratorStays()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using var me = await admin.GetAsync("/api/v1/auth/me");
|
||||
var self = await me.ReadAsync(ProtocolJson.Default.UserDto);
|
||||
using (var demote = await admin.PatchAsJsonAsync($"/api/v1/users/{self.Id}", new UpdateUserRequest(null, "editor", null, null, null), ProtocolJson.Default.UpdateUserRequest))
|
||||
await demote.ExpectErrorAsync(HttpStatusCode.Conflict, "self");
|
||||
using (var delete = await admin.DeleteAsync($"/api/v1/users/{self.Id}")) await delete.ExpectErrorAsync(HttpStatusCode.Conflict, "self");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeletingAUserHandsTheirSequencesToTheAdmin()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, user, _) = await server.UserAsync("simone", "editor");
|
||||
using var created = await editor.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest("Comet", null), ProtocolJson.Default.CreateSequenceRequest);
|
||||
var sequence = await created.ReadAsync(ProtocolJson.Default.SequenceDto);
|
||||
var admin = await server.AdminAsync();
|
||||
using (var deleted = await admin.DeleteAsync($"/api/v1/users/{user.Id}")) await deleted.ExpectAsync(HttpStatusCode.NoContent);
|
||||
using var me = await admin.GetAsync("/api/v1/auth/me");
|
||||
var adminId = (await me.ReadAsync(ProtocolJson.Default.UserDto)).Id;
|
||||
Assert.Equal(adminId, (await server.Database.Sequences.GetAsync(sequence.Id))!.OwnerUserId);
|
||||
using var audit = await admin.GetAsync("/api/v1/audit?action=user_deleted");
|
||||
Assert.Count(1, (await audit.ReadAsync(ProtocolJson.Default.PageAuditEntryDto)).Items);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests;
|
||||
|
||||
/// <summary>Assertions and JSON helpers for API calls.</summary>
|
||||
internal static class Http
|
||||
{
|
||||
/// <summary>The body of a successful response (an error response fails the test with its body).</summary>
|
||||
public static async Task<T> ReadAsync<T>(this HttpResponseMessage response, JsonTypeInfo<T> type) where T : class
|
||||
{
|
||||
if (!response.IsSuccessStatusCode) Assert.Fail($"Expected success, got {(int)response.StatusCode}: {await response.Content.ReadAsStringAsync()}");
|
||||
return Assert.NotNull(await response.Content.ReadFromJsonAsync(type));
|
||||
}
|
||||
|
||||
/// <summary>Asserts the status, the <c>{ code, message, details }</c> body with <paramref name="code"/>, and the API header.</summary>
|
||||
public static async Task<ApiError> ExpectErrorAsync(this HttpResponseMessage response, HttpStatusCode status, string code)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
Assert.Equal(status, response.StatusCode, body);
|
||||
Assert.Equal("1", response.Headers.GetValues(ApiVersion.Header).Single());
|
||||
var error = Assert.NotNull(System.Text.Json.JsonSerializer.Deserialize(body, ProtocolJson.Default.ApiError));
|
||||
Assert.Equal(code, error.Code, body);
|
||||
Assert.False(string.IsNullOrWhiteSpace(error.Message), "message is empty");
|
||||
return error;
|
||||
}
|
||||
|
||||
public static async Task BytesEqualAsync(byte[] expected, Task<byte[]> actual)
|
||||
{
|
||||
var bytes = await actual;
|
||||
Assert.BytesEqual(expected, bytes);
|
||||
}
|
||||
|
||||
public static async Task ExpectAsync(this HttpResponseMessage response, HttpStatusCode status)
|
||||
{
|
||||
if (response.StatusCode != status) Assert.Fail($"Expected {(int)status}, got {(int)response.StatusCode}: {await response.Content.ReadAsStringAsync()}");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Protocol;
|
||||
using Titano.Protocol.Client;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Jobs;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class RenderNodeTests(TestContext context)
|
||||
{
|
||||
private static readonly string HevcJob = JsonSerializer.Serialize(
|
||||
new RenderJobParameters("4K HEVC", """{"codec":"hevc","bitrate":60}""", "hevc", 3840, 2160, UseProxies: false, null), ProtocolJson.Default.RenderJobParameters);
|
||||
|
||||
[Test]
|
||||
public async Task NodesClaimWhatTheyCanEncodeAndLoseJobsWhenSilent()
|
||||
{
|
||||
var clock = new ManualTimeProvider();
|
||||
await using var server = await TestServer.StartAsync(context, o => o.NodeTimeoutSeconds = 60, s => s.AddSingleton<TimeProvider>(clock));
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true);
|
||||
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(5000, 51), "IMG_0001.CR3");
|
||||
var job = await JobApiTests.CreateAsync(editor, "render", frame.SequenceId, HevcJob);
|
||||
|
||||
var studio = await RegisterAsync(editor, "studio-pc", "hevc", 8192);
|
||||
var laptop = await RegisterAsync(editor, "laptop", "h264", 4096);
|
||||
var spare = await RegisterAsync(editor, "spare-pc", "HEVC", 0);
|
||||
using (var none = await editor.PostAsync($"/api/v1/nodes/{laptop.Id}/claim", null)) await none.ExpectAsync(HttpStatusCode.NoContent);
|
||||
|
||||
ClaimedJob claimed;
|
||||
using (var claim = await editor.PostAsync($"/api/v1/nodes/{studio.Id}/claim", null))
|
||||
{
|
||||
await claim.ExpectAsync(HttpStatusCode.OK);
|
||||
claimed = await claim.ReadAsync(ProtocolJson.Default.ClaimedJob);
|
||||
}
|
||||
Assert.Equal((job.Id, frame.SequenceId, "running"), (claimed.Job.Id, claimed.SequenceId, claimed.Job.State));
|
||||
Assert.Equal(frame.FrameId, claimed.Frames.Single().Id);
|
||||
Assert.Equal(studio.Id.ToString(), claimed.Job.NodeId);
|
||||
using (var second = await editor.PostAsync($"/api/v1/nodes/{spare.Id}/claim", null)) await second.ExpectAsync(HttpStatusCode.NoContent);
|
||||
|
||||
using (var progress = await editor.PostAsJsonAsync($"/api/v1/nodes/{studio.Id}/jobs/{job.Id}/progress", new NodeProgress(120, 3600, 40, "encoding"),
|
||||
ProtocolJson.Default.NodeProgress))
|
||||
await progress.ExpectAsync(HttpStatusCode.NoContent);
|
||||
Assert.Equal(120L, (await JobApiTests.GetAsync(editor, job.Id)).ProgressDone);
|
||||
|
||||
// studio-pc goes silent; the others keep heart-beating.
|
||||
clock.Advance(TimeSpan.FromSeconds(90));
|
||||
foreach (var alive in new[] { laptop, spare })
|
||||
using (var beat = await editor.PostAsync($"/api/v1/nodes/{alive.Id}/heartbeat", null)) await beat.ExpectAsync(HttpStatusCode.NoContent);
|
||||
await server.Services.GetRequiredService<RenderNodeMonitor>().SweepAsync(context.Cancellation);
|
||||
|
||||
var requeued = await JobApiTests.GetAsync(editor, job.Id);
|
||||
Assert.Equal("queued", requeued.State);
|
||||
Assert.Contains("studio-pc stopped responding", requeued.LogTail);
|
||||
using (var nodes = await editor.GetAsync("/api/v1/nodes"))
|
||||
{
|
||||
var states = (await nodes.ReadAsync(ProtocolJson.Default.ListRenderNodeDto)).ToDictionary(n => n.ClientName, n => n.State);
|
||||
Assert.Equal(("offline", "idle"), (states["studio-pc"], states["laptop"]));
|
||||
}
|
||||
using (var late = await editor.PostAsJsonAsync($"/api/v1/nodes/{studio.Id}/jobs/{job.Id}/progress", new NodeProgress(200, 3600, 40, null),
|
||||
ProtocolJson.Default.NodeProgress))
|
||||
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "job_not_assigned");
|
||||
|
||||
using (var reassigned = await editor.PostAsync($"/api/v1/nodes/{spare.Id}/claim", null))
|
||||
{
|
||||
var again = await reassigned.ReadAsync(ProtocolJson.Default.ClaimedJob);
|
||||
Assert.Equal((job.Id, 2), (again.Job.Id, again.Job.Attempts));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheOutputUploadBecomesARenderAndFinishesTheJob()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = 64 * 1024);
|
||||
var (editor, _, token) = await server.UserAsync("alberto", "editor", renderNode: true);
|
||||
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(5000, 52), "IMG_0001.CR3");
|
||||
var job = await JobApiTests.CreateAsync(editor, "render", frame.SequenceId, HevcJob);
|
||||
|
||||
using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "studio-pc") { Token = token };
|
||||
var node = await client.RegisterNodeAsync(new RegisterNodeRequest("studio-pc", [new NodeEncoder("hevc", "NVENC HEVC", true, true)], 8192, 8192));
|
||||
var claimed = Assert.NotNull(await client.ClaimAsync(node.Id));
|
||||
await client.ReportNodeProgressAsync(node.Id, claimed.Job.Id, new NodeProgress(3600, 3600, 50, "muxing"));
|
||||
|
||||
var output = Path.Combine(context.CreateTempDirectory("render"), "Harbour sunset - 4K HEVC.mp4");
|
||||
var video = Uploads.Photo(150_000, 53);
|
||||
await File.WriteAllBytesAsync(output, video);
|
||||
var render = await client.UploadRenderResultAsync(node.Id, job.Id, output,
|
||||
new RenderResultRequest(Path.GetFileName(output), video.Length, Uploads.Sha(video), "4K HEVC", "hevc", 3840, 2160, 25, 144));
|
||||
Assert.Equal(("4K HEVC", "studio-pc", (long)video.Length), (render.PresetName, render.ProducedBy, render.Bytes));
|
||||
|
||||
var done = await JobApiTests.GetAsync(editor, job.Id);
|
||||
Assert.Equal("done", done.State);
|
||||
var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!;
|
||||
Assert.Equal(1, sequence.RenderedCount);
|
||||
var file = Path.Combine(server.Options.ArchivePath, sequence.Folder, "exports", "Harbour sunset - 4K HEVC.mp4");
|
||||
await Http.BytesEqualAsync(video, File.ReadAllBytesAsync(file));
|
||||
using var renders = await editor.GetAsync($"/api/v1/sequences/{sequence.Id}/renders");
|
||||
Assert.Equal(render.Id, (await renders.ReadAsync(ProtocolJson.Default.ListRenderDto)).Single().Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task NodesReportFailuresAndNeedTheRight()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true);
|
||||
var (plain, _, _) = await server.UserAsync("giulia", "editor");
|
||||
var sequence = await JobApiTests.CreateSequenceAsync(editor);
|
||||
var job = await JobApiTests.CreateAsync(editor, "render", sequence, HevcJob);
|
||||
var node = await RegisterAsync(editor, "studio-pc", "hevc", 8192);
|
||||
using (var claim = await editor.PostAsync($"/api/v1/nodes/{node.Id}/claim", null)) await claim.ExpectAsync(HttpStatusCode.OK);
|
||||
using (var failed = await editor.PostAsJsonAsync($"/api/v1/nodes/{node.Id}/jobs/{job.Id}/failed", new NodeJobFailure("NVENC session limit reached"),
|
||||
ProtocolJson.Default.NodeJobFailure))
|
||||
await failed.ExpectAsync(HttpStatusCode.NoContent);
|
||||
var state = await JobApiTests.GetAsync(editor, job.Id);
|
||||
Assert.Equal(("failed", "NVENC session limit reached"), (state.State, state.Error));
|
||||
|
||||
using var refused = await plain.PostAsJsonAsync("/api/v1/nodes/register", new RegisterNodeRequest("giulia-pc", [new NodeEncoder("hevc", "QSV", true, false)], 0, 0),
|
||||
ProtocolJson.Default.RegisterNodeRequest);
|
||||
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using var foreign = await plain.PostAsync($"/api/v1/nodes/{node.Id}/heartbeat", null);
|
||||
await foreign.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
}
|
||||
|
||||
private static async Task<RenderNodeDto> RegisterAsync(HttpClient client, string name, string codec, int maxWidth)
|
||||
{
|
||||
using var response = await client.PostAsJsonAsync("/api/v1/nodes/register",
|
||||
new RegisterNodeRequest(name, [new NodeEncoder(codec, codec.ToUpperInvariant() + " encoder", true, false)], maxWidth, maxWidth), ProtocolJson.Default.RegisterNodeRequest);
|
||||
await response.ExpectAsync(HttpStatusCode.OK);
|
||||
return await response.ReadAsync(ProtocolJson.Default.RenderNodeDto);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Security;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class AuthTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task BootstrapAdminMustChangePasswordBeforeAnythingElse()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var login = await server.LoginAsync("admin", TestServer.AdminPassword);
|
||||
Assert.True(login.MustChangePassword);
|
||||
Assert.Equal("admin", login.User.Role);
|
||||
var client = server.NewClient(login.Token);
|
||||
|
||||
using (var blocked = await client.GetAsync("/api/v1/users")) await blocked.ExpectErrorAsync(HttpStatusCode.Forbidden, "password_change_required");
|
||||
using (var me = await client.GetAsync("/api/v1/auth/me")) await me.ExpectAsync(HttpStatusCode.OK);
|
||||
using (var weak = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(TestServer.AdminPassword, "short"),
|
||||
ProtocolJson.Default.ChangePasswordRequest))
|
||||
await weak.ExpectErrorAsync(HttpStatusCode.BadRequest, "weak_password");
|
||||
using (var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(TestServer.AdminPassword, TestServer.Password),
|
||||
ProtocolJson.Default.ChangePasswordRequest))
|
||||
await changed.ExpectAsync(HttpStatusCode.NoContent);
|
||||
using var users = await client.GetAsync("/api/v1/users");
|
||||
await users.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Count(1, await users.ReadAsync(ProtocolJson.Default.ListUserSummaryDto));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task BootstrapWithoutPasswordWritesItToAFileNeverToTheLog()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.AdminPassword = null);
|
||||
var file = Path.Combine(server.Options.DataPath, "initial-admin-password.txt");
|
||||
Assert.FileExists(file);
|
||||
var password = File.ReadAllText(file).Trim();
|
||||
if (!OperatingSystem.IsWindows()) Assert.Equal(UnixFileMode.UserRead | UnixFileMode.UserWrite, File.GetUnixFileMode(file));
|
||||
var login = await server.LoginAsync("admin", password);
|
||||
Assert.True(login.MustChangePassword);
|
||||
foreach (var log in Directory.EnumerateFiles(server.Options.LogPath))
|
||||
{
|
||||
using var reader = new StreamReader(new FileStream(log, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
|
||||
Assert.False((await reader.ReadToEndAsync()).Contains(password, StringComparison.Ordinal), "the password reached the log");
|
||||
}
|
||||
|
||||
var client = server.NewClient(login.Token);
|
||||
using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(password, TestServer.Password),
|
||||
ProtocolJson.Default.ChangePasswordRequest);
|
||||
await changed.ExpectAsync(HttpStatusCode.NoContent);
|
||||
Assert.FileMissing(file);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task WrongPasswordAndUnknownUserFailAlike()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
using var wrong = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", "not-the-password", "tests"), ProtocolJson.Default.LoginRequest);
|
||||
var a = await wrong.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials");
|
||||
using var unknown = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("nobody", "not-the-password", "tests"), ProtocolJson.Default.LoginRequest);
|
||||
var b = await unknown.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials");
|
||||
Assert.Equal(a.Message, b.Message);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RepeatedFailuresLockTheAccountNameEvenWithTheRightPassword()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o =>
|
||||
{
|
||||
o.LoginAttemptsPerMinute = 100;
|
||||
o.LockoutThreshold = 3;
|
||||
});
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
using var failed = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest);
|
||||
await failed.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials");
|
||||
}
|
||||
using var locked = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.AdminPassword, "tests"), ProtocolJson.Default.LoginRequest);
|
||||
await locked.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "account_locked");
|
||||
Assert.True(locked.Headers.RetryAfter?.Delta > TimeSpan.Zero);
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
(await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("ghost", "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest)).Dispose();
|
||||
using var ghost = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("ghost", "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest);
|
||||
await ghost.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "account_locked");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SignInsAreRateLimitedPerAddress()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.LoginAttemptsPerMinute = 5);
|
||||
for (var i = 0; i < 5; i++)
|
||||
(await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("user" + i, "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest)).Dispose();
|
||||
using var limited = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.AdminPassword, "tests"), ProtocolJson.Default.LoginRequest);
|
||||
await limited.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "rate_limited");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LogoutRevokesTheToken()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using (var me = await admin.GetAsync("/api/v1/auth/me")) await me.ExpectAsync(HttpStatusCode.OK);
|
||||
using (var logout = await admin.PostAsync("/api/v1/auth/logout", null)) await logout.ExpectAsync(HttpStatusCode.NoContent);
|
||||
using var after = await admin.GetAsync("/api/v1/auth/me");
|
||||
await after.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
|
||||
Assert.Equal("Bearer", after.Headers.WwwAuthenticate.Single().Scheme);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TokensExpireUnlessUsedWithinTheirLifetime()
|
||||
{
|
||||
var clock = new ManualTimeProvider();
|
||||
await using var server = await TestServer.StartAsync(context, o => o.TokenLifetimeDays = 30, s => s.AddSingleton<TimeProvider>(clock));
|
||||
var admin = await server.AdminAsync();
|
||||
clock.Advance(TimeSpan.FromDays(20));
|
||||
using (var used = await admin.GetAsync("/api/v1/auth/me")) await used.ExpectAsync(HttpStatusCode.OK);
|
||||
clock.Advance(TimeSpan.FromDays(20));
|
||||
using (var slid = await admin.GetAsync("/api/v1/auth/me")) await slid.ExpectAsync(HttpStatusCode.OK);
|
||||
clock.Advance(TimeSpan.FromDays(31));
|
||||
using var expired = await admin.GetAsync("/api/v1/auth/me");
|
||||
await expired.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TokensAreStoredOnlyAsHashes()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var login = await server.LoginAsync("admin", TestServer.AdminPassword);
|
||||
await using var session = await server.Database.Database.OpenAsync(context.Cancellation);
|
||||
var hashes = new List<string>();
|
||||
await foreach (var row in session.QueryAsync("SELECT token_hash FROM tokens", [], context.Cancellation)) hashes.Add(row.GetString(0));
|
||||
Assert.Contains(Titano.Server.Security.TokenService.HashOf(login.Token!), hashes);
|
||||
Assert.DoesNotContain(login.Token!, hashes);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DisablingAUserEndsTheirSessions()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, user, _) = await server.UserAsync("giulia", "editor");
|
||||
var admin = await server.AdminAsync();
|
||||
using (var disable = await admin.PatchAsJsonAsync($"/api/v1/users/{user.Id}", new UpdateUserRequest(null, null, null, true, null), ProtocolJson.Default.UpdateUserRequest))
|
||||
await disable.ExpectAsync(HttpStatusCode.OK);
|
||||
using var me = await editor.GetAsync("/api/v1/auth/me");
|
||||
await me.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
|
||||
using var login = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("giulia", TestServer.Password, "tests"), ProtocolJson.Default.LoginRequest);
|
||||
await login.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PermissionsSeparateMissingSignInFromMissingRights()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
using (var anonymous = await server.Client.GetAsync("/api/v1/users")) await anonymous.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
|
||||
using (var anonymousSequences = await server.Client.GetAsync("/api/v1/sequences")) await anonymousSequences.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
|
||||
|
||||
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
|
||||
using (var users = await viewer.GetAsync("/api/v1/users")) await users.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using (var sequences = await viewer.GetAsync("/api/v1/sequences")) await sequences.ExpectAsync(HttpStatusCode.OK);
|
||||
using (var upload = await viewer.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "a.jpg", 10, new string('a', 64), null, null),
|
||||
ProtocolJson.Default.StartUploadRequest))
|
||||
await upload.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using (var settings = await viewer.GetAsync("/api/v1/server/settings")) await settings.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
using (var start = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "a.jpg", 10, new string('a', 64), null, null),
|
||||
ProtocolJson.Default.StartUploadRequest))
|
||||
await start.ExpectAsync(HttpStatusCode.Created);
|
||||
using var edit = await uploader.PatchAsJsonAsync($"/api/v1/sequences/{Guid.NewGuid()}", new UpdateSequenceRequest("x", null, null, null, null, null),
|
||||
ProtocolJson.Default.UpdateSequenceRequest);
|
||||
await edit.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CookieSessionsNeedTheCsrfHeaderForChanges()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
await server.AdminAsync();
|
||||
var cookies = new CookieContainer();
|
||||
var browser = server.NewCookieClient(cookies);
|
||||
using (var login = await browser.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.Password, "admin-web", Cookie: true),
|
||||
ProtocolJson.Default.LoginRequest))
|
||||
{
|
||||
await login.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Null((await login.ReadAsync(ProtocolJson.Default.LoginResponse)).Token);
|
||||
var session = login.Headers.GetValues("Set-Cookie").Single(c => c.StartsWith("titano_session=", StringComparison.Ordinal));
|
||||
Assert.Contains("httponly", session.ToLowerInvariant());
|
||||
Assert.Contains("secure", session.ToLowerInvariant());
|
||||
Assert.Contains("samesite=strict", session.ToLowerInvariant());
|
||||
}
|
||||
using (var me = await browser.GetAsync("/api/v1/auth/me")) await me.ExpectAsync(HttpStatusCode.OK);
|
||||
using (var forged = await browser.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest("x", null), ProtocolJson.Default.CreateSequenceRequest))
|
||||
await forged.ExpectErrorAsync(HttpStatusCode.Forbidden, "csrf_failed");
|
||||
using (var wrong = new HttpRequestMessage(HttpMethod.Post, "/api/v1/sequences") { Content = JsonContent.Create(new CreateSequenceRequest("x", null), ProtocolJson.Default.CreateSequenceRequest) })
|
||||
{
|
||||
wrong.Headers.Add("X-CSRF-Token", "not-the-token");
|
||||
using var response = await browser.SendAsync(wrong);
|
||||
await response.ExpectErrorAsync(HttpStatusCode.Forbidden, "csrf_failed");
|
||||
}
|
||||
var csrf = cookies.GetCookies(server.HttpsBase!)["titano_csrf"]!.Value;
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/sequences") { Content = JsonContent.Create(new CreateSequenceRequest("x", null), ProtocolJson.Default.CreateSequenceRequest) };
|
||||
request.Headers.Add("X-CSRF-Token", csrf);
|
||||
using var created = await browser.SendAsync(request);
|
||||
await created.ExpectAsync(HttpStatusCode.Created);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ErrorsHaveTheApiShapeAndHeader()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using (var missing = await server.Client.GetAsync("/api/v1/nothing-here")) await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
using (var badJson = await admin.PostAsync("/api/v1/sequences", new StringContent("{ not json", MediaTypeHeaderValue.Parse("application/json"))))
|
||||
await badJson.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request");
|
||||
using (var badGuid = await admin.GetAsync($"/api/v1/sequences/{Guid.NewGuid()}")) await badGuid.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
using var italian = new HttpRequestMessage(HttpMethod.Get, "/api/v1/users");
|
||||
italian.Headers.AcceptLanguage.ParseAdd("it-IT");
|
||||
using var response = await server.Client.SendAsync(italian);
|
||||
var error = await response.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized");
|
||||
Assert.Equal("Accedi per continuare.", error.Message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Server.Storage;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Storage;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class FileTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task OriginalsAreStreamedWithRangesAndEtags()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var data = Uploads.Photo(10_000, 21);
|
||||
var frame = await Uploads.UploadAsync(editor, data);
|
||||
|
||||
using (var full = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original"))
|
||||
{
|
||||
await full.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Equal("bytes", full.Headers.AcceptRanges.Single());
|
||||
Assert.Equal($"\"{Uploads.Sha(data)}\"", full.Headers.ETag!.Tag);
|
||||
await Http.BytesEqualAsync(data, full.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
|
||||
using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original");
|
||||
range.Headers.Range = new RangeHeaderValue(100, 199);
|
||||
using (var partial = await editor.SendAsync(range))
|
||||
{
|
||||
await partial.ExpectAsync(HttpStatusCode.PartialContent);
|
||||
Assert.Equal((100L, 199L, 10_000L), (partial.Content.Headers.ContentRange!.From!.Value, partial.Content.Headers.ContentRange.To!.Value,
|
||||
partial.Content.Headers.ContentRange.Length!.Value));
|
||||
await Http.BytesEqualAsync(data[100..200], partial.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
|
||||
using var tail = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original");
|
||||
tail.Headers.Range = new RangeHeaderValue(9_990, null);
|
||||
using (var end = await editor.SendAsync(tail)) await Http.BytesEqualAsync(data[9_990..], end.Content.ReadAsByteArrayAsync());
|
||||
|
||||
using var cached = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original");
|
||||
cached.Headers.IfNoneMatch.Add(new EntityTagHeaderValue($"\"{Uploads.Sha(data)}\""));
|
||||
using (var notModified = await editor.SendAsync(cached)) await notModified.ExpectAsync(HttpStatusCode.NotModified);
|
||||
|
||||
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
|
||||
using var refused = await viewer.GetAsync($"/api/v1/frames/{frame.FrameId}/original");
|
||||
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ThumbnailsAndProxiesComeFromTheLocatedFiles()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var first = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 22), "a.jpg", sessionKey: "s");
|
||||
var second = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 23), "b.jpg", sessionKey: "s");
|
||||
var sequence = (await server.Database.Sequences.GetAsync(first.SequenceId))!;
|
||||
var folder = Path.Combine(server.Options.ArchivePath, sequence.Folder);
|
||||
var pack = Uploads.Photo(FakeAssetLocator.ThumbnailSize * 2, 24);
|
||||
Directory.CreateDirectory(Path.Combine(folder, ".titano"));
|
||||
await File.WriteAllBytesAsync(Path.Combine(folder, ".titano", "thumbs.pack"), pack);
|
||||
|
||||
using (var thumb = await editor.GetAsync($"/api/v1/frames/{second.FrameId}/thumb"))
|
||||
{
|
||||
await thumb.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Equal("image/jpeg", thumb.Content.Headers.ContentType!.MediaType);
|
||||
await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(2 * FakeAssetLocator.ThumbnailSize)], thumb.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{second.FrameId}/thumb");
|
||||
range.Headers.Range = new RangeHeaderValue(0, 9);
|
||||
using (var partial = await editor.SendAsync(range))
|
||||
{
|
||||
await partial.ExpectAsync(HttpStatusCode.PartialContent);
|
||||
await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(FakeAssetLocator.ThumbnailSize + 10)], partial.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
|
||||
using (var missing = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy"))
|
||||
Assert.Equal("asset_missing", (await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
|
||||
var proxy = Uploads.Photo(500, 25);
|
||||
Directory.CreateDirectory(Path.Combine(folder, "proxies"));
|
||||
await File.WriteAllBytesAsync(Path.Combine(folder, "proxies", "0000.jpg"), proxy);
|
||||
using var found = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy");
|
||||
await Http.BytesEqualAsync(proxy, found.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PathsOutsideTheArchiveAreNeverServed()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 26));
|
||||
var secret = Path.Combine(Path.GetDirectoryName(server.Options.ArchivePath)!, "secret.txt");
|
||||
await File.WriteAllTextAsync(secret, "not for clients");
|
||||
|
||||
foreach (var hostile in new[] { "../secret.txt", "2026/../../secret.txt", "..\\secret.txt", secret, "/etc/passwd" })
|
||||
{
|
||||
await using (var session = await server.Database.Database.OpenAsync(context.Cancellation))
|
||||
await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", [hostile, frame.FrameId], context.Cancellation);
|
||||
using var response = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original");
|
||||
await response.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
}
|
||||
|
||||
var locator = server.Services.GetRequiredService<FakeAssetLocator>();
|
||||
locator.Override = new AssetLocation(secret, 0, 5, "text/plain");
|
||||
using (var proxy = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/proxy")) await proxy.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
locator.Override = new AssetLocation(Path.Combine(server.Options.ArchivePath, "..", "secret.txt"), 0, 5, "text/plain");
|
||||
using (var dotted = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/thumb")) await dotted.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
var link = Path.Combine(server.Options.ArchivePath, "link.jpg");
|
||||
File.CreateSymbolicLink(link, secret);
|
||||
await using (var session = await server.Database.Database.OpenAsync(context.Cancellation))
|
||||
await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", ["link.jpg", frame.FrameId], context.Cancellation);
|
||||
using var linked = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original");
|
||||
await linked.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ExportsAreDownloadableWithRanges()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 27));
|
||||
var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!;
|
||||
var video = Uploads.Photo(4096, 28);
|
||||
var relative = $"{sequence.Folder}/exports/Harbour sunset - 4K.mp4";
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(server.Options.ArchivePath, relative))!);
|
||||
await File.WriteAllBytesAsync(Path.Combine(server.Options.ArchivePath, relative), video);
|
||||
var render = new Render { Id = Guid.CreateVersion7(), SequenceId = sequence.Id, PresetName = "4K HEVC", OutputPath = relative, Bytes = video.Length, Created = DateTimeOffset.UtcNow };
|
||||
await server.Database.Renders.InsertAsync(render);
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/renders/{render.Id}/file");
|
||||
request.Headers.Range = new RangeHeaderValue(1000, 1999);
|
||||
using var response = await viewer.SendAsync(request);
|
||||
await response.ExpectAsync(HttpStatusCode.PartialContent);
|
||||
Assert.Equal("video/mp4", response.Content.Headers.ContentType!.MediaType);
|
||||
await Http.BytesEqualAsync(video[1000..2000], response.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Protocol;
|
||||
using Titano.Protocol.Client;
|
||||
using Titano.Server.Storage;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Storage;
|
||||
|
||||
[Category("server")]
|
||||
public sealed class UploadTests(TestContext context)
|
||||
{
|
||||
private const int Chunk = 64 * 1024;
|
||||
|
||||
[Test]
|
||||
public async Task ChunksInAnyOrderCommitAVerifiedFrame()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
var data = Uploads.Photo(Chunk * 3 + 1234, 1);
|
||||
var start = await Uploads.StartAsync(uploader, data, sessionKey: "roof-2026-09-28");
|
||||
var id = Assert.NotNull(start.UploadId);
|
||||
Assert.Equal(Chunk, start.ChunkSize);
|
||||
Assert.Empty(start.Received);
|
||||
|
||||
foreach (var index in new[] { 3, 0, 2, 1, 0 })
|
||||
{
|
||||
using var put = await Uploads.PutChunkAsync(uploader, id, index, Uploads.Chunk(data, Chunk, index));
|
||||
await put.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Equal(index, (await put.ReadAsync(ProtocolJson.Default.ChunkReceived)).Index);
|
||||
}
|
||||
using (var state = await uploader.GetAsync($"/api/v1/uploads/{id}"))
|
||||
{
|
||||
var resume = await state.ReadAsync(ProtocolJson.Default.UploadState);
|
||||
Assert.SequenceEqual([0, 1, 2, 3], resume.Received);
|
||||
Assert.Equal("receiving", resume.State);
|
||||
}
|
||||
|
||||
using var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null);
|
||||
await complete.ExpectAsync(HttpStatusCode.OK);
|
||||
var frame = await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse);
|
||||
var ingest = server.Services.GetRequiredService<FakeArchiveIngest>();
|
||||
var request = Assert.NotNull(ingest.Requests.LastOrDefault());
|
||||
Assert.Equal("roof-2026-09-28", request.SessionKey);
|
||||
Assert.Null(request.SequenceId);
|
||||
Assert.Equal(Uploads.Sha(data), request.Sha256);
|
||||
Assert.True(request.StagedPath.StartsWith(server.Options.StagingPath, StringComparison.Ordinal), request.StagedPath);
|
||||
|
||||
var stored = Assert.NotNull(await server.Database.Frames.GetAsync(frame.FrameId));
|
||||
await Http.BytesEqualAsync(data, File.ReadAllBytesAsync(Path.Combine(server.Options.ArchivePath, stored.RelativePath)));
|
||||
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(id), "the partial file is gone");
|
||||
Assert.Empty(Directory.EnumerateFiles(server.Options.StagingPath));
|
||||
using (var me = await uploader.GetAsync("/api/v1/auth/me")) Assert.Equal(data.Length, (await me.ReadAsync(ProtocolJson.Default.UserDto)).UsedBytes);
|
||||
|
||||
// A completion whose response was lost can be repeated.
|
||||
using var again = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null);
|
||||
await again.ExpectAsync(HttpStatusCode.OK);
|
||||
Assert.Equal(frame.FrameId, (await again.ReadAsync(ProtocolJson.Default.CompleteUploadResponse)).FrameId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnInterruptedUploadResumesWithItsReceivedChunks()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
var data = Uploads.Photo(Chunk * 4, 2);
|
||||
var first = await Uploads.StartAsync(uploader, data);
|
||||
foreach (var index in new[] { 0, 2 })
|
||||
{
|
||||
using var put = await Uploads.PutChunkAsync(uploader, first.UploadId!.Value, index, Uploads.Chunk(data, Chunk, index));
|
||||
await put.ExpectAsync(HttpStatusCode.OK);
|
||||
}
|
||||
var resumed = await Uploads.StartAsync(uploader, data);
|
||||
Assert.Equal(first.UploadId, resumed.UploadId);
|
||||
Assert.SequenceEqual([0, 2], resumed.Received);
|
||||
foreach (var index in new[] { 1, 3 })
|
||||
{
|
||||
using var put = await Uploads.PutChunkAsync(uploader, first.UploadId!.Value, index, Uploads.Chunk(data, Chunk, index));
|
||||
await put.ExpectAsync(HttpStatusCode.OK);
|
||||
}
|
||||
using var complete = await uploader.PostAsync($"/api/v1/uploads/{first.UploadId}/complete", null);
|
||||
await complete.ExpectAsync(HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AKnownHashShortCircuitsTheUpload()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
var data = Uploads.Photo(5000, 3);
|
||||
var frame = await Uploads.UploadAsync(uploader, data);
|
||||
var again = await Uploads.StartAsync(uploader, data, expected: HttpStatusCode.OK);
|
||||
Assert.True(again.AlreadyExists);
|
||||
Assert.Null(again.UploadId);
|
||||
Assert.Equal(frame.FrameId, again.FrameId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheProtocolClientUploadsAndDownloadsLikeTheUploader()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
||||
var (_, _, token) = await server.UserAsync("alberto", "editor");
|
||||
var folder = context.CreateTempDirectory("source");
|
||||
var path = Path.Combine(folder, "2026-09-28_18-02-11_0413.CR3");
|
||||
var data = Uploads.Photo(Chunk * 2 + 77, 4);
|
||||
await File.WriteAllBytesAsync(path, data);
|
||||
|
||||
using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "Titano Uploader (tests)") { Token = token };
|
||||
var sequence = await client.CreateSequenceAsync(new CreateSequenceRequest("Harbour sunset", null));
|
||||
var progress = new List<UploadProgress>();
|
||||
var result = Assert.NotNull(await client.UploadFileAsync(path, sequence.Id, new DateTime(2026, 9, 28), progress: new SyncProgress<UploadProgress>(progress.Add)));
|
||||
Assert.Equal(sequence.Id, result.SequenceId);
|
||||
Assert.Equal(0, result.Index);
|
||||
Assert.Equal(data.Length, progress[^1].BytesSent);
|
||||
|
||||
var duplicate = Assert.NotNull(await client.UploadFileAsync(path, sequence.Id, null));
|
||||
Assert.Equal((result.FrameId, -1), (duplicate.FrameId, duplicate.Index));
|
||||
|
||||
using var downloaded = new MemoryStream();
|
||||
await client.DownloadFrameAsync(result.FrameId, "original", downloaded);
|
||||
Assert.BytesEqual(data, downloaded.ToArray());
|
||||
var manifest = await client.GetManifestAsync(sequence.Id);
|
||||
Assert.Equal(Uploads.Sha(data), manifest.Frames.Single().Sha256);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ADamagedChunkFailsTheHashCheck()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
var data = Uploads.Photo(Chunk * 2, 5);
|
||||
var start = await Uploads.StartAsync(uploader, data);
|
||||
var id = start.UploadId!.Value;
|
||||
var damaged = Uploads.Chunk(data, Chunk, 1).ToArray();
|
||||
damaged[100] ^= 0xFF;
|
||||
using (var a = await Uploads.PutChunkAsync(uploader, id, 0, Uploads.Chunk(data, Chunk, 0))) await a.ExpectAsync(HttpStatusCode.OK);
|
||||
using (var b = await Uploads.PutChunkAsync(uploader, id, 1, damaged)) await b.ExpectAsync(HttpStatusCode.OK);
|
||||
|
||||
using var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null);
|
||||
await complete.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "hash_mismatch");
|
||||
using (var state = await uploader.GetAsync($"/api/v1/uploads/{id}")) Assert.Equal("failed", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State);
|
||||
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(id));
|
||||
Assert.Empty(await server.Database.Frames.FindBySha256Async(Uploads.Sha(data)));
|
||||
Assert.Empty(server.Services.GetRequiredService<FakeArchiveIngest>().Requests);
|
||||
|
||||
var retry = await Uploads.StartAsync(uploader, data);
|
||||
Assert.NotEqual(id, retry.UploadId!.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ChunksAndCompletionsAreValidated()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
var data = Uploads.Photo(Chunk + 10, 6);
|
||||
var id = (await Uploads.StartAsync(uploader, data)).UploadId!.Value;
|
||||
using (var small = await Uploads.PutChunkAsync(uploader, id, 0, new byte[10])) await small.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_size");
|
||||
using (var range = await Uploads.PutChunkAsync(uploader, id, 2, new byte[10])) await range.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_out_of_range");
|
||||
using (var early = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null)) await early.ExpectErrorAsync(HttpStatusCode.Conflict, "incomplete");
|
||||
|
||||
using (var name = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "../../etc/passwd", 10, Uploads.Sha(data), null, null),
|
||||
ProtocolJson.Default.StartUploadRequest))
|
||||
await name.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_file_name");
|
||||
using (var hash = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "a.jpg", 10, "abc", null, null), ProtocolJson.Default.StartUploadRequest))
|
||||
await hash.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_hash");
|
||||
|
||||
var (other, _, _) = await server.UserAsync("intruder", "uploader");
|
||||
using (var foreign = await Uploads.PutChunkAsync(other, id, 0, Uploads.Chunk(data, Chunk, 0))) await foreign.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
using (var cancel = await uploader.DeleteAsync($"/api/v1/uploads/{id}")) await cancel.ExpectAsync(HttpStatusCode.NoContent);
|
||||
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(id));
|
||||
using var late = await Uploads.PutChunkAsync(uploader, id, 0, Uploads.Chunk(data, Chunk, 0));
|
||||
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task QuotaCountsCommittedAndPendingUploads()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader", quotaBytes: 100_000);
|
||||
var first = Uploads.Photo(80_000, 7);
|
||||
var pending = await Uploads.StartAsync(uploader, first);
|
||||
using (var over = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "b.jpg", 40_000, Uploads.Sha(Uploads.Photo(40_000, 8)), null, null),
|
||||
ProtocolJson.Default.StartUploadRequest))
|
||||
await over.ExpectErrorAsync(HttpStatusCode.Forbidden, "quota_exceeded");
|
||||
using (var cancel = await uploader.DeleteAsync($"/api/v1/uploads/{pending.UploadId}")) await cancel.ExpectAsync(HttpStatusCode.NoContent);
|
||||
await Uploads.UploadAsync(uploader, first);
|
||||
using var after = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "c.jpg", 30_000, Uploads.Sha(Uploads.Photo(30_000, 9)), null, null),
|
||||
ProtocolJson.Default.StartUploadRequest);
|
||||
await after.ExpectErrorAsync(HttpStatusCode.Forbidden, "quota_exceeded");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FilesAboveTheLimitAreRefused()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, o => o.MaxUploadBytes = 1000);
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
using var response = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "big.jpg", 1001, new string('0', 64), null, null),
|
||||
ProtocolJson.Default.StartUploadRequest);
|
||||
var error = await response.ExpectErrorAsync(HttpStatusCode.RequestEntityTooLarge, "too_large");
|
||||
Assert.Equal("1000", error.Details);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AbandonedUploadsExpireAndLoseTheirFiles()
|
||||
{
|
||||
var clock = new ManualTimeProvider();
|
||||
await using var server = await TestServer.StartAsync(context, o => o.UploadRetentionHours = 48, s => s.AddSingleton<TimeProvider>(clock));
|
||||
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
|
||||
var id = (await Uploads.StartAsync(uploader, Uploads.Photo(1000, 10))).UploadId!.Value;
|
||||
var store = server.Services.GetRequiredService<UploadStore>();
|
||||
Assert.True(store.Exists(id));
|
||||
|
||||
// Upload rows carry real timestamps; the sweep looks 48 h past the moved clock.
|
||||
clock.Advance(TimeSpan.FromHours(49));
|
||||
await server.Services.GetRequiredService<UploadMaintenance>().SweepAsync(context.Cancellation);
|
||||
Assert.False(store.Exists(id));
|
||||
using var state = await uploader.GetAsync($"/api/v1/uploads/{id}");
|
||||
Assert.Equal("cancelled", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State);
|
||||
}
|
||||
|
||||
/// <summary>Reports synchronously (Progress<T> posts to the thread pool and may arrive late).</summary>
|
||||
private sealed class SyncProgress<T>(Action<T> report) : IProgress<T>
|
||||
{
|
||||
public void Report(T value) => report(value);
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,8 @@ public sealed class TestServer : IAsyncDisposable
|
||||
Workers = 2,
|
||||
AdminUser = "admin",
|
||||
AdminPassword = AdminPassword,
|
||||
// Tests sign in many times from one address; the rate-limit test sets the production value.
|
||||
LoginAttemptsPerMinute = 1000,
|
||||
};
|
||||
Directory.CreateDirectory(options.ArchivePath);
|
||||
configure?.Invoke(options);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Security.Cryptography;
|
||||
using Titano.Protocol;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests;
|
||||
|
||||
/// <summary>Raw-HTTP upload helpers (the protocol client is exercised separately).</summary>
|
||||
internal static class Uploads
|
||||
{
|
||||
public static byte[] Photo(int size, int seed)
|
||||
{
|
||||
var bytes = new byte[size];
|
||||
new Random(seed).NextBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static string Sha(byte[] data) => Convert.ToHexStringLower(SHA256.HashData(data));
|
||||
|
||||
public static async Task<StartUploadResponse> StartAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null,
|
||||
string? sessionKey = null, HttpStatusCode expected = HttpStatusCode.Created)
|
||||
{
|
||||
using var response = await client.PostAsJsonAsync("/api/v1/uploads",
|
||||
new StartUploadRequest(sequenceId, fileName, data.Length, Sha(data), new DateTime(2026, 9, 28, 18, 2, 11), sessionKey), ProtocolJson.Default.StartUploadRequest);
|
||||
await response.ExpectAsync(expected);
|
||||
return await response.ReadAsync(ProtocolJson.Default.StartUploadResponse);
|
||||
}
|
||||
|
||||
public static async Task<HttpResponseMessage> PutChunkAsync(HttpClient client, Guid uploadId, int index, ReadOnlyMemory<byte> data)
|
||||
{
|
||||
var content = new ByteArrayContent(data.ToArray());
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
||||
return await client.PutAsync($"/api/v1/uploads/{uploadId}/chunks/{index}", content);
|
||||
}
|
||||
|
||||
public static ReadOnlyMemory<byte> Chunk(byte[] data, int chunkSize, int index) =>
|
||||
data.AsMemory(index * chunkSize, Math.Min(chunkSize, data.Length - index * chunkSize));
|
||||
|
||||
/// <summary>Start, every chunk, complete; returns the committed frame.</summary>
|
||||
public static async Task<CompleteUploadResponse> UploadAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null,
|
||||
string? sessionKey = null)
|
||||
{
|
||||
var start = await StartAsync(client, data, fileName, sequenceId, sessionKey);
|
||||
var id = Assert.NotNull(start.UploadId);
|
||||
var chunks = (data.Length + start.ChunkSize - 1) / start.ChunkSize;
|
||||
for (var i = 0; i < chunks; i++)
|
||||
{
|
||||
using var put = await PutChunkAsync(client, id, i, Chunk(data, start.ChunkSize, i));
|
||||
await put.ExpectAsync(HttpStatusCode.OK);
|
||||
}
|
||||
using var complete = await client.PostAsync($"/api/v1/uploads/{id}/complete", null);
|
||||
await complete.ExpectAsync(HttpStatusCode.OK);
|
||||
return await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user