91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
using System.Net.Http.Json;
|
|||
|
|
using Heimdall.IntegrationTests.Factories;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
||
|
|
|
||
|
|
public class AuthControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||
|
|
{
|
||
|
|
private readonly HttpClient _client;
|
||
|
|
|
||
|
|
public AuthControllerTests(HeimdallWebApplicationFactory factory)
|
||
|
|
{
|
||
|
|
_client = factory.CreateClient();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Register_ValidRequest_ReturnsOk()
|
||
|
|
{
|
||
|
|
var response = await _client.PostAsJsonAsync("api/auth/register", new
|
||
|
|
{
|
||
|
|
email = $"test-{Guid.NewGuid()}@heimdall.test",
|
||
|
|
password = "TestPassword123!",
|
||
|
|
displayName = "Test User"
|
||
|
|
});
|
||
|
|
|
||
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Register_DuplicateEmail_ReturnsConflict()
|
||
|
|
{
|
||
|
|
var email = $"dup-{Guid.NewGuid()}@heimdall.test";
|
||
|
|
|
||
|
|
var first = await _client.PostAsJsonAsync("api/auth/register", new
|
||
|
|
{
|
||
|
|
email,
|
||
|
|
password = "TestPassword123!",
|
||
|
|
displayName = "User 1"
|
||
|
|
});
|
||
|
|
first.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||
|
|
|
||
|
|
var second = await _client.PostAsJsonAsync("api/auth/register", new
|
||
|
|
{
|
||
|
|
email,
|
||
|
|
password = "TestPassword123!",
|
||
|
|
displayName = "User 2"
|
||
|
|
});
|
||
|
|
|
||
|
|
second.StatusCode.ShouldBe(System.Net.HttpStatusCode.Conflict);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Login_ValidCredentials_ReturnsOk()
|
||
|
|
{
|
||
|
|
var email = $"login-{Guid.NewGuid()}@heimdall.test";
|
||
|
|
await _client.PostAsJsonAsync("api/auth/register", new
|
||
|
|
{
|
||
|
|
email,
|
||
|
|
password = "TestPassword123!",
|
||
|
|
displayName = "Login User"
|
||
|
|
});
|
||
|
|
|
||
|
|
var response = await _client.PostAsJsonAsync("api/auth/login", new
|
||
|
|
{
|
||
|
|
email,
|
||
|
|
password = "TestPassword123!"
|
||
|
|
});
|
||
|
|
|
||
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Login_InvalidPassword_ReturnsUnauthorized()
|
||
|
|
{
|
||
|
|
var email = $"badpwd-{Guid.NewGuid()}@heimdall.test";
|
||
|
|
await _client.PostAsJsonAsync("api/auth/register", new
|
||
|
|
{
|
||
|
|
email,
|
||
|
|
password = "TestPassword123!",
|
||
|
|
displayName = "Bad Pwd User"
|
||
|
|
});
|
||
|
|
|
||
|
|
var response = await _client.PostAsJsonAsync("api/auth/login", new
|
||
|
|
{
|
||
|
|
email,
|
||
|
|
password = "WrongPassword!"
|
||
|
|
});
|
||
|
|
|
||
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
||
|
|
}
|
||
|
|
}
|