144 lines
3.8 KiB
C#
144 lines
3.8 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using FluentAssertions;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Xunit;
|
|
|
|
namespace SocialService.FunctionalTests.Controllers;
|
|
|
|
/// <summary>
|
|
/// EN: Functional tests for Blocks API endpoints.
|
|
/// VI: Functional tests cho các endpoints API Blocks.
|
|
/// </summary>
|
|
public class BlocksControllerTests : IClassFixture<CustomWebApplicationFactory>
|
|
{
|
|
private readonly HttpClient _client;
|
|
|
|
public BlocksControllerTests(CustomWebApplicationFactory factory)
|
|
{
|
|
_client = factory.CreateClient(new WebApplicationFactoryClientOptions
|
|
{
|
|
AllowAutoRedirect = false
|
|
});
|
|
}
|
|
|
|
#region Block User Tests
|
|
|
|
[Fact]
|
|
public async Task BlockUser_ValidRequest_ReturnsCreated()
|
|
{
|
|
// Arrange
|
|
var request = new
|
|
{
|
|
BlockerId = Guid.NewGuid(),
|
|
BlockedId = Guid.NewGuid(),
|
|
Reason = "Spam content"
|
|
};
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/v1/blocks", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
|
var content = await response.Content.ReadFromJsonAsync<BlockUserResponse>();
|
|
content!.BlockId.Should().NotBeEmpty();
|
|
content.Success.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BlockUser_SelfBlock_ReturnsBadRequest()
|
|
{
|
|
// Arrange
|
|
var userId = Guid.NewGuid();
|
|
var request = new
|
|
{
|
|
BlockerId = userId,
|
|
BlockedId = userId // Same user
|
|
};
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/v1/blocks", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BlockUser_WithoutReason_ReturnsCreated()
|
|
{
|
|
// Arrange
|
|
var request = new
|
|
{
|
|
BlockerId = Guid.NewGuid(),
|
|
BlockedId = Guid.NewGuid()
|
|
// No reason provided
|
|
};
|
|
|
|
// Act
|
|
var response = await _client.PostAsJsonAsync("/api/v1/blocks", request);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Unblock User Tests
|
|
|
|
[Fact]
|
|
public async Task UnblockUser_ValidRequest_ReturnsOk()
|
|
{
|
|
// Arrange - First create a block
|
|
var blockerId = Guid.NewGuid();
|
|
var blockedId = Guid.NewGuid();
|
|
var blockRequest = new { BlockerId = blockerId, BlockedId = blockedId };
|
|
await _client.PostAsJsonAsync("/api/v1/blocks", blockRequest);
|
|
|
|
// Act - Unblock
|
|
var unblockRequest = new HttpRequestMessage(HttpMethod.Delete, "/api/v1/blocks")
|
|
{
|
|
Content = JsonContent.Create(new { BlockerId = blockerId, BlockedId = blockedId })
|
|
};
|
|
var response = await _client.SendAsync(unblockRequest);
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Get Blocked Users Tests
|
|
|
|
[Fact]
|
|
public async Task GetBlockedUsers_ValidUser_ReturnsOkWithList()
|
|
{
|
|
// Arrange
|
|
var userId = Guid.NewGuid();
|
|
|
|
// Act
|
|
var response = await _client.GetAsync($"/api/v1/blocks/users/{userId}");
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetBlockedUsers_WithPagination_ReturnsOk()
|
|
{
|
|
// Arrange
|
|
var userId = Guid.NewGuid();
|
|
|
|
// Act
|
|
var response = await _client.GetAsync($"/api/v1/blocks/users/{userId}?skip=0&take=10");
|
|
|
|
// Assert
|
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
|
}
|
|
|
|
#endregion
|
|
|
|
// EN: Helper DTOs for deserialization
|
|
// VI: Helper DTOs để deserialize
|
|
private record BlockUserResponse(Guid BlockId, bool Success);
|
|
}
|