mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
was added firebase nitifying
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using Govor.API.Hubs.Infrastructure;
|
||||
|
||||
namespace Govor.API.Tests.IntegrationTests.Hubs.Infrastructure;
|
||||
|
||||
[TestFixture]
|
||||
public class ConnectionStoreTests
|
||||
{
|
||||
private ConnectionStore _store;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_store = new ConnectionStore();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddConnection_AddsConnection()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
var connectionId = "conn1";
|
||||
|
||||
// Act
|
||||
_store.AddConnection(userId, connectionId);
|
||||
|
||||
var connections = _store.GetConnections(userId);
|
||||
|
||||
// Assert
|
||||
Assert.That(connections, Contains.Item(connectionId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddConnection_DoesNotDuplicateConnection()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
var connectionId = "conn1";
|
||||
|
||||
// Act
|
||||
_store.AddConnection(userId, connectionId);
|
||||
_store.AddConnection(userId, connectionId);
|
||||
|
||||
var connections = _store.GetConnections(userId).ToList();
|
||||
|
||||
// Assert
|
||||
Assert.That(connections.Count, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveConnection_RemovesConnection()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
var connectionId = "conn1";
|
||||
|
||||
// Act
|
||||
_store.AddConnection(userId, connectionId);
|
||||
_store.RemoveConnection(userId, connectionId);
|
||||
|
||||
var connections = _store.GetConnections(userId);
|
||||
// Assert
|
||||
Assert.That(connections, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveConnection_LastConnection_RemovesUserEntry()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
var connectionId = "conn1";
|
||||
|
||||
// Act
|
||||
_store.AddConnection(userId, connectionId);
|
||||
_store.RemoveConnection(userId, connectionId);
|
||||
|
||||
var connections = _store.GetConnections(userId);
|
||||
|
||||
// Assert
|
||||
Assert.That(connections, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveConnection_NonExistingUser_DoesNotThrow()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
|
||||
// Act & Assert
|
||||
Assert.DoesNotThrow(() =>
|
||||
_store.RemoveConnection(userId, "conn1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetConnections_NonExistingUser_ReturnsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
var connections = _store.GetConnections(userId);
|
||||
|
||||
// Assert
|
||||
Assert.That(connections, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultipleUsers_IsolatedConnections()
|
||||
{
|
||||
// Arrange
|
||||
var userA = Guid.NewGuid();
|
||||
var userB = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
_store.AddConnection(userA, "A1");
|
||||
_store.AddConnection(userB, "B1");
|
||||
|
||||
var connectionsA = _store.GetConnections(userA);
|
||||
var connectionsB = _store.GetConnections(userB);
|
||||
|
||||
// Assert
|
||||
Assert.That(connectionsA, Contains.Item("A1"));
|
||||
Assert.That(connectionsB, Contains.Item("B1"));
|
||||
Assert.That(connectionsA, Does.Not.Contain("B1"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultipleConnections_ForSameUser_WorkCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var userId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
_store.AddConnection(userId, "conn1");
|
||||
_store.AddConnection(userId, "conn2");
|
||||
|
||||
var connections = _store.GetConnections(userId).ToList();
|
||||
|
||||
// Assert
|
||||
Assert.That(connections.Count, Is.EqualTo(2));
|
||||
Assert.That(connections, Contains.Item("conn1"));
|
||||
Assert.That(connections, Contains.Item("conn2"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
using Govor.API.Hubs;
|
||||
using Govor.API.Hubs.Infrastructure;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Core.Models;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.API.Tests.IntegrationTests.Hubs.Infrastructure;
|
||||
|
||||
[TestFixture]
|
||||
public class PrivateChatGroupManagerTests
|
||||
{
|
||||
private Mock<IConnectionStore> _connectionStoreMock;
|
||||
private Mock<IHubContext<ChatsHub>> _hubContextMock;
|
||||
private Mock<IGroupManager> _groupsMock;
|
||||
|
||||
private PrivateChatGroupManager _service;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_connectionStoreMock = new Mock<IConnectionStore>();
|
||||
|
||||
_groupsMock = new Mock<IGroupManager>();
|
||||
|
||||
_hubContextMock = new Mock<IHubContext<ChatsHub>>();
|
||||
_hubContextMock
|
||||
.SetupGet(h => h.Groups)
|
||||
.Returns(_groupsMock.Object);
|
||||
|
||||
_service = new PrivateChatGroupManager(
|
||||
_connectionStoreMock.Object,
|
||||
_hubContextMock.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddUsersToPrivateChatGroupAsync_AddsAllConnectionsToGroup()
|
||||
{
|
||||
// Arrange
|
||||
var chat = new PrivateChat
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserAId = Guid.NewGuid(),
|
||||
UserBId = Guid.NewGuid()
|
||||
};
|
||||
|
||||
var connectionsA = new List<string> { "connA1", "connA2" };
|
||||
var connectionsB = new List<string> { "connB1" };
|
||||
|
||||
_connectionStoreMock
|
||||
.Setup(s => s.GetConnections(chat.UserAId))
|
||||
.Returns(connectionsA);
|
||||
|
||||
_connectionStoreMock
|
||||
.Setup(s => s.GetConnections(chat.UserBId))
|
||||
.Returns(connectionsB);
|
||||
|
||||
var expectedGroup = ChatHubConstants.GetPrivateChat(chat.Id);
|
||||
|
||||
// Act
|
||||
await _service.AddUsersToPrivateChatGroupAsync(chat);
|
||||
|
||||
// Assert
|
||||
foreach (var conn in connectionsA.Concat(connectionsB))
|
||||
{
|
||||
_groupsMock.Verify(
|
||||
g => g.AddToGroupAsync(conn, expectedGroup, default),
|
||||
Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RemoveUsersFromPrivateChatGroupAsync_RemovesAllConnectionsFromGroup()
|
||||
{
|
||||
// Arrange
|
||||
var chat = new PrivateChat
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserAId = Guid.NewGuid(),
|
||||
UserBId = Guid.NewGuid()
|
||||
};
|
||||
|
||||
var connectionsA = new List<string> { "connA1" };
|
||||
var connectionsB = new List<string> { "connB1", "connB2" };
|
||||
|
||||
_connectionStoreMock
|
||||
.Setup(s => s.GetConnections(chat.UserAId))
|
||||
.Returns(connectionsA);
|
||||
|
||||
_connectionStoreMock
|
||||
.Setup(s => s.GetConnections(chat.UserBId))
|
||||
.Returns(connectionsB);
|
||||
|
||||
var expectedGroup = ChatHubConstants.GetPrivateChat(chat.Id);
|
||||
|
||||
// Act
|
||||
await _service.RemoveUsersFromPrivateChatGroupAsync(chat);
|
||||
|
||||
// Assert
|
||||
foreach (var conn in connectionsA.Concat(connectionsB))
|
||||
{
|
||||
_groupsMock.Verify(
|
||||
g => g.RemoveFromGroupAsync(conn, expectedGroup, default),
|
||||
Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddUsersToPrivateChatGroupAsync_NoConnections_DoesNothing()
|
||||
{
|
||||
// Arrange
|
||||
var chat = new PrivateChat
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserAId = Guid.NewGuid(),
|
||||
UserBId = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_connectionStoreMock
|
||||
.Setup(s => s.GetConnections(It.IsAny<Guid>()))
|
||||
.Returns(new List<string>());
|
||||
|
||||
// Act
|
||||
await _service.AddUsersToPrivateChatGroupAsync(chat);
|
||||
|
||||
// Assert
|
||||
_groupsMock.Verify(
|
||||
g => g.AddToGroupAsync(It.IsAny<string>(), It.IsAny<string>(), default),
|
||||
Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RemoveUsersFromPrivateChatGroupAsync_NoConnections_DoesNothing()
|
||||
{
|
||||
// Arrange
|
||||
var chat = new PrivateChat
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserAId = Guid.NewGuid(),
|
||||
UserBId = Guid.NewGuid()
|
||||
};
|
||||
|
||||
_connectionStoreMock
|
||||
.Setup(s => s.GetConnections(It.IsAny<Guid>()))
|
||||
.Returns(new List<string>());
|
||||
|
||||
// Act
|
||||
await _service.RemoveUsersFromPrivateChatGroupAsync(chat);
|
||||
|
||||
// Assert
|
||||
_groupsMock.Verify(
|
||||
g => g.RemoveFromGroupAsync(It.IsAny<string>(), It.IsAny<string>(), default),
|
||||
Times.Never);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user