WishlistServiceTests.cs
2,414 bytes
| 1 | using App.BLL.DTO; |
|---|---|
| 2 | using App.BLL.Services; |
| 3 | using App.Domain; |
| 4 | using App.Domain.Contracts; |
| 5 | using FluentAssertions; |
| 6 | using Moq; |
| 7 | |
| 8 | namespace App.Tests.BLL; |
| 9 | |
| 10 | public class WishlistServiceTests |
| 11 | { |
| 12 | private readonly Mock<IAppUnitOfWork> _uow = new(); |
| 13 | private readonly Mock<ITripWishlistItemRepository> _items = new(); |
| 14 | private readonly Mock<ITripParticipantRepository> _participants = new(); |
| 15 | private readonly WishlistService _sut; |
| 16 | |
| 17 | public WishlistServiceTests() |
| 18 | { |
| 19 | _uow.Setup(u => u.TripWishlistItems).Returns(_items.Object); |
| 20 | _uow.Setup(u => u.TripParticipants).Returns(_participants.Object); |
| 21 | _sut = new WishlistService(_uow.Object); |
| 22 | } |
| 23 | |
| 24 | [Fact] |
| 25 | public async Task GetByTripIdAsync_WhenUserIsNotParticipant_ReturnsEmptyList() |
| 26 | { |
| 27 | // Arrange — IDOR check: non-participants get an empty list, not an exception |
| 28 | var tripId = Guid.NewGuid(); |
| 29 | var userId = Guid.NewGuid(); |
| 30 | _participants.Setup(r => r.IsParticipantAsync(tripId, userId)).ReturnsAsync(false); |
| 31 | |
| 32 | // Act |
| 33 | var result = await _sut.GetByTripIdAsync(tripId, userId); |
| 34 | |
| 35 | // Assert |
| 36 | result.Should().BeEmpty(); |
| 37 | _items.Verify(r => r.GetByTripIdAsync(It.IsAny<Guid>()), Times.Never); |
| 38 | } |
| 39 | |
| 40 | [Fact] |
| 41 | public async Task CreateAsync_WhenUserIsParticipant_AddsItemAndStampsAddedByUser() |
| 42 | { |
| 43 | // Arrange |
| 44 | var tripId = Guid.NewGuid(); |
| 45 | var userId = Guid.NewGuid(); |
| 46 | var input = new TripWishlistItemBllDto |
| 47 | { |
| 48 | TripId = tripId, |
| 49 | Title = "Eiffel Tower", |
| 50 | Category = EWishlistCategory.Activity, |
| 51 | Priority = EWishlistPriority.MustDo |
| 52 | }; |
| 53 | |
| 54 | _participants.Setup(r => r.IsParticipantAsync(tripId, userId)).ReturnsAsync(true); |
| 55 | TripWishlistItem? added = null; |
| 56 | _items.Setup(r => r.Add(It.IsAny<TripWishlistItem>())) |
| 57 | .Callback<TripWishlistItem>(i => added = i) |
| 58 | .Returns((TripWishlistItem i) => i); |
| 59 | _items.Setup(r => r.GetByIdAsync(It.IsAny<Guid>())) |
| 60 | .ReturnsAsync(() => added); |
| 61 | |
| 62 | // Act |
| 63 | var (result, error) = await _sut.CreateAsync(input, userId); |
| 64 | |
| 65 | // Assert |
| 66 | error.Should().BeNull(); |
| 67 | result.Should().NotBeNull(); |
| 68 | added.Should().NotBeNull(); |
| 69 | added!.AddedByUserId.Should().Be(userId); |
| 70 | _uow.Verify(u => u.SaveChangesAsync(), Times.Once); |
| 71 | } |
| 72 | } |
| 73 | |