La prova iniettava una tabella dei montaggi di Linux (cifs), che su Windows non esiste: lì il controllo riconosce la rete dai percorsi UNC e dalle unità di rete, quindi il server partiva normalmente e la prova falliva. Ora su Windows la cartella dei dati è un percorso UNC verso un nome .invalid, riconosciuto dalla sola forma senza interrogare la rete; su Linux la prova è quella di prima. Il prodotto era già giusto: cambia solo la prova. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
8.8 KiB
C#
180 lines
8.8 KiB
C#
using System.Net;
|
|
using System.Net.WebSockets;
|
|
using System.Text;
|
|
using Febe.Core.Library;
|
|
using Febe.Server;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Server;
|
|
|
|
/// <summary>
|
|
/// Spec §8.2, §11: with the password missing, or the Data folder not writable or on a network share, neither the
|
|
/// engine nor the browser start; the interface shows only the page that explains it and <c>/salute</c> says «problema».
|
|
/// The remote view is never reachable without a password. No browser needed.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class StartupProblemTests
|
|
{
|
|
public TestContext TestContext { get; set; } = null!;
|
|
|
|
[TestMethod]
|
|
public async Task WithoutPasswordOnlyTheErrorPageAnswersAndNothingStarts()
|
|
{
|
|
using var temp = new TempFolder("server-problemi");
|
|
await using var server = await FebeServer.StartAsync(Options(temp) with { Password = null }, Hooks());
|
|
var problem = server.Problems.Single();
|
|
Assert.AreEqual(StartupChecks.PasswordMissing, problem.Code);
|
|
StringAssert.Contains(problem.Remedy, "FEBE_PASSWORD");
|
|
Assert.IsNull(server.Browser, "no browser");
|
|
Assert.IsNull(server.Engine, "no engine");
|
|
Assert.IsFalse(Directory.Exists(temp.Combine("dati", "registro")), "nothing written in the Data folder");
|
|
Assert.IsFalse(File.Exists(temp.Combine("dati", "febe.sqlite")));
|
|
|
|
using var client = Client(server);
|
|
await AssertHealthAsync(client, HttpStatusCode.ServiceUnavailable, """{"stato":"problema"}""");
|
|
|
|
// The error page and what it needs.
|
|
foreach (var path in new[] { "/", "/libreria", "/impostazioni/collegamento", "/css/febe.css", "/js/app.js", "/testi/it.json", "/api/capabilities" })
|
|
{
|
|
using var response = await client.GetAsync(path, TestContext.CancellationToken);
|
|
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, path);
|
|
}
|
|
|
|
// Nothing else: library, accounts, engine commands, login, the remote view.
|
|
foreach (var path in new[] { "/api/accounts", "/api/library/days", "/api/engine", "/api/eventi" })
|
|
{
|
|
using var response = await client.GetAsync(path, TestContext.CancellationToken);
|
|
Assert.AreEqual(HttpStatusCode.ServiceUnavailable, response.StatusCode, path);
|
|
}
|
|
|
|
using (var login = await PostAsync(client, server, "/api/accesso", """{"password":"qualunque"}"""))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.ServiceUnavailable, login.StatusCode);
|
|
}
|
|
|
|
await AssertNoRemoteViewAsync(server);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ADataFolderThatIsAFileStopsTheEngineEvenWithThePassword()
|
|
{
|
|
using var temp = new TempFolder("server-problemi");
|
|
var file = temp.Combine("dati-file");
|
|
await File.WriteAllTextAsync(file, "non una cartella", TestContext.CancellationToken);
|
|
await using var server = await FebeServer.StartAsync(Options(temp) with { DataFolder = file }, Hooks());
|
|
var problem = server.Problems.Single();
|
|
Assert.AreEqual(StartupChecks.DataNotWritable, problem.Code);
|
|
Assert.IsNull(server.Engine);
|
|
|
|
var cookies = new CookieContainer();
|
|
using var client = Client(server, cookies);
|
|
await AssertHealthAsync(client, HttpStatusCode.ServiceUnavailable, """{"stato":"problema"}""");
|
|
|
|
// The password works, then only the problem page's API answers.
|
|
using (var login = await PostAsync(client, server, "/api/accesso", """{"password":"una password"}"""))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.OK, login.StatusCode, await login.Content.ReadAsStringAsync());
|
|
}
|
|
|
|
using (var capabilities = await client.GetAsync("/api/capabilities", TestContext.CancellationToken))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.OK, capabilities.StatusCode);
|
|
}
|
|
|
|
using (var engine = await client.GetAsync("/api/engine", TestContext.CancellationToken))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.OK, engine.StatusCode, "the frame of the page may ask for the (offline) engine");
|
|
}
|
|
|
|
using (var accounts = await client.GetAsync("/api/accounts", TestContext.CancellationToken))
|
|
{
|
|
Assert.AreEqual(HttpStatusCode.ServiceUnavailable, accounts.StatusCode);
|
|
using var json = System.Text.Json.JsonDocument.Parse(await accounts.Content.ReadAsStringAsync());
|
|
StringAssert.Contains(json.RootElement.GetProperty("message").GetString(), "Il motore non è partito");
|
|
}
|
|
|
|
await AssertNoRemoteViewAsync(server, cookies);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ADataFolderOnANetworkShareIsRefused()
|
|
{
|
|
// A share shows itself differently on the two systems: on Linux as a cifs mount in the mount table (injected
|
|
// here), on Windows as a UNC path, recognised from its form before anything touches it. The Windows server name
|
|
// ends in .invalid (RFC 6761): no machine answers to it, and nothing here looks it up.
|
|
using var temp = new TempFolder("server-problemi");
|
|
var windows = OperatingSystem.IsWindows();
|
|
var data = windows ? @"\\febe-prova.invalid\condivisa\dati" : temp.Combine("dati");
|
|
var mounts = $"/dev/sda1 / ext4 rw 0 0\n//nas/condivisa {temp.Path} cifs rw 0 0\n";
|
|
await using var server = await FebeServer.StartAsync(Options(temp) with { DataFolder = data }, Hooks() with { DataFolderCheck = new DataFolderCheck(() => mounts) });
|
|
var problem = server.Problems.Single();
|
|
Assert.AreEqual(StartupChecks.DataOnNetwork, problem.Code);
|
|
StringAssert.Contains(problem.Message, windows ? "condivisione di rete" : "cifs");
|
|
if (!windows)
|
|
{
|
|
// Nothing written in the Data folder (on Windows the share does not exist: looking would mean asking the network).
|
|
Assert.IsFalse(Directory.Exists(Path.Combine(data, "registro")));
|
|
}
|
|
|
|
using var client = Client(server);
|
|
await AssertHealthAsync(client, HttpStatusCode.ServiceUnavailable, """{"stato":"problema"}""");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheHealthCheckCommandReadsSalute()
|
|
{
|
|
using var temp = new TempFolder("server-problemi");
|
|
await using var server = await FebeServer.StartAsync(Options(temp) with { Password = null }, Hooks());
|
|
using var output = new StringWriter();
|
|
Assert.AreEqual(1, await HealthCheckCommand.CheckAsync(new Uri(server.Address, "/salute"), output));
|
|
StringAssert.Contains(output.ToString(), "problema");
|
|
|
|
// Nobody listening: not healthy.
|
|
Assert.AreEqual(1, await HealthCheckCommand.CheckAsync(new Uri($"http://127.0.0.1:{Febe.Tests.Browser.TestSupport.LocalTestSite.FreePort()}/salute"), TextWriter.Null));
|
|
}
|
|
|
|
internal static ServerOptions Options(TempFolder temp) => new()
|
|
{
|
|
Password = "una password",
|
|
ListenAddress = IPAddress.Loopback,
|
|
Port = 0,
|
|
DataFolder = temp.Combine("dati"),
|
|
LibraryFolder = temp.Combine("libreria"),
|
|
ImportFolder = temp.Combine("importa"),
|
|
};
|
|
|
|
internal static ServerHooks Hooks() => new() { Console = TextWriter.Null };
|
|
|
|
private static HttpClient Client(FebeServer server, CookieContainer? cookies = null) =>
|
|
new(new HttpClientHandler { UseCookies = cookies is not null, CookieContainer = cookies ?? new CookieContainer(), AllowAutoRedirect = false }) { BaseAddress = server.Address };
|
|
|
|
private static async Task<HttpResponseMessage> PostAsync(HttpClient client, FebeServer server, string path, string json)
|
|
{
|
|
using var request = new HttpRequestMessage(HttpMethod.Post, path) { Content = new StringContent(json, Encoding.UTF8, "application/json") };
|
|
request.Headers.TryAddWithoutValidation("Origin", $"http://{server.Address.Authority}");
|
|
return await client.SendAsync(request);
|
|
}
|
|
|
|
private async Task AssertHealthAsync(HttpClient client, HttpStatusCode status, string body)
|
|
{
|
|
using var response = await client.GetAsync("/salute", TestContext.CancellationToken);
|
|
Assert.AreEqual(status, response.StatusCode);
|
|
Assert.AreEqual(body, await response.Content.ReadAsStringAsync(TestContext.CancellationToken));
|
|
}
|
|
|
|
private async Task AssertNoRemoteViewAsync(FebeServer server, CookieContainer? session = null)
|
|
{
|
|
using var socket = new ClientWebSocket();
|
|
socket.Options.SetRequestHeader("Origin", $"http://{server.Address.Authority}");
|
|
if (session is not null)
|
|
{
|
|
socket.Options.Cookies = session;
|
|
}
|
|
|
|
socket.Options.CollectHttpResponseDetails = true;
|
|
var address = new UriBuilder(server.Address) { Scheme = "ws", Path = "/api/vista" }.Uri;
|
|
await Assert.ThrowsAsync<WebSocketException>(() => socket.ConnectAsync(address, TestContext.CancellationToken));
|
|
Assert.AreNotEqual(HttpStatusCode.SwitchingProtocols, socket.HttpStatusCode);
|
|
}
|
|
}
|