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:
Artemy
2026-10-24 15:30:32 +07:00
parent 33550d4fc9
commit 2d99c57a3b
161 changed files with 5515 additions and 8419 deletions
@@ -0,0 +1,85 @@
using Govor.Application.Groups;
using Govor.Application.Infrastructure.Common;
using Govor.Application.Reactions;
using Govor.Application.Tests.Support;
using Govor.Domain.Common;
using Govor.Domain.Models;
using Govor.Domain.Models.Reactions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
namespace Govor.Application.Tests.Integration.Groups;
[TestFixture]
public class GroupAvatarBoundaryTests
{
private TestDatabase _db = null!;
private Guid _group;
private Mock<IReactionMediaProcessor> _processor = null!;
private Mock<IStorageService> _storage = null!;
private readonly ProcessedReactionMedia _image = new(new byte[] { 1 }, ReactionKind.Image, "image/png", ".png", 10, 10, 0);
private GroupAvatarService Service(GovorDbContext? context = null) => new(context ?? _db.Context,
Mock.Of<INowDateTimeProvider>(c => c.Now == DateTime.UtcNow.Date), _processor.Object, _storage.Object, NullLogger<GroupAvatarService>.Instance);
[SetUp]
public async Task SetUp()
{
_db = new TestDatabase();
_group = Guid.NewGuid();
_db.Context.ChatGroups.Add(new ChatGroup { Id = _group, Name = "group", Description = "", OwnerUserId = _db.Alice });
_db.Context.GroupMemberships.Add(new GroupMembership { Id = Guid.NewGuid(), GroupId = _group, UserId = _db.Alice });
await _db.Context.SaveChangesAsync();
_processor = new Mock<IReactionMediaProcessor>(MockBehavior.Strict);
_storage = new Mock<IStorageService>(MockBehavior.Strict);
}
[TearDown] public void TearDown() => _db.Dispose();
[Test]
public async Task NonOwnerCannotProcessSaveOrRemoveAvatar()
{
Assert.That((await Service().UploadAsync(_db.Bob, _group, new byte[] { 1 })).Error.Type, Is.EqualTo(ErrorType.Forbidden));
Assert.That((await Service().RemoveAsync(_db.Bob, _group)).Error.Type, Is.EqualTo(ErrorType.Forbidden));
_processor.VerifyNoOtherCalls();
_storage.VerifyNoOtherCalls();
}
[Test]
public async Task InvalidAndAnimatedImagesAreRejectedBeforeStorage()
{
var error = Error.Validation("Media.Invalid", "invalid");
_processor.SetupSequence(p => p.ProcessAsync(It.IsAny<byte[]>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Result.Failure<ProcessedReactionMedia>(error)).ReturnsAsync(_image with { Kind = ReactionKind.Gif });
Assert.That((await Service().UploadAsync(_db.Alice, _group, new byte[] { 1 })).Error, Is.SameAs(error));
Assert.That((await Service().UploadAsync(_db.Alice, _group, new byte[] { 1 })).Error.Code, Is.EqualTo("Group.Avatar.Invalid"));
_storage.VerifyNoOtherCalls();
}
[Test]
public async Task LossOfOwnershipDuringImageProcessingPreventsUpload()
{
_processor.Setup(p => p.ProcessAsync(It.IsAny<byte[]>(), It.IsAny<CancellationToken>())).Returns(async () =>
{
await _db.Context.ChatGroups.Where(g => g.Id == _group).ExecuteUpdateAsync(s => s.SetProperty(g => g.OwnerUserId, _db.Bob));
return Result.Success(_image);
});
Assert.That((await Service().UploadAsync(_db.Alice, _group, new byte[] { 1 })).Error.Type, Is.EqualTo(ErrorType.Forbidden));
_storage.VerifyNoOtherCalls();
}
[TestCase(false)]
[TestCase(true)]
public async Task FailedMetadataWriteRollsBackAndCleansStoredImage(bool cleanupFails)
{
_processor.Setup(p => p.ProcessAsync(It.IsAny<byte[]>(), It.IsAny<CancellationToken>())).ReturnsAsync(_image);
_storage.Setup(s => s.SaveAsync(It.IsAny<byte[]>(), It.IsAny<string>())).ReturnsAsync("failed.png");
if (cleanupFails) _storage.Setup(s => s.RemoveAsync("failed.png")).ThrowsAsync(new IOException("cleanup failure"));
else _storage.Setup(s => s.RemoveAsync("failed.png")).Returns(Task.CompletedTask);
using var failed = _db.CreateContext(new FailDatabaseCommand(sql => sql.Contains("INSERT INTO \"MediaFiles\"", StringComparison.Ordinal)));
Assert.ThrowsAsync<DbUpdateException>(() => Service(failed).UploadAsync(_db.Alice, _group, new byte[] { 1 }));
_storage.Verify(s => s.RemoveAsync("failed.png"), Times.Once);
Assert.That(await _db.Context.MediaFiles.CountAsync(), Is.Zero);
Assert.That((await _db.Context.ChatGroups.AsNoTracking().SingleAsync()).ImageId, Is.EqualTo(Guid.Empty));
Assert.That(failed.ChangeTracker.Entries(), Is.Empty);
}
}
@@ -0,0 +1,78 @@
using Govor.Application.Groups;
using Govor.Application.Infrastructure.Common;
using Govor.Domain.Common;
using Microsoft.EntityFrameworkCore;
using Moq;
namespace Govor.Application.Tests.Integration.Groups;
[TestFixture]
public class GroupManagementBoundaryTests
{
private TestDatabase _db = null!;
private GroupManagementService _service = null!;
private Guid _group;
[SetUp]
public async Task SetUp()
{
_db = new TestDatabase();
_service = new(_db.Context, Mock.Of<INowDateTimeProvider>(c => c.Now == DateTime.UtcNow.Date));
_group = (await _service.CreateAsync(_db.Alice, "group", "", false, false)).Value.Group.Id;
await _service.JoinPublicAsync(_db.Bob, _group);
}
[TearDown] public void TearDown() => _db.Dispose();
[TestCase("name")]
[TestCase("description")]
[TestCase("null-description")]
public async Task InvalidGroupProfilesCannotCreateOrUpdate(string kind)
{
var name = kind == "name" ? new string('x', 101) : "group";
var description = kind == "description" ? new string('x', 501) : kind == "null-description" ? null : "";
Assert.That((await _service.CreateAsync(_db.Alice, name, description!, false, false)).IsFailure, Is.True);
Assert.That((await _service.UpdateAsync(_db.Alice, _group, name, description!, false)).IsFailure, Is.True);
Assert.That(await _db.Context.ChatGroups.CountAsync(), Is.EqualTo(1));
}
[Test]
public async Task UnknownActorCannotCreateOrJoinGroups()
{
Assert.That((await _service.CreateAsync(Guid.NewGuid(), "name", "", false, false)).Error.Type, Is.EqualTo(ErrorType.Forbidden));
Assert.That((await _service.JoinPublicAsync(Guid.NewGuid(), _group)).Error.Type, Is.EqualTo(ErrorType.Forbidden));
Assert.That((await _service.UpdateAsync(_db.Alice, Guid.NewGuid(), "name", "", false)).Error.Type, Is.EqualTo(ErrorType.NotFound));
}
[Test]
public async Task MembershipAndAdministrationGuardsProtectOwnerAndOutsiders()
{
Assert.That((await _service.MembersAsync(_db.Outsider, _group, false, 0, 20)).IsFailure, Is.True);
Assert.That((await _service.MembersAsync(_db.Bob, _group, true, 0, 20)).IsFailure, Is.True);
Assert.That((await _service.LeaveAsync(_db.Outsider, _group)).IsFailure, Is.True);
Assert.That((await _service.LeaveAsync(_db.Alice, _group)).IsFailure, Is.True);
Assert.That((await _service.SetAdministratorAsync(_db.Alice, _group, _db.Alice, true)).IsFailure, Is.True);
Assert.That((await _service.SetAdministratorAsync(_db.Alice, _group, _db.Outsider, true)).IsFailure, Is.True);
Assert.That((await _service.TransferOwnershipAsync(_db.Alice, _group, _db.Outsider)).IsFailure, Is.True);
Assert.That((await _service.TransferOwnershipAsync(_db.Alice, _group, _db.Alice)).IsFailure, Is.True);
Assert.That((await _service.SetBanAsync(_db.Alice, _group, _db.Outsider, true)).Error.Type, Is.EqualTo(ErrorType.NotFound));
}
[TestCase(0, 1, "")]
[TestCase(366, 1, "")]
[TestCase(1, -1, "")]
[TestCase(1, 1000001, "")]
[TestCase(1, 1, null)]
public async Task InvalidInvitationLimitsAreRejected(int days, int limit, string? description) =>
Assert.That((await _service.CreateInvitationAsync(_db.Alice, _group, days, limit, description!)).Error.Type, Is.EqualTo(ErrorType.Validation));
[Test]
public async Task MissingAndUnauthorizedInvitationsCannotBeViewedOrRevoked()
{
Assert.That((await _service.PreviewInvitationAsync(_db.Bob, "unknown")).IsFailure, Is.True);
Assert.That((await _service.JoinByInvitationAsync(_db.Bob, "unknown")).IsFailure, Is.True);
Assert.That((await _service.InvitationsAsync(_db.Bob, _group)).IsFailure, Is.True);
Assert.That((await _service.RevokeInvitationAsync(_db.Bob, _group, Guid.NewGuid())).IsFailure, Is.True);
Assert.That((await _service.RevokeInvitationAsync(_db.Alice, _group, Guid.NewGuid())).Error.Type, Is.EqualTo(ErrorType.NotFound));
Assert.That((await _service.SetRequiredChannelAsync(_db.Alice, Guid.Empty, true)).IsFailure, Is.True);
}
}