mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
6d1c53beeb
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
208 lines
7.0 KiB
C#
208 lines
7.0 KiB
C#
using AutoFixture;
|
|
using AutoMapper;
|
|
using Govor.API.Controllers.Friends;
|
|
using Govor.Application.Interfaces.Friends;
|
|
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
|
using Govor.Contracts.DTOs;
|
|
using Govor.Domain.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
|
|
namespace Govor.API.Tests.IntegrationTests.Controllers;
|
|
|
|
[TestFixture]
|
|
public class FriendsRequestQueryControllerTests
|
|
{
|
|
private Fixture _fixture;
|
|
private Mock<ILogger<FriendsRequestQueryController>> _loggerMock;
|
|
private Mock<IFriendRequestQueryService> _friendsServiceMock;
|
|
private Mock<ICurrentUserService> _currentUserServiceMock;
|
|
private Mock<IMapper> _mapperMock;
|
|
private FriendsRequestQueryController _controller;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_fixture = new Fixture();
|
|
_fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => _fixture.Behaviors.Remove(b));
|
|
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
|
|
|
|
_loggerMock = new Mock<ILogger<FriendsRequestQueryController>>();
|
|
_friendsServiceMock = new Mock<IFriendRequestQueryService>();
|
|
_currentUserServiceMock = new Mock<ICurrentUserService>();
|
|
_mapperMock = new Mock<IMapper>();
|
|
|
|
_controller = new FriendsRequestQueryController(
|
|
_loggerMock.Object,
|
|
_friendsServiceMock.Object,
|
|
_currentUserServiceMock.Object,
|
|
_mapperMock.Object
|
|
);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetIncomingRequests_ValidRequest_ReturnsOkResult()
|
|
{
|
|
// Arrange
|
|
var currentId = _fixture.Create<Guid>();
|
|
var friendships = _fixture.CreateMany<Friendship>().ToList();
|
|
var dtos = friendships.Select(f => new FriendshipDto()
|
|
{
|
|
AddresseeId = f.AddresseeId,
|
|
RequesterId = f.RequesterId
|
|
}).ToList();
|
|
|
|
_currentUserServiceMock.Setup(c => c.GetCurrentUserId())
|
|
.Returns(currentId);
|
|
|
|
_friendsServiceMock.Setup(f => f.GetIncomingAsync(currentId))
|
|
.ReturnsAsync(friendships);
|
|
|
|
_mapperMock.Setup(f => f.Map<List<FriendshipDto>>(friendships)).Returns(dtos);
|
|
|
|
// Act
|
|
var result = await _controller.GetIncomingRequests();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.InstanceOf<OkObjectResult>());
|
|
var okResult = result as OkObjectResult;
|
|
List<FriendshipDto> value = okResult.Value as List<FriendshipDto>;
|
|
|
|
Assert.That(value, Is.Not.Null);
|
|
Assert.That(value.Count, Is.EqualTo(friendships.Count));
|
|
Assert.That(value.Select(f => f.AddresseeId), Is.EquivalentTo(friendships.Select(f => f.AddresseeId)));
|
|
Assert.That(value.Select(f => f.RequesterId), Is.EquivalentTo(friendships.Select(f => f.RequesterId)));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetIncomingRequests_Throws_InvalidOperationException_ReturnsBadRequest()
|
|
{
|
|
// Arrange
|
|
var currentId = _fixture.Create<Guid>();
|
|
|
|
_currentUserServiceMock.Setup(c => c.GetCurrentUserId())
|
|
.Returns(currentId);
|
|
|
|
_friendsServiceMock.Setup(f => f.GetIncomingAsync(currentId))
|
|
.ThrowsAsync(new InvalidOperationException());
|
|
|
|
// Act
|
|
var result = await _controller.GetIncomingRequests();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.InstanceOf<OkObjectResult>());
|
|
var okResult = result as OkObjectResult;
|
|
List<FriendshipDto> value = okResult.Value as List<FriendshipDto>;
|
|
|
|
Assert.That(value, Is.Not.Null);
|
|
Assert.That(value.Count, Is.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetIncomingRequest_StatusCode500_IfThrowsSomeException()
|
|
{
|
|
// Arrange
|
|
var currentId = _fixture.Create<Guid>();
|
|
|
|
_currentUserServiceMock.Setup(c => c.GetCurrentUserId())
|
|
.Returns(currentId);
|
|
|
|
_friendsServiceMock.Setup(f => f.GetIncomingAsync(currentId))
|
|
.ThrowsAsync(new Exception());
|
|
|
|
// Act
|
|
var result = await _controller.GetIncomingRequests();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.InstanceOf<ObjectResult>());
|
|
var objectResult = result as ObjectResult;
|
|
Assert.That(objectResult.StatusCode, Is.EqualTo(500));
|
|
}
|
|
|
|
// Test for GetResponses action
|
|
[Test]
|
|
public async Task GetResponses_ValidRequest_ReturnsOkResult()
|
|
{
|
|
// Arrange
|
|
var currentId = _fixture.Create<Guid>();
|
|
var friendships = _fixture.CreateMany<Friendship>().ToList();
|
|
var dtos = friendships.Select(f => new FriendshipDto()
|
|
{
|
|
AddresseeId = f.AddresseeId,
|
|
RequesterId = f.RequesterId
|
|
}).ToList();
|
|
|
|
_currentUserServiceMock.Setup(c => c.GetCurrentUserId())
|
|
.Returns(currentId);
|
|
|
|
_friendsServiceMock.Setup(f => f.GetResponsesAsync(currentId))
|
|
.ReturnsAsync(friendships);
|
|
|
|
_mapperMock.Setup(f => f.Map<List<FriendshipDto>>(friendships)).Returns(dtos);
|
|
|
|
// Act
|
|
var result = await _controller.GetResponses();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.InstanceOf<OkObjectResult>());
|
|
var okResult = result as OkObjectResult;
|
|
List<FriendshipDto> value = okResult.Value as List<FriendshipDto>;
|
|
|
|
Assert.That(value, Is.Not.Null);
|
|
Assert.That(value.Count, Is.EqualTo(friendships.Count));
|
|
Assert.That(value.Select(f => f.AddresseeId), Is.EquivalentTo(friendships.Select(f => f.AddresseeId)));
|
|
Assert.That(value.Select(f => f.RequesterId), Is.EquivalentTo(friendships.Select(f => f.RequesterId)));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetResponses_Throws_InvalidOperationException_ReturnsEmptyList()
|
|
{
|
|
// Arrange
|
|
var currentId = _fixture.Create<Guid>();
|
|
|
|
_currentUserServiceMock.Setup(c => c.GetCurrentUserId())
|
|
.Returns(currentId);
|
|
|
|
_friendsServiceMock.Setup(f => f.GetResponsesAsync(currentId))
|
|
.ThrowsAsync(new InvalidOperationException());
|
|
|
|
// Act
|
|
var result = await _controller.GetResponses();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.InstanceOf<OkObjectResult>());
|
|
var okResult = result as OkObjectResult;
|
|
List<FriendshipDto> value = okResult.Value as List<FriendshipDto>;
|
|
|
|
Assert.That(value, Is.Not.Null);
|
|
Assert.That(value.Count, Is.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetResponses_StatusCode500_IfThrowsSomeException()
|
|
{
|
|
// Arrange
|
|
var currentId = _fixture.Create<Guid>();
|
|
|
|
_currentUserServiceMock.Setup(c => c.GetCurrentUserId())
|
|
.Returns(currentId);
|
|
|
|
_friendsServiceMock.Setup(f => f.GetResponsesAsync(currentId))
|
|
.ThrowsAsync(new Exception());
|
|
|
|
// Act
|
|
var result = await _controller.GetResponses();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.InstanceOf<ObjectResult>());
|
|
var objectResult = result as ObjectResult;
|
|
Assert.That(objectResult.StatusCode, Is.EqualTo(500));
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_controller?.Dispose();
|
|
}
|
|
} |