diff --git a/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs b/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs index 2cb0d9b..e1e77b7 100644 --- a/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs +++ b/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs @@ -271,4 +271,88 @@ public class FriendsServiceTests Assert.That(updatedFriendship.Status, Is.EqualTo(FriendshipStatus.Accepted)); } + // GetFriendsAsync + [Test] + public async Task GetFriendsAsync_ReturnsUsers_IfUserExists() + { + // Arrange + var userId = Guid.NewGuid(); + var friendships = _fixture.CreateMany().ToList(); + + friendships.ForEach(f => + { + f.RequesterId = userId; + f.Status = FriendshipStatus.Accepted; + }); + + _friendshipsRepositoryMock.Setup(f => f.FindByUserIdAsync(userId)) + .ReturnsAsync(friendships); + + // Act + var result = await _service.GetFriendsAsync(userId); + + // Assert + Assert.That(result.Count, Is.EqualTo(friendships.Count)); + Assert.That(result, Is.EquivalentTo(friendships.Select(f => f.Addressee))); + } + + [Test] + public void GetFriendsAsync_Throws_InvalidOperationException_IfUserNotExists() + { + // Arrange + _friendshipsRepositoryMock.Setup(f => f.FindByUserIdAsync(It.IsAny())) + .ThrowsAsync(new NotFoundByKeyException(Guid.NewGuid())); + + // Act & Assert + Assert.ThrowsAsync(async () => await _service.GetFriendsAsync(Guid.NewGuid())); + } + + // GetIncomingRequestsAsync + + [Test] + public async Task GetIncomingRequestsAsync_ReturnsFriendships_IfFriendshipsExists() + { + // Arrange + var userId = Guid.NewGuid(); + + var friendships = _fixture.CreateMany().ToList(); + + var user = _fixture.Build() + .With(u => u.Id, userId) + .With(u => u.ReceivedFriendRequests, friendships) + .Create(); + + friendships.ForEach(f => + { + f.AddresseeId = userId; + f.Addressee = user; + f.Status = FriendshipStatus.Pending; + }); + + _usersRepositoryMock.Setup(f => f.FindByIdAsync(userId)) + .ReturnsAsync(user); + + // Act + var result = await _service.GetIncomingRequestsAsync(userId); + + // Assert + Assert.That(result.Count, Is.EqualTo(friendships.Count)); + Assert.That(result.Select(u => u.Id), Is.EquivalentTo(friendships.Select(f => f.Id))); + } + + [Test] + public void GetIncomingRequestsAsync_ThrowsInvalidOperationException_WhenUserNotFound() + { + // Arrange + var userId = Guid.NewGuid(); + + _usersRepositoryMock + .Setup(r => r.FindByIdAsync(userId)) + .ThrowsAsync(new NotFoundByKeyException(userId)); + + // Act & Assert + Assert.ThrowsAsync(async () => + await _service.GetIncomingRequestsAsync(userId)); + } + } diff --git a/Govor.Application/Services/FriendsService.cs b/Govor.Application/Services/FriendsService.cs index f2d62fe..b906207 100644 --- a/Govor.Application/Services/FriendsService.cs +++ b/Govor.Application/Services/FriendsService.cs @@ -1,4 +1,3 @@ -using Govor.Application.Exceptions; using Govor.Application.Exceptions.FriendsService; using Govor.Application.Interfaces; using Govor.Core.Models; @@ -110,7 +109,9 @@ public class FriendsService : IFriendsService try { var user = await _usersRepository.FindByIdAsync(userId); - return user.ReceivedFriendRequests; + return user.ReceivedFriendRequests + .Where(f => f.Status == FriendshipStatus.Pending) + .ToList(); } catch (NotFoundByKeyException ex) {