From 681a55c198fb1392b528e93e7d339fde6133e875 Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Mon, 30 Jun 2025 16:34:31 +0700 Subject: [PATCH] Update FriendsServiceTests.cs --- .../UnitTests/Services/FriendsServiceTests.cs | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs b/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs index fac5fe0..2cb0d9b 100644 --- a/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs +++ b/Govor.API.Tests/UnitTests/Services/FriendsServiceTests.cs @@ -206,7 +206,69 @@ public class FriendsServiceTests .With(f => f.Status, FriendshipStatus.Accepted) .Create(); - + _friendshipsRepositoryMock + .Setup(r => r.GetByIdAsync(friendship.Id)) + .ReturnsAsync(friendship); + // Act & Assert + Assert.ThrowsAsync(async () => await _service.AcceptFriendRequestAsync(friendship.Id, friendship.AddresseeId)); } + [Test] + public void AcceptFriendRequestAsync_Throws_UnauthorizedAccessException_IfCurrentUserIdIsNotAddressee() + { + // Arrange + var friendship = _fixture.Build() + .With(f => f.Status, FriendshipStatus.Pending) + .Create(); + + _friendshipsRepositoryMock + .Setup(r => r.GetByIdAsync(friendship.Id)) + .ReturnsAsync(friendship); + + var wrongUserId = Guid.NewGuid(); // не совпадает с AddresseeId + + // Act & Assert + Assert.ThrowsAsync(async () => + await _service.AcceptFriendRequestAsync(friendship.Id, wrongUserId)); + } + + [Test] + public void AcceptFriendRequestAsync_Throws_InvalidOperationException_IfFriendshipNotFound() + { + // Arrange + var requestId = Guid.NewGuid(); + var userId = Guid.NewGuid(); + + _friendshipsRepositoryMock + .Setup(r => r.GetByIdAsync(requestId)) + .ThrowsAsync(new NotFoundByKeyException(requestId)); + + // Act & Assert + Assert.ThrowsAsync(async () => + await _service.AcceptFriendRequestAsync(requestId, userId)); + } + + [Test] + public async Task AcceptFriendRequestAsync_ChangesStatusToAccepted() + { + var friendship = _fixture.Build() + .With(f => f.Status, FriendshipStatus.Pending) + .Create(); + + _friendshipsRepositoryMock + .Setup(r => r.GetByIdAsync(friendship.Id)) + .ReturnsAsync(friendship); + + Friendship updatedFriendship = null; + + _friendshipsRepositoryMock + .Setup(r => r.UpdateAsync(It.IsAny())) + .Callback(f => updatedFriendship = f) + .Returns(Task.CompletedTask); + + await _service.AcceptFriendRequestAsync(friendship.Id, friendship.AddresseeId); + + Assert.That(updatedFriendship.Status, Is.EqualTo(FriendshipStatus.Accepted)); + } + }