using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using FluentAssertions; using Xunit; using System.Net; using System.Net.Http.Json; using Fengling.AuthService.ViewModels; namespace Fengling.AuthService.Tests; public class AccountIntegrationTests : IClassFixture> { private readonly WebApplicationFactory _factory; private readonly HttpClient _client; public AccountIntegrationTests(WebApplicationFactory factory) { _factory = factory.WithWebHostBuilder(builder => { builder.UseEnvironment("Testing"); }); _client = factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); } [Fact] public async Task Login_Get_ReturnsLoginPage() { var response = await _client.GetAsync("/account/login"); response.StatusCode.Should().Be(HttpStatusCode.OK); var content = await response.Content.ReadAsStringAsync(); content.Should().Contain("欢迎回来"); content.Should().Contain("登录到 Fengling Auth"); content.Should().Contain("用户名"); content.Should().Contain("密码"); } [Fact] public async Task Login_Get_WithReturnUrl_ReturnsLoginPageWithReturnUrl() { var response = await _client.GetAsync("/account/login?returnUrl=/dashboard"); response.StatusCode.Should().Be(HttpStatusCode.OK); var content = await response.Content.ReadAsStringAsync(); content.Should().Contain("value=\"/dashboard\""); } [Fact] public async Task Register_Get_ReturnsRegisterPage() { var response = await _client.GetAsync("/account/register"); response.StatusCode.Should().Be(HttpStatusCode.OK); var content = await response.Content.ReadAsStringAsync(); content.Should().Contain("创建账号"); content.Should().Contain("加入 Fengling Auth"); content.Should().Contain("用户名"); content.Should().Contain("邮箱"); content.Should().Contain("密码"); content.Should().Contain("确认密码"); } [Fact] public async Task Register_Get_WithReturnUrl_ReturnsRegisterPageWithReturnUrl() { var response = await _client.GetAsync("/account/register?returnUrl=/dashboard"); response.StatusCode.Should().Be(HttpStatusCode.OK); var content = await response.Content.ReadAsStringAsync(); content.Should().Contain("value=\"/dashboard\""); } [Fact] public async Task Dashboard_Index_ReturnsLoginPageWhenNotAuthenticated() { var response = await _client.GetAsync("/dashboard"); response.StatusCode.Should().Be(HttpStatusCode.Redirect); var location = response.Headers.Location.ToString(); location.Should().StartWith("/account/login"); location.Should().Contain("dashboard"); } [Fact] public async Task Dashboard_Profile_ReturnsLoginPageWhenNotAuthenticated() { var response = await _client.GetAsync("/dashboard/profile"); response.StatusCode.Should().Be(HttpStatusCode.Redirect); var location = response.Headers.Location.ToString(); location.Should().StartWith("/account/login"); location.Should().Contain("dashboard%2Fprofile"); } [Fact] public async Task Dashboard_Settings_ReturnsLoginPageWhenNotAuthenticated() { var response = await _client.GetAsync("/dashboard/settings"); response.StatusCode.Should().Be(HttpStatusCode.Redirect); var location = response.Headers.Location.ToString(); location.Should().StartWith("/account/login"); location.Should().Contain("dashboard%2Fsettings"); } }