Path: redb-identity/tests/redb.Identity.Tests/Groups/GroupIntegrationTests.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/Groups/GroupIntegrationTests.cs
----
using AwesomeAssertions;
using redb.Identity.Contracts.Groups;
using redb.Identity.Contracts.Routes;
using redb.Identity.Tests.Infrastructure;
using redb.Route.Core;
using Xunit;
using Xunit.Abstractions;
namespace redb.Identity.Tests.Groups;
///
/// Integration tests for group management via direct-vm routes.
/// Uses with full OpenIddict pipeline.
///
[Collection("ProductionBootstrap")]
public class GroupIntegrationTests
{
private readonly ProductionBootstrapFixture _fx;
private readonly ITestOutputHelper _output;
public GroupIntegrationTests(ProductionBootstrapFixture fx, ITestOutputHelper output)
{
_fx = fx;
_output = output;
}
private Task Send(string operation, Dictionary body)
{
return _fx.RequestWithHeaders(
IdentityEndpoints.ManageGroups,
body,
new Dictionary { ["operation"] = operation });
}
// ── Group CRUD ──────────────────────────────────────────────
[Fact]
public async Task Create_And_Read_Group()
{
var createEx = await Send("create", new Dictionary
{
["name"] = "IntGroup1",
["groupType"] = "team",
["description"] = "Integration test group"
});
var created = createEx.Out?.Body;
created.Should().NotBeNull();
var id = GetId(created!);
id.Should().BeGreaterThan(0);
var readEx = await Send("read", new Dictionary { ["groupId"] = id });
var read = readEx.Out?.Body;
read.Should().NotBeNull();
_output.WriteLine($"Created and read group {id}");
}
[Fact]
public async Task List_ReturnsRootGroups()
{
await Send("create", new Dictionary
{
["name"] = "IntListRoot",
["groupType"] = "organization"
});
var listEx = await Send("list", new Dictionary());
var list = listEx.Out?.Body as IEnumerable;
list.Should().NotBeNullOrEmpty();
_output.WriteLine($"Root groups count: {list!.Count()}");
}
[Fact]
public async Task Update_Group()
{
var createEx = await Send("create", new Dictionary
{
["name"] = "IntUpdateBefore",
["groupType"] = "team"
});
var id = GetId(createEx.Out!.Body!);
var updateEx = await Send("update", new Dictionary
{
["groupId"] = id,
["name"] = "IntUpdateAfter",
["description"] = "Updated"
});
updateEx.Out?.Body.Should().NotBeNull();
var readEx = await Send("read", new Dictionary { ["groupId"] = id });
var body = readEx.Out?.Body;
body.Should().BeOfType().Subject.Name.Should().Be("IntUpdateAfter");
_output.WriteLine($"Updated group {id}");
}
[Fact]
public async Task Delete_Group()
{
var createEx = await Send("create", new Dictionary
{
["name"] = "IntDeleteTarget",
["groupType"] = "team"
});
var id = GetId(createEx.Out!.Body!);
await Send("delete", new Dictionary { ["groupId"] = id });
var readEx = await Send("read", new Dictionary { ["groupId"] = id });
// Should be not_found or null
var body = readEx.Out?.Body;
if (body is IDictionary dict)
dict.ContainsKey("error").Should().BeTrue();
}
// ── Tree ────────────────────────────────────────────────────
[Fact]
public async Task CreateChild_And_GetTree()
{
var parentEx = await Send("create", new Dictionary
{
["name"] = "IntTreeRoot",
["groupType"] = "organization"
});
var parentId = GetId(parentEx.Out!.Body!);
await Send("create-child", new Dictionary
{
["parentGroupId"] = parentId,
["name"] = "IntTreeChild",
["groupType"] = "team"
});
var treeEx = await Send("tree", new Dictionary { ["groupId"] = parentId });
treeEx.Out?.Body.Should().NotBeNull();
_output.WriteLine($"Tree loaded for group {parentId}");
}
[Fact]
public async Task Children_ReturnsDirectChildren()
{
var parentEx = await Send("create", new Dictionary
{
["name"] = "IntChildrenRoot",
["groupType"] = "organization"
});
var parentId = GetId(parentEx.Out!.Body!);
await Send("create-child", new Dictionary
{
["parentGroupId"] = parentId,
["name"] = "IntChild1",
["groupType"] = "team"
});
var childrenEx = await Send("children", new Dictionary { ["groupId"] = parentId });
var children = childrenEx.Out?.Body as IEnumerable;
children.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task Path_ReturnsAncestors()
{
var rootEx = await Send("create", new Dictionary
{
["name"] = "IntPathRoot",
["groupType"] = "organization"
});
var rootId = GetId(rootEx.Out!.Body!);
var childEx = await Send("create-child", new Dictionary
{
["parentGroupId"] = rootId,
["name"] = "IntPathChild",
["groupType"] = "team"
});
var childId = GetId(childEx.Out!.Body!);
var pathEx = await Send("path", new Dictionary { ["groupId"] = childId });
var path = pathEx.Out?.Body as IEnumerable;
path.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task Move_Group()
{
// Unique-per-run names so re-runs against a shared DB don't collide with
// residue from a previous run (the create endpoint returns an error
// response rather than a group when a same-named group already exists, and
// GetId() then fails on the type).
var runTag = Guid.NewGuid().ToString("N")[..8];
var org1Ex = await Send("create", new Dictionary
{
["name"] = $"IntMoveOrg1-{runTag}",
["groupType"] = "organization"
});
var org1Id = GetId(org1Ex.Out!.Body!);
var org2Ex = await Send("create", new Dictionary
{
["name"] = $"IntMoveOrg2-{runTag}",
["groupType"] = "organization"
});
var org2Id = GetId(org2Ex.Out!.Body!);
var teamEx = await Send("create-child", new Dictionary
{
["parentGroupId"] = org1Id,
["name"] = $"IntMoveTeam-{runTag}",
["groupType"] = "team"
});
var teamId = GetId(teamEx.Out!.Body!);
await Send("move", new Dictionary
{
["groupId"] = teamId,
["newParentGroupId"] = org2Id
});
var children2 = await Send("children", new Dictionary { ["groupId"] = org2Id });
var list = children2.Out?.Body as IEnumerable;
list.Should().NotBeNullOrEmpty("team should have moved to org2");
}
// ── Membership ──────────────────────────────────────────────
[Fact]
public async Task AddMember_And_ListMembers()
{
var groupEx = await Send("create", new Dictionary
{
["name"] = "IntMemberGroup",
["groupType"] = "team"
});
var groupId = GetId(groupEx.Out!.Body!);
await Send("add-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = _fx.TestUserId,
["role"] = "admin"
});
var membersEx = await Send("list-members", new Dictionary { ["groupId"] = groupId });
var members = membersEx.Out?.Body as IEnumerable;
members.Should().NotBeNullOrEmpty();
_output.WriteLine($"Group {groupId} has {members!.Count()} member(s)");
}
[Fact]
public async Task UpdateMember_ChangesRole()
{
var groupEx = await Send("create", new Dictionary
{
["name"] = "IntUpdateMemberGroup",
["groupType"] = "team"
});
var groupId = GetId(groupEx.Out!.Body!);
var userId = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
await Send("add-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId,
["role"] = "member"
});
await Send("update-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId,
["role"] = "admin"
});
var membersEx = await Send("list-members", new Dictionary { ["groupId"] = groupId });
var members = membersEx.Out?.Body as IEnumerable;
members.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task RemoveMember_DeletesMembership()
{
var groupEx = await Send("create", new Dictionary
{
["name"] = "IntRemoveMemberGroup",
["groupType"] = "team"
});
var groupId = GetId(groupEx.Out!.Body!);
var userId = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
await Send("add-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId,
["role"] = "member"
});
await Send("remove-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId
});
var membersEx = await Send("list-members", new Dictionary { ["groupId"] = groupId });
var members = membersEx.Out?.Body as IEnumerable;
// After removing the only member, list should be empty or not contain the user
var memberList = members?.ToList() ?? [];
var hasUser = memberList.OfType>()
.Any(d => d.TryGetValue("UserId", out var uid) && uid?.ToString() == userId.ToString());
hasUser.Should().BeFalse("removed member should not appear in list");
}
[Fact]
public async Task UserGroups_ReturnsUserMemberships()
{
var groupEx = await Send("create", new Dictionary
{
["name"] = "IntUserGroupsGroup",
["groupType"] = "team"
});
var groupId = GetId(groupEx.Out!.Body!);
var userId = _fx.TestUserId;
await Send("add-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId,
["role"] = "member"
});
var userGroupsEx = await Send("user-groups", new Dictionary { ["userId"] = userId });
var groups = userGroupsEx.Out?.Body as IEnumerable;
groups.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task IsMember_ReturnsCorrectResult()
{
var groupEx = await Send("create", new Dictionary
{
["name"] = "IntIsMemberGroup",
["groupType"] = "team"
});
var groupId = GetId(groupEx.Out!.Body!);
var userId = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
// Not a member yet
var checkEx1 = await Send("is-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId
});
var result1 = checkEx1.Out?.Body;
result1.Should().BeOfType().Subject.IsMember.Should().BeFalse();
// Add member
await Send("add-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId,
["role"] = "member"
});
var checkEx2 = await Send("is-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = userId
});
var result2 = checkEx2.Out?.Body;
result2.Should().BeOfType().Subject.IsMember.Should().BeTrue();
}
// ── Events ──────────────────────────────────────────────────
[Fact]
public async Task Create_SetsEventProperties()
{
var ex = await Send("create", new Dictionary
{
["name"] = "IntEventGroup",
["groupType"] = "team"
});
ex.Properties.Should().ContainKey("identity-event-type");
ex.Properties["identity-event-type"].Should().Be("GroupCreated");
ex.Properties.Should().ContainKey("identity-event-data");
}
[Fact]
public async Task AddMember_SetsEventProperties()
{
var groupEx = await Send("create", new Dictionary
{
["name"] = "IntEventMemberGroup",
["groupType"] = "team"
});
var groupId = GetId(groupEx.Out!.Body!);
var addEx = await Send("add-member", new Dictionary
{
["groupId"] = groupId,
["userId"] = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
["role"] = "member"
});
addEx.Properties.Should().ContainKey("identity-event-type");
addEx.Properties["identity-event-type"].Should().Be("MemberAdded");
}
// ── Helpers ─────────────────────────────────────────────────
private static long GetId(object body) => body.Should().BeOfType().Subject.Id;
}