mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-18 05:15:06 +00:00
Expose chat read positions and E2EE errors
Add optional chat activity and read-position details to the unread-count endpoint. Return stable E2EE rejection codes for stale timestamps and untrusted signing devices, with tests for both behaviors.
This commit is contained in:
@@ -2,6 +2,7 @@ using AutoMapper;
|
||||
using Govor.API.Common.SignalR.Helpers;
|
||||
using Govor.API.Hubs;
|
||||
using Govor.API.Hubs.Infrastructure;
|
||||
using Govor.Application.E2ee;
|
||||
using Govor.Application.Exceptions.VerifyFriendship;
|
||||
using Govor.Application.Infrastructure.Common;
|
||||
using Govor.Application.Messages;
|
||||
@@ -106,6 +107,21 @@ public class ChatsHubTests
|
||||
Assert.That((await _hub.Send(new(){EncryptedContent="cipher"})).Status,Is.EqualTo(HubResultStatus.BadRequest));
|
||||
}
|
||||
|
||||
[TestCase(E2eeProtocol.SigningDeviceRejectedError, HubResultStatus.Forbidden)]
|
||||
[TestCase(E2eeProtocol.TimestampOutOfRangeError, HubResultStatus.BadRequest)]
|
||||
[TestCase(E2eeProtocol.DevicesChangedError, HubResultStatus.BadRequest)]
|
||||
public async Task SendReturnsSafeE2eeRejectionCodeWithoutNotifying(string code, HubResultStatus status)
|
||||
{
|
||||
Exception error = status == HubResultStatus.Forbidden
|
||||
? new UnauthorizedAccessException(code) : new ArgumentException(code);
|
||||
_send.Setup(x => x.SendMessageAsync(It.IsAny<SendMessage>()))
|
||||
.ReturnsAsync(new SendMessageResult(false, error, null!));
|
||||
var response = await _hub.Send(new() { RecipientId = _chat, EncryptedContent = "cipher" });
|
||||
Assert.That(response.Status, Is.EqualTo(status));
|
||||
Assert.That(response.ErrorMessage, Is.EqualTo(code));
|
||||
_notifier.VerifyNoOtherCalls();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReadMapsCurrentReadersViewAndNotifies()
|
||||
{
|
||||
|
||||
@@ -10,6 +10,34 @@ namespace Govor.Application.Tests.Integration.Messages;
|
||||
[TestFixture]
|
||||
public class ReadBoundaryTests
|
||||
{
|
||||
[Test]
|
||||
public async Task ActivityAndReadingPositionIncludeOutgoingWithoutSkippingUnreadIncoming()
|
||||
{
|
||||
using var db = new TestDatabase();
|
||||
var reader = new MessageReadingService(db.Context, NullLogger<MessageReadingService>.Instance,
|
||||
Mock.Of<INowDateTimeProvider>(c => c.Now == DateTime.UtcNow.Date));
|
||||
var first = db.AddMessage(sender: db.Bob);
|
||||
var incoming = db.AddMessage(sender: db.Alice);
|
||||
var last = db.AddMessage(sender: db.Bob);
|
||||
first.SentAt = DateTime.UtcNow.AddMinutes(-3);
|
||||
incoming.SentAt = first.SentAt.AddMinutes(1);
|
||||
last.SentAt = first.SentAt.AddMinutes(2);
|
||||
await db.Context.SaveChangesAsync();
|
||||
|
||||
var before = (await reader.GetChatActivityAsync(db.Bob, db.ChatId, RecipientType.User, true)).Value;
|
||||
Assert.That(before.LastMessageAt, Is.EqualTo(last.SentAt));
|
||||
Assert.That(before.UnreadCount, Is.EqualTo(1));
|
||||
Assert.That(before.LastReadMessageId, Is.EqualTo(first.Id));
|
||||
Assert.That(before.FirstUnreadMessageId, Is.EqualTo(incoming.Id));
|
||||
Assert.That((await reader.GetChatActivityAsync(db.Outsider, db.ChatId, RecipientType.User, true)).IsFailure, Is.True);
|
||||
|
||||
await reader.ReadChatAsync(db.Bob, db.ChatId, RecipientType.User, upToMessageId: last.Id);
|
||||
var after = (await reader.GetChatActivityAsync(db.Bob, db.ChatId, RecipientType.User, true)).Value;
|
||||
Assert.That(after.UnreadCount, Is.Zero);
|
||||
Assert.That(after.LastReadMessageId, Is.EqualTo(last.Id));
|
||||
Assert.That(after.FirstUnreadMessageId, Is.Null);
|
||||
Assert.That(await db.Context.MessageViews.Select(v => v.MessageId).SingleAsync(), Is.EqualTo(incoming.Id));
|
||||
}
|
||||
[Test]
|
||||
public async Task EmptyChatReturnsZeroAndMissingBoundaryDoesNotCreateReceipts()
|
||||
{
|
||||
|
||||
@@ -90,13 +90,17 @@ public class E2eeTests
|
||||
[TestCase("malformed")]
|
||||
[TestCase("routing")]
|
||||
[TestCase("stale")]
|
||||
[TestCase("future")]
|
||||
public async Task InvalidEnvelopeContextIsReturnedAsValidationFailure(string reason)
|
||||
{
|
||||
var directory = await _e2ee.GetConversationKeysAsync(_db.Alice, _db.ChatId, RecipientType.User);
|
||||
var content = reason == "malformed" ? E2eeProtocol.Prefix + "invalid" : E2eeProtocol.Encrypt(new("secret", []),
|
||||
_alice, _a, directory, Guid.NewGuid(), createdAt: DateTimeOffset.UtcNow.AddMinutes(reason == "stale" ? -6 : 0).ToUnixTimeMilliseconds());
|
||||
Assert.ThrowsAsync<ArgumentException>(() => _e2ee.ValidateAsync(content, reason == "routing" ? _db.Bob : _db.Alice,
|
||||
_alice, _a, directory, Guid.NewGuid(), createdAt: DateTimeOffset.UtcNow.AddMinutes(reason switch
|
||||
{ "stale" => -6, "future" => 6, _ => 0 }).ToUnixTimeMilliseconds());
|
||||
var failure = Assert.ThrowsAsync<ArgumentException>(() => _e2ee.ValidateAsync(content, reason == "routing" ? _db.Bob : _db.Alice,
|
||||
_db.ChatId, RecipientType.User, null, []));
|
||||
if (reason is "stale" or "future")
|
||||
Assert.That(failure!.Message, Is.EqualTo(E2eeProtocol.TimestampOutOfRangeError));
|
||||
}
|
||||
|
||||
[TestCase(false)]
|
||||
@@ -206,8 +210,9 @@ public class E2eeTests
|
||||
var ciphertext = await Encrypt();
|
||||
await _db.Context.UserSessions.Where(s => s.Id == _a.SessionId)
|
||||
.ExecuteUpdateAsync(u => u.SetProperty(s => s.IsRevoked, true));
|
||||
Assert.ThrowsAsync<UnauthorizedAccessException>(() => _e2ee.ValidateAsync(ciphertext,
|
||||
var failure = Assert.ThrowsAsync<UnauthorizedAccessException>(() => _e2ee.ValidateAsync(ciphertext,
|
||||
_db.Alice, _db.ChatId, RecipientType.User, null, []));
|
||||
Assert.That(failure!.Message, Is.EqualTo(E2eeProtocol.SigningDeviceRejectedError));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
Reference in New Issue
Block a user