using System.Globalization; using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text; using Microsoft.Extensions.DependencyInjection; using Chronos.Core.Codecs.Jpeg; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Protocol; using Chronos.Server.Storage; using Chronos.Testing; namespace Chronos.Server.Tests.Storage; /// /// The admin panel's upload dialog (owner review 2, R3; Admin/web/upload.js) speaks the upload API through the /// cookie session: requests shaped like api.js sends them (JSON bodies written as the page writes them, the /// page's language, the CSRF header on every unsafe call, chunks as octet-stream), against the real archive engine. /// [Category("server")] public sealed class BrowserUploadTests(TestContext context) { private const int Chunk = 64 * 1024; private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(30)); [Test] public async Task TheUploadDialogFilesPhotosThroughTheCookieSession() { await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register); var browser = await Browser.SignInAsync(server); // What the dialog offers and checks before it hashes anything. ServerInfo info; using (var response = await browser.SendAsync(HttpMethod.Get, "/api/v1/server/info")) info = await response.ReadAsync(ProtocolJson.Default.ServerInfo); var extensions = Assert.NotNull(info.Extensions); foreach (var extension in new[] { "jpg", "jpeg", "tif", "dng", "cr3", "nef", "heic" }) Assert.Contains(extension, extensions); Assert.True(extensions.All(e => e.Length > 0 && e == e.ToLowerInvariant() && !e.StartsWith('.')), string.Join(",", extensions)); // New sequence: the title as typed, no camera hint. SequenceDto sequence; using (var created = await browser.SendAsync(HttpMethod.Post, "/api/v1/sequences", Json("""{"title":"Roof","cameraHint":null}"""))) { await created.ExpectAsync(HttpStatusCode.Created); sequence = await created.ReadAsync(ProtocolJson.Default.SequenceDto); } var photos = new[] { Photo(1), Photo(2) }; var frames = new List(); for (var i = 0; i < photos.Length; i++) { var photo = photos[i]; var start = await StartAsync(browser, sequence.Id, $"IMG_{i + 1:0000}.JPG", photo, HttpStatusCode.Created); var id = Assert.NotNull(start.UploadId); Assert.Equal(Chunk, start.ChunkSize); var count = (photo.Length + Chunk - 1) / Chunk; Assert.True(count >= 2, $"{photo.Length} bytes make one chunk"); // putBytes sends the session's headers: a chunk without the CSRF header is refused like any other change. using (var refused = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/0", Bytes(photo, 0), csrf: false)) await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "csrf_failed"); // Two chunks in flight, finishing in any order. for (var last = count - 1; last >= 0; last -= 2) { var pair = new[] { last, last - 1 }.Where(n => n >= 0).Select(async n => { using var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/{n}", Bytes(photo, n)); return await put.ReadAsync(ProtocolJson.Default.ChunkReceived); }); foreach (var received in await Task.WhenAll(pair)) Assert.Contains(received.Index, received.Received); } using var complete = await browser.SendAsync(HttpMethod.Post, $"/api/v1/uploads/{id}/complete", Json("{}")); var frame = await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse); Assert.Equal(sequence.Id, frame.SequenceId); frames.Add(frame); } var stored = await server.Database.Frames.ListBySequenceAsync(sequence.Id); Assert.SequenceEqual(frames.Select(f => f.FrameId).Order(), stored.Select(f => f.Id).Order()); for (var i = 0; i < photos.Length; i++) { var file = Path.Combine(server.Options.ArchivePath, stored.Single(f => f.Id == frames[i].FrameId).RelativePath); await Http.BytesEqualAsync(photos[i], File.ReadAllBytesAsync(file)); } // Picked again: already there, with its frame. var again = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photos[0], HttpStatusCode.OK); Assert.True(again.AlreadyExists); Assert.Equal(frames[0].FrameId, again.FrameId); // Deleted from the sequence: also answered as there, without a frame ("deleted before; restore it from the trash"). await UntilIdleAsync(server); var current = Assert.NotNull(await server.Database.Sequences.GetAsync(sequence.Id)); using (var delete = await browser.SendAsync(HttpMethod.Post, $"/api/v1/sequences/{sequence.Id}/frames/delete", Json($$"""{"frameIds":["{{frames[1].FrameId}}"],"expectedFramesVersion":{{current.FramesVersion}},"dryRun":false}"""))) Assert.NotNull((await delete.ReadAsync(ProtocolJson.Default.FrameDeleteResultDto)).TrashEntry); var deleted = await StartAsync(browser, sequence.Id, "IMG_0002.JPG", photos[1], HttpStatusCode.OK); Assert.True(deleted.AlreadyExists); Assert.Null(deleted.UploadId); Assert.Null(deleted.FrameId); // Cancel: an upload the dialog started is discarded on the server. var cancelled = Photo(3); var started = await StartAsync(browser, sequence.Id, "IMG_0003.JPG", cancelled, HttpStatusCode.Created); var cancelledId = Assert.NotNull(started.UploadId); using (var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{cancelledId}/chunks/0", Bytes(cancelled, 0))) await put.ExpectAsync(HttpStatusCode.OK); using (var discard = await browser.SendAsync(HttpMethod.Delete, $"/api/v1/uploads/{cancelledId}")) await discard.ExpectAsync(HttpStatusCode.NoContent); using (var state = await browser.SendAsync(HttpMethod.Get, $"/api/v1/uploads/{cancelledId}")) Assert.Equal("cancelled", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State); using (var late = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{cancelledId}/chunks/1", Bytes(cancelled, 1))) await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state"); Assert.False(server.Services.GetRequiredService().Exists(cancelledId), "the partial file is gone"); } [Test] public async Task AQuotaRefusalCarriesTheMessageTheDialogShows() { // A quota or space refusal stops the dialog's run with the server's message, in the page's language. await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); var browser = await Browser.SignInAsync(server); Guid me; using (var response = await browser.SendAsync(HttpMethod.Get, "/api/v1/auth/me")) me = (await response.ReadAsync(ProtocolJson.Default.UserDto)).Id; using (var quota = await browser.SendAsync(HttpMethod.Patch, $"/api/v1/users/{me}", Json("""{"quotaBytes":1000}"""))) await quota.ExpectAsync(HttpStatusCode.OK); using var start = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(null, "IMG_0001.JPG", Uploads.Photo(Chunk + 10, 7))); var error = await start.ExpectErrorAsync(HttpStatusCode.Forbidden, "quota_exceeded"); Assert.Equal(Loc.Get("Server.Error.QuotaExceeded", CultureInfo.GetCultureInfo("it")), error.Message); } /// The bootstrap administrator signed in to the panel (a cookie session over HTTPS), calling as api.js does. private sealed class Browser(HttpClient client, string csrfToken) { public static async Task SignInAsync(TestServer server) { await server.AdminAsync(); var cookies = new CookieContainer(); var client = server.NewCookieClient(cookies); using var login = await client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.Password, "admin-web", Cookie: true), ProtocolJson.Default.LoginRequest); await login.ExpectAsync(HttpStatusCode.OK); return new Browser(client, Assert.NotNull(cookies.GetCookies(Assert.NotNull(server.HttpsBase))["chronos_csrf"]).Value); } /// JSON accepted, the page's language (Italian here), and the CSRF header on every call but a GET. public async Task SendAsync(HttpMethod method, string url, HttpContent? content = null, bool csrf = true) { using var request = new HttpRequestMessage(method, url) { Content = content }; request.Headers.Accept.ParseAdd("application/json"); request.Headers.AcceptLanguage.ParseAdd("it"); if (method != HttpMethod.Get && csrf) request.Headers.Add("X-CSRF-Token", csrfToken); return await client.SendAsync(request); } } private static StringContent Json(string json) => new(json, Encoding.UTF8, "application/json"); /// A chunk as putBytes sends a File.slice(): its bytes, octet-stream. private static ByteArrayContent Bytes(byte[] data, int index) { var content = new ByteArrayContent(Uploads.Chunk(data, Chunk, index).ToArray()); content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return content; } /// The start body upload.js writes: no capture time (the server reads it from the photo), no session key. private static StringContent StartBody(Guid? sequenceId, string fileName, byte[] data) => Json($$"""{"sequenceId":{{(sequenceId is { } id ? $"\"{id}\"" : "null")}},"fileName":"{{fileName}}","size":{{data.Length}},"sha256":"{{Uploads.Sha(data)}}","capturedAt":null,"sessionKey":null}"""); private static async Task StartAsync(Browser browser, Guid sequenceId, string fileName, byte[] data, HttpStatusCode expected) { using var response = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(sequenceId, fileName, data)); await response.ExpectAsync(expected); return await response.ReadAsync(ProtocolJson.Default.StartUploadResponse); } /// Waits until no job is queued or running (the thumbnails, proxies and analyses uploads queue). private static async Task UntilIdleAsync(TestServer server) { var until = DateTime.UtcNow + Wait; foreach (var state in new[] { JobState.Queued, JobState.Running, JobState.Queued, JobState.Running }) { while ((await server.Database.Jobs.ListAsync(state)).Items.Count > 0) { Assert.True(DateTime.UtcNow < until, $"jobs still {state}"); await Task.Delay(50); } } } /// A JPEG of a few chunks: noise does not compress below the chunk size. private static byte[] Photo(int seed) { const int width = 512, height = 384; var pixels = new byte[width * height * 3]; new Random(seed).NextBytes(pixels); return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 95 }); } }