mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-16 20:56:03 +00:00
31d704712c
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.
72 lines
4.0 KiB
C#
72 lines
4.0 KiB
C#
using Govor.Application.Messages;
|
|
using Govor.Application.Infrastructure.Common;
|
|
using Govor.Domain.Models.Messages;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Moq;
|
|
|
|
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()
|
|
{
|
|
using var db = new TestDatabase();
|
|
var reader = new MessageReadingService(db.Context, NullLogger<MessageReadingService>.Instance,
|
|
Mock.Of<INowDateTimeProvider>(c => c.Now == DateTime.UtcNow.Date));
|
|
var empty = await reader.ReadChatAsync(db.Bob, db.ChatId, RecipientType.User);
|
|
Assert.That(empty.IsSuccess, Is.True);
|
|
Assert.That(empty.Value.ReadCount, Is.Zero);
|
|
Assert.That(empty.Value.UnreadCount, Is.Zero);
|
|
Assert.That((await reader.ReadChatAsync(db.Bob, db.ChatId, RecipientType.User, upToMessageId: Guid.NewGuid())).Error.Code,
|
|
Is.EqualTo("Message.Read.BoundaryNotFound"));
|
|
Assert.That((await reader.ReadMessageAsync(db.Bob, Guid.NewGuid())).Error.Code, Is.EqualTo("Message.NotFound"));
|
|
Assert.That((await reader.GetUnreadCountAsync(db.Outsider, db.ChatId, RecipientType.User)).IsFailure, Is.True);
|
|
Assert.That(await db.Context.MessageViews.CountAsync(), Is.Zero);
|
|
}
|
|
|
|
[Test]
|
|
public async Task InvalidSelectionsAndOwnMessagesCannotBeMarkedRead()
|
|
{
|
|
using var db = new TestDatabase();
|
|
var reader = new MessageReadingService(db.Context, NullLogger<MessageReadingService>.Instance, Mock.Of<INowDateTimeProvider>());
|
|
var own = db.AddMessage(sender: db.Bob);
|
|
Assert.That((await reader.ReadChatAsync(db.Bob, db.ChatId, RecipientType.User, new[] { own.Id })).Error.Code, Is.EqualTo("Message.Read.OwnMessage"));
|
|
foreach (var selection in new[] { Array.Empty<Guid>(), new[] { Guid.Empty }, Enumerable.Repeat(own.Id, 101).ToArray() })
|
|
Assert.That((await reader.ReadChatAsync(db.Bob, db.ChatId, RecipientType.User, selection)).IsFailure, Is.True);
|
|
Assert.That((await reader.ReadChatAsync(db.Bob, db.ChatId, RecipientType.User, new[] { own.Id }, own.Id)).IsFailure, Is.True);
|
|
Assert.That(await db.Context.MessageViews.CountAsync(), Is.Zero);
|
|
}
|
|
}
|
|
|