IUserGroupsService + tests

This commit is contained in:
Artemy
2025-07-07 21:06:04 +07:00
parent a68feb4a17
commit 92c1ff6458
5 changed files with 121 additions and 13 deletions
@@ -0,0 +1,70 @@
using AutoFixture;
using Govor.Application.Interfaces;
using Govor.Application.Services;
using Govor.Core.Models;
using Govor.Core.Repositories.Groups;
using Govor.Data.Repositories.Exceptions;
using Moq;
namespace Govor.API.Tests.UnitTests.Services;
[TestFixture]
public class UserGroupsServiceTests
{
private Fixture _fixture;
private Mock<IGroupsRepository> _repositoryMock;
private IUserGroupsService _service;
[SetUp]
public void SetUp()
{
_fixture = new Fixture();
_fixture.Behaviors
.OfType<ThrowingRecursionBehavior>()
.ToList()
.ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
_repositoryMock = new Mock<IGroupsRepository>();
_service = new UserGroupsService(_repositoryMock.Object);
}
[Test]
public async Task GetUserGroups_ShouldReturnAllUserGroups()
{
// Arrange
var chats = _fixture.CreateMany<ChatGroup>();
var userId = chats.First().Members.First().Id;
_repositoryMock.Setup(r => r.GetByUserIdAsync(userId))
.ReturnsAsync([chats.First()]);
// Act
var result = await _service.GetUserGroupsAsync(userId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count(), Is.EqualTo(1));
Assert.That(result, Is.EquivalentTo([chats.First()]));
}
[Test]
public async Task GetUserGroups_ButGroupDoesNotExist_ShouldReturnEmptyList()
{
// Arrange
var userId = _fixture.Create<Guid>();
_repositoryMock.Setup(r => r.GetByUserIdAsync(userId))
.ThrowsAsync(new NotFoundByKeyException<Guid>(userId));
// Act
var result = await _service.GetUserGroupsAsync(userId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count(), Is.EqualTo(0));
}
}
@@ -52,6 +52,7 @@ public static class ConfigurationProgramExtensions
services.AddScoped<IMessageService, MessageService>();
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
services.AddScoped<IUserGroupsService, UserGroupsService>();
}
public static void AddRepositories(this IServiceCollection services)
+13 -12
View File
@@ -1,4 +1,5 @@
using Govor.API.Services;
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Messages;
using Govor.Application.Interfaces.Messages.Parameters;
using Govor.Contracts.Requests.SignalR;
@@ -14,6 +15,7 @@ public class ChatsHub : Hub
{
private readonly ILogger<ChatsHub> _logger;
private readonly IMessageService _messageService;
private readonly IUserGroupsService _userService;
public ChatsHub(ILogger<ChatsHub> logger, IMessageService messageService)
{
@@ -35,12 +37,11 @@ public class ChatsHub : Hub
await Groups.AddToGroupAsync(Context.ConnectionId, userId.ToString());
_logger.LogInformation("User {UserId} connected with ConnectionId {ConnectionId} and added to their group", userId, Context.ConnectionId);
// TODO: Add user to their chat groups - this might require fetching user's groups from a service
// var userGroups = await _userService.GetUserGroupsAsync(userId);
// foreach (var group in userGroups)
// {
// await Groups.AddToGroupAsync(Context.ConnectionId, $"group_{group.Id}");
// }
var userGroups = await _userService.GetUserGroupsAsync(userId);
foreach (var group in userGroups)
{
await Groups.AddToGroupAsync(Context.ConnectionId, $"group_{group.Id}");
}
await base.OnConnectedAsync();
}
@@ -54,12 +55,12 @@ public class ChatsHub : Hub
await Groups.RemoveFromGroupAsync(Context.ConnectionId, userId.ToString());
_logger.LogInformation("User {UserId} disconnected with ConnectionId {ConnectionId} and removed from their group", userId, Context.ConnectionId);
// TODO: Remove user from their chat groups
// var userGroups = await _userService.GetUserGroupsAsync(userId); // This might be problematic if the service relies on the user being connected
// foreach (var group in userGroups)
// {
// await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"group_{group.Id}");
// }
var userGroups = await _userService.GetUserGroupsAsync(userId); // This might be problematic if the service relies on the user being connected
foreach (var group in userGroups)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"group_{group.Id}");
}
}
else if (exception != null)
{
@@ -0,0 +1,8 @@
using Govor.Core.Models;
namespace Govor.Application.Interfaces;
public interface IUserGroupsService
{
Task<List<ChatGroup>> GetUserGroupsAsync(Guid userId);
}
@@ -0,0 +1,28 @@
using Govor.Application.Interfaces;
using Govor.Core.Models;
using Govor.Core.Repositories.Groups;
using Govor.Data.Repositories.Exceptions;
namespace Govor.Application.Services;
public class UserGroupsService : IUserGroupsService
{
private readonly IGroupsRepository _groupRep;
public UserGroupsService(IGroupsRepository groupsRepository)
{
_groupRep = groupsRepository;
}
public async Task<List<ChatGroup>> GetUserGroupsAsync(Guid userId)
{
try
{
return await _groupRep.GetByUserIdAsync(userId);
}
catch (NotFoundByKeyException<Guid> ex)
{
return new List<ChatGroup>();
}
}
}