2026-07-17 06:18:56 +00:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Heimdall.IntegrationTests.Factories;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.IntegrationTests.Controllers;
|
|
|
|
|
|
2026-07-21 10:24:11 +00:00
|
|
|
public class UsersControllerTests : PostgresTestBase
|
2026-07-17 06:18:56 +00:00
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
public UsersControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
|
2026-07-17 06:18:56 +00:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ListUsers_WithAdminAuth_ReturnsOk()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
2026-07-17 06:18:56 +00:00
|
|
|
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()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
2026-07-17 06:18:56 +00:00
|
|
|
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()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
2026-07-17 06:18:56 +00:00
|
|
|
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()
|
|
|
|
|
{
|
2026-07-21 10:24:11 +00:00
|
|
|
var client = await Factory.CreateAdminClientAsync();
|
2026-07-17 06:18:56 +00:00
|
|
|
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();
|
|
|
|
|
}
|
2026-11-04 13:33:30 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Last_unlocked_admin_cannot_be_locked_when_another_admin_is_already_locked()
|
|
|
|
|
{
|
|
|
|
|
var admin = await Factory.CreateAdminClientAsync();
|
|
|
|
|
var list = await admin.GetFromJsonAsync<JsonElement>("api/users?page=1&pageSize=20");
|
|
|
|
|
var firstAdminId = list.GetProperty("items").EnumerateArray()
|
|
|
|
|
.Single(user => user.GetProperty("roles").EnumerateArray().Any(role => role.GetString() == "Admin"))
|
|
|
|
|
.GetProperty("id").GetGuid();
|
|
|
|
|
|
|
|
|
|
var created = await admin.PostAsJsonAsync("api/users", new
|
|
|
|
|
{
|
|
|
|
|
email = $"extra-admin-{Guid.NewGuid():N}@heimdall.test",
|
|
|
|
|
password = "ExtraAdmin123!",
|
|
|
|
|
displayName = "Extra Admin",
|
|
|
|
|
roles = new[] { "Admin" }
|
|
|
|
|
});
|
|
|
|
|
created.EnsureSuccessStatusCode();
|
|
|
|
|
var secondAdminId = (await created.Content.ReadFromJsonAsync<JsonElement>())
|
|
|
|
|
.GetProperty("id").GetGuid();
|
|
|
|
|
(await admin.PostAsync($"api/users/{secondAdminId}/lock", null)).StatusCode
|
|
|
|
|
.ShouldBe(System.Net.HttpStatusCode.OK);
|
|
|
|
|
|
|
|
|
|
var lockLast = await admin.PostAsync($"api/users/{firstAdminId}/lock", null);
|
|
|
|
|
lockLast.StatusCode.ShouldBe(System.Net.HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
2026-07-17 06:18:56 +00:00
|
|
|
}
|