mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-18 05:15:06 +00:00
Expand server tests and enforce coverage
Consolidate server tests under tests/, upgrade them to .NET 10, and add CI coverage thresholds and reports. Expand API and application boundary coverage while fixing related issues in media handling, friend blocking, session crypto, presence, username validation, and local storage.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using Govor.Application.Messages;
|
||||
using Govor.Application.PrivateUserChats;
|
||||
using Govor.Domain.Models;
|
||||
using Govor.Domain.Models.Messages;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Govor.Application.Tests.Integration.Friends;
|
||||
|
||||
[TestFixture]
|
||||
public class FriendsBlockServiceTests
|
||||
{
|
||||
private TestDatabase _db = null!;
|
||||
private FriendsBlockService _service = null!;
|
||||
[SetUp] public void SetUp() { _db = new TestDatabase(); _service = new(_db.Context); }
|
||||
[TearDown] public void TearDown() => _db.Dispose();
|
||||
|
||||
[TestCase(false)]
|
||||
[TestCase(true)]
|
||||
public async Task BlockCreatesOrUpdatesRelationshipAndPreventsNewMessages(bool existing)
|
||||
{
|
||||
Guid? original = null;
|
||||
if (existing)
|
||||
{
|
||||
var friend = new Friendship { Id = Guid.NewGuid(), RequesterId = _db.Bob, AddresseeId = _db.Alice, Status = FriendshipStatus.Accepted };
|
||||
_db.Context.Friendships.Add(friend);
|
||||
await _db.Context.SaveChangesAsync();
|
||||
original = friend.Id;
|
||||
}
|
||||
await _service.BlockFriendRequestAsync(_db.Bob, _db.Alice);
|
||||
await _service.BlockFriendRequestAsync(_db.Bob, _db.Alice);
|
||||
_db.Context.ChangeTracker.Clear();
|
||||
var block = await _db.Context.Friendships.SingleAsync();
|
||||
Assert.That(block.RequesterId, Is.EqualTo(_db.Alice));
|
||||
Assert.That(block.AddresseeId, Is.EqualTo(_db.Bob));
|
||||
Assert.That(block.Status, Is.EqualTo(FriendshipStatus.Blocked));
|
||||
if (original.HasValue) Assert.That(block.Id, Is.EqualTo(original.Value));
|
||||
var sender = new MessageSendingService(_db.Context, Microsoft.Extensions.Logging.Abstractions.NullLogger<MessageSendingService>.Instance);
|
||||
var sent = await sender.SendMessageAsync(new("hello", null, _db.ChatId, RecipientType.User, _db.Bob, DateTime.UtcNow, []));
|
||||
Assert.That(sent.IsSuccess, Is.False);
|
||||
Assert.That(sent.Exception, Is.TypeOf<UnauthorizedAccessException>());
|
||||
var requests = new FriendRequestCommandService(_db.Context, Moq.Mock.Of<IUserPrivateChatsCreator>());
|
||||
Assert.That((await requests.SendAsync(_db.Bob, _db.Alice)).IsFailure, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OnlyBlockOwnerCanRemoveOrReplaceBlockAndUnblockAllowsNewRequest()
|
||||
{
|
||||
await _service.BlockFriendRequestAsync(_db.Bob, _db.Alice);
|
||||
Assert.ThrowsAsync<UnauthorizedAccessException>(() => _service.UnblockFriendRequestAsync(_db.Alice, _db.Bob));
|
||||
Assert.ThrowsAsync<UnauthorizedAccessException>(() => _service.BlockFriendRequestAsync(_db.Alice, _db.Bob));
|
||||
await _service.UnblockFriendRequestAsync(_db.Bob, _db.Alice);
|
||||
await _service.UnblockFriendRequestAsync(_db.Bob, _db.Alice);
|
||||
Assert.That((await _db.Context.Friendships.SingleAsync()).Status, Is.EqualTo(FriendshipStatus.Rejected));
|
||||
var requests = new FriendRequestCommandService(_db.Context, Moq.Mock.Of<IUserPrivateChatsCreator>());
|
||||
Assert.That((await requests.SendAsync(_db.Alice, _db.Bob)).IsSuccess, Is.True);
|
||||
Assert.That((await _db.Context.Friendships.SingleAsync()).Status, Is.EqualTo(FriendshipStatus.Pending));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UnblockingMissingOrUnblockedRelationshipDoesNotChangeFriendship()
|
||||
{
|
||||
await _service.UnblockFriendRequestAsync(_db.Bob, _db.Alice);
|
||||
_db.Context.Friendships.Add(new Friendship { Id = Guid.NewGuid(), RequesterId = _db.Alice,
|
||||
AddresseeId = _db.Bob, Status = FriendshipStatus.Accepted });
|
||||
await _db.Context.SaveChangesAsync();
|
||||
await _service.UnblockFriendRequestAsync(_db.Bob, _db.Alice);
|
||||
Assert.That((await _db.Context.Friendships.SingleAsync()).Status, Is.EqualTo(FriendshipStatus.Accepted));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidOrMissingUsersCannotBeBlocked()
|
||||
{
|
||||
Assert.ThrowsAsync<ArgumentException>(() => _service.BlockFriendRequestAsync(_db.Alice, _db.Alice));
|
||||
Assert.ThrowsAsync<ArgumentException>(() => _service.BlockFriendRequestAsync(Guid.Empty, _db.Alice));
|
||||
Assert.ThrowsAsync<ArgumentException>(() => _service.UnblockFriendRequestAsync(_db.Alice, Guid.Empty));
|
||||
Assert.ThrowsAsync<ArgumentException>(() => _service.UnblockFriendRequestAsync(_db.Alice, _db.Alice));
|
||||
Assert.ThrowsAsync<KeyNotFoundException>(() => _service.BlockFriendRequestAsync(Guid.NewGuid(), _db.Alice));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
using Govor.Application.Friends;
|
||||
using Govor.Application.PrivateUserChats;
|
||||
using Govor.Application.Exceptions.VerifyFriendship;
|
||||
using Govor.Domain.Common;
|
||||
using Govor.Domain.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.Application.Tests.Friends;
|
||||
|
||||
[TestFixture]
|
||||
public class FriendshipTests
|
||||
{
|
||||
private TestDatabase _db = null!;
|
||||
private FriendRequestCommandService _commands = null!;
|
||||
private Mock<IUserPrivateChatsCreator> _chats = null!;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_db = new TestDatabase();
|
||||
_chats = new Mock<IUserPrivateChatsCreator>();
|
||||
_commands = new FriendRequestCommandService(_db.Context, _chats.Object);
|
||||
}
|
||||
[TearDown] public void TearDown() => _db.Dispose();
|
||||
|
||||
[Test]
|
||||
public async Task RequestCanBeAcceptedOnlyByAddresseeAndCreatesChat()
|
||||
{
|
||||
var sent = await _commands.SendAsync(_db.Alice, _db.Bob);
|
||||
Assert.That(sent.Value.Status, Is.EqualTo(FriendshipStatus.Pending));
|
||||
Assert.That((await _commands.AcceptAsync(sent.Value.Id, _db.Outsider)).Error.Type, Is.EqualTo(ErrorType.Forbidden));
|
||||
Assert.That((await _commands.AcceptAsync(sent.Value.Id, _db.Bob)).Value.Status, Is.EqualTo(FriendshipStatus.Accepted));
|
||||
Assert.That((await _commands.AcceptAsync(sent.Value.Id, _db.Bob)).IsFailure, Is.True);
|
||||
_chats.Verify(c => c.CreateAsync(_db.Bob, _db.Alice), Times.Once);
|
||||
var verifier = new VerifyFriendship(_db.Context, NullLogger<VerifyFriendship>.Instance);
|
||||
Assert.That(await verifier.TryVerifyAsync(_db.Alice, _db.Bob), Is.True);
|
||||
Assert.That(await verifier.TryVerifyAsync(_db.Bob, _db.Alice), Is.True);
|
||||
Assert.DoesNotThrowAsync(() => verifier.VerifyAsync(_db.Alice, _db.Bob));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DuplicateAndReversePendingRequestsReturnConflict()
|
||||
{
|
||||
await _commands.SendAsync(_db.Alice, _db.Bob);
|
||||
Assert.That((await _commands.SendAsync(_db.Alice, _db.Bob)).Error.Type, Is.EqualTo(ErrorType.Conflict));
|
||||
Assert.That((await _commands.SendAsync(_db.Bob, _db.Alice)).Error.Type, Is.EqualTo(ErrorType.Conflict));
|
||||
Assert.That(await _db.Context.Friendships.CountAsync(), Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SelfRequestDoesNotPersist()
|
||||
{
|
||||
Assert.That((await _commands.SendAsync(_db.Alice, _db.Alice)).IsFailure, Is.True);
|
||||
Assert.That(await _db.Context.Friendships.CountAsync(), Is.Zero);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RejectedRequestCanBeResentInOtherDirection()
|
||||
{
|
||||
var sent = (await _commands.SendAsync(_db.Alice, _db.Bob)).Value;
|
||||
Assert.That((await _commands.RejectAsync(sent.Id, _db.Alice)).Error.Type, Is.EqualTo(ErrorType.Forbidden));
|
||||
Assert.That((await _commands.RejectAsync(sent.Id, _db.Bob)).Value.Status, Is.EqualTo(FriendshipStatus.Rejected));
|
||||
var resent = (await _commands.SendAsync(_db.Bob, _db.Alice)).Value;
|
||||
Assert.That(resent.Id, Is.EqualTo(sent.Id));
|
||||
Assert.That(resent.AddresseeId, Is.EqualTo(_db.Alice));
|
||||
Assert.That(resent.Status, Is.EqualTo(FriendshipStatus.Pending));
|
||||
_chats.VerifyNoOtherCalls();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task MissingRequestsReturnNotFound()
|
||||
{
|
||||
Assert.That((await _commands.AcceptAsync(Guid.NewGuid(), _db.Bob)).Error.Type, Is.EqualTo(ErrorType.NotFound));
|
||||
Assert.That((await _commands.RejectAsync(Guid.NewGuid(), _db.Bob)).Error.Type, Is.EqualTo(ErrorType.NotFound));
|
||||
}
|
||||
|
||||
[TestCase(FriendshipStatus.Accepted)]
|
||||
[TestCase(FriendshipStatus.Blocked)]
|
||||
public async Task EstablishedOrBlockedFriendshipCannotBeRequestedOrRejected(FriendshipStatus status)
|
||||
{
|
||||
var sent = (await _commands.SendAsync(_db.Alice, _db.Bob)).Value;
|
||||
sent.Status = status;
|
||||
await _db.Context.SaveChangesAsync();
|
||||
Assert.That((await _commands.SendAsync(_db.Bob, _db.Alice)).Error.Type, Is.EqualTo(ErrorType.Conflict));
|
||||
Assert.That((await _commands.RejectAsync(sent.Id, _db.Bob)).Error.Type, Is.EqualTo(ErrorType.Conflict));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task QueriesFilterStatusAndDirection()
|
||||
{
|
||||
var sent = (await _commands.SendAsync(_db.Alice, _db.Bob)).Value;
|
||||
var query = new FriendRequestQueryService(_db.Context);
|
||||
var friends = new FriendshipService(_db.Context);
|
||||
Assert.That((await query.GetIncomingAsync(_db.Bob)).Select(f => f.Id), Is.EqualTo(new[] { sent.Id }));
|
||||
Assert.That(await query.GetIncomingAsync(_db.Alice), Is.Empty);
|
||||
Assert.That((await friends.GetPotentialFriendsAsync(_db.Alice)).Select(u => u.Id), Is.EqualTo(new[] { _db.Bob }));
|
||||
Assert.That(await friends.GetFriendsAsync(_db.Alice), Is.Empty);
|
||||
await _commands.AcceptAsync(sent.Id, _db.Bob);
|
||||
Assert.That(await query.GetIncomingAsync(_db.Bob), Is.Empty);
|
||||
Assert.That(await query.GetResponsesAsync(_db.Alice), Is.Empty);
|
||||
Assert.That((await friends.GetFriendsAsync(_db.Alice)).Select(u => u.Id), Is.EqualTo(new[] { _db.Bob }));
|
||||
Assert.That((await friends.GetFriendsAsync(_db.Bob)).Select(u => u.Id), Is.EqualTo(new[] { _db.Alice }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SearchExcludesCurrentUserAndEmptyQuery()
|
||||
{
|
||||
var service = new FriendshipService(_db.Context);
|
||||
Assert.That(await service.SearchUsersAsync(" ", _db.Alice), Is.Empty);
|
||||
Assert.That((await service.SearchUsersAsync("-", _db.Alice)).Select(u => u.Id),
|
||||
Is.EquivalentTo(new[] { _db.Bob, _db.Outsider }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task VerifierRejectsMissingOrPendingFriendshipAndEmptyIds()
|
||||
{
|
||||
var verifier = new VerifyFriendship(_db.Context, NullLogger<VerifyFriendship>.Instance);
|
||||
await _commands.SendAsync(_db.Alice, _db.Bob);
|
||||
Assert.That(await verifier.TryVerifyAsync(_db.Alice, _db.Bob), Is.False);
|
||||
Assert.That(await verifier.TryVerifyAsync(Guid.Empty, _db.Bob), Is.False);
|
||||
Assert.ThrowsAsync<FriendshipException>(() => verifier.VerifyAsync(_db.Alice, _db.Bob));
|
||||
Assert.ThrowsAsync<ArgumentException>(() => verifier.VerifyAsync(Guid.Empty, _db.Bob));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user