GET /api/v1/logs/files/{name} served the file with Results.File, which sets
Content-Length from the file's size and then copies it to its end. The current
log grows whenever the server logs, so a line written meanwhile made Kestrel
abort the response ("too many bytes written"); AdminTests failed on it once in
three full runs. The endpoint now sends the bytes the file held when it was
listed through FileResponses.Serve, a fixed-length, range-capable slice.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
123 lines
6.6 KiB
C#
123 lines
6.6 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json.Nodes;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
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, o => o.LogLevel = Microsoft.Extensions.Logging.LogLevel.Information);
|
|
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 TheCurrentLogDownloadsWhileTheServerKeepsLogging()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, o => o.LogLevel = Microsoft.Extensions.Logging.LogLevel.Information);
|
|
var admin = await server.AdminAsync();
|
|
using var files = await admin.GetAsync("/api/v1/logs/files");
|
|
var name = (await files.ReadAsync(ProtocolJson.Default.ListLogFileDto)).First().Name;
|
|
var logger = server.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Titano.Server.Tests");
|
|
using var stop = new CancellationTokenSource();
|
|
// Jobs, uploads and sign-ins keep writing to the file an administrator downloads.
|
|
var logging = Task.Run(() =>
|
|
{
|
|
while (!stop.IsCancellationRequested) logger.LogInformation("Busy");
|
|
});
|
|
try
|
|
{
|
|
for (var i = 0; i < 5; i++)
|
|
{
|
|
using var download = await admin.GetAsync($"/api/v1/logs/files/{name}");
|
|
await download.ExpectAsync(HttpStatusCode.OK);
|
|
Assert.Equal(download.Content.Headers.ContentLength, (await download.Content.ReadAsByteArrayAsync()).LongLength);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
await stop.CancelAsync();
|
|
await logging;
|
|
}
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|