318 lines
11 KiB
C#
318 lines
11 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.AdminApi;
|
||
using XiangYi.Application.DTOs.Requests;
|
||
using XiangYi.Application.DTOs.Responses;
|
||
using XiangYi.Application.Interfaces;
|
||
|
||
namespace XiangYi.Api.Tests.AdminApi;
|
||
|
||
/// <summary>
|
||
/// 后台弹窗配置管理控制器集成测<E68890>?
|
||
/// </summary>
|
||
public class AdminPopupControllerIntegrationTests : IClassFixture<WebApplicationFactory<IAdminApiMarker>>
|
||
{
|
||
private readonly WebApplicationFactory<IAdminApiMarker> _factory;
|
||
private readonly IAdminPopupService _mockAdminPopupService;
|
||
|
||
public AdminPopupControllerIntegrationTests(WebApplicationFactory<IAdminApiMarker> factory)
|
||
{
|
||
_mockAdminPopupService = Substitute.For<IAdminPopupService>();
|
||
|
||
_factory = factory.WithWebHostBuilder(builder =>
|
||
{
|
||
builder.UseEnvironment("Testing");
|
||
builder.ConfigureServices(services =>
|
||
{
|
||
services.RemoveAll<IAdminPopupService>();
|
||
services.AddSingleton(_mockAdminPopupService);
|
||
|
||
services.AddAuthentication(options =>
|
||
{
|
||
options.DefaultAuthenticateScheme = AdminTestAuthHandler.AuthenticationScheme;
|
||
options.DefaultChallengeScheme = AdminTestAuthHandler.AuthenticationScheme;
|
||
})
|
||
.AddScheme<AuthenticationSchemeOptions, AdminTestAuthHandler>(
|
||
AdminTestAuthHandler.AuthenticationScheme, options => { });
|
||
});
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试获取弹窗配置列表 - 未授权返<E69D83>?01
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task GetPopupList_WithoutAuth_ReturnsUnauthorized()
|
||
{
|
||
var client = _factory.CreateClient();
|
||
var response = await client.GetAsync("/api/admin/popups");
|
||
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试获取弹窗配置列表 - 授权后成<E5908E>?
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task GetPopupList_WithAuth_ReturnsSuccess()
|
||
{
|
||
var expectedResult = new PagedResult<AdminPopupDto>
|
||
{
|
||
Items = new List<AdminPopupDto>
|
||
{
|
||
new AdminPopupDto
|
||
{
|
||
Id = 1,
|
||
PopupType = 1,
|
||
PopupTypeName = "每日首次",
|
||
Title = "欢迎弹窗",
|
||
ImageUrl = "https://example.com/popup.jpg",
|
||
Status = 1,
|
||
StatusName = "启用"
|
||
}
|
||
},
|
||
Total = 1,
|
||
PageIndex = 1,
|
||
PageSize = 10
|
||
};
|
||
|
||
_mockAdminPopupService.GetPopupListAsync(Arg.Any<AdminPopupQueryRequest>())
|
||
.Returns(Task.FromResult(expectedResult));
|
||
|
||
var client = _factory.CreateClient();
|
||
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
||
|
||
var response = await client.GetAsync("/api/admin/popups");
|
||
|
||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||
|
||
var content = await response.Content.ReadAsStringAsync();
|
||
var result = JsonSerializer.Deserialize<ApiResponse<PagedResult<AdminPopupDto>>>(content,
|
||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||
|
||
Assert.NotNull(result);
|
||
Assert.Equal(0, result.Code);
|
||
Assert.NotNull(result.Data);
|
||
Assert.Single(result.Data.Items);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试获取弹窗配置详情 - 成功
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task GetPopupDetail_WithAuth_ReturnsSuccess()
|
||
{
|
||
var expectedResult = new AdminPopupDto
|
||
{
|
||
Id = 1,
|
||
PopupType = 1,
|
||
PopupTypeName = "每日首次",
|
||
Title = "欢迎弹窗",
|
||
ImageUrl = "https://example.com/popup.jpg",
|
||
LinkUrl = "/pages/member/index",
|
||
ButtonText = "立即查看",
|
||
Status = 1,
|
||
StatusName = "启用"
|
||
};
|
||
|
||
_mockAdminPopupService.GetPopupByIdAsync(1)
|
||
.Returns(Task.FromResult(expectedResult));
|
||
|
||
var client = _factory.CreateClient();
|
||
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
||
|
||
var response = await client.GetAsync("/api/admin/popups/1");
|
||
|
||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||
|
||
var content = await response.Content.ReadAsStringAsync();
|
||
var result = JsonSerializer.Deserialize<ApiResponse<AdminPopupDto>>(content,
|
||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||
|
||
Assert.NotNull(result);
|
||
Assert.Equal(0, result.Code);
|
||
Assert.NotNull(result.Data);
|
||
Assert.Equal(expectedResult.Id, result.Data.Id);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 测试根据类型获取弹窗配置 - 成功
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task GetPopupByType_WithAuth_ReturnsSuccess()
|
||
{
|
||
var expectedResult = new AdminPopupDto
|
||
{
|
||
Id = 1,
|
||
PopupType = 1,
|
||
PopupTypeName = "每日首次",
|
||
Title = "欢迎弹窗",
|
||
ImageUrl = "https://example.com/popup.jpg",
|
||
Status = 1,
|
||
StatusName = "启用"
|
||
};
|
||
|
||
_mockAdminPopupService.GetPopupByTypeAsync(1)
|
||
.Returns(Task.FromResult<AdminPopupDto?>(expectedResult));
|
||
|
||
var client = _factory.CreateClient();
|
||
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
||
|
||
var response = await client.GetAsync("/api/admin/popups/type/1");
|
||
|
||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||
|
||
var content = await response.Content.ReadAsStringAsync();
|
||
var result = JsonSerializer.Deserialize<ApiResponse<AdminPopupDto>>(content,
|
||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||
|
||
Assert.NotNull(result);
|
||
Assert.Equal(0, result.Code);
|
||
Assert.NotNull(result.Data);
|
||
Assert.Equal(1, result.Data.PopupType);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试更新弹窗配置 - 成功
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task UpdatePopup_WithAuth_ReturnsSuccess()
|
||
{
|
||
_mockAdminPopupService.UpdatePopupAsync(1, Arg.Any<UpdatePopupRequest>(), 1)
|
||
.Returns(Task.FromResult(true));
|
||
|
||
var client = _factory.CreateClient();
|
||
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
||
|
||
var request = new UpdatePopupRequest
|
||
{
|
||
Title = "更新后的弹窗",
|
||
ImageUrl = "https://example.com/updated-popup.jpg",
|
||
LinkUrl = "/pages/member/index",
|
||
ButtonText = "立即查看",
|
||
Status = 1
|
||
};
|
||
|
||
var response = await client.PutAsJsonAsync("/api/admin/popups/1", request);
|
||
|
||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||
|
||
var content = await response.Content.ReadAsStringAsync();
|
||
var result = JsonSerializer.Deserialize<ApiResponse>(content,
|
||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||
|
||
Assert.NotNull(result);
|
||
Assert.Equal(0, result.Code);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试根据类型更新弹窗配置 - 成功
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task UpdatePopupByType_WithAuth_ReturnsSuccess()
|
||
{
|
||
_mockAdminPopupService.UpdatePopupByTypeAsync(1, Arg.Any<UpdatePopupRequest>(), 1)
|
||
.Returns(Task.FromResult(100L));
|
||
|
||
var client = _factory.CreateClient();
|
||
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
||
|
||
var request = new UpdatePopupRequest
|
||
{
|
||
Title = "每日首次弹窗",
|
||
ImageUrl = "https://example.com/popup.jpg",
|
||
Status = 1
|
||
};
|
||
|
||
var response = await client.PutAsJsonAsync("/api/admin/popups/type/1", request);
|
||
|
||
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>
|
||
/// 测试更新弹窗状<E7AA97>?- 成功
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task UpdatePopupStatus_WithAuth_ReturnsSuccess()
|
||
{
|
||
_mockAdminPopupService.UpdatePopupStatusAsync(1, 2, 1)
|
||
.Returns(Task.FromResult(true));
|
||
|
||
var client = _factory.CreateClient();
|
||
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
||
|
||
var response = await client.PutAsync("/api/admin/popups/1/status/2", null);
|
||
|
||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||
|
||
var content = await response.Content.ReadAsStringAsync();
|
||
var result = JsonSerializer.Deserialize<ApiResponse>(content,
|
||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||
|
||
Assert.NotNull(result);
|
||
Assert.Equal(0, result.Code);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试弹窗配置流程 - 完整流程测试
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task PopupConfiguration_CompleteFlow_Success()
|
||
{
|
||
var popupId = 100L;
|
||
|
||
_mockAdminPopupService.UpdatePopupByTypeAsync(1, Arg.Any<UpdatePopupRequest>(), 1)
|
||
.Returns(Task.FromResult(popupId));
|
||
|
||
_mockAdminPopupService.GetPopupByIdAsync(popupId)
|
||
.Returns(Task.FromResult(new AdminPopupDto
|
||
{
|
||
Id = popupId,
|
||
PopupType = 1,
|
||
PopupTypeName = "每日首次",
|
||
Title = "测试弹窗",
|
||
Status = 1
|
||
}));
|
||
|
||
_mockAdminPopupService.UpdatePopupStatusAsync(popupId, 2, 1)
|
||
.Returns(Task.FromResult(true));
|
||
|
||
var client = _factory.CreateClient();
|
||
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
||
|
||
// Create/Update by type
|
||
var createRequest = new UpdatePopupRequest
|
||
{
|
||
Title = "测试弹窗",
|
||
ImageUrl = "https://example.com/popup.jpg",
|
||
Status = 1
|
||
};
|
||
var createResponse = await client.PutAsJsonAsync("/api/admin/popups/type/1", createRequest);
|
||
Assert.Equal(HttpStatusCode.OK, createResponse.StatusCode);
|
||
|
||
// Read
|
||
var readResponse = await client.GetAsync($"/api/admin/popups/{popupId}");
|
||
Assert.Equal(HttpStatusCode.OK, readResponse.StatusCode);
|
||
|
||
// Update status (disable)
|
||
var statusResponse = await client.PutAsync($"/api/admin/popups/{popupId}/status/2", null);
|
||
Assert.Equal(HttpStatusCode.OK, statusResponse.StatusCode);
|
||
}
|
||
}
|