mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
IUserGroupsService + tests
This commit is contained in:
@@ -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<IMessageService, MessageService>();
|
||||||
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
|
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
|
||||||
|
services.AddScoped<IUserGroupsService, UserGroupsService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddRepositories(this IServiceCollection services)
|
public static void AddRepositories(this IServiceCollection services)
|
||||||
|
|||||||
+14
-13
@@ -1,4 +1,5 @@
|
|||||||
using Govor.API.Services;
|
using Govor.API.Services;
|
||||||
|
using Govor.Application.Interfaces;
|
||||||
using Govor.Application.Interfaces.Messages;
|
using Govor.Application.Interfaces.Messages;
|
||||||
using Govor.Application.Interfaces.Messages.Parameters;
|
using Govor.Application.Interfaces.Messages.Parameters;
|
||||||
using Govor.Contracts.Requests.SignalR;
|
using Govor.Contracts.Requests.SignalR;
|
||||||
@@ -14,6 +15,7 @@ public class ChatsHub : Hub
|
|||||||
{
|
{
|
||||||
private readonly ILogger<ChatsHub> _logger;
|
private readonly ILogger<ChatsHub> _logger;
|
||||||
private readonly IMessageService _messageService;
|
private readonly IMessageService _messageService;
|
||||||
|
private readonly IUserGroupsService _userService;
|
||||||
|
|
||||||
public ChatsHub(ILogger<ChatsHub> logger, IMessageService messageService)
|
public ChatsHub(ILogger<ChatsHub> logger, IMessageService messageService)
|
||||||
{
|
{
|
||||||
@@ -34,13 +36,12 @@ public class ChatsHub : Hub
|
|||||||
// Add user to their own group (for private messages and notifications)
|
// Add user to their own group (for private messages and notifications)
|
||||||
await Groups.AddToGroupAsync(Context.ConnectionId, userId.ToString());
|
await Groups.AddToGroupAsync(Context.ConnectionId, userId.ToString());
|
||||||
_logger.LogInformation("User {UserId} connected with ConnectionId {ConnectionId} and added to their group", userId, Context.ConnectionId);
|
_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);
|
||||||
// var userGroups = await _userService.GetUserGroupsAsync(userId);
|
foreach (var group in userGroups)
|
||||||
// foreach (var group in userGroups)
|
{
|
||||||
// {
|
await Groups.AddToGroupAsync(Context.ConnectionId, $"group_{group.Id}");
|
||||||
// await Groups.AddToGroupAsync(Context.ConnectionId, $"group_{group.Id}");
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
await base.OnConnectedAsync();
|
await base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
@@ -54,12 +55,12 @@ public class ChatsHub : Hub
|
|||||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, userId.ToString());
|
await Groups.RemoveFromGroupAsync(Context.ConnectionId, userId.ToString());
|
||||||
_logger.LogInformation("User {UserId} disconnected with ConnectionId {ConnectionId} and removed from their group", userId, Context.ConnectionId);
|
_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
|
var userGroups = await _userService.GetUserGroupsAsync(userId); // This might be problematic if the service relies on the user being connected
|
||||||
// foreach (var group in userGroups)
|
foreach (var group in userGroups)
|
||||||
// {
|
{
|
||||||
// await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"group_{group.Id}");
|
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"group_{group.Id}");
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
else if (exception != null)
|
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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user