using InvoiceMaster.IntegrationTests.Factories; using System.Net; using System.Net.Http.Json; using System.Text.Json; using Xunit; using FluentAssertions; namespace InvoiceMaster.IntegrationTests.Controllers; public class AuthControllerTests : IClassFixture { private readonly CustomWebApplicationFactory _factory; private readonly HttpClient _client; public AuthControllerTests(CustomWebApplicationFactory factory) { _factory = factory; _client = factory.CreateClient(); } [Fact] public async Task Register_WithValidData_ShouldReturnSuccess() { var request = new { email = $"test{Guid.NewGuid()}@example.com", password = "Password123!", fullName = "Test User" }; var response = await _client.PostAsJsonAsync("/api/v1/auth/register", request); var content = await response.Content.ReadAsStringAsync(); response.StatusCode.Should().Be(HttpStatusCode.OK); content.Should().Contain("success\""); content.Should().Contain("accessToken\""); } [Fact] public async Task Register_WithInvalidEmail_ShouldReturnBadRequest() { var request = new { email = "invalid-email", password = "Password123!" }; var response = await _client.PostAsJsonAsync("/api/v1/auth/register", request); response.StatusCode.Should().Be(HttpStatusCode.BadRequest); } [Fact] public async Task Login_WithValidCredentials_ShouldReturnTokens() { var email = $"login{Guid.NewGuid()}@example.com"; var password = "Password123!"; var registerRequest = new { email, password, fullName = "Test User" }; await _client.PostAsJsonAsync("/api/v1/auth/register", registerRequest); var loginRequest = new { email, password }; var response = await _client.PostAsJsonAsync("/api/v1/auth/login", loginRequest); var content = await response.Content.ReadAsStringAsync(); response.StatusCode.Should().Be(HttpStatusCode.OK); content.Should().Contain("accessToken\""); content.Should().Contain("refreshToken\""); } [Fact] public async Task Login_WithInvalidCredentials_ShouldReturnUnauthorized() { var request = new { email = "nonexistent@example.com", password = "WrongPassword" }; var response = await _client.PostAsJsonAsync("/api/v1/auth/login", request); response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); } [Fact] public async Task HealthCheck_ShouldReturnHealthy() { var response = await _client.GetAsync("/api/v1/health"); var content = await response.Content.ReadAsStringAsync(); response.StatusCode.Should().Be(HttpStatusCode.OK); content.Should().Contain("healthy"); } }