chore: tests

This commit is contained in:
2026-09-28 09:38:31 +04:00
parent 7e1e036bf1
commit 0c7f3bce90
5 changed files with 425 additions and 1 deletions
@@ -0,0 +1,88 @@
using MemeVault.Domain.Aggregates.CollectionInvite;
using MemeVault.Domain.Enums;
using MemeVault.Domain.Exceptions;
using Xunit;
namespace MemeVault.Tests.Unit.Domain;
public sealed class CollectionInviteTests
{
private static readonly Guid CollectionId = Guid.NewGuid();
private const long CreatorId = 42L;
// ── Create ────────────────────────────────────────────────────────────────
[Theory]
[InlineData(CollectionRole.Viewer)]
[InlineData(CollectionRole.Manager)]
public void Create_WithAllowedRole_Succeeds(CollectionRole role)
{
var invite = CollectionInvite.Create(CollectionId, CreatorId, role);
Assert.Equal(CollectionId, invite.CollectionId);
Assert.Equal(CreatorId, invite.CreatedByTelegramId);
Assert.Equal(role, invite.Role);
Assert.False(invite.IsRevoked);
Assert.Null(invite.RevokedAt);
Assert.NotEqual(Guid.Empty, invite.Id);
}
[Fact]
public void Create_WithOwnerRole_ThrowsDomainException()
{
Assert.Throws<DomainException>(() =>
CollectionInvite.Create(CollectionId, CreatorId, CollectionRole.Owner));
}
[Fact]
public void Create_Token_IsNotEmpty()
{
var invite = CollectionInvite.Create(CollectionId, CreatorId, CollectionRole.Viewer);
Assert.NotEmpty(invite.Token);
}
[Fact]
public void Create_Token_ContainsNoUnsafeBase64Chars()
{
// Токен используется в URL (deep link), не должен содержать / + =
var invite = CollectionInvite.Create(CollectionId, CreatorId, CollectionRole.Viewer);
Assert.DoesNotContain("/", invite.Token);
Assert.DoesNotContain("+", invite.Token);
Assert.DoesNotContain("=", invite.Token);
}
[Fact]
public void Create_TwoInvites_HaveDifferentTokens()
{
var a = CollectionInvite.Create(CollectionId, CreatorId, CollectionRole.Viewer);
var b = CollectionInvite.Create(CollectionId, CreatorId, CollectionRole.Viewer);
Assert.NotEqual(a.Token, b.Token);
}
// ── Revoke ────────────────────────────────────────────────────────────────
[Fact]
public void Revoke_ActiveInvite_SetsIsRevokedAndRevokedAt()
{
var invite = CollectionInvite.Create(CollectionId, CreatorId, CollectionRole.Viewer);
var before = DateTimeOffset.UtcNow;
invite.Revoke();
Assert.True(invite.IsRevoked);
Assert.NotNull(invite.RevokedAt);
Assert.True(invite.RevokedAt >= before);
}
[Fact]
public void Revoke_AlreadyRevoked_ThrowsDomainException()
{
var invite = CollectionInvite.Create(CollectionId, CreatorId, CollectionRole.Viewer);
invite.Revoke();
Assert.Throws<DomainException>(() => invite.Revoke());
}
}
@@ -0,0 +1,121 @@
using MemeVault.Application.Commands.AcceptCollectionInvite;
using MemeVault.Application.Common;
using MemeVault.Domain.Aggregates.Collection;
using MemeVault.Domain.Aggregates.CollectionInvite;
using MemeVault.Domain.Enums;
using MemeVault.Domain.Repositories;
using NSubstitute;
using Xunit;
namespace MemeVault.Tests.Unit.Handlers;
public sealed class AcceptCollectionInviteCommandHandlerTests
{
private readonly ICollectionInviteRepository _invites = Substitute.For<ICollectionInviteRepository>();
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
private readonly AcceptCollectionInviteCommandHandler _handler;
private const long OwnerId = 10L;
private const long GuestId = 30L;
private const long MemberId = 20L;
public AcceptCollectionInviteCommandHandlerTests()
{
_handler = new AcceptCollectionInviteCommandHandler(_invites, _collections, _uow);
}
private static CollectionInvite MakeInvite(Collection col, CollectionRole role = CollectionRole.Viewer)
=> CollectionInvite.Create(col.Id, OwnerId, role);
// ── Невалидные токены ─────────────────────────────────────────────────────
[Fact]
public async Task Handle_TokenNotFound_ReturnsNull()
{
_invites.GetByTokenAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns((CollectionInvite?)null);
var result = await _handler.Handle(
new AcceptCollectionInviteCommand("bad-token", GuestId), default);
Assert.Null(result);
await _collections.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_RevokedInvite_ReturnsNull()
{
var col = Collection.Create(OwnerId, "Memes");
var invite = MakeInvite(col);
invite.Revoke();
_invites.GetByTokenAsync(invite.Token, Arg.Any<CancellationToken>()).Returns(invite);
var result = await _handler.Handle(
new AcceptCollectionInviteCommand(invite.Token, GuestId), default);
Assert.Null(result);
await _collections.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
// ── Идемпотентность ───────────────────────────────────────────────────────
[Fact]
public async Task Handle_AcceptorIsOwner_ReturnsCollectionIdWithoutAddingMember()
{
var col = Collection.Create(OwnerId, "Memes");
var invite = MakeInvite(col);
_invites.GetByTokenAsync(invite.Token, Arg.Any<CancellationToken>()).Returns(invite);
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
var result = await _handler.Handle(
new AcceptCollectionInviteCommand(invite.Token, OwnerId), default);
Assert.Equal(col.Id, result);
// Число участников не изменилось (только owner)
Assert.Single(col.Members);
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_AlreadyMember_ReturnsCollectionIdWithoutDuplicate()
{
var col = Collection.Create(OwnerId, "Memes");
col.AddMember(MemberId, CollectionRole.Viewer, OwnerId);
var invite = MakeInvite(col);
_invites.GetByTokenAsync(invite.Token, Arg.Any<CancellationToken>()).Returns(invite);
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
var result = await _handler.Handle(
new AcceptCollectionInviteCommand(invite.Token, MemberId), default);
Assert.Equal(col.Id, result);
Assert.Equal(2, col.Members.Count); // owner + member, без дублей
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
// ── Успешное принятие ─────────────────────────────────────────────────────
[Theory]
[InlineData(CollectionRole.Viewer)]
[InlineData(CollectionRole.Manager)]
public async Task Handle_ValidAccept_AddsMemberWithCorrectRole(CollectionRole role)
{
var col = Collection.Create(OwnerId, "Memes");
var invite = MakeInvite(col, role);
_invites.GetByTokenAsync(invite.Token, Arg.Any<CancellationToken>()).Returns(invite);
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
var result = await _handler.Handle(
new AcceptCollectionInviteCommand(invite.Token, GuestId), default);
Assert.Equal(col.Id, result);
var newMember = col.Members.Single(m => m.TelegramId == GuestId);
Assert.Equal(role, newMember.Role);
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
}
@@ -0,0 +1,72 @@
using MemeVault.Application.Commands.CreateCollectionInvite;
using MemeVault.Application.Common;
using MemeVault.Domain.Aggregates.Collection;
using MemeVault.Domain.Aggregates.CollectionInvite;
using MemeVault.Domain.Enums;
using MemeVault.Domain.Repositories;
using NSubstitute;
using Xunit;
namespace MemeVault.Tests.Unit.Handlers;
public sealed class CreateCollectionInviteCommandHandlerTests
{
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
private readonly ICollectionInviteRepository _invites = Substitute.For<ICollectionInviteRepository>();
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
private readonly CreateCollectionInviteCommandHandler _handler;
private const long OwnerId = 10L;
private const long MemberId = 20L;
public CreateCollectionInviteCommandHandlerTests()
{
_handler = new CreateCollectionInviteCommandHandler(_collections, _invites, _uow);
}
[Fact]
public async Task Handle_CollectionNotFound_ReturnsNull()
{
_collections.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns((Collection?)null);
var result = await _handler.Handle(
new CreateCollectionInviteCommand(Guid.NewGuid(), OwnerId, CollectionRole.Viewer), default);
Assert.Null(result);
await _invites.DidNotReceive().AddAsync(Arg.Any<CollectionInvite>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_RequestedByNonOwner_ReturnsNull()
{
var col = Collection.Create(OwnerId, "Memes");
col.AddMember(MemberId, CollectionRole.Manager, OwnerId);
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
var result = await _handler.Handle(
new CreateCollectionInviteCommand(col.Id, MemberId, CollectionRole.Viewer), default);
Assert.Null(result);
await _invites.DidNotReceive().AddAsync(Arg.Any<CollectionInvite>(), Arg.Any<CancellationToken>());
}
[Theory]
[InlineData(CollectionRole.Viewer)]
[InlineData(CollectionRole.Manager)]
public async Task Handle_OwnerRequest_CreatesInviteAndReturnsToken(CollectionRole role)
{
var col = Collection.Create(OwnerId, "Memes");
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
var result = await _handler.Handle(
new CreateCollectionInviteCommand(col.Id, OwnerId, role), default);
Assert.NotNull(result);
Assert.NotEmpty(result);
await _invites.Received(1).AddAsync(
Arg.Is<CollectionInvite>(i => i.CollectionId == col.Id && i.Role == role),
Arg.Any<CancellationToken>());
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
}
@@ -0,0 +1,92 @@
using MemeVault.Application.Commands.RevokeCollectionInvite;
using MemeVault.Application.Common;
using MemeVault.Domain.Aggregates.Collection;
using MemeVault.Domain.Aggregates.CollectionInvite;
using MemeVault.Domain.Enums;
using MemeVault.Domain.Repositories;
using NSubstitute;
using Xunit;
namespace MemeVault.Tests.Unit.Handlers;
public sealed class RevokeCollectionInviteCommandHandlerTests
{
private readonly ICollectionInviteRepository _invites = Substitute.For<ICollectionInviteRepository>();
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
private readonly RevokeCollectionInviteCommandHandler _handler;
private const long OwnerId = 10L;
private const long MemberId = 20L;
public RevokeCollectionInviteCommandHandlerTests()
{
_handler = new RevokeCollectionInviteCommandHandler(_invites, _collections, _uow);
}
[Fact]
public async Task Handle_InviteNotFound_ReturnsFalse()
{
_invites.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns((CollectionInvite?)null);
var result = await _handler.Handle(
new RevokeCollectionInviteCommand(Guid.NewGuid(), OwnerId), default);
Assert.False(result);
await _collections.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_CollectionNotFound_ReturnsFalse()
{
var col = Collection.Create(OwnerId, "Memes");
var invite = CollectionInvite.Create(col.Id, OwnerId, CollectionRole.Viewer);
_invites.GetByIdAsync(invite.Id, Arg.Any<CancellationToken>()).Returns(invite);
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns((Collection?)null);
var result = await _handler.Handle(
new RevokeCollectionInviteCommand(invite.Id, OwnerId), default);
Assert.False(result);
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_RequestedByNonOwner_ReturnsFalse()
{
var col = Collection.Create(OwnerId, "Memes");
col.AddMember(MemberId, CollectionRole.Manager, OwnerId);
var invite = CollectionInvite.Create(col.Id, OwnerId, CollectionRole.Viewer);
_invites.GetByIdAsync(invite.Id, Arg.Any<CancellationToken>()).Returns(invite);
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
var result = await _handler.Handle(
new RevokeCollectionInviteCommand(invite.Id, MemberId), default);
Assert.False(result);
Assert.False(invite.IsRevoked);
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ValidRevoke_RevokesInviteAndReturnsTrue()
{
var col = Collection.Create(OwnerId, "Memes");
var invite = CollectionInvite.Create(col.Id, OwnerId, CollectionRole.Viewer);
_invites.GetByIdAsync(invite.Id, Arg.Any<CancellationToken>()).Returns(invite);
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
var result = await _handler.Handle(
new RevokeCollectionInviteCommand(invite.Id, OwnerId), default);
Assert.True(result);
Assert.True(invite.IsRevoked);
Assert.NotNull(invite.RevokedAt);
await _invites.Received(1).UpdateAsync(invite, Arg.Any<CancellationToken>());
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
}