Add primitive groups and required channel

Adds server-side group and channel management, private invitations, member moderation, avatar handling, and required-channel configuration. This change also introduces SignalR group profile/member notifications, updates registration to atomically join required channels, and adds EF migration and test coverage for group membership, permissions, and invitation expiry rules.
This commit is contained in:
Artemy
2026-10-04 10:35:07 +07:00
parent fed8f518ee
commit 2e9d7e625e
48 changed files with 3593 additions and 167 deletions
+288 -5
View File
@@ -2,8 +2,10 @@ using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Net.WebSockets;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using Govor.API.Hubs.Infrastructure;
using Govor.API.Hubs;
using Govor.Application.Storage;
@@ -52,6 +54,9 @@ public class ReadApiTests
_notifier.Setup(n => n.NotifyChatReadAsync(It.IsAny<ChatReadResponse>())).Returns(Task.CompletedTask);
_notifier.Setup(n => n.NotifyMessageReactionsChangedAsync(It.IsAny<MessageReactionsChangedResponse>())).Returns(Task.CompletedTask);
_notifier.Setup(n => n.NotifyChannelReactionPolicyChangedAsync(It.IsAny<ChannelReactionPolicyResponse>())).Returns(Task.CompletedTask);
_notifier.Setup(n => n.NotifyGroupProfileChangedAsync(It.IsAny<Guid>())).Returns(Task.CompletedTask);
_notifier.Setup(n => n.NotifyGroupMemberChangedAsync(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.CompletedTask);
_notifier.Setup(n => n.NotifyUserJoinedGroupsAsync(It.IsAny<Guid>())).Returns(Task.CompletedTask);
_factory = new TestFactory(_db, _notifier.Object);
_client = _factory.CreateClient();
}
@@ -200,18 +205,291 @@ public class ReadApiTests
Assert.That((await hub.React(Guid.NewGuid(), new SetReactionRequest { ReactionId = reaction })).Status, Is.EqualTo(HubResultStatus.NotFound));
}
private void Authorize(string tokenType = "access", string role = "User")
[Test]
public async Task PrivateGroupHttpInvitationPreviewJoinAndRolesRespectPermissions()
{
Authorize();
var created = await _client.PostAsJsonAsync("/api/groups", new { name = "PrivateGroup", description = "test", isPrivate = true });
Assert.That(created.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var group = (await created.Content.ReadFromJsonAsync<GroupResponse>())!;
Assert.That(group.MyRole, Is.EqualTo(Govor.Domain.Models.GroupRole.Owner));
Assert.That(group.MemberCount, Is.EqualTo(1));
var invitation = await _client.PostAsJsonAsync($"/api/groups/{group.Id}/invitations", new { maxParticipants = 1 });
var link = (await invitation.Content.ReadFromJsonAsync<GroupInvitationResponse>())!;
Authorize(userId: _db.Alice);
Assert.That((await _client.GetAsync($"/api/groups/{group.Id}")).StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
Assert.That((await _client.GetFromJsonAsync<List<GroupResponse>>("/api/groups/search?q=PrivateGroup")), Is.Empty);
Assert.That((await _client.GetAsync(link.PreviewPath)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That(await _db.Context.GroupMemberships.AnyAsync(m => m.GroupId == group.Id && m.UserId == _db.Alice), Is.False);
Assert.That((await _client.PostAsync(link.JoinPath, null)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That((await _client.PutAsJsonAsync($"/api/groups/{group.Id}", new { name = "Hijacked" })).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Assert.That((await _client.PutAsync($"/api/groups/{group.Id}/members/{_db.Alice}/administrator", null)).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Authorize();
Assert.That((await _client.PutAsync($"/api/groups/{group.Id}/members/{_db.Alice}/administrator", null)).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
Authorize(userId: _db.Alice);
Assert.That((await _client.PutAsync($"/api/groups/{group.Id}/members/{_db.Bob}/ban", null)).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Assert.That((await _client.DeleteAsync($"/api/groups/{group.Id}/members/me")).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
Assert.That((await _client.GetAsync($"/api/groups/{group.Id}/messages")).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
}
[Test]
public async Task ChannelOwnerControlsReactionsAndModerationHttpNotifiesDeletion()
{
Authorize();
var created = await _client.PostAsJsonAsync("/api/groups", new { name = "Channel", isPrivate = false, isChannel = true });
var group = (await created.Content.ReadFromJsonAsync<GroupResponse>())!;
var id = group.Id;
var policy = await _client.PutAsJsonAsync($"/api/groups/{id}/reactions", new
{ mode = 2, reactionIds = new[] { DefaultReactionPack.Items()[0].Id } });
Assert.That(policy.StatusCode, Is.EqualTo(HttpStatusCode.OK));
_notifier.Verify(n => n.NotifyChannelReactionPolicyChangedAsync(It.Is<ChannelReactionPolicyResponse>(p => p.GroupId == id)), Times.Once);
Authorize(userId: _db.Alice);
await _client.PostAsync($"/api/groups/{id}/join", null);
Assert.That((await _client.PutAsJsonAsync($"/api/groups/{id}/reactions", new { mode = 1 })).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Authorize();
var own = _db.AddMessage(sender: _db.Bob, chat: id, type: Govor.Domain.Models.Messages.RecipientType.Group);
Assert.That((await _client.DeleteAsync($"/api/groups/{Guid.NewGuid()}/messages/{own.Id}")).StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
Assert.That((await _client.DeleteAsync($"/api/groups/{id}/messages/{own.Id}")).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
_notifier.Verify(n => n.NotifyMessageRemovedAsync(It.Is<MessageRemovedResponse>(m => m.MessageId == own.Id && m.RecipientId == id)), Times.Once);
}
[Test]
public async Task RequiredChannelHttpRequiresServerAdminAndRegistrationAddsMembership()
{
Authorize();
var created = await _client.PostAsJsonAsync("/api/groups", new { name = "Required", isPrivate = true, isChannel = true });
var id = (await created.Content.ReadFromJsonAsync<GroupResponse>())!.Id;
Assert.That((await _client.PutAsJsonAsync("/api/admin/required-channel", new { channelId = id, allowLeave = false })).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Authorize(role: "Admin");
Assert.That((await _client.PutAsJsonAsync("/api/admin/required-channel", new { channelId = id })).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
ReactionTests.MakeAdmin(_db.Context, _db.Bob);
Assert.That((await _client.PutAsJsonAsync("/api/admin/required-channel", new { channelId = id, allowLeave = false })).StatusCode, Is.EqualTo(HttpStatusCode.OK));
var invite = _db.Context.Invitations.Single(i => i.Code == "test");
invite.Code = Guid.NewGuid().ToString("N");
await _db.Context.SaveChangesAsync();
_client.DefaultRequestHeaders.Authorization = null;
var registered = await _client.PostAsJsonAsync("/api/auth/register", new
{ name = "НовыйУчастник123", password = "Secure-test123!", inviteLink = invite.Code, deviceInfo = "test" });
Assert.That(registered.StatusCode, Is.EqualTo(HttpStatusCode.OK), await registered.Content.ReadAsStringAsync());
var user = await _db.Context.Users.AsNoTracking().SingleAsync(u => u.Username == "НовыйУчастник123");
_notifier.Verify(n => n.NotifyUserJoinedGroupsAsync(user.Id), Times.Once);
Assert.That(await _db.Context.GroupMemberships.AnyAsync(m => m.UserId == user.Id && m.GroupId == id && !m.IsBanned), Is.True);
Authorize(userId: user.Id);
Assert.That((await _client.DeleteAsync($"/api/groups/{id}/members/me")).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
}
[Test]
public async Task GroupAvatarIsOwnerControlledAndPrivateFilesAreProtected()
{
Authorize();
var created = await _client.PostAsJsonAsync("/api/groups", new { name = "AvatarGroup", isPrivate = false });
var group = (await created.Content.ReadFromJsonAsync<GroupResponse>())!;
using var image = new Image<Rgba32>(256, 256);
using var bytes = new MemoryStream();
await image.SaveAsPngAsync(bytes);
using var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(bytes.ToArray()), "file", "avatar.png");
Assert.That((await _client.PostAsync($"/api/groups/{group.Id}/avatar", form)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(group.Id), Times.Once);
var updated = (await _client.GetFromJsonAsync<GroupResponse>($"/api/groups/{group.Id}"))!;
Assert.That(updated.ImageId, Is.Not.EqualTo(Guid.Empty));
Authorize(userId: _db.Alice);
Assert.That((await _client.GetAsync(updated.ImageUrl)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That((await _client.DeleteAsync($"/api/groups/{group.Id}/avatar")).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(group.Id), Times.Once);
Authorize();
await _client.PutAsJsonAsync($"/api/groups/{group.Id}", new { name = "AvatarGroup", isPrivate = true });
var invitation = await _client.PostAsJsonAsync($"/api/groups/{group.Id}/invitations", new { });
var link = (await invitation.Content.ReadFromJsonAsync<GroupInvitationResponse>())!;
Authorize(userId: _db.Alice);
Assert.That((await _client.GetAsync(updated.ImageUrl)).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
await _client.PostAsync(link.JoinPath, null);
Assert.That((await _client.GetAsync(updated.ImageUrl)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
Authorize();
await _client.PutAsync($"/api/groups/{group.Id}/members/{_db.Alice}/ban", null);
Authorize(userId: _db.Alice);
Assert.That((await _client.GetAsync(updated.ImageUrl)).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Authorize();
Assert.That((await _client.DeleteAsync($"/api/groups/{group.Id}/avatar")).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(group.Id), Times.Exactly(3));
}
[Test]
public async Task GroupCommandsNotifyAfterSuccessAndDeniedCommandsDoNotNotify()
{
Authorize();
var created = await _client.PostAsJsonAsync("/api/groups", new { name = "Live", isPrivate = false });
var id = (await created.Content.ReadFromJsonAsync<GroupResponse>())!.Id;
_notifier.Verify(n => n.NotifyGroupMemberChangedAsync(id, _db.Bob), Times.Once);
Authorize(userId: _db.Alice);
Assert.That((await _client.PostAsync($"/api/groups/{id}/join", null)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
_notifier.Verify(n => n.NotifyGroupMemberChangedAsync(id, _db.Alice), Times.Once);
_notifier.Invocations.Clear();
Assert.That((await _client.PutAsJsonAsync($"/api/groups/{id}", new { name = "Forbidden", isPrivate = false })).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Assert.That((await _client.PutAsync($"/api/groups/{id}/members/{_db.Bob}/administrator", null)).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Assert.That((await _client.PutAsync($"/api/groups/{id}/members/{_db.Bob}/ban", null)).StatusCode, Is.EqualTo(HttpStatusCode.Forbidden));
Assert.That(_notifier.Invocations, Is.Empty);
Authorize();
Assert.That((await _client.PutAsJsonAsync($"/api/groups/{id}", new { name = "Renamed", isPrivate = true })).StatusCode, Is.EqualTo(HttpStatusCode.OK));
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(id), Times.Once);
Assert.That((await _client.PutAsync($"/api/groups/{id}/members/{_db.Alice}/administrator", null)).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
Assert.That((await _client.DeleteAsync($"/api/groups/{id}/members/{_db.Alice}/administrator")).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
Assert.That((await _client.PutAsync($"/api/groups/{id}/members/{_db.Alice}/ban", null)).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
Assert.That((await _client.DeleteAsync($"/api/groups/{id}/members/{_db.Alice}/ban")).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
_notifier.Verify(n => n.NotifyGroupMemberChangedAsync(id, _db.Alice), Times.Exactly(4));
_notifier.Invocations.Clear();
Assert.That((await _client.PostAsync($"/api/groups/{id}/ownership/{_db.Alice}", null)).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(id), Times.Once);
_notifier.Verify(n => n.NotifyGroupMemberChangedAsync(id, _db.Bob), Times.Once);
_notifier.Verify(n => n.NotifyGroupMemberChangedAsync(id, _db.Alice), Times.Once);
Assert.That((await _client.DeleteAsync($"/api/groups/{id}/members/me")).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
_notifier.Verify(n => n.NotifyGroupMemberChangedAsync(id, _db.Bob), Times.Exactly(2));
}
[Test]
public async Task InvitationPreviewDoesNotNotifyAndSuccessfulJoinDoes()
{
Authorize();
var created = await _client.PostAsJsonAsync("/api/groups", new { name = "Invite", isPrivate = true });
var id = (await created.Content.ReadFromJsonAsync<GroupResponse>())!.Id;
var invitation = await _client.PostAsJsonAsync($"/api/groups/{id}/invitations", new { });
var link = (await invitation.Content.ReadFromJsonAsync<GroupInvitationResponse>())!;
_notifier.Invocations.Clear();
Authorize(userId: _db.Alice);
Assert.That((await _client.GetAsync(link.PreviewPath)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That((await _client.PostAsync("/api/group-invites/invalid/join", null)).StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
Assert.That(_notifier.Invocations, Is.Empty);
Assert.That((await _client.PostAsync(link.JoinPath, null)).StatusCode, Is.EqualTo(HttpStatusCode.OK));
_notifier.Verify(n => n.NotifyGroupMemberChangedAsync(id, _db.Alice), Times.Once);
}
[Test]
public async Task RequiredChannelChangeNotifiesOldAndNewChannelButFailedChangeDoesNot()
{
ReactionTests.MakeAdmin(_db.Context, _db.Bob);
Authorize(role: "Admin");
var first = await _client.PostAsJsonAsync("/api/groups", new { name = "First", isChannel = true });
var second = await _client.PostAsJsonAsync("/api/groups", new { name = "Second", isChannel = true });
var firstId = (await first.Content.ReadFromJsonAsync<GroupResponse>())!.Id;
var secondId = (await second.Content.ReadFromJsonAsync<GroupResponse>())!.Id;
_notifier.Invocations.Clear();
Assert.That((await _client.PutAsJsonAsync("/api/admin/required-channel", new { channelId = firstId, allowLeave = false })).StatusCode, Is.EqualTo(HttpStatusCode.OK));
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(firstId), Times.Once);
_notifier.Invocations.Clear();
Assert.That((await _client.PutAsJsonAsync("/api/admin/required-channel", new { channelId = secondId, allowLeave = true })).StatusCode, Is.EqualTo(HttpStatusCode.OK));
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(firstId), Times.Once);
_notifier.Verify(n => n.NotifyGroupProfileChangedAsync(secondId), Times.Once);
_notifier.Invocations.Clear();
Assert.That((await _client.PutAsJsonAsync("/api/admin/required-channel", new { channelId = Guid.NewGuid() })).StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
Assert.That(_notifier.Invocations, Is.Empty);
}
private void Authorize(string tokenType = "access", string role = "User", Guid? userId = null)
{
var actorId = userId ?? _db.Bob;
var sessionId = _session;
if (actorId != _db.Bob)
{
var session = new UserSession { UserId = actorId, RefreshTokenHash = Guid.NewGuid().ToString("N"), ExpiresAt = DateTime.UtcNow.AddDays(1) };
_db.Context.UserSessions.Add(session);
_db.Context.SaveChanges();
sessionId = session.Id;
}
var token = new JwtSecurityToken(claims: new[]
{
new Claim("userId", _db.Bob.ToString()), new Claim("sid", _session.ToString()),
new Claim("userId", actorId.ToString()), new Claim("sid", sessionId.ToString()),
new Claim("tokenType", tokenType), new Claim(ClaimTypes.Role, role)
}, expires: DateTime.UtcNow.AddMinutes(10),
signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Secret)), SecurityAlgorithms.HmacSha256));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", new JwtSecurityTokenHandler().WriteToken(token));
}
private sealed class TestFactory(TestDatabase db, IChatNotificationService notifier) : WebApplicationFactory<Program>
[Test]
public async Task GroupChangesArriveOverRealSignalRConnectionsForBothParticipants()
{
_client.Dispose();
_factory.Dispose();
_factory = new TestFactory(_db, null);
_client = _factory.CreateClient();
Authorize();
var created = await _client.PostAsJsonAsync("/api/groups", new { name = "Realtime", isPrivate = false });
var id = (await created.Content.ReadFromJsonAsync<GroupResponse>())!.Id;
Authorize(userId: _db.Alice);
await _client.PostAsync($"/api/groups/{id}/join", null);
using var alice = await ConnectHubAsync();
Authorize();
using var bob = await ConnectHubAsync();
Assert.That((await _client.PutAsJsonAsync($"/api/groups/{id}", new { name = "Live profile", isPrivate = true })).StatusCode, Is.EqualTo(HttpStatusCode.OK));
foreach (var connection in new[] { alice, bob })
{
var frame = await connection.ReadAsync();
Assert.That(frame.GetProperty("target").GetString(), Is.EqualTo("GroupProfileChanged"));
Assert.That(frame.GetProperty("arguments")[0].GetProperty("groupId").GetGuid(), Is.EqualTo(id));
}
Assert.That((await _client.PutAsync($"/api/groups/{id}/members/{_db.Alice}/administrator", null)).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
foreach (var connection in new[] { alice, bob })
{
var frame = await connection.ReadAsync();
Assert.That(frame.GetProperty("target").GetString(), Is.EqualTo("GroupMemberChanged"));
var change = frame.GetProperty("arguments")[0];
Assert.That(change.GetProperty("userId").GetGuid(), Is.EqualTo(_db.Alice));
Assert.That(change.GetProperty("status").GetInt32(), Is.Zero);
Assert.That(change.GetProperty("role").GetInt32(), Is.EqualTo(1));
}
Assert.That((await _client.PutAsync($"/api/groups/{id}/members/{_db.Alice}/ban", null)).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
foreach (var connection in new[] { alice, bob })
{
var change = (await connection.ReadAsync()).GetProperty("arguments")[0];
Assert.That(change.GetProperty("status").GetInt32(), Is.EqualTo(1));
Assert.That(change.GetProperty("role").ValueKind, Is.EqualTo(JsonValueKind.Null));
}
Authorize(userId: _db.Alice);
Assert.That((await _client.GetAsync($"/api/groups/{id}")).StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
}
private async Task<TestHubConnection> ConnectHubAsync()
{
var client = _factory.Server.CreateWebSocketClient();
var authorization = _client.DefaultRequestHeaders.Authorization;
client.ConfigureRequest = request => request.Headers.Authorization = authorization!.ToString();
var connection = new TestHubConnection(await client.ConnectAsync(new Uri("ws://localhost/hubs/chats"), CancellationToken.None));
await connection.SendAsync("{\"protocol\":\"json\",\"version\":1}");
Assert.That((await connection.ReadAsync()).EnumerateObject().Count(), Is.Zero);
// A completion proves OnConnectedAsync has finished joining the session group.
await connection.SendAsync("{\"type\":1,\"invocationId\":\"ready\",\"target\":\"Unknown\",\"arguments\":[]}");
Assert.That((await connection.ReadAsync()).GetProperty("type").GetInt32(), Is.EqualTo(3));
return connection;
}
private sealed class TestHubConnection(WebSocket socket) : IDisposable
{
private string _pending = "";
public Task SendAsync(string json) => socket.SendAsync(Encoding.UTF8.GetBytes(json + '\u001e'), WebSocketMessageType.Text, true, CancellationToken.None);
public async Task<JsonElement> ReadAsync()
{
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var buffer = new byte[4096];
while (true)
{
var separator = _pending.IndexOf('\u001e');
if (separator >= 0)
{
using var document = JsonDocument.Parse(_pending[..separator]);
_pending = _pending[(separator + 1)..];
if (document.RootElement.TryGetProperty("type", out var type) && type.GetInt32() == 6) continue;
return document.RootElement.Clone();
}
var result = await socket.ReceiveAsync(buffer, timeout.Token);
Assert.That(result.MessageType, Is.EqualTo(WebSocketMessageType.Text));
_pending += Encoding.UTF8.GetString(buffer, 0, result.Count);
}
}
public void Dispose() => socket.Dispose();
}
private sealed class TestFactory(TestDatabase db, IChatNotificationService? notifier) : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
@@ -221,13 +499,18 @@ public class ReadApiTests
builder.ConfigureServices(services =>
{
services.AddDataProtection().UseEphemeralDataProtectionProvider();
services.RemoveAll<IDataProtectionProvider>();
services.AddSingleton<IDataProtectionProvider>(new EphemeralDataProtectionProvider());
var worker = services.Single(d => d.ServiceType == typeof(IHostedService) && d.ImplementationType == typeof(ChatPushWorker));
services.Remove(worker);
services.RemoveAll<DbContextOptions<GovorDbContext>>();
services.RemoveAll<IDbContextOptionsConfiguration<GovorDbContext>>();
services.AddDbContext<GovorDbContext>(db.ConfigureOptions);
services.RemoveAll<IChatNotificationService>();
services.AddSingleton(notifier);
if (notifier is not null)
{
services.RemoveAll<IChatNotificationService>();
services.AddSingleton(notifier);
}
services.RemoveAll<IStorageService>();
services.AddSingleton<IStorageService>(new TestReactionStorage());
});