From a43a26b3076786c72f318e1713ee0ba8ae9af105 Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Thu, 10 Jul 2025 16:12:32 +0700 Subject: [PATCH] FriendsHubTests --- .../IntegrationTests/Hubs/FriendsHubTests.cs | 270 ++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 Govor.API.Tests/IntegrationTests/Hubs/FriendsHubTests.cs diff --git a/Govor.API.Tests/IntegrationTests/Hubs/FriendsHubTests.cs b/Govor.API.Tests/IntegrationTests/Hubs/FriendsHubTests.cs new file mode 100644 index 0000000..9f88695 --- /dev/null +++ b/Govor.API.Tests/IntegrationTests/Hubs/FriendsHubTests.cs @@ -0,0 +1,270 @@ +using Govor.API.Hubs; +using Govor.Application.Exceptions.FriendsService; +using Govor.Application.Interfaces.Friends; +using Govor.Application.Interfaces.Infrastructure.Extensions; +using Govor.Contracts.Responses.SignalR; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Logging; +using Moq; + +namespace Govor.API.Tests.IntegrationTests.Hubs; + +[TestFixture] +public class FriendsHubTests +{ + private Mock _friendRequestServiceMock = null!; + private Mock _currentUserServiceMock = null!; + private Mock _clientsMock = null!; + private Mock _clientProxyMock = null!; + private Mock> _loggerMock = null!; + private FriendsHub _hub = null!; + private readonly Guid _userId = Guid.NewGuid(); + private readonly Guid _targetUserId = Guid.NewGuid(); + + [SetUp] + public void Setup() + { + _friendRequestServiceMock = new Mock(); + _currentUserServiceMock = new Mock(); + _clientsMock = new Mock(); + _clientProxyMock = new Mock(); + _loggerMock = new Mock>(); + + _currentUserServiceMock.Setup(x => x.GetCurrentUserId()).Returns(_userId); + _clientsMock.Setup(c => c.User(It.IsAny())).Returns(_clientProxyMock.Object); + + _hub = new FriendsHub( + _friendRequestServiceMock.Object, + _currentUserServiceMock.Object, + _loggerMock.Object) + { + Clients = _clientsMock.Object + }; + } + + // Tests for SendRequest action + [Test] + public async Task SendRequest_ShouldReturnCreated_WhenSuccess() + { + // Act + var result = await _hub.SendRequest(_targetUserId); + + // Assert + Assert.That(result, Is.Not.Null); + Assert.That(result.Status, Is.EqualTo(HubResultStatus.Created)); + + _friendRequestServiceMock.Verify(s => s.SendAsync(_userId, _targetUserId), Times.Once); + + _clientProxyMock.Verify(c => c.SendCoreAsync( + "FriendRequestReceived", + It.Is(o => o[0]!.Equals(_userId)), + default), Times.Once); + } + + [Test] + public async Task SendRequest_ShouldReturnBadRequest_WhenInvalidOperation() + { + // Arrange + _friendRequestServiceMock + .Setup(s => s.SendAsync(_userId, _targetUserId)) + .ThrowsAsync(new InvalidOperationException("Invalid")); + + // Act + var result = await _hub.SendRequest(_targetUserId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.BadRequest)); + Assert.That(result.ErrorMessage, Is.EqualTo("Invalid")); + } + + [Test] + public async Task SendRequest_ShouldReturnConflict_WhenAlreadySent() + { + // Arrange + _friendRequestServiceMock + .Setup(s => s.SendAsync(_userId, _targetUserId)) + .ThrowsAsync(new RequestAlreadySentException(_userId, _targetUserId)); + + // Act + var result = await _hub.SendRequest(_targetUserId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.Conflict)); + Assert.That(result.ErrorMessage, Is.EqualTo($"Request was already sent from {_userId} to {_targetUserId}")); + } + + [Test] + public async Task SendRequest_ShouldReturnUnauthorized_WhenCurrentUserIsNotAuthenticated() + { + // Arrange + _currentUserServiceMock.Setup(x => x.GetCurrentUserId()) + .Throws(new UnauthorizedAccessException("userId claim is missing or invalid")); + + // Act + var result = await _hub.SendRequest(_targetUserId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.Unauthorized)); + Assert.That(result.ErrorMessage, Is.EqualTo("userId claim is missing or invalid")); + } + + [Test] + public async Task SendRequest_ShouldReturnError_WhenSomeExceptionOccurs() + { + // Arrange + _friendRequestServiceMock + .Setup(s => s.SendAsync(_userId, _targetUserId)) + .ThrowsAsync(new Exception("error")); + + // Act + var result = await _hub.SendRequest(_targetUserId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.ServerError)); + Assert.That(result.ErrorMessage, Is.EqualTo("Unexpected error! Please try later!")); + } + + // Test for AcceptRequest action + [Test] + public async Task AcceptRequest_ShouldReturnOk_WhenSuccess() + { + // Arrange + var friendshipId = Guid.NewGuid(); + + // Act + var result = await _hub.AcceptRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.Success)); + _friendRequestServiceMock.Verify(s => s.AcceptAsync(friendshipId, _userId), Times.Once); + + _clientProxyMock.Verify(c => c.SendCoreAsync( + "FriendRequestAccepted", + It.Is(o => o[0]!.Equals(friendshipId)), + default), Times.Once); + } + + [Test] + public async Task AcceptRequest_ShouldReturnBadRequest_InvalidOperation() + { + // Arrange + var friendshipId = Guid.NewGuid(); + _friendRequestServiceMock + .Setup(s => s.AcceptAsync(friendshipId, _userId)) + .ThrowsAsync(new InvalidOperationException("Invalid")); + + // Act + var result = await _hub.AcceptRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.BadRequest)); + Assert.That(result.ErrorMessage, Is.EqualTo("Invalid")); + } + + + [Test] + public async Task AcceptRequest_ShouldReturnUnauthorized_UnauthorizedAccess() + { + // Arrange + var friendshipId = Guid.NewGuid(); + _friendRequestServiceMock + .Setup(s => s.AcceptAsync(friendshipId, _userId)) + .ThrowsAsync(new UnauthorizedAccessException("userId claim is missing or invalid")); + + // Act + var result = await _hub.AcceptRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.Unauthorized)); + Assert.That(result.ErrorMessage, Is.EqualTo("userId claim is missing or invalid")); + } + + [Test] + public async Task AcceptRequest_ShouldReturnError_WhenSomeExceptionOccurs() + { + // Arrange + var friendshipId = Guid.NewGuid(); + _friendRequestServiceMock + .Setup(s => s.AcceptAsync(friendshipId, _userId)) + .ThrowsAsync(new Exception("error")); + + // Act + var result = await _hub.AcceptRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.ServerError)); + Assert.That(result.ErrorMessage, Is.EqualTo("Unexpected error! Please try later!")); + } + + // Test for RejectRequest action + [Test] + public async Task RejectRequest_ShouldReturnOk_WhenSuccess() + { + // Arrange + var friendshipId = Guid.NewGuid(); + + // Act + var result = await _hub.RejectRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.Success)); + _friendRequestServiceMock.Verify(s => s.RejectAsync(friendshipId, _userId), Times.Once); + + _clientProxyMock.Verify(c => c.SendCoreAsync( + "FriendRequestRejected", + It.Is(o => o[0]!.Equals(friendshipId)), + default), Times.Once); + } + + [Test] + public async Task RejectRequest_ShouldReturnBadRequest_InvalidOperation() + { + // Arrange + var friendshipId = Guid.NewGuid(); + _friendRequestServiceMock + .Setup(s => s.RejectAsync(friendshipId, _userId)) + .ThrowsAsync(new InvalidOperationException("Invalid")); + + // Act + var result = await _hub.RejectRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.BadRequest)); + Assert.That(result.ErrorMessage, Is.EqualTo("Invalid")); + } + + + [Test] + public async Task RejectRequest_ShouldReturnUnauthorized_UnauthorizedAccess() + { + // Arrange + var friendshipId = Guid.NewGuid(); + _friendRequestServiceMock + .Setup(s => s.RejectAsync(friendshipId, _userId)) + .ThrowsAsync(new UnauthorizedAccessException("userId claim is missing or invalid")); + + // Act + var result = await _hub.RejectRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.Unauthorized)); + Assert.That(result.ErrorMessage, Is.EqualTo("userId claim is missing or invalid")); + } + + [Test] + public async Task RejectRequest_ShouldReturnError_WhenSomeExceptionOccurs() + { + // Arrange + var friendshipId = Guid.NewGuid(); + _friendRequestServiceMock + .Setup(s => s.RejectAsync(friendshipId, _userId)) + .ThrowsAsync(new Exception("error")); + + // Act + var result = await _hub.RejectRequest(friendshipId); + + // Assert + Assert.That(result.Status, Is.EqualTo(HubResultStatus.ServerError)); + Assert.That(result.ErrorMessage, Is.EqualTo("Unexpected error! Please try later!")); + } +}