End-to-end tests against the pasture; profiles, follows, search, votes, reactions
tests/e2e (Playwright for .NET 1.61.0 on xunit v3, the Chromium 1228 already cached, never downloaded, never in CI) runs the published app against SocialPub's pasture and its town: run.sh publishes it with the Pasture environment (wwwroot/appsettings.Pasture.json, published only then; the deploy fails if it ever ships) and serves it as https://decepub.test. The tests sign in through the real login page and persona chooser, check that every request goes only to decepub.test and privapub.test with the right credential on the right API, render one post of each kind from each platform as its author wrote it, and check every action on the other side of the wire (town.sh stored): favourites, boosts and replies with their undoing, follows from a profile, votes, reactions. Each test is also a cell of the town's report (out/client.jsonl). They found two PrivaPub bugs fixed there (a blocked author's status answered null; sensitive remote media invented a content warning) and a reply addressed to nobody. New in the app, all checked by those tests: - profiles at /account/{id}: header, bio, fields, counts, the account's posts as posts, posts and replies, or media, and follow, unfollow, cancel a request, mute and block (AccountService); post authors and notifications open them in the app; - search at /search: accounts, posts and hashtags, a handle or an address resolved by PrivaPub; - poll voting on the card, and emoji reactions shown and given with one tap; before a persona's first reaction or vote it is told once that both are public (PublicNoticeService, the owner's decision); - follow requests answered from their notification. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
This commit is contained in:
1 parent
0f68e74e38
commit
011bafb6af
39 files changed
+2074
-17
No files matched your search
@@ -0,0 +1,101 @@
|
||||
using Microsoft.Playwright;
|
||||
|
||||
[assembly: AssemblyFixture(typeof(decePubClient.E2E.Support.Browser))]
|
||||
|
||||
namespace decePubClient.E2E.Support
|
||||
{
|
||||
// One Chromium for the whole run. *.test names go to Caddy on 127.0.0.1:6443 (Chromium's own --resolve), whose
|
||||
// certificates come from the pasture's private CA, so certificate errors are ignored; nothing else is reachable.
|
||||
public sealed class Browser : IAsyncLifetime
|
||||
{
|
||||
IPlaywright _playwright;
|
||||
public IBrowser Chromium { get; private set; }
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
_playwright = await Playwright.CreateAsync();
|
||||
Chromium = await _playwright.Chromium.LaunchAsync(new()
|
||||
{
|
||||
Headless = Environment.GetEnvironmentVariable("E2E_HEADED") != "1",
|
||||
Args = ["--host-resolver-rules=MAP *.test 127.0.0.1:6443"]
|
||||
});
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (Chromium is not null)
|
||||
await Chromium.CloseAsync();
|
||||
_playwright?.Dispose();
|
||||
}
|
||||
|
||||
public async Task<Session> Open(string storageState = null)
|
||||
{
|
||||
var context = await Chromium.NewContextAsync(new()
|
||||
{
|
||||
IgnoreHTTPSErrors = true,
|
||||
Locale = "en-GB",
|
||||
TimezoneId = "Europe/Rome",
|
||||
ViewportSize = new() { Width = 1440, Height = 900 },
|
||||
ServiceWorkers = ServiceWorkerPolicy.Block,
|
||||
StorageStatePath = storageState
|
||||
});
|
||||
return new Session(context, await context.NewPageAsync());
|
||||
}
|
||||
}
|
||||
|
||||
// A browser tab and what it did: every request's host, every console error, every 5xx and Blazor's error bar. The
|
||||
// guards hold for every test: the app talks only to its own site and to PrivaPub, and never fails silently.
|
||||
public sealed class Session : IAsyncDisposable
|
||||
{
|
||||
public IBrowserContext Context { get; }
|
||||
public IPage Page { get; }
|
||||
public List<string> Hosts { get; } = [];
|
||||
public List<string> ConsoleErrors { get; } = [];
|
||||
public List<string> ServerErrors { get; } = [];
|
||||
public List<(string Url, string Authorization)> ApiCalls { get; } = [];
|
||||
|
||||
public Session(IBrowserContext context, IPage page)
|
||||
{
|
||||
Context = context;
|
||||
Page = page;
|
||||
page.Request += (_, request) =>
|
||||
{
|
||||
var uri = new Uri(request.Url);
|
||||
if (uri.Scheme is "http" or "https")
|
||||
lock (Hosts)
|
||||
Hosts.Add(uri.Host);
|
||||
if (uri.Host == "privapub.test")
|
||||
lock (ApiCalls)
|
||||
ApiCalls.Add((request.Url, request.Headers.TryGetValue("authorization", out var auth) ? auth : null));
|
||||
};
|
||||
page.Console += (_, message) =>
|
||||
{
|
||||
if (message.Type == "error")
|
||||
lock (ConsoleErrors)
|
||||
ConsoleErrors.Add(message.Text);
|
||||
};
|
||||
page.Response += (_, response) =>
|
||||
{
|
||||
if (response.Status >= 500)
|
||||
lock (ServerErrors)
|
||||
ServerErrors.Add($"{response.Status} {response.Url}");
|
||||
};
|
||||
}
|
||||
|
||||
public async Task AssertGuards()
|
||||
{
|
||||
Assert.All(Hosts.Distinct(), host => Assert.Contains(host, new[] { "decepub.test", "privapub.test" }));
|
||||
Assert.Empty(ServerErrors);
|
||||
Assert.False(await Page.Locator("#blazor-error-ui").IsVisibleAsync(), "Blazor's error bar is showing");
|
||||
// /clientapi carries the root's JWT and /api a persona's token: no credential ever crosses over
|
||||
var clientApi = ApiCalls.Where(c => c.Authorization is not null && new Uri(c.Url).AbsolutePath.StartsWith("/clientapi/"))
|
||||
.Select(c => c.Authorization).ToHashSet();
|
||||
var mastodonApi = ApiCalls.Where(c => c.Authorization is not null && new Uri(c.Url).AbsolutePath.StartsWith("/api/"))
|
||||
.Select(c => c.Authorization).ToHashSet();
|
||||
Assert.Empty(clientApi.Intersect(mastodonApi));
|
||||
Assert.All(clientApi, auth => Assert.Equal(3, auth["Bearer ".Length..].Split('.').Length));
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync() => await Context.CloseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace decePubClient.E2E.Support
|
||||
{
|
||||
// The pasture and its town as the tests see them: the sites, the accounts the town made (read from the town's own
|
||||
// state), and the town's command line for what each server holds, so a test checks the other side of the wire.
|
||||
public static class Pasture
|
||||
{
|
||||
public const string Site = "https://decepub.test";
|
||||
public const string Api = "https://privapub.test";
|
||||
|
||||
public static readonly string Root = Path.GetFullPath(Path.Combine(RepoRoot(), "..", "SocialPub", "tools", "pasture"));
|
||||
|
||||
static string RepoRoot()
|
||||
{
|
||||
var dir = AppContext.BaseDirectory;
|
||||
while (dir is not null && !File.Exists(Path.Combine(dir, "decePubClient.csproj")))
|
||||
dir = Path.GetDirectoryName(dir);
|
||||
return dir ?? throw new InvalidOperationException("decePubClient.csproj not found above the test output");
|
||||
}
|
||||
|
||||
public sealed record Persona(string UserName, string Name, string Root, string Password, string Token, string LocalId, string ActorUri);
|
||||
|
||||
// every PrivaPub persona the town made, with its root's login
|
||||
public static IReadOnlyList<Persona> Personas()
|
||||
{
|
||||
var sessions = JsonNode.Parse(File.ReadAllText(Path.Combine(Root, ".state", "town", "sessions.json")))!.AsObject();
|
||||
var personas = new List<Persona>();
|
||||
foreach (var (key, value) in sessions)
|
||||
{
|
||||
// st_* are the drivers' selftest accounts, not the town's
|
||||
if (!key.StartsWith("privapub/") || key.StartsWith("privapub/st_") || value?["account"]?["root"]?.GetValue<string>() is not { } root)
|
||||
continue;
|
||||
var account = value["account"]!;
|
||||
personas.Add(new Persona(account["username"]!.GetValue<string>(), account["name"]?.GetValue<string>(), root,
|
||||
account["password"]!.GetValue<string>(),
|
||||
value["token"]!.GetValue<string>(), value["local_id"]!.GetValue<string>(), value["actor_uri"]!.GetValue<string>()));
|
||||
}
|
||||
return personas;
|
||||
}
|
||||
|
||||
// tools/pasture/town.sh <args>, its standard output parsed as JSON
|
||||
public static async Task<JsonNode> Town(params string[] args)
|
||||
{
|
||||
var start = new ProcessStartInfo("python3") { RedirectStandardOutput = true, RedirectStandardError = true };
|
||||
start.ArgumentList.Add(Path.Combine(Root, "town", "town.py"));
|
||||
foreach (var arg in args)
|
||||
start.ArgumentList.Add(arg);
|
||||
using var process = Process.Start(start)!;
|
||||
var output = await process.StandardOutput.ReadToEndAsync();
|
||||
var error = await process.StandardError.ReadToEndAsync();
|
||||
await process.WaitForExitAsync();
|
||||
if (process.ExitCode != 0)
|
||||
throw new InvalidOperationException($"town.sh {string.Join(' ', args)} failed: {error}");
|
||||
return JsonNode.Parse(output.Trim().Split('\n').Last())!;
|
||||
}
|
||||
|
||||
// what a server holds of each object, from its database (town.sh stored)
|
||||
public static async Task<JsonObject> Stored(string platform, params string[] uris) =>
|
||||
(await Town(["stored", platform, .. uris])).AsObject();
|
||||
|
||||
public static async Task<bool> Eventually(Func<Task<bool>> condition, int seconds = 30)
|
||||
{
|
||||
var until = DateTime.UtcNow.AddSeconds(seconds);
|
||||
while (true)
|
||||
{
|
||||
if (await condition())
|
||||
return true;
|
||||
if (DateTime.UtcNow > until)
|
||||
return false;
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web);
|
||||
|
||||
// an HttpClient that reaches https://<name>.test through Caddy on 127.0.0.1:6443, as the browser does
|
||||
public static HttpClient Http() => new(new SocketsHttpHandler
|
||||
{
|
||||
ConnectCallback = async (context, token) =>
|
||||
{
|
||||
var socket = new System.Net.Sockets.Socket(System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
|
||||
await socket.ConnectAsync("127.0.0.1", 6443, token);
|
||||
return new System.Net.Sockets.NetworkStream(socket, ownsSocket: true);
|
||||
},
|
||||
SslOptions = { RemoteCertificateValidationCallback = (_, _, _, _) => true }
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace decePubClient.E2E.Support
|
||||
{
|
||||
// Each render and action test is also a cell of the town's matrix, appended to the pasture's out/client.jsonl in the
|
||||
// cell format town.sh check writes, so report.html shows decePub next to the servers.
|
||||
public static class Results
|
||||
{
|
||||
static readonly object Gate = new();
|
||||
static readonly string FilePath = Path.Combine(Pasture.Root, "out", "client.jsonl");
|
||||
|
||||
public static async Task Cell(string feature, string origin, string reference, Func<Task> test) =>
|
||||
await Cell(feature, origin, reference, test, page: null);
|
||||
|
||||
// with a page, a failure also leaves its screenshot and text in the test output's .state/ (SignIn.Evidence)
|
||||
public static async Task Cell(string feature, string origin, string reference, Func<Task> test, Microsoft.Playwright.IPage page)
|
||||
{
|
||||
var ok = false;
|
||||
string got = null;
|
||||
try
|
||||
{
|
||||
await test();
|
||||
ok = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
got = e.Message.Length > 400 ? e.Message[..400] : e.Message;
|
||||
if (page is not null)
|
||||
await SignIn.Evidence(page, $"{feature}-{origin}-{reference}".Replace('/', '_').Replace('@', '_'));
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
var line = JsonSerializer.Serialize(new
|
||||
{
|
||||
source = "client", cell = $"{feature}|{origin}|decepub|{(origin == "privapub" ? "local" : "in")}", feature, origin,
|
||||
observer = "decepub", direction = origin == "privapub" ? "local" : "in", ok, @ref = reference, got,
|
||||
t = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
|
||||
});
|
||||
lock (Gate)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(FilePath)!);
|
||||
File.AppendAllText(FilePath, line + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace decePubClient.E2E.Support
|
||||
{
|
||||
// Signs a town persona in through the real login page and persona chooser, once per persona per run; later sessions
|
||||
// start from the saved browser state (the AuthData decePub keeps in localStorage).
|
||||
public static class SignIn
|
||||
{
|
||||
static readonly string StateDir = Path.Combine(AppContext.BaseDirectory, ".state");
|
||||
|
||||
public static async Task<Session> As(Browser browser, Pasture.Persona persona, bool fresh = false)
|
||||
{
|
||||
var state = Path.Combine(StateDir, persona.UserName + ".json");
|
||||
if (!fresh && File.Exists(state))
|
||||
{
|
||||
var known = await browser.Open(state);
|
||||
await known.Page.GotoAsync(Pasture.Site + "/");
|
||||
await known.Page.Locator("article[data-uri], [data-feed-empty]").First.WaitForAsync(new() { Timeout = 30000 });
|
||||
return known;
|
||||
}
|
||||
var session = await browser.Open();
|
||||
await Through(session.Page, persona);
|
||||
Directory.CreateDirectory(StateDir);
|
||||
await session.Context.StorageStateAsync(new() { Path = state });
|
||||
return session;
|
||||
}
|
||||
|
||||
public static async Task Through(IPage page, Pasture.Persona persona)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Steps(page, persona);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await Evidence(page, "signin-" + persona.UserName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
// a screenshot and the page's text next to the test output, for a failure seen only in a headless browser
|
||||
public static async Task Evidence(IPage page, string name)
|
||||
{
|
||||
Directory.CreateDirectory(StateDir);
|
||||
await page.ScreenshotAsync(new() { Path = Path.Combine(StateDir, name + ".png"), FullPage = true });
|
||||
await File.WriteAllTextAsync(Path.Combine(StateDir, name + ".txt"), page.Url + "\n" + await page.InnerTextAsync("body"));
|
||||
}
|
||||
|
||||
static async Task Steps(IPage page, Pasture.Persona persona)
|
||||
{
|
||||
await page.GotoAsync(Pasture.Site + "/login");
|
||||
await page.Locator("input[autocomplete=username]").FillAsync(persona.Root);
|
||||
await page.Locator("input[autocomplete=current-password]").FillAsync(persona.Password);
|
||||
await page.Locator("button[type=submit]").ClickAsync();
|
||||
// a root with several personas picks one; with one it goes straight on
|
||||
var chooser = page.Locator("section button").Filter(new() { HasText = persona.UserName + "@privapub.test" });
|
||||
await chooser.Or(page.Locator("article[data-uri]")).First.WaitForAsync(new() { Timeout = 30000 });
|
||||
if (await chooser.CountAsync() > 0)
|
||||
await chooser.First.ClickAsync();
|
||||
await page.WaitForURLAsync(url => new Uri(url).AbsolutePath == "/", new() { Timeout = 30000 });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user