228 lines
7.5 KiB
C#
228 lines
7.5 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>
|
|
/// 后台认证控制器集成测试
|
|
/// </summary>
|
|
public class AdminAuthControllerIntegrationTests : IClassFixture<WebApplicationFactory<IAdminApiMarker>>
|
|
{
|
|
private readonly WebApplicationFactory<IAdminApiMarker> _factory;
|
|
private readonly IAdminAuthService _mockAdminAuthService;
|
|
|
|
public AdminAuthControllerIntegrationTests(WebApplicationFactory<IAdminApiMarker> factory)
|
|
{
|
|
_mockAdminAuthService = Substitute.For<IAdminAuthService>();
|
|
|
|
_factory = factory.WithWebHostBuilder(builder =>
|
|
{
|
|
builder.UseEnvironment("Testing");
|
|
builder.ConfigureServices(services =>
|
|
{
|
|
services.RemoveAll<IAdminAuthService>();
|
|
services.AddSingleton(_mockAdminAuthService);
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = AdminTestAuthHandler.AuthenticationScheme;
|
|
})
|
|
.AddScheme<AuthenticationSchemeOptions, AdminTestAuthHandler>(
|
|
AdminTestAuthHandler.AuthenticationScheme, options => { });
|
|
});
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试管理员登录 - 成功场景
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Login_WithValidCredentials_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResponse = new AdminLoginResponse
|
|
{
|
|
Token = "admin-jwt-token-123",
|
|
AdminId = 1,
|
|
Username = "admin",
|
|
RealName = "管理员",
|
|
Roles = new List<string> { "admin" },
|
|
Permissions = new List<string> { "user:list", "user:view" }
|
|
};
|
|
|
|
_mockAdminAuthService.LoginAsync("admin", "password123", Arg.Any<string?>())
|
|
.Returns(Task.FromResult(expectedResponse));
|
|
|
|
var client = _factory.CreateClient();
|
|
var request = new AdminLoginRequest
|
|
{
|
|
Username = "admin",
|
|
Password = "password123"
|
|
};
|
|
|
|
// Act
|
|
var response = await client.PostAsJsonAsync("/api/admin/auth/login", request);
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminLoginResponse>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResponse.AdminId, result.Data.AdminId);
|
|
Assert.Equal(expectedResponse.Username, result.Data.Username);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取管理员信息 - 未授权返回401
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetInfo_WithoutAuth_ReturnsUnauthorized()
|
|
{
|
|
// Arrange
|
|
var client = _factory.CreateClient();
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/auth/info");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取管理员信息 - 授权后成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetInfo_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedResponse = new AdminInfoResponse
|
|
{
|
|
AdminId = 1,
|
|
Username = "admin",
|
|
RealName = "管理员",
|
|
Roles = new List<AdminRoleDto>
|
|
{
|
|
new AdminRoleDto { RoleId = 1, RoleName = "超级管理员", RoleCode = "admin" }
|
|
},
|
|
Permissions = new List<string> { "user:list", "user:view" }
|
|
};
|
|
|
|
_mockAdminAuthService.GetAdminInfoAsync(1)
|
|
.Returns(Task.FromResult(expectedResponse));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/auth/info");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<AdminInfoResponse>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Equal(expectedResponse.AdminId, result.Data.AdminId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试获取菜单权限 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task GetMenus_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
var expectedMenus = new List<AdminMenuResponse>
|
|
{
|
|
new AdminMenuResponse
|
|
{
|
|
MenuId = 1,
|
|
MenuName = "用户管理",
|
|
MenuType = 1,
|
|
Path = "/users",
|
|
Children = new List<AdminMenuResponse>
|
|
{
|
|
new AdminMenuResponse
|
|
{
|
|
MenuId = 2,
|
|
ParentId = 1,
|
|
MenuName = "用户列表",
|
|
MenuType = 2,
|
|
Path = "/users/list"
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
_mockAdminAuthService.GetAdminMenusAsync(1)
|
|
.Returns(Task.FromResult(expectedMenus));
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
// Act
|
|
var response = await client.GetAsync("/api/admin/auth/menus");
|
|
|
|
// Assert
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<ApiResponse<List<AdminMenuResponse>>>(content,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(0, result.Code);
|
|
Assert.NotNull(result.Data);
|
|
Assert.Single(result.Data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 测试退出登录 - 成功
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Logout_WithAuth_ReturnsSuccess()
|
|
{
|
|
// Arrange
|
|
_mockAdminAuthService.LogoutAsync(1)
|
|
.Returns(Task.CompletedTask);
|
|
|
|
var client = _factory.CreateClient();
|
|
client.DefaultRequestHeaders.Add("Authorization", "Bearer admin-token-1");
|
|
|
|
// Act
|
|
var response = await client.PostAsync("/api/admin/auth/logout", null);
|
|
|
|
// Assert
|
|
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);
|
|
}
|
|
}
|