From 73f22a3acebb976f8015fc2426508c92e01bc010 Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Thu, 24 Jul 2025 13:54:59 +0700 Subject: [PATCH] more tests for PresenceHub --- Govor.API.Tests/Hubs/PresenceHubTests.cs | 83 ++++++++++++++++++++++++ Govor.API/Hubs/FriendsHub.cs | 4 +- Govor.API/Hubs/PresenceHub.cs | 2 +- 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/Govor.API.Tests/Hubs/PresenceHubTests.cs b/Govor.API.Tests/Hubs/PresenceHubTests.cs index 3a4df98..316c00f 100644 --- a/Govor.API.Tests/Hubs/PresenceHubTests.cs +++ b/Govor.API.Tests/Hubs/PresenceHubTests.cs @@ -2,6 +2,7 @@ using AutoFixture; using Govor.API.Common.SignalR.Helpers; using Govor.API.Hubs; using Govor.Application.Interfaces.UserOnlineStatus; +using Govor.Core.Models.Users; using Govor.Core.Repositories.Users; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; @@ -103,4 +104,86 @@ public class PresenceHubTests } } + [Test] + public async Task OnConnectedAsync_UserIdInvalidOrNotExists_AbortsConnection() + { + // Arrange + _mockHubUserAccessor.Setup(h => h.GetUserId(It.IsAny(), false)) + .Returns(Guid.Empty); + + // Act + await _hub.OnConnectedAsync(); + + // Assert + _mockOnlineUserStore.Verify(store => store.SetOnlineUser(It.IsAny()), Times.Never); + _mockContext.Verify(c => c.Abort(), Times.Once); + } + + [Test] + public async Task OnConnectedAsync_UserNotExists_AbortsConnection() + { + // Arrange + _mockUserRepository.Setup(f => f.ExistsByIdAsync(_userId)) + .ReturnsAsync(false); + + // Act + await _hub.OnConnectedAsync(); + + // Assert + _mockOnlineUserStore.Verify(store => store.SetOnlineUser(It.IsAny()), Times.Never); + _mockContext.Verify(c => c.Abort(), Times.Once); + } + + // Tests for OnDisconnectedAsync action + [Test] + public async Task OnDisconnectedAsync_SetsUserOffline_UpdatesWasOnline_NotifiesFriends() + { + // Arrange + var user = new User { Id = _userId, WasOnline = DateTime.MinValue }; + + var friendsIds = _fixture.CreateMany().ToList(); + _mockUserRepository.Setup(r => r.FindByIdAsync(_userId)) + .ReturnsAsync(user); + + _mockUserNotification.Setup(n => n.GetNotifiedUsers(_userId)) + .ReturnsAsync(friendsIds); + + // Act + await _hub.OnDisconnectedAsync(null!); + + // Assert + _mockOnlineUserStore.Verify(store => store.SetOfflineUser(_userId), Times.Once); + _mockUserRepository.Verify(r => r.UpdateAsync(It.Is(u => u.Id == _userId && u.WasOnline > DateTime.MinValue)), Times.Once); + _clientsMock.Verify(c => c.Group(It.IsAny()), Times.AtLeastOnce); + + foreach (var friendId in friendsIds) + { + _clientProxyMock.Verify(proxy => + proxy.SendCoreAsync("UserOffline", + It.Is(args => args.Length == 1 && (Guid)args[0] == _userId), + It.IsAny()), + Times.Exactly(friendsIds.Count)); + } + } + + [Test] + public async Task OnDisconnectedAsync_UserIdEmpty_LogsAndSkips() + { + // Arrange + _mockHubUserAccessor.Setup(h => h.GetUserId(It.IsAny(), true)) + .Returns(Guid.Empty); + + // Act + await _hub.OnDisconnectedAsync(null!); + + // Assert + _mockOnlineUserStore.Verify(s => s.SetOfflineUser(It.IsAny()), Times.Never); + _mockUserRepository.Verify(r => r.UpdateAsync(It.IsAny()), Times.Never); + + _clientProxyMock.Verify(proxy => + proxy.SendCoreAsync("UserOffline", + It.Is(args => args.Length == 1 && (Guid)args[0] == _userId), + It.IsAny()), + Times.Never); + } } \ No newline at end of file diff --git a/Govor.API/Hubs/FriendsHub.cs b/Govor.API/Hubs/FriendsHub.cs index 75cf871..94fa19a 100644 --- a/Govor.API/Hubs/FriendsHub.cs +++ b/Govor.API/Hubs/FriendsHub.cs @@ -57,13 +57,13 @@ public class FriendsHub : Hub else if (exception != null) { _logger.LogWarning(exception, - "User disconnected with an exception and invalid UserID claim. ConnectionId: {ConnectionId}", + "User disconnected with an exception and invalid UserId claim. ConnectionId: {ConnectionId}", Context.ConnectionId); } else { _logger.LogInformation( - "User disconnected with no exception and invalid UserID claim. ConnectionId: {ConnectionId}", + "User disconnected with no exception and invalid UserId claim. ConnectionId: {ConnectionId}", Context.ConnectionId); } diff --git a/Govor.API/Hubs/PresenceHub.cs b/Govor.API/Hubs/PresenceHub.cs index 990bf23..c06de5c 100644 --- a/Govor.API/Hubs/PresenceHub.cs +++ b/Govor.API/Hubs/PresenceHub.cs @@ -69,7 +69,7 @@ public class PresenceHub : Hub else { _logger.LogInformation( - "User disconnected with no exception and invalid UserID claim. ConnectionId: {ConnectionId}", + "User disconnected with no exception and invalid UserId claim. ConnectionId: {ConnectionId}", Context.ConnectionId); return; }