Files
Heimdall/tests/Heimdall.IntegrationTests/Controllers/UsersControllerTests.cs
T
Arnaud Dartois 6d5ec0c201 feat(mvp): cameras API, Docker production, tests, security, Makefile
- Cameras API: CRUD with service/controller/DTOs + Blazor page
- Docker: production-grade compose with healthchecks, .env, Makefile, networks
- Healthchecks: /healthz (liveness) and /readyz (readiness) endpoints
- Testing: 13 integration tests (Auth, Sites, Users, Cameras controllers)
  - Users: create, list, lock, reset-password
  - Cameras: create with site, list by site
- Environment: .env.example, docker-compose.override.yml for dev
- Infrastructure: ICameraService registered in DI
2026-07-17 06:18:56 +00:00

92 lines
3.3 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 UsersControllerTests : IClassFixture<HeimdallWebApplicationFactory>
{
private readonly HeimdallWebApplicationFactory _factory;
public UsersControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
[Fact]
public async Task ListUsers_WithAdminAuth_ReturnsOk()
{
var client = await CreateAdminClient();
var response = await client.GetAsync("api/users?page=1&pageSize=20");
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
}
[Fact]
public async Task CreateUser_WithAdminAuth_ReturnsOk()
{
var client = await CreateAdminClient();
var response = await client.PostAsJsonAsync("api/users", new
{
email = $"newuser-{Guid.NewGuid()}@heimdall.test",
password = "NewUserPwd123!",
displayName = "New User",
roles = new[] { "Viewer" }
});
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
}
[Fact]
public async Task LockUser_WithAdminAuth_ReturnsOk()
{
var client = await CreateAdminClient();
var createResponse = await client.PostAsJsonAsync("api/users", new
{
email = $"lockuser-{Guid.NewGuid()}@heimdall.test",
password = "LockUserPwd123!",
displayName = "Lock Test User",
roles = new[] { "Viewer" }
});
var content = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
var userId = content.GetProperty("id").GetGuid();
var lockResponse = await client.PostAsync($"api/users/{userId}/lock", null);
lockResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
}
[Fact]
public async Task ResetPassword_WithAdminAuth_ReturnsTemporaryPassword()
{
var client = await CreateAdminClient();
var createResponse = await client.PostAsJsonAsync("api/users", new
{
email = $"resetpwd-{Guid.NewGuid()}@heimdall.test",
password = "ResetPwd123!",
displayName = "Reset Pwd User",
roles = new[] { "Viewer" }
});
var content = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
var userId = content.GetProperty("id").GetGuid();
var resetResponse = await client.PostAsync($"api/users/{userId}/reset-password", null);
resetResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
var resetContent = await resetResponse.Content.ReadFromJsonAsync<JsonElement>();
resetContent.GetProperty("temporaryPassword").GetString().ShouldNotBeNullOrEmpty();
}
private async Task<HttpClient> CreateAdminClient()
{
var client = _factory.CreateClient();
var loginResponse = await client.PostAsJsonAsync("api/auth/login", new
{
email = "testadmin@heimdall.test",
password = "TestAdmin123!"
});
var content = await loginResponse.Content.ReadFromJsonAsync<AuthTestResponse>();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", content!.AccessToken);
return client;
}
}