Files
raceplanner/tests/integration/backend/AuthIntegrationTests.cs
T
Denis Urs Rudolph 0cdb391393 Fix integration test infrastructure
- Create CustomWebApplicationFactory to properly handle DI
- Fix IntegrationTestBase to use proper service scope pattern
- Update AuthIntegrationTests and EventsIntegrationTests to use CustomWebApplicationFactory
- Resolve EF Core provider conflict by using ConfigureWebHost override

Tests now run: 7 passed, 6 failed (auth/response format issues)
2026-04-09 21:11:03 +02:00

132 lines
3.8 KiB
C#

using System.Net;
using System.Net.Http.Json;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Testing;
using RacePlannerApi.DTOs;
using RacePlannerApi.Models;
using Xunit;
namespace backend.Tests.Integration;
public class AuthIntegrationTests : IntegrationTestBase
{
public AuthIntegrationTests(CustomWebApplicationFactory factory) : base(factory) { }
[Fact]
public async Task Register_WithValidData_ReturnsSuccess()
{
// Arrange
var request = new RegisterRequest
{
Email = "test@example.com",
Password = "SecurePass123!",
Name = "Test User",
Role = UserRole.Participant
};
// Act
var response = await _client.PostAsJsonAsync("/api/auth/register", request);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var result = await response.Content.ReadFromJsonAsync<AuthResponse>();
result.Should().NotBeNull();
result!.Token.Should().NotBeNullOrEmpty();
result.User.Email.Should().Be(request.Email);
}
[Fact]
public async Task Register_WithDuplicateEmail_ReturnsConflict()
{
// Arrange
var request = new RegisterRequest
{
Email = "duplicate@example.com",
Password = "SecurePass123!",
Name = "Test User",
Role = UserRole.Participant
};
// Register first user
await _client.PostAsJsonAsync("/api/auth/register", request);
// Act - Try to register again with same email
var response = await _client.PostAsJsonAsync("/api/auth/register", request);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.Conflict);
}
[Fact]
public async Task Login_WithValidCredentials_ReturnsToken()
{
// Arrange
var registerRequest = new RegisterRequest
{
Email = "login@example.com",
Password = "SecurePass123!",
Name = "Test User",
Role = UserRole.Participant
};
await _client.PostAsJsonAsync("/api/auth/register", registerRequest);
var loginRequest = new LoginRequest
{
Email = "login@example.com",
Password = "SecurePass123!"
};
// Act
var response = await _client.PostAsJsonAsync("/api/auth/login", loginRequest);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var result = await response.Content.ReadFromJsonAsync<AuthResponse>();
result.Should().NotBeNull();
result!.Token.Should().NotBeNullOrEmpty();
}
[Fact]
public async Task Login_WithInvalidCredentials_ReturnsUnauthorized()
{
// Arrange
var loginRequest = new LoginRequest
{
Email = "nonexistent@example.com",
Password = "WrongPassword123!"
};
// Act
var response = await _client.PostAsJsonAsync("/api/auth/login", loginRequest);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
}
[Fact]
public async Task Login_WithIncorrectPassword_ReturnsUnauthorized()
{
// Arrange
var registerRequest = new RegisterRequest
{
Email = "wrongpass@example.com",
Password = "CorrectPass123!",
Name = "Test User",
Role = UserRole.Participant
};
await _client.PostAsJsonAsync("/api/auth/register", registerRequest);
var loginRequest = new LoginRequest
{
Email = "wrongpass@example.com",
Password = "WrongPass123!"
};
// Act
var response = await _client.PostAsJsonAsync("/api/auth/login", loginRequest);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
}
}