Files
Heimdall/tests/Heimdall.IntegrationTests/Controllers/PersonsControllerTests.cs
T

129 lines
4.7 KiB
C#
Raw Normal View History

using System.Net.Http.Json;
using System.Text.Json;
using Heimdall.IntegrationTests.Factories;
using Shouldly;
namespace Heimdall.IntegrationTests.Controllers;
public class PersonsControllerTests : PostgresTestBase
{
public PersonsControllerTests(PostgreSqlContainerFixture postgres) : base(postgres) { }
[Fact]
public async Task GetAll_WithoutAuth_ReturnsUnauthorized()
{
var client = Factory.CreateClient();
var response = await client.GetAsync("api/persons");
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
}
[Fact]
public async Task GetAll_WithAdminAuth_ReturnsOk()
{
var client = await Factory.CreateAdminClientAsync();
var response = await client.GetAsync("api/persons");
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
content.ValueKind.ShouldBe(System.Text.Json.JsonValueKind.Array);
}
[Fact]
public async Task Create_WithoutAuth_ReturnsUnauthorized()
{
var client = Factory.CreateClient();
var response = await client.PostAsJsonAsync("api/persons", new
{
displayName = "Test Person",
category = "employee"
});
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
}
[Fact]
public async Task Create_WithAdminAuth_ReturnsOk()
{
var client = await Factory.CreateAdminClientAsync();
var response = await client.PostAsJsonAsync("api/persons", new
{
displayName = $"Person {Guid.NewGuid():N}",
category = "employee"
});
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
}
[Fact]
public async Task Create_GetById_ReturnsPerson()
{
var client = await Factory.CreateAdminClientAsync();
var createResponse = await client.PostAsJsonAsync("api/persons", new
{
displayName = $"Get Person {Guid.NewGuid():N}",
category = "visitor"
});
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
var personId = createContent.GetProperty("id").GetGuid();
var getResponse = await client.GetAsync($"api/persons/{personId}");
getResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
var getContent = await getResponse.Content.ReadFromJsonAsync<JsonElement>();
getContent.GetProperty("id").GetGuid().ShouldBe(personId);
}
[Fact]
public async Task GetById_NonExistent_ReturnsNotFound()
{
var client = await Factory.CreateAdminClientAsync();
var response = await client.GetAsync($"api/persons/{Guid.NewGuid()}");
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound);
}
[Fact]
public async Task Update_WithAdminAuth_ReturnsOk()
{
var client = await Factory.CreateAdminClientAsync();
var createResponse = await client.PostAsJsonAsync("api/persons", new
{
displayName = $"Update Person {Guid.NewGuid():N}",
category = "employee"
});
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
var personId = createContent.GetProperty("id").GetGuid();
var updateResponse = await client.PutAsJsonAsync($"api/persons/{personId}", new
{
displayName = $"Updated Person {Guid.NewGuid():N}",
category = "employee",
isWatchlisted = false
});
updateResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
}
[Fact]
public async Task Delete_WithAdminAuth_ReturnsOk()
{
var client = await Factory.CreateAdminClientAsync();
var createResponse = await client.PostAsJsonAsync("api/persons", new
{
displayName = $"Delete Person {Guid.NewGuid():N}",
category = "employee"
});
var createContent = await createResponse.Content.ReadFromJsonAsync<JsonElement>();
var personId = createContent.GetProperty("id").GetGuid();
var deleteResponse = await client.DeleteAsync($"api/persons/{personId}");
deleteResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
}
[Fact]
public async Task GetAll_WithCategoryFilter_ReturnsOk()
{
var client = await Factory.CreateAdminClientAsync();
var response = await client.GetAsync("api/persons?category=employee");
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
var content = await response.Content.ReadFromJsonAsync<JsonElement>();
content.ValueKind.ShouldBe(System.Text.Json.JsonValueKind.Array);
}
}