275 lines
9.3 KiB
C#
275 lines
9.3 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
using XiangYi.AppApi;
|
|
using XiangYi.Application.DTOs.Requests;
|
|
using XiangYi.Application.DTOs.Responses;
|
|
using XiangYi.Application.Interfaces;
|
|
using XiangYi.AppApi.Controllers;
|
|
|
|
namespace XiangYi.Api.Tests.AppApi;
|
|
|
|
/// <summary>
|
|
/// 互动控制器集成测试
|
|
/// </summary>
|
|
public class InteractControllerIntegrationTests : IClassFixture<WebApplicationFactory<IAppApiMarker>>
|
|
{
|
|
private readonly WebApplicationFactory<IAppApiMarker> _factory;
|
|
private readonly IInteractService _mockInteractService;
|
|
private readonly IReportService _mockReportService;
|
|
|
|
public InteractControllerIntegrationTests(WebApplicationFactory<IAppApiMarker> factory)
|
|
{
|
|
_mockInteractService = Substitute.For<IInteractService>();
|
|
_mockReportService = Substitute.For<IReportService>();
|
|
|
|
_factory = factory.WithWebHostBuilder(builder =>
|
|
{
|
|
builder.UseEnvironment("Testing");
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
services.RemoveAll<IInteractService>();
|
|
services.RemoveAll<IReportService>();
|
|
services.AddSingleton(_mockInteractService);
|
|
services.AddSingleton(_mockReportService);
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = TestAuthHandler.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = TestAuthHandler.AuthenticationScheme;
|
|
})
|
|
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
|
|
TestAuthHandler.AuthenticationScheme, options => { });
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试记录浏览 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task RecordView_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
_mockInteractService.RecordViewAsync(1, 2)
|
|
.Returns(Task.FromResult(100L));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer test-token-1");
|
|
|
|
var request = new ViewRequest { TargetUserId = 2 };
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/app/interact/view", request);
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<long>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.Equal(100L, result.Data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试收藏 - 首次收藏成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Favorite_FirstTime_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
_mockInteractService.IsFavoritedAsync(1, 2)
|
|
.Returns(Task.FromResult(false));
|
|
_mockInteractService.FavoriteAsync(1, 2)
|
|
.Returns(Task.FromResult(100L));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer test-token-1");
|
|
|
|
var request = new FavoriteRequest { TargetUserId = 2 };
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/app/interact/favorite", request);
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<FavoriteResponse>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.True(result.Data.IsFavorited);
|
|
Assert.Equal(100L, result.Data.FavoriteId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试收藏 - 取消收藏
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Favorite_AlreadyFavorited_Unfavorites()
|
|
{
|
|
// Arrange
|
|
_mockInteractService.IsFavoritedAsync(1, 2)
|
|
.Returns(Task.FromResult(true));
|
|
_mockInteractService.UnfavoriteAsync(1, 2)
|
|
.Returns(Task.FromResult(true));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer test-token-1");
|
|
|
|
var request = new FavoriteRequest { TargetUserId = 2 };
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/app/interact/favorite", request);
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<FavoriteResponse>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.False(result.Data.IsFavorited);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试解锁联系方式 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Unlock_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResponse = new UnlockResponse
|
|
{
|
|
UnlockId = 100,
|
|
SessionId = 200,
|
|
RemainingContactCount = 4
|
|
};
|
|
|
|
_mockInteractService.UnlockAsync(1, 2)
|
|
.Returns(Task.FromResult(expectedResponse));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer test-token-1");
|
|
|
|
var request = new UnlockRequest { TargetUserId = 2 };
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/app/interact/unlock", request);
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<UnlockResponse>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResponse.SessionId, result.Data.SessionId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试举报 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Report_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResponse = new ReportResponse
|
|
{
|
|
ReportId = 100,
|
|
ReporterId = 1,
|
|
ReportedUserId = 2,
|
|
ReportType = 1,
|
|
Reason = "虚假信息",
|
|
Status = 0
|
|
};
|
|
|
|
_mockReportService.CreateReportAsync(1, Arg.Any<CreateReportRequest>())
|
|
.Returns(Task.FromResult(expectedResponse));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer test-token-1");
|
|
|
|
var request = new CreateReportRequest
|
|
{
|
|
ReportedUserId = 2,
|
|
ReportType = 1,
|
|
Reason = "虚假信息"
|
|
};
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/app/interact/report", request);
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<ReportResponse>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResponse.ReportId, result.Data.ReportId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取"看过我"列表 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetViewedMeList_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResult = new PagedResult<ViewRecordResponse>
|
|
{
|
|
Items = new List<ViewRecordResponse>
|
|
{
|
|
new ViewRecordResponse { UserId = 2, Nickname = "用户2", LastViewTime = DateTime.Now }
|
|
},
|
|
Total = 1,
|
|
PageIndex = 1,
|
|
PageSize = 20
|
|
};
|
|
|
|
_mockInteractService.GetViewedMeListAsync(1, 1, 20)
|
|
.Returns(Task.FromResult(expectedResult));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer test-token-1");
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/app/interact/viewedMe?pageIndex=1&pageSize=20");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<ViewRecordResponse>>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Single(result.Data.Items);
|
|
}
|
|
}
|