chore: tests
This commit is contained in:
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MemeVault.Migrator", "src/M
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MemeVault.Bot", "src/MemeVault.Bot/MemeVault.Bot.csproj", "{E3A2F1B8-5C4D-4E6A-9F0B-1D2E3A4B5C6D}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MemeVault.Tests.Unit", "src/MemeVault.Tests.Unit/MemeVault.Tests.Unit.csproj", "{F1A2B3C4-D5E6-7890-ABCD-EF1234567890}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -45,6 +47,10 @@ Global
|
||||
{E3A2F1B8-5C4D-4E6A-9F0B-1D2E3A4B5C6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E3A2F1B8-5C4D-4E6A-9F0B-1D2E3A4B5C6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E3A2F1B8-5C4D-4E6A-9F0B-1D2E3A4B5C6D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F1A2B3C4-D5E6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F1A2B3C4-D5E6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F1A2B3C4-D5E6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F1A2B3C4-D5E6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -4,6 +4,7 @@ using MemeVault.Application.Commands.CreateCollection;
|
||||
using MemeVault.Application.Commands.DeleteCollection;
|
||||
using MemeVault.Application.Commands.ShareCollection;
|
||||
using MemeVault.Application.Commands.UpdateCollection;
|
||||
using MemeVault.Application.Queries.GetCollectionItems;
|
||||
using MemeVault.Application.Queries.GetUserCollections;
|
||||
using MemeVault.Domain.Enums;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -59,6 +60,18 @@ public sealed class CollectionsController(IMediator mediator, CurrentUser curren
|
||||
return ok ? NoContent() : NotFound();
|
||||
}
|
||||
|
||||
// GET /api/collections/{id}/items
|
||||
[HttpGet("{id:guid}/items")]
|
||||
[ProducesResponseType(typeof(IReadOnlyList<CollectionMediaItemDto>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetItems(Guid id, CancellationToken ct)
|
||||
{
|
||||
var result = await mediator.Send(
|
||||
new GetCollectionItemsQuery(id, currentUser.TelegramId), ct);
|
||||
|
||||
return result is null ? NotFound() : Ok(result);
|
||||
}
|
||||
|
||||
// PUT /api/collections/{id}/visibility
|
||||
[HttpPut("{id:guid}/visibility")]
|
||||
public async Task<IActionResult> SetVisibility(
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using MediatR;
|
||||
using MemeVault.Domain.Enums;
|
||||
|
||||
namespace MemeVault.Application.Queries.GetCollectionItems;
|
||||
|
||||
/// <summary>
|
||||
/// Returns all media items inside a collection owned by the requesting user.
|
||||
/// Returns null if the collection doesn't exist or belongs to another user.
|
||||
/// </summary>
|
||||
public sealed record GetCollectionItemsQuery(
|
||||
Guid CollectionId,
|
||||
long RequestingTelegramId
|
||||
) : IRequest<IReadOnlyList<CollectionMediaItemDto>?>;
|
||||
|
||||
public sealed record CollectionMediaItemDto(
|
||||
Guid Id,
|
||||
string? Reference,
|
||||
MediaType Type,
|
||||
string? Caption,
|
||||
IReadOnlyList<string> Tags,
|
||||
DateTimeOffset CreatedAt
|
||||
);
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
using MediatR;
|
||||
using MemeVault.Domain.Repositories;
|
||||
|
||||
namespace MemeVault.Application.Queries.GetCollectionItems;
|
||||
|
||||
public sealed class GetCollectionItemsQueryHandler
|
||||
: IRequestHandler<GetCollectionItemsQuery, IReadOnlyList<CollectionMediaItemDto>?>
|
||||
{
|
||||
private readonly ICollectionRepository _collections;
|
||||
private readonly IMediaItemRepository _mediaItems;
|
||||
|
||||
public GetCollectionItemsQueryHandler(
|
||||
ICollectionRepository collections,
|
||||
IMediaItemRepository mediaItems)
|
||||
{
|
||||
_collections = collections;
|
||||
_mediaItems = mediaItems;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<CollectionMediaItemDto>?> Handle(
|
||||
GetCollectionItemsQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var collection = await _collections.GetByIdAsync(request.CollectionId, cancellationToken);
|
||||
|
||||
// Return null → 404 when collection missing or belongs to someone else
|
||||
if (collection is null || collection.OwnerTelegramId != request.RequestingTelegramId)
|
||||
return null;
|
||||
|
||||
var items = await _mediaItems.GetByCollectionAsync(request.CollectionId, cancellationToken);
|
||||
|
||||
return items
|
||||
.Select(i => new CollectionMediaItemDto(
|
||||
i.Id,
|
||||
i.Source?.Reference,
|
||||
i.Type,
|
||||
i.Caption,
|
||||
i.Tags.Select(t => t.Value).ToList(),
|
||||
i.CreatedAt))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Domain;
|
||||
|
||||
public sealed class CollectionTests
|
||||
{
|
||||
private const long Owner = 100L;
|
||||
private const long Other = 200L;
|
||||
|
||||
// ── Create ────────────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Create_WithValidName_SetsPropertiesCorrectly()
|
||||
{
|
||||
var c = Collection.Create(Owner, "My Memes", "some desc");
|
||||
|
||||
Assert.Equal(Owner, c.OwnerTelegramId);
|
||||
Assert.Equal("My Memes", c.Name);
|
||||
Assert.Equal("some desc", c.Description);
|
||||
Assert.False(c.IsInbox);
|
||||
Assert.Equal(CollectionVisibility.Private, c.Visibility);
|
||||
Assert.NotEqual(Guid.Empty, c.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_OwnerAutoAddedWithOwnerRole()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
|
||||
var member = Assert.Single(c.Members);
|
||||
Assert.Equal(Owner, member.TelegramId);
|
||||
Assert.Equal(CollectionRole.Owner, member.Role);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Create_WithEmptyName_ThrowsDomainException(string name)
|
||||
{
|
||||
Assert.Throws<DomainException>(() => Collection.Create(Owner, name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithNameOver100Chars_ThrowsDomainException()
|
||||
{
|
||||
var longName = new string('x', 101);
|
||||
Assert.Throws<DomainException>(() => Collection.Create(Owner, longName));
|
||||
}
|
||||
|
||||
// ── CreateInbox ───────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void CreateInbox_SetsIsInboxTrue()
|
||||
{
|
||||
var inbox = Collection.CreateInbox(Owner);
|
||||
|
||||
Assert.True(inbox.IsInbox);
|
||||
Assert.Equal("Inbox", inbox.Name);
|
||||
Assert.Equal(Owner, inbox.OwnerTelegramId);
|
||||
}
|
||||
|
||||
// ── Rename ────────────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Rename_ByOwner_UpdatesName()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Old");
|
||||
c.Rename("New", Owner);
|
||||
|
||||
Assert.Equal("New", c.Name);
|
||||
Assert.NotNull(c.UpdatedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rename_ByManager_Succeeds()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Old");
|
||||
c.AddMember(Other, CollectionRole.Manager, Owner);
|
||||
|
||||
c.Rename("New", Other);
|
||||
|
||||
Assert.Equal("New", c.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rename_OnInbox_ThrowsDomainException()
|
||||
{
|
||||
var inbox = Collection.CreateInbox(Owner);
|
||||
|
||||
Assert.Throws<DomainException>(() => inbox.Rename("Renamed", Owner));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rename_ByViewer_ThrowsDomainException()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.AddMember(Other, CollectionRole.Viewer, Owner);
|
||||
|
||||
Assert.Throws<DomainException>(() => c.Rename("New", Other));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rename_ByNonMember_ThrowsDomainException()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
|
||||
Assert.Throws<DomainException>(() => c.Rename("New", Other));
|
||||
}
|
||||
|
||||
// ── AddMember ─────────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void AddMember_ByOwner_AddsMember()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.AddMember(Other, CollectionRole.Viewer, Owner);
|
||||
|
||||
Assert.Equal(2, c.Members.Count);
|
||||
Assert.Contains(c.Members, m => m.TelegramId == Other && m.Role == CollectionRole.Viewer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMember_WithOwnerRole_ThrowsDomainException()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
|
||||
Assert.Throws<DomainException>(() => c.AddMember(Other, CollectionRole.Owner, Owner));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMember_ExistingMember_ChangesRole()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.AddMember(Other, CollectionRole.Viewer, Owner);
|
||||
c.AddMember(Other, CollectionRole.Manager, Owner);
|
||||
|
||||
var member = c.Members.Single(m => m.TelegramId == Other);
|
||||
Assert.Equal(CollectionRole.Manager, member.Role);
|
||||
Assert.Equal(2, c.Members.Count); // не дублируется
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMember_ByNonOwner_ThrowsDomainException()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.AddMember(Other, CollectionRole.Manager, Owner);
|
||||
|
||||
Assert.Throws<DomainException>(() => c.AddMember(300L, CollectionRole.Viewer, Other));
|
||||
}
|
||||
|
||||
// ── RemoveMember ──────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void RemoveMember_ByOwner_RemovesMember()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.AddMember(Other, CollectionRole.Viewer, Owner);
|
||||
|
||||
c.RemoveMember(Other, Owner);
|
||||
|
||||
Assert.DoesNotContain(c.Members, m => m.TelegramId == Other);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveMember_Owner_ThrowsDomainException()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
|
||||
Assert.Throws<DomainException>(() => c.RemoveMember(Owner, Owner));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveMember_NotFound_ThrowsDomainException()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
|
||||
Assert.Throws<DomainException>(() => c.RemoveMember(Other, Owner));
|
||||
}
|
||||
|
||||
// ── SetVisibility ─────────────────────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(CollectionVisibility.LinkOnly)]
|
||||
[InlineData(CollectionVisibility.Public)]
|
||||
public void SetVisibility_ToShared_GeneratesShareToken(CollectionVisibility visibility)
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.SetVisibility(visibility, Owner);
|
||||
|
||||
Assert.NotNull(c.ShareToken);
|
||||
Assert.NotEmpty(c.ShareToken);
|
||||
Assert.Equal(visibility, c.Visibility);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetVisibility_ToPublic_ThenPrivate_KeepsShareToken()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.SetVisibility(CollectionVisibility.Public, Owner);
|
||||
var token = c.ShareToken;
|
||||
|
||||
c.SetVisibility(CollectionVisibility.Private, Owner);
|
||||
|
||||
// Токен сохраняется — повторное открытие использует тот же URL
|
||||
Assert.Equal(token, c.ShareToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetVisibility_ByNonOwner_ThrowsDomainException()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.AddMember(Other, CollectionRole.Manager, Owner);
|
||||
|
||||
Assert.Throws<DomainException>(() => c.SetVisibility(CollectionVisibility.Public, Other));
|
||||
}
|
||||
|
||||
// ── CanView ───────────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void CanView_PublicCollection_ReturnsTrueForAnyone()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.SetVisibility(CollectionVisibility.Public, Owner);
|
||||
|
||||
Assert.True(c.CanView(Other));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanView_PrivateCollection_ReturnsTrueForMember()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
c.AddMember(Other, CollectionRole.Viewer, Owner);
|
||||
|
||||
Assert.True(c.CanView(Other));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanView_PrivateCollection_ReturnsFalseForNonMember()
|
||||
{
|
||||
var c = Collection.Create(Owner, "Test");
|
||||
|
||||
Assert.False(c.CanView(Other));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
using MemeVault.Domain.Aggregates.MediaItem;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Events;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Domain;
|
||||
|
||||
public sealed class MediaItemTests
|
||||
{
|
||||
private static readonly Guid CollectionId = Guid.NewGuid();
|
||||
private const long UserId = 42L;
|
||||
|
||||
private static MediaSource TelegramSource() =>
|
||||
MediaSource.FromTelegram("file_abc123");
|
||||
|
||||
// ── Create ────────────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Create_TextType_WithCaption_Succeeds()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, source: null, MediaType.Text, "hello");
|
||||
|
||||
Assert.Equal(UserId, item.OwnerTelegramId);
|
||||
Assert.Equal(CollectionId, item.CollectionId);
|
||||
Assert.Equal(MediaType.Text, item.Type);
|
||||
Assert.Equal("hello", item.Caption);
|
||||
Assert.Null(item.Source);
|
||||
Assert.NotEqual(Guid.Empty, item.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_TextType_WithoutCaption_ThrowsDomainException()
|
||||
{
|
||||
Assert.Throws<DomainException>(() =>
|
||||
MediaItem.Create(UserId, CollectionId, source: null, MediaType.Text, caption: null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_TextType_WithEmptyCaption_ThrowsDomainException()
|
||||
{
|
||||
Assert.Throws<DomainException>(() =>
|
||||
MediaItem.Create(UserId, CollectionId, source: null, MediaType.Text, caption: " "));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_ImageType_WithSource_Succeeds()
|
||||
{
|
||||
var source = TelegramSource();
|
||||
var item = MediaItem.Create(UserId, CollectionId, source, MediaType.Photo, caption: null);
|
||||
|
||||
Assert.Equal(source, item.Source);
|
||||
Assert.Equal(MediaType.Photo, item.Type);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_ImageType_WithoutSource_ThrowsDomainException()
|
||||
{
|
||||
Assert.Throws<DomainException>(() =>
|
||||
MediaItem.Create(UserId, CollectionId, source: null, MediaType.Photo, caption: null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_RaisesMediaItemSavedEvent()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null);
|
||||
|
||||
var evt = Assert.Single(item.DomainEvents);
|
||||
var saved = Assert.IsType<MediaItemSavedEvent>(evt);
|
||||
Assert.Equal(item.Id, saved.MediaItemId);
|
||||
Assert.Equal(UserId, saved.OwnerTelegramId);
|
||||
Assert.Equal(MediaType.Photo, saved.MediaType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithTags_AssignsTagsCorrectly()
|
||||
{
|
||||
var tags = new[] { new Tag("funny"), new Tag("cats") };
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null, tags: tags);
|
||||
|
||||
Assert.Equal(2, item.Tags.Count);
|
||||
Assert.Contains(item.Tags, t => t.Value == "funny");
|
||||
Assert.Contains(item.Tags, t => t.Value == "cats");
|
||||
}
|
||||
|
||||
// ── MoveToCollection ──────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void MoveToCollection_UpdatesCollectionId()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null);
|
||||
var newCollection = Guid.NewGuid();
|
||||
|
||||
item.MoveToCollection(newCollection);
|
||||
|
||||
Assert.Equal(newCollection, item.CollectionId);
|
||||
Assert.NotNull(item.UpdatedAt);
|
||||
}
|
||||
|
||||
// ── UpdateCaption ─────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void UpdateCaption_SetsNewCaption()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, "old");
|
||||
item.UpdateCaption("new");
|
||||
|
||||
Assert.Equal("new", item.Caption);
|
||||
Assert.NotNull(item.UpdatedAt);
|
||||
}
|
||||
|
||||
// ── AddTag ────────────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void AddTag_NewTag_AddsIt()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null);
|
||||
item.AddTag(new Tag("meme"));
|
||||
|
||||
Assert.Single(item.Tags);
|
||||
Assert.Equal("meme", item.Tags[0].Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddTag_Duplicate_DoesNotAdd()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null);
|
||||
item.AddTag(new Tag("meme"));
|
||||
item.AddTag(new Tag("meme"));
|
||||
|
||||
Assert.Single(item.Tags);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddTag_MoreThan20_ThrowsDomainException()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null);
|
||||
|
||||
for (var i = 0; i < 20; i++)
|
||||
item.AddTag(new Tag($"tag{i}"));
|
||||
|
||||
Assert.Throws<DomainException>(() => item.AddTag(new Tag("overflow")));
|
||||
}
|
||||
|
||||
// ── RemoveTag ─────────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void RemoveTag_ExistingTag_RemovesIt()
|
||||
{
|
||||
var tag = new Tag("meme");
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null, tags: [tag]);
|
||||
|
||||
item.RemoveTag(tag);
|
||||
|
||||
Assert.Empty(item.Tags);
|
||||
Assert.NotNull(item.UpdatedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveTag_NonExistingTag_DoesNotThrow()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null);
|
||||
|
||||
// Не должно бросать исключение
|
||||
item.RemoveTag(new Tag("ghost"));
|
||||
}
|
||||
|
||||
// ── ClearDomainEvents ─────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void ClearDomainEvents_EmptiesEvents()
|
||||
{
|
||||
var item = MediaItem.Create(UserId, CollectionId, TelegramSource(), MediaType.Photo, null);
|
||||
item.ClearDomainEvents();
|
||||
|
||||
Assert.Empty(item.DomainEvents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Domain;
|
||||
|
||||
public sealed class TagTests
|
||||
{
|
||||
[Fact]
|
||||
public void Tag_TrimsWhitespace()
|
||||
{
|
||||
var tag = new Tag(" meme ");
|
||||
Assert.Equal("meme", tag.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_NormalizesToLowercase()
|
||||
{
|
||||
var tag = new Tag("FUNNY");
|
||||
Assert.Equal("funny", tag.Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Tag_EmptyOrWhitespace_ThrowsArgumentException(string value)
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new Tag(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_ExactlyMaxLength_Succeeds()
|
||||
{
|
||||
var value = new string('a', 50);
|
||||
var tag = new Tag(value);
|
||||
Assert.Equal(value, tag.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_ExceedsMaxLength_ThrowsArgumentException()
|
||||
{
|
||||
var value = new string('a', 51);
|
||||
Assert.Throws<ArgumentException>(() => new Tag(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_ImplicitConversionToString_ReturnsValue()
|
||||
{
|
||||
var tag = new Tag("cats");
|
||||
string str = tag;
|
||||
Assert.Equal("cats", str);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_EqualityByValue()
|
||||
{
|
||||
var a = new Tag("meme");
|
||||
var b = new Tag("meme");
|
||||
Assert.Equal(a, b);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tag_InequalityByValue()
|
||||
{
|
||||
var a = new Tag("cats");
|
||||
var b = new Tag("dogs");
|
||||
Assert.NotEqual(a, b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using MemeVault.Application.Commands.CopyMediaToCollection;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Aggregates.MediaItem;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Handlers;
|
||||
|
||||
public sealed class CopyMediaCommandHandlerTests
|
||||
{
|
||||
private readonly IMediaItemRepository _mediaItems = Substitute.For<IMediaItemRepository>();
|
||||
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
|
||||
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
|
||||
private readonly CopyMediaToCollectionCommandHandler _handler;
|
||||
|
||||
private const long UserId = 42L;
|
||||
private static readonly Guid SrcCollectionId = Guid.NewGuid();
|
||||
|
||||
public CopyMediaCommandHandlerTests()
|
||||
{
|
||||
_handler = new CopyMediaToCollectionCommandHandler(_mediaItems, _collections, _uow);
|
||||
}
|
||||
|
||||
private static MediaItem MakePhoto() =>
|
||||
MediaItem.Create(UserId, SrcCollectionId, MediaSource.FromTelegram("f1"), MediaType.Photo, "funny");
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_SourceNotFound_ReturnsNull()
|
||||
{
|
||||
_mediaItems.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns((MediaItem?)null);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new CopyMediaToCollectionCommand(Guid.NewGuid(), Guid.NewGuid(), UserId), default);
|
||||
|
||||
Assert.Null(result);
|
||||
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_SourceWrongOwner_ReturnsNull()
|
||||
{
|
||||
var item = MediaItem.Create(999L, SrcCollectionId,
|
||||
MediaSource.FromTelegram("f1"), MediaType.Photo, null);
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new CopyMediaToCollectionCommand(item.Id, Guid.NewGuid(), UserId), default);
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_TargetCollectionNotFound_ThrowsDomainException()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
_collections.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns((Collection?)null);
|
||||
|
||||
await Assert.ThrowsAsync<DomainException>(() =>
|
||||
_handler.Handle(
|
||||
new CopyMediaToCollectionCommand(item.Id, Guid.NewGuid(), UserId), default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_TargetCollectionWrongOwner_ThrowsDomainException()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
var alienCol = Collection.Create(999L, "Alien");
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
_collections.GetByIdAsync(alienCol.Id, Arg.Any<CancellationToken>()).Returns(alienCol);
|
||||
|
||||
await Assert.ThrowsAsync<DomainException>(() =>
|
||||
_handler.Handle(
|
||||
new CopyMediaToCollectionCommand(item.Id, alienCol.Id, UserId), default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ValidCopy_AddsNewItemAndReturnsId()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
item.AddTag(new Tag("meme"));
|
||||
var targetCol = Collection.Create(UserId, "Saved");
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
_collections.GetByIdAsync(targetCol.Id, Arg.Any<CancellationToken>()).Returns(targetCol);
|
||||
|
||||
var newId = await _handler.Handle(
|
||||
new CopyMediaToCollectionCommand(item.Id, targetCol.Id, UserId), default);
|
||||
|
||||
Assert.NotNull(newId);
|
||||
Assert.NotEqual(item.Id, newId); // копия, не тот же объект
|
||||
|
||||
await _mediaItems.Received(1).AddAsync(
|
||||
Arg.Is<MediaItem>(i =>
|
||||
i.CollectionId == targetCol.Id &&
|
||||
i.Caption == item.Caption &&
|
||||
i.OwnerTelegramId == UserId),
|
||||
Arg.Any<CancellationToken>());
|
||||
|
||||
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using MemeVault.Application.Commands.DeleteCollection;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Handlers;
|
||||
|
||||
public sealed class DeleteCollectionCommandHandlerTests
|
||||
{
|
||||
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
|
||||
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
|
||||
private readonly DeleteCollectionCommandHandler _handler;
|
||||
|
||||
private const long UserId = 42L;
|
||||
|
||||
public DeleteCollectionCommandHandlerTests()
|
||||
{
|
||||
_handler = new DeleteCollectionCommandHandler(_collections, _uow);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_CollectionNotFound_ReturnsFalse()
|
||||
{
|
||||
_collections.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns((Collection?)null);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new DeleteCollectionCommand(Guid.NewGuid(), UserId), default);
|
||||
|
||||
Assert.False(result);
|
||||
await _collections.DidNotReceive().RemoveAsync(Arg.Any<Collection>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WrongOwner_ReturnsFalse()
|
||||
{
|
||||
var col = Collection.Create(999L, "Alien");
|
||||
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new DeleteCollectionCommand(col.Id, UserId), default);
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_InboxCollection_ThrowsDomainException()
|
||||
{
|
||||
var inbox = Collection.CreateInbox(UserId);
|
||||
_collections.GetByIdAsync(inbox.Id, Arg.Any<CancellationToken>()).Returns(inbox);
|
||||
|
||||
await Assert.ThrowsAsync<DomainException>(() =>
|
||||
_handler.Handle(new DeleteCollectionCommand(inbox.Id, UserId), default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ValidCollection_RemovesAndReturnsTrue()
|
||||
{
|
||||
var col = Collection.Create(UserId, "Memes");
|
||||
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new DeleteCollectionCommand(col.Id, UserId), default);
|
||||
|
||||
Assert.True(result);
|
||||
await _collections.Received(1).RemoveAsync(col, Arg.Any<CancellationToken>());
|
||||
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using MemeVault.Application.Commands.EnsureUser;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.BotUser;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Repositories;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Handlers;
|
||||
|
||||
public sealed class EnsureUserCommandHandlerTests
|
||||
{
|
||||
private readonly IBotUserRepository _users = Substitute.For<IBotUserRepository>();
|
||||
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
|
||||
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
|
||||
private readonly EnsureUserCommandHandler _handler;
|
||||
|
||||
public EnsureUserCommandHandlerTests()
|
||||
{
|
||||
_handler = new EnsureUserCommandHandler(_users, _collections, _uow);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NewUser_CreatesUserAndInbox()
|
||||
{
|
||||
_users.GetByTelegramIdAsync(123L, Arg.Any<CancellationToken>())
|
||||
.Returns((BotUser?)null);
|
||||
|
||||
await _handler.Handle(new EnsureUserCommand(123L, "vasya", "Вася"), default);
|
||||
|
||||
await _users.Received(1).AddAsync(
|
||||
Arg.Is<BotUser>(u => u.TelegramId == 123L && u.Username == "vasya"),
|
||||
Arg.Any<CancellationToken>());
|
||||
|
||||
await _collections.Received(1).AddAsync(
|
||||
Arg.Is<Collection>(c => c.IsInbox && c.OwnerTelegramId == 123L),
|
||||
Arg.Any<CancellationToken>());
|
||||
|
||||
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ExistingUser_UpdatesActivityAndDoesNotCreateInbox()
|
||||
{
|
||||
var existing = BotUser.Create(123L, "vasya", null);
|
||||
_users.GetByTelegramIdAsync(123L, Arg.Any<CancellationToken>())
|
||||
.Returns(existing);
|
||||
|
||||
var beforeLastSeen = existing.LastSeenAt;
|
||||
|
||||
await _handler.Handle(new EnsureUserCommand(123L, "vasya", null), default);
|
||||
|
||||
await _users.Received(1).UpdateAsync(existing, Arg.Any<CancellationToken>());
|
||||
await _collections.DidNotReceive().AddAsync(Arg.Any<Collection>(), Arg.Any<CancellationToken>());
|
||||
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using MemeVault.Application.Queries.GetCollectionItems;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Aggregates.MediaItem;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Repositories;
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Handlers;
|
||||
|
||||
public sealed class GetCollectionItemsQueryHandlerTests
|
||||
{
|
||||
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
|
||||
private readonly IMediaItemRepository _mediaItems = Substitute.For<IMediaItemRepository>();
|
||||
private readonly GetCollectionItemsQueryHandler _handler;
|
||||
|
||||
private const long UserId = 42L;
|
||||
|
||||
public GetCollectionItemsQueryHandlerTests()
|
||||
{
|
||||
_handler = new GetCollectionItemsQueryHandler(_collections, _mediaItems);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_CollectionNotFound_ReturnsNull()
|
||||
{
|
||||
_collections.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns((Collection?)null);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new GetCollectionItemsQuery(Guid.NewGuid(), UserId), default);
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WrongOwner_ReturnsNull()
|
||||
{
|
||||
var col = Collection.Create(999L, "Alien");
|
||||
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new GetCollectionItemsQuery(col.Id, UserId), default);
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ValidCollection_ReturnsEmptyList()
|
||||
{
|
||||
var col = Collection.Create(UserId, "Memes");
|
||||
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
|
||||
_mediaItems.GetByCollectionAsync(col.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<MediaItem>());
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new GetCollectionItemsQuery(col.Id, UserId), default);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ValidCollection_MapsItemsToDto()
|
||||
{
|
||||
var col = Collection.Create(UserId, "Memes");
|
||||
var item1 = MediaItem.Create(UserId, col.Id,
|
||||
MediaSource.FromTelegram("file1"), MediaType.Photo, "funny cat");
|
||||
item1.AddTag(new Tag("cats"));
|
||||
|
||||
var item2 = MediaItem.Create(UserId, col.Id, source: null, MediaType.Text, "hello world");
|
||||
|
||||
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
|
||||
_mediaItems.GetByCollectionAsync(col.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(new[] { item1, item2 });
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new GetCollectionItemsQuery(col.Id, UserId), default);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(2, result.Count);
|
||||
|
||||
var dto1 = result.Single(d => d.Type == MediaType.Photo);
|
||||
Assert.Equal("file1", dto1.Reference);
|
||||
Assert.Equal("funny cat", dto1.Caption);
|
||||
Assert.Equal("cats", Assert.Single(dto1.Tags));
|
||||
|
||||
var dto2 = result.Single(d => d.Type == MediaType.Text);
|
||||
Assert.Null(dto2.Reference);
|
||||
Assert.Equal("hello world", dto2.Caption);
|
||||
Assert.Empty(dto2.Tags);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using MemeVault.Application.Commands.SaveMedia;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Aggregates.MediaItem;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Handlers;
|
||||
|
||||
public sealed class SaveMediaCommandHandlerTests
|
||||
{
|
||||
private readonly IMediaItemRepository _mediaItems = Substitute.For<IMediaItemRepository>();
|
||||
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
|
||||
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
|
||||
private readonly SaveMediaCommandHandler _handler;
|
||||
|
||||
private const long UserId = 42L;
|
||||
|
||||
public SaveMediaCommandHandlerTests()
|
||||
{
|
||||
_handler = new SaveMediaCommandHandler(_mediaItems, _collections, _uow);
|
||||
}
|
||||
|
||||
private static SaveMediaCommand PhotoCommand(Guid? collectionId = null) =>
|
||||
new(UserId, MediaSource.FromTelegram("file_abc"), MediaType.Photo,
|
||||
Caption: null, Tags: [], CollectionId: collectionId);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NoCollectionId_SavesItemToInbox()
|
||||
{
|
||||
var inbox = Collection.CreateInbox(UserId);
|
||||
_collections.GetInboxAsync(UserId, Arg.Any<CancellationToken>()).Returns(inbox);
|
||||
|
||||
var id = await _handler.Handle(PhotoCommand(), default);
|
||||
|
||||
Assert.NotEqual(Guid.Empty, id);
|
||||
await _mediaItems.Received(1).AddAsync(
|
||||
Arg.Is<MediaItem>(i => i.CollectionId == inbox.Id && i.OwnerTelegramId == UserId),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithCollectionId_SavesItemToThatCollection()
|
||||
{
|
||||
var col = Collection.Create(UserId, "Memes");
|
||||
_collections.GetByIdAsync(col.Id, Arg.Any<CancellationToken>()).Returns(col);
|
||||
|
||||
var id = await _handler.Handle(PhotoCommand(col.Id), default);
|
||||
|
||||
Assert.NotEqual(Guid.Empty, id);
|
||||
await _mediaItems.Received(1).AddAsync(
|
||||
Arg.Is<MediaItem>(i => i.CollectionId == col.Id),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_InboxNotFound_ThrowsDomainException()
|
||||
{
|
||||
_collections.GetInboxAsync(UserId, Arg.Any<CancellationToken>())
|
||||
.Returns((Collection?)null);
|
||||
|
||||
await Assert.ThrowsAsync<DomainException>(() =>
|
||||
_handler.Handle(PhotoCommand(), default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ExplicitCollectionNotFound_ThrowsDomainException()
|
||||
{
|
||||
var missingId = Guid.NewGuid();
|
||||
_collections.GetByIdAsync(missingId, Arg.Any<CancellationToken>())
|
||||
.Returns((Collection?)null);
|
||||
|
||||
await Assert.ThrowsAsync<DomainException>(() =>
|
||||
_handler.Handle(PhotoCommand(missingId), default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ExplicitCollectionWrongOwner_ThrowsDomainException()
|
||||
{
|
||||
var otherUserCol = Collection.Create(999L, "Alien");
|
||||
_collections.GetByIdAsync(otherUserCol.Id, Arg.Any<CancellationToken>())
|
||||
.Returns(otherUserCol);
|
||||
|
||||
await Assert.ThrowsAsync<DomainException>(() =>
|
||||
_handler.Handle(PhotoCommand(otherUserCol.Id), default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_TextMedia_WithCaption_Succeeds()
|
||||
{
|
||||
var inbox = Collection.CreateInbox(UserId);
|
||||
_collections.GetInboxAsync(UserId, Arg.Any<CancellationToken>()).Returns(inbox);
|
||||
|
||||
var cmd = new SaveMediaCommand(UserId, Source: null, MediaType.Text,
|
||||
Caption: "interesting text", Tags: []);
|
||||
|
||||
var id = await _handler.Handle(cmd, default);
|
||||
|
||||
Assert.NotEqual(Guid.Empty, id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using MemeVault.Application.Commands.UpdateMedia;
|
||||
using MemeVault.Application.Common;
|
||||
using MemeVault.Domain.Aggregates.Collection;
|
||||
using MemeVault.Domain.Aggregates.MediaItem;
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.Repositories;
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace MemeVault.Tests.Unit.Handlers;
|
||||
|
||||
public sealed class UpdateMediaCommandHandlerTests
|
||||
{
|
||||
private readonly IMediaItemRepository _mediaItems = Substitute.For<IMediaItemRepository>();
|
||||
private readonly ICollectionRepository _collections = Substitute.For<ICollectionRepository>();
|
||||
private readonly IUnitOfWork _uow = Substitute.For<IUnitOfWork>();
|
||||
private readonly UpdateMediaCommandHandler _handler;
|
||||
|
||||
private const long UserId = 42L;
|
||||
private static readonly Guid CollectionId = Guid.NewGuid();
|
||||
|
||||
public UpdateMediaCommandHandlerTests()
|
||||
{
|
||||
_handler = new UpdateMediaCommandHandler(_mediaItems, _collections, _uow);
|
||||
}
|
||||
|
||||
private static MediaItem MakePhoto() =>
|
||||
MediaItem.Create(UserId, CollectionId, MediaSource.FromTelegram("f1"), MediaType.Photo, null);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ItemNotFound_ReturnsFalse()
|
||||
{
|
||||
_mediaItems.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns((MediaItem?)null);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new UpdateMediaCommand(Guid.NewGuid(), UserId, null, []), default);
|
||||
|
||||
Assert.False(result);
|
||||
await _uow.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WrongOwner_ReturnsFalse()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new UpdateMediaCommand(item.Id, RequestingTelegramId: 999L, null, []), default);
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_UpdatesCaptionAndTags_ReturnsTrue()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
item.AddTag(new Tag("old"));
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new UpdateMediaCommand(item.Id, UserId, "new caption", ["funny", "cats"]), default);
|
||||
|
||||
Assert.True(result);
|
||||
Assert.Equal("new caption", item.Caption);
|
||||
Assert.Equal(2, item.Tags.Count);
|
||||
Assert.DoesNotContain(item.Tags, t => t.Value == "old");
|
||||
await _uow.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithNewCollectionId_MovesItem()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
var newCol = Collection.Create(UserId, "Cats");
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
_collections.GetByIdAsync(newCol.Id, Arg.Any<CancellationToken>()).Returns(newCol);
|
||||
|
||||
var result = await _handler.Handle(
|
||||
new UpdateMediaCommand(item.Id, UserId, null, [], CollectionId: newCol.Id), default);
|
||||
|
||||
Assert.True(result);
|
||||
Assert.Equal(newCol.Id, item.CollectionId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithSameCollectionId_DoesNotMove()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
|
||||
// Передаём тот же collectionId — перемещения не должно быть
|
||||
var result = await _handler.Handle(
|
||||
new UpdateMediaCommand(item.Id, UserId, null, [], CollectionId: CollectionId), default);
|
||||
|
||||
Assert.True(result);
|
||||
await _collections.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_TargetCollectionWrongOwner_ThrowsDomainException()
|
||||
{
|
||||
var item = MakePhoto();
|
||||
var alienCol = Collection.Create(999L, "Alien");
|
||||
_mediaItems.GetByIdAsync(item.Id, Arg.Any<CancellationToken>()).Returns(item);
|
||||
_collections.GetByIdAsync(alienCol.Id, Arg.Any<CancellationToken>()).Returns(alienCol);
|
||||
|
||||
await Assert.ThrowsAsync<DomainException>(() =>
|
||||
_handler.Handle(
|
||||
new UpdateMediaCommand(item.Id, UserId, null, [], CollectionId: alienCol.Id), default));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>MemeVault.Tests.Unit</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MemeVault.Domain\MemeVault.Domain.csproj" />
|
||||
<ProjectReference Include="..\MemeVault.Application\MemeVault.Application.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user