Path: redb-identity/tests/redb.Identity.Tests/FullStack/AdminResultWireTests.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/FullStack/AdminResultWireTests.cs
----
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.RegularExpressions;
using AwesomeAssertions;
using redb.Identity.Tests.Infrastructure;
using Xunit;
namespace redb.Identity.Tests.FullStack;
///
/// The answers of the remaining management routes (groups, roles, claim definitions, claim mappers, claim
/// scopes, federation providers, webhooks, impersonation) as bytes on the wire. The expected strings are what
/// these routes answered while their bodies were anonymous objects; values the store decides (ids, times) are
/// matched by pattern, every name, its order and every null are matched exactly.
///
[Collection("ProductionHttp")]
public class AdminResultWireTests
{
private const string Time = "\"[^\"]+\"";
private readonly ProductionHttpFixture _fx;
public AdminResultWireTests(ProductionHttpFixture fx) => _fx = fx;
[Fact]
public async Task A_group_and_its_members_answer_in_their_shapes()
{
var name = "wire-" + Guid.NewGuid().ToString("N")[..8];
var (_, parent) = await Ok(HttpMethod.Post, "/api/v1/identity/groups", new { name, groupType = "team", description = "d" });
var parentId = Id(parent);
Match(parent, $$"""^\{"id":{{parentId}},"name":"{{name}}","parentId":(null|\d+),"groupType":"team","description":"d","createdAt":{{Time}},"modifiedAt":(null|{{Time}})\}$""");
var (_, child) = await Ok(HttpMethod.Post, $"/api/v1/identity/groups/{parentId}/children", new { name = name + "-c" });
var childId = Id(child);
Match(child, $$"""^\{"id":{{childId}},"name":"{{name}}-c","parentId":{{parentId}},"groupType":null,"description":null,"createdAt":{{Time}},"modifiedAt":(null|{{Time}})\}$""");
var (_, read) = await Ok(HttpMethod.Get, $"/api/v1/identity/groups/{childId}");
Match(read, $$"""^\{"id":{{childId}},"name":"{{name}}-c","parentId":{{parentId}},"groupType":null,"description":null,"createdAt":{{Time}},"modifiedAt":(null|{{Time}})\}$""");
var (_, updated) = await Ok(HttpMethod.Put, $"/api/v1/identity/groups/{childId}", new { name = name + "-c2" });
updated.Should().Be($$"""{"success":true,"groupId":{{childId}}}""");
var (_, tree) = await Ok(HttpMethod.Get, $"/api/v1/identity/groups/{parentId}/tree");
Match(tree, $$"""^\{"id":{{parentId}},"name":"{{name}}","parentId":(null|\d+),"groupType":"team","description":"d","level":\d+,"children":\[\{"id":{{childId}},"name":"{{name}}-c2","parentId":{{parentId}},"groupType":null,"description":null,"level":\d+,"children":\[\]\}\]\}$""");
var (_, path) = await Ok(HttpMethod.Get, $"/api/v1/identity/groups/{childId}/path");
Match(path, $$"""^\[(\{"id":\d+,"name":"[^"]*","parentId":(null|\d+),"groupType":(null|"[^"]*"),"description":(null|"[^"]*"),"createdAt":{{Time}},"modifiedAt":(null|{{Time}})\},?)+\]$""");
var (_, children) = await Ok(HttpMethod.Get, $"/api/v1/identity/groups/{parentId}/children");
Match(children, $$"""^\[\{"id":{{childId}},"name":"{{name}}-c2","parentId":{{parentId}},"groupType":null,"description":null,"createdAt":{{Time}},"modifiedAt":(null|{{Time}})\}\]$""");
var (_, search) = await Ok(HttpMethod.Get, $"/api/v1/identity/groups/search?query={name}-c2");
Match(search, """^\{"items":\[.*\],"total":\d+,"offset":0,"count":\d+\}$""");
Match(search, $$"""\{"id":{{childId}},"name":"{{name}}-c2","parentId":{{parentId}},"groupType":null,"description":null,"createdAt":{{Time}},"modifiedAt":(null|{{Time}}),"memberCount":0\}""");
var userId = await CreateUserAsync();
var (_, added) = await Ok(HttpMethod.Post, $"/api/v1/identity/groups/{childId}/members", new { userId = long.Parse(userId), role = "member" });
Match(added, $$"""^\{"success":true,"membershipId":\d+,"groupId":{{childId}},"userId":{{userId}},"role":"member"\}$""");
var (_, changed) = await Ok(HttpMethod.Put, $"/api/v1/identity/groups/{childId}/members/{userId}", new { role = "owner" });
changed.Should().Be($$"""{"success":true,"groupId":{{childId}},"userId":{{userId}},"role":"owner"}""");
var (_, check) = await Ok(HttpMethod.Get, $"/api/v1/identity/groups/{childId}/members/{userId}/check");
check.Should().Be($$"""{"isMember":true,"groupId":{{childId}},"userId":{{userId}}}""");
var (_, removed) = await Ok(HttpMethod.Delete, $"/api/v1/identity/groups/{childId}/members/{userId}");
removed.Should().Be($$"""{"success":true,"groupId":{{childId}},"userId":{{userId}}}""");
var (_, other) = await Ok(HttpMethod.Post, "/api/v1/identity/groups", new { name = name + "-o" });
var otherId = Id(other);
var (_, moved) = await Ok(HttpMethod.Post, $"/api/v1/identity/groups/{childId}/move", new { newParentGroupId = long.Parse(otherId) });
moved.Should().Be($$"""{"success":true,"groupId":{{childId}},"newParentGroupId":{{otherId}}}""");
var (_, deleted) = await Ok(HttpMethod.Delete, $"/api/v1/identity/groups/{childId}");
deleted.Should().Be($$"""{"success":true,"groupId":{{childId}}}""");
}
[Fact]
public async Task Roles_answer_success_and_the_error_document()
{
var (_, created) = await Ok(HttpMethod.Post, "/api/v1/identity/roles", new { name = "wire_" + Guid.NewGuid().ToString("N")[..8] });
var (_, deleted) = await Ok(HttpMethod.Delete, $"/api/v1/identity/roles/{Id(created)}");
deleted.Should().Be("""{"success":true}""");
var (status, missing) = await SendAsync(HttpMethod.Get, "/api/v1/identity/roles/987654321");
status.Should().Be(HttpStatusCode.NotFound, missing);
Match(missing, """^\{"error":"not_found","error_description":"[^"]+"\}$""");
}
[Fact]
public async Task Claim_definitions_answer_success_and_the_error_document()
{
var (_, created) = await Ok(HttpMethod.Post, "/api/v1/identity/claim-definitions", new { claimName = "wire_" + Guid.NewGuid().ToString("N")[..8] });
var (_, deleted) = await Ok(HttpMethod.Delete, $"/api/v1/identity/claim-definitions/{Id(created)}");
deleted.Should().Be("""{"success":true}""");
var (status, missing) = await SendAsync(HttpMethod.Get, "/api/v1/identity/claim-definitions/987654321");
status.Should().Be(HttpStatusCode.NotFound, missing);
missing.Should().Be("""{"error":"not_found","error_description":"ClaimDefinition 987654321 not found"}""");
}
[Fact]
public async Task A_claim_mapper_delete_answers_success()
{
var (_, created) = await Ok(HttpMethod.Post, "/api/v1/identity/claim-mappers",
new { name = "wire_" + Guid.NewGuid().ToString("N")[..8], claimType = "wire_claim", sourceKind = "Constant", constantValue = "x" });
var (_, deleted) = await Ok(HttpMethod.Delete, $"/api/v1/identity/claim-mappers/{Id(created)}");
deleted.Should().Be("""{"success":true}""");
}
[Fact]
public async Task A_claim_scope_delete_answers_what_it_cascaded()
{
var name = "wire_" + Guid.NewGuid().ToString("N")[..8];
var (_, created) = await Ok(HttpMethod.Post, "/api/v1/identity/claim-scopes", new { name });
var (_, deleted) = await Ok(HttpMethod.Delete, $"/api/v1/identity/claim-scopes/{Id(created)}");
deleted.Should().Be("""{"success":true,"cascadedAssignments":0,"cascadedMappers":0}""");
}
[Fact]
public async Task A_federation_provider_delete_answers_success()
{
var providerId = "wire-" + Guid.NewGuid().ToString("N")[..8];
var (_, created) = await Ok(HttpMethod.Post, "/api/v1/identity/federation-providers",
new { providerId, kind = "oidc", displayName = "Wire", authority = "http://example.invalid/", clientId = "x" });
var (_, deleted) = await Ok(HttpMethod.Delete, $"/api/v1/identity/federation-providers/{Id(created)}");
deleted.Should().Be("""{"success":true}""");
}
[Fact]
public async Task Webhooks_answer_success_the_deliveries_and_the_error_document()
{
var (_, created) = await Ok(HttpMethod.Post, "/api/v1/identity/webhooks",
new { url = "http://127.0.0.1:9/wire", displayName = "wire", eventTypeFilter = "UserCreated", enabled = false });
var id = Id(created);
var (_, updated) = await Ok(HttpMethod.Put, $"/api/v1/identity/webhooks/{id}", new { displayName = "wire2" });
updated.Should().Be("""{"success":true}""");
var (_, deliveries) = await Ok(HttpMethod.Get, $"/api/v1/identity/webhooks/deliveries?destination={id}");
Match(deliveries, """^\{"items":\[\],"offset":0,"count":\d+\}$""");
var (status, missing) = await SendAsync(HttpMethod.Get, "/api/v1/identity/webhooks/987654321");
status.Should().Be(HttpStatusCode.NotFound, missing);
missing.Should().Be("""{"error":"not_found","error_description":"Webhook 987654321 not found"}""");
var (_, deleted) = await Ok(HttpMethod.Delete, $"/api/v1/identity/webhooks/{id}");
deleted.Should().Be("""{"success":true}""");
}
[Fact]
public async Task Impersonation_answers_its_target_the_stop_and_the_error_document()
{
var userId = await CreateUserAsync();
var (startStatus, started) = await SendAsync(HttpMethod.Post, $"/api/v1/identity/admin/impersonate/start/{userId}", new { reason = "wire" });
startStatus.Should().Be(HttpStatusCode.OK, started);
Match(started, $$"""^\{"targetUserId":{{userId}},"targetLogin":"wire-[0-9a-f]+"\}$""");
var (_, stopped) = await Ok(HttpMethod.Post, $"/api/v1/identity/admin/impersonate/stop/{userId}", new { });
stopped.Should().Be("""{"stopped":true}""");
var (status, missing) = await SendAsync(HttpMethod.Post, "/api/v1/identity/admin/impersonate/start/987654321", new { reason = "wire" });
status.Should().Be(HttpStatusCode.NotFound, missing);
missing.Should().Be("""{"error":"not_found","error_description":"User 987654321 not found"}""");
}
private static void Match(string body, string pattern) => Regex.IsMatch(body, pattern).Should().BeTrue(body);
[Fact]
public async Task Sessions_answer_the_page_the_preview_and_the_counts()
{
var (_, page) = await Ok(HttpMethod.Get, "/api/v1/identity/sessions?offset=0&count=25");
Match(page, """^\{"items":\[.*\],"total":\d+,"offset":0,"count":25\}$""");
var userId = await CreateUserAsync();
var (_, preview) = await Ok(HttpMethod.Delete, $"/api/v1/identity/sessions/all?userId={userId}&dryRun=true");
preview.Should().Be("""{"success":true,"dryRun":true,"wouldRevoke":0,"sampleSessionIds":[]}""");
var (_, revokedAll) = await Ok(HttpMethod.Delete, $"/api/v1/identity/sessions/all?userId={userId}");
revokedAll.Should().Be("""{"success":true,"revoked":0}""");
var (_, revoked) = await Ok(HttpMethod.Delete, "/api/v1/identity/sessions?sessionId=987654321");
revoked.Should().Be("""{"success":true,"revoked":0}""");
var (_, loggedOut) = await Ok(HttpMethod.Post, $"/api/v1/identity/sessions/logout?userId={userId}");
loggedOut.Should().Be("""{"success":true,"sessionsRevoked":0}""");
}
private async Task CreateUserAsync()
{
var login = "wire-" + Guid.NewGuid().ToString("N")[..10];
var (_, body) = await Ok(HttpMethod.Post, "/api/v1/identity/users", new { login, password = "Str0ng!Passw0rd", displayName = login });
return Id(body);
}
private async Task<(HttpStatusCode Status, string Body)> Ok(HttpMethod method, string path, object? content = null)
{
var (status, body) = await SendAsync(method, path, content);
status.Should().Be(HttpStatusCode.OK, $"{method} {path}: {body}");
return (status, body);
}
private async Task<(HttpStatusCode Status, string Body)> SendAsync(HttpMethod method, string path, object? content = null)
{
using var request = new HttpRequestMessage(method, path);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ManagementToken);
if (content is not null) request.Content = JsonContent.Create(content);
using var response = await _fx.Http.SendAsync(request);
return (response.StatusCode, await response.Content.ReadAsStringAsync());
}
private static string Id(string json)
{
using var document = JsonDocument.Parse(json);
var id = document.RootElement.GetProperty("id");
return id.ValueKind == JsonValueKind.Number ? id.GetInt64().ToString() : id.GetString()!;
}
}