test: share one operator and trust root across the trust tests; make the setup test cleanup best-effort [CI SKIP]

This commit is contained in:
justme committed 2026-10-08 15:30:12 +08:00
1 parent f22fdf8b68
commit 4a1e48db20
4 files changed
+91 -87

No files matched your search

@@ -55,42 +55,12 @@ public sealed class OwnerEndToEndTests : IDisposable
public void Dispose() => _options.RequireOwner = _before;
private static string Base64UrlOf(string statement) => Fleet.Core.Base64Url.Encode(SHA256.HashData(Encoding.UTF8.GetBytes(statement)));
/// <summary>建立信任根:一个有 passkey 的操作员给操作员集合签名。</summary>
private async Task<(HttpClient Session, SoftwarePasskey Passkey)> EstablishTrustAsync()
{
(HttpClient session, SoftwarePasskey passkey, _) = await _backend.CreateStrongSessionAsync("owner");
JsonNode state = JsonNode.Parse(await session.GetStringAsync("/api/admin/trust"))!;
if (!state["initialized"]!.GetValue<bool>())
{
string statement = state["proposal"]!["statement"]!.GetValue<string>();
JsonObject assertion = passkey.Assert(Base64UrlOf(statement));
var body = new JsonObject
{
["statement"] = statement,
["credential_id"] = assertion["credential_id"]!.DeepClone(),
["authenticator_data"] = assertion["authenticator_data"]!.DeepClone(),
["client_data_json"] = assertion["client_data_json"]!.DeepClone(),
["signature"] = assertion["signature"]!.DeepClone(),
};
using HttpResponseMessage committed = await session.PostAsync("/api/admin/trust", BackendFixture.Json(body));
Assert.Equal(HttpStatusCode.Created, committed.StatusCode);
}
return (session, passkey);
}
private async Task<JsonNode> OperatorsAsync(HttpClient session) =>
JsonNode.Parse(await session.GetStringAsync("/api/admin/trust"))!["operators"]!;
[Fact]
public async Task ANodeCannotBeAddedWithoutAnOwner()
{
(HttpClient session, SoftwarePasskey passkey) = await EstablishTrustAsync();
using HttpClient sessionScope = session;
using SoftwarePasskey passkeyScope = passkey;
string known = (await OperatorsAsync(session))[0]!.GetValue<string>();
Operator op = await SharedOperator.GetAsync(_backend); // 共享的操作员:不要释放它
HttpClient session = op.Session;
string known = op.Passkey.Id;
using HttpResponseMessage none = await session.PostAsync("/api/admin/join-tokens", BackendFixture.Json(new JsonObject { ["role"] = "agent", ["controllers"] = new JsonArray() }));
Assert.Equal(HttpStatusCode.BadRequest, none.StatusCode);