mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-16 20:56:03 +00:00
29cc51ee0c
Implements message reactions and reaction packs (domain models, services, admin HTTP API, media processing via ImageSharp and limits) and adds reliable push delivery (ChatPushNotification, ChatPushDispatcher, ChatPushWorker, improved ChatNotificationService). Enhances message reading (ReadChat API, unread counts, concurrency-safe receipts) and tightens session security (access-token session validation in JWT, hub filter, SessionAuthorization). Improves SignalR behavior (session groups, reaction/channel events, hub/error mappings). Adds EF migrations, many controller fixes (Forbid handling, password API), tests (Govor.Server.Tests), docs, CI and tooling updates (Dockerfile → .NET 10, GH Actions matrix for sqlite/postgres, Govor.Server.slnx, global.json), and small infra changes (AutoMapper bump, NullPushProvider, storage/token handling).
123 lines
5.8 KiB
C#
123 lines
5.8 KiB
C#
using Govor.API.Hubs.Infrastructure;
|
|
using Govor.Application.Infrastructure.Common;
|
|
using Govor.Application.Messages;
|
|
using Govor.Application.Messages.Parameters;
|
|
using Govor.Application.Profiles;
|
|
using Govor.Application.PushNotifications;
|
|
using Govor.Contracts.Responses.SignalR;
|
|
using Govor.Domain.Common;
|
|
using Govor.Domain.Models;
|
|
using Govor.Domain.Models.Users;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Moq;
|
|
|
|
namespace Govor.Server.Tests;
|
|
|
|
[TestFixture]
|
|
public class PushReliabilityTests
|
|
{
|
|
private TestDatabase _db = null!;
|
|
private Mock<INowDateTimeProvider> _clock = null!;
|
|
private PushTokenService _tokens = null!;
|
|
private DateTime _now;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_db = new TestDatabase();
|
|
_now = DateTime.UtcNow.AddMinutes(1);
|
|
_clock = new Mock<INowDateTimeProvider>();
|
|
_clock.SetupGet(c => c.Now).Returns(() => _now);
|
|
_tokens = new PushTokenService(_db.Context, _clock.Object, NullLogger<PushTokenService>.Instance);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown() => _db.Dispose();
|
|
|
|
[Test]
|
|
public async Task SavedMessageHasDurablePushAndFailureRetriesAfterBackoff()
|
|
{
|
|
var sender = new MessageSendingService(_db.Context, NullLogger<MessageSendingService>.Instance);
|
|
var sent = await sender.SendMessageAsync(new SendMessage("hello", null, _db.ChatId,
|
|
Govor.Domain.Models.Messages.RecipientType.User, _db.Alice, _now, []));
|
|
Assert.That(sent.IsSuccess, Is.True);
|
|
Assert.That(await _db.Context.ChatPushNotifications.CountAsync(), Is.EqualTo(1));
|
|
var notifier = new Mock<IChatNotificationService>();
|
|
notifier.SetupSequence(n => n.DeliverPushAsync(It.IsAny<UserMessageResponse>())).ReturnsAsync(false).ReturnsAsync(true);
|
|
var dispatcher = new ChatPushDispatcher(_db.Context, notifier.Object, _clock.Object);
|
|
await dispatcher.DispatchAsync();
|
|
var pending = await _db.Context.ChatPushNotifications.AsNoTracking().SingleAsync();
|
|
Assert.That(pending.Attempts, Is.EqualTo(1));
|
|
Assert.That(pending.NextAttemptAt, Is.GreaterThan(_now));
|
|
await dispatcher.DispatchAsync();
|
|
notifier.Verify(n => n.DeliverPushAsync(It.IsAny<UserMessageResponse>()), Times.Once);
|
|
_now = pending.NextAttemptAt.AddSeconds(1);
|
|
// A new scope simulates worker restart, while the queued row remains durable.
|
|
using var restarted = _db.CreateContext();
|
|
await new ChatPushDispatcher(restarted, notifier.Object, _clock.Object).DispatchAsync();
|
|
notifier.Verify(n => n.DeliverPushAsync(It.IsAny<UserMessageResponse>()), Times.Exactly(2));
|
|
Assert.That(await _db.Context.ChatPushNotifications.CountAsync(), Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReassignedDeviceTokenCannotNotifyThePreviousAccount()
|
|
{
|
|
var aliceSession = AddSession(_db.Alice);
|
|
var bobSession = AddSession(_db.Bob);
|
|
await _tokens.AddOrUpdateTokenAsync(_db.Alice, aliceSession, "device-token", "android");
|
|
await _tokens.AddOrUpdateTokenAsync(_db.Bob, bobSession, "other-token", "android");
|
|
var result = await _tokens.AddOrUpdateTokenAsync(_db.Bob, bobSession, "device-token", "ios");
|
|
Assert.That(result.IsSuccess, Is.True);
|
|
Assert.That((await _tokens.GetStringsActiveTokensAsync(_db.Alice)).Value, Is.Empty);
|
|
Assert.That((await _tokens.GetStringsActiveTokensAsync(_db.Bob)).Value, Is.EqualTo(new[] { "device-token" }));
|
|
Assert.That(await _db.Context.UserPushTokens.CountAsync(), Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public async Task InactiveTokenCanBeReactivatedButForeignSessionCannotRegisterIt()
|
|
{
|
|
var session = AddSession(_db.Bob);
|
|
await _tokens.AddOrUpdateTokenAsync(_db.Bob, session, "token", "android");
|
|
await _tokens.DeactivateTokenBySessionAsync(session);
|
|
_db.Context.ChangeTracker.Clear();
|
|
Assert.That((await _tokens.GetStringsActiveTokensAsync(_db.Bob)).Value, Is.Empty);
|
|
await _tokens.AddOrUpdateTokenAsync(_db.Bob, session, "token", "android");
|
|
Assert.That((await _tokens.GetStringsActiveTokensAsync(_db.Bob)).Value, Has.Count.EqualTo(1));
|
|
var foreign = await _tokens.AddOrUpdateTokenAsync(_db.Alice, session, "stolen", "android");
|
|
Assert.That(foreign.Error.Type, Is.EqualTo(ErrorType.Forbidden));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ExpiredOrRevokedSessionTokensAreExcludedWithoutWaitingForCleanup()
|
|
{
|
|
var session = AddSession(_db.Bob);
|
|
await _tokens.AddOrUpdateTokenAsync(_db.Bob, session, "token", "android");
|
|
await _db.Context.UserSessions.Where(s => s.Id == session)
|
|
.ExecuteUpdateAsync(s => s.SetProperty(row => row.IsRevoked, true));
|
|
Assert.That((await _tokens.GetStringsActiveTokensAsync(_db.Bob)).Value, Is.Empty);
|
|
Assert.That((await _tokens.GetUsersStringsActiveTokensAsync(new[] { _db.Bob })).Value, Is.Empty);
|
|
Assert.That((await _tokens.GetActiveTokenBySessionAsync(session)).Value, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ForeignAttachmentCannotBePublishedAsAnAvatar()
|
|
{
|
|
var media = new MediaFile { Id = Guid.NewGuid(), UploaderId = _db.Alice,
|
|
OwnerType = MediaOwnerType.Message, Url = "test", MineType = "image/png" };
|
|
_db.Context.MediaFiles.Add(media);
|
|
_db.Context.SaveChanges();
|
|
var profiles = new ProfileService(_db.Context, NullLogger<ProfileService>.Instance);
|
|
var forbidden = await profiles.SetNewIcon(_db.Bob, media.Id);
|
|
Assert.That(forbidden.Error.Type, Is.EqualTo(ErrorType.Forbidden));
|
|
}
|
|
|
|
private Guid AddSession(Guid user)
|
|
{
|
|
var session = new UserSession { UserId = user, ExpiresAt = _now.AddDays(1), RefreshTokenHash = Guid.NewGuid().ToString() };
|
|
_db.Context.UserSessions.Add(session);
|
|
_db.Context.SaveChanges();
|
|
return session.Id;
|
|
}
|
|
}
|