130 lines
3.7 KiB
C#
130 lines
3.7 KiB
C#
|
|
using System.Net;
|
||
|
|
using System.Net.Http.Json;
|
||
|
|
using FluentAssertions;
|
||
|
|
using RacePlannerApi.DTOs;
|
||
|
|
using Xunit;
|
||
|
|
|
||
|
|
namespace backend.Tests.Integration;
|
||
|
|
|
||
|
|
public class AuthIntegrationTests : IntegrationTestBase
|
||
|
|
{
|
||
|
|
public AuthIntegrationTests(WebApplicationFactory<Program> 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);
|
||
|
|
}
|
||
|
|
}
|