using System.Net; using System.Text.Json; namespace OpenWaf.Tests.Integration; /// /// A 404 is an answer, and until now this API gave two different qualities of /// answer and the page guessed across them. /// /// Measured on the shipped image (openwaf:ci, 08bebf43): /// DELETE /admin-api/countries/ZZ → 404 {"error":"no country rule for \"ZZ\""} /// DELETE /admin-api/allow/999999 → 404, Content-Length: 0 /// Same status, one explained and one silent. The silent half is what the /// console then had to invent an explanation for: apiRaw read every 404 as /// "admin api disabled — set OpenWaf__Admin__Token" and threw, so removing a row /// that another tab had already removed told the operator to configure a token /// that is already configured — and because the throw landed in a catch that /// said nothing, the status line ended up reading "refreshed" in the success /// colour. Measured in the console gate before the fix: /// FAIL a missing row is reported as a missing row… — ok: refreshed 4:50:18 AM /// public sealed class NotFoundExplanationTests { private static async Task<(HttpStatusCode Status, string Body)> AdminAsync( EdgeFixture f, HttpMethod method, string url, string? json = null) { using var request = new HttpRequestMessage(method, url); if (json is not null) request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken); using var response = await f.Client.SendAsync(request); return (response.StatusCode, await response.Content.ReadAsStringAsync()); } [Fact] public async Task A_missing_allow_entry_names_the_id_that_was_not_there() { await using var f = new EdgeFixture(); var (status, body) = await AdminAsync(f, HttpMethod.Delete, "/admin-api/allow/999999"); Assert.Equal(HttpStatusCode.NotFound, status); Assert.NotEmpty(body); Assert.Equal("no allow entry with id 999999", JsonDocument.Parse(body).RootElement.GetProperty("error").GetString()); } [Fact] public async Task An_allow_entry_that_is_there_is_removed_with_no_body_and_the_row_goes() { await using var f = new EdgeFixture(); var (created, _) = await AdminAsync(f, HttpMethod.Post, "/admin-api/allow", """{"match":"203.0.113.77/32","note":"probe"}"""); Assert.Equal(HttpStatusCode.NoContent, created); var listed = await AdminAsync(f, HttpMethod.Get, "/admin-api/allow"); var id = JsonDocument.Parse(listed.Body).RootElement[0].GetProperty("id").GetInt64(); var (removed, removedBody) = await AdminAsync(f, HttpMethod.Delete, $"/admin-api/allow/{id}"); Assert.Equal(HttpStatusCode.NoContent, removed); Assert.Equal("", removedBody); var after = await AdminAsync(f, HttpMethod.Get, "/admin-api/allow"); Assert.Equal("[]", after.Body); // Removing it twice is the stale-tab case, and it names the id. var (again, againBody) = await AdminAsync(f, HttpMethod.Delete, $"/admin-api/allow/{id}"); Assert.Equal(HttpStatusCode.NotFound, again); Assert.Contains($"no allow entry with id {id}", againBody); } [Fact] public async Task A_closed_admin_surface_says_so_in_the_body_not_only_in_the_status() { // The console no longer guesses "the admin API is off" from a 404; it // shows what the edge said. That is only honest while the edge does say // it, so this pins the sentence. // "Closed" has to be asked for in both places. Program reads the token // from configuration and falls back to OPENWAF_ADMIN_TOKEN when that is // blank, and the gate scripts export a throwaway value into the test // process — so a blank configured token with the environment set is an // OPEN surface (measured: the request came back 401, not 404). Only this // fixture leaves its configured token blank, so clearing the variable // for the duration of this test cannot change what another one sees. var saved = Environment.GetEnvironmentVariable("OPENWAF_ADMIN_TOKEN"); Environment.SetEnvironmentVariable("OPENWAF_ADMIN_TOKEN", null); try { await using var f = new EdgeFixture(adminToken: ""); using var request = new HttpRequestMessage(HttpMethod.Get, "/admin-api/state"); request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken); using var response = await f.Client.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Assert.Equal("admin api disabled: set OpenWaf__Admin__Token", JsonDocument.Parse(body).RootElement.GetProperty("error").GetString()); } finally { if (saved is not null) Environment.SetEnvironmentVariable("OPENWAF_ADMIN_TOKEN", saved); } } }