74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Heimdall.IntegrationTests.Factories;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
public class SitesControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
|
{
|
|
private readonly HeimdallWebApplicationFactory _factory;
|
|
private static readonly JsonSerializerOptions Json = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
};
|
|
|
|
public SitesControllerTests(HeimdallWebApplicationFactory factory)
|
|
{
|
|
_factory = factory;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSites_WithoutAuth_ReturnsUnauthorized()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
var response = await client.GetAsync("api/sites?page=1&pageSize=10");
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateSite_WithAuth_ReturnsOk()
|
|
{
|
|
var client = await CreateAuthenticatedAdminClient();
|
|
|
|
var response = await client.PostAsJsonAsync("api/sites", new
|
|
{
|
|
name = "Site Test",
|
|
address = "Paris",
|
|
timezone = "Europe/Paris"
|
|
});
|
|
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListSites_WithAuth_ReturnsOk()
|
|
{
|
|
var client = await CreateAuthenticatedAdminClient();
|
|
|
|
var response = await client.GetAsync("api/sites?page=1&pageSize=10");
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
}
|
|
|
|
private async Task<HttpClient> CreateAuthenticatedAdminClient()
|
|
{
|
|
var client = _factory.CreateClient();
|
|
|
|
// Login with seeded admin user
|
|
var loginResponse = await client.PostAsJsonAsync("api/auth/login", new
|
|
{
|
|
email = "testadmin@heimdall.test",
|
|
password = "TestAdmin123!"
|
|
});
|
|
|
|
var content = await loginResponse.Content
|
|
.ReadFromJsonAsync<AuthTestResponse>(Json);
|
|
|
|
client.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", content!.AccessToken);
|
|
|
|
return client;
|
|
}
|
|
}
|