118 lines
4.4 KiB
C#
118 lines
4.4 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 AgentsControllerTests : IClassFixture<HeimdallWebApplicationFactory>
|
||
|
|
{
|
||
|
|
private readonly HeimdallWebApplicationFactory _factory;
|
||
|
|
|
||
|
|
public AgentsControllerTests(HeimdallWebApplicationFactory factory) => _factory = factory;
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task RegisterAgent_WithValidToken_ReturnsApiKey()
|
||
|
|
{
|
||
|
|
var client = await CreateAdminClient();
|
||
|
|
|
||
|
|
// Create a site
|
||
|
|
var siteResponse = await client.PostAsJsonAsync("api/sites", new
|
||
|
|
{
|
||
|
|
name = "Agent Test Site",
|
||
|
|
timezone = "UTC"
|
||
|
|
});
|
||
|
|
var siteContent = await siteResponse.Content.ReadFromJsonAsync<JsonElement>();
|
||
|
|
var siteId = siteContent.GetProperty("id").GetGuid();
|
||
|
|
|
||
|
|
// Generate a registration token
|
||
|
|
var tokenResponse = await client.PostAsync($"api/sites/{siteId}/tokens", null);
|
||
|
|
var tokenContent = await tokenResponse.Content.ReadFromJsonAsync<JsonElement>();
|
||
|
|
var token = tokenContent.GetProperty("token").GetString()!;
|
||
|
|
|
||
|
|
// Register an agent with the token
|
||
|
|
var registerResponse = await client.PostAsJsonAsync("api/agents/register", new
|
||
|
|
{
|
||
|
|
token,
|
||
|
|
hostname = "agent-test-host",
|
||
|
|
version = "1.0.0-test"
|
||
|
|
});
|
||
|
|
|
||
|
|
registerResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||
|
|
|
||
|
|
var agentContent = await registerResponse.Content.ReadFromJsonAsync<JsonElement>();
|
||
|
|
agentContent.GetProperty("agentId").GetGuid().ShouldNotBe(Guid.Empty);
|
||
|
|
agentContent.GetProperty("apiKey").GetString().ShouldNotBeNullOrEmpty();
|
||
|
|
agentContent.GetProperty("apiKeyPrefix").GetString().ShouldNotBeNullOrEmpty();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task AgentHeartbeat_WithApiKey_ReturnsOk()
|
||
|
|
{
|
||
|
|
var client = await CreateAdminClient();
|
||
|
|
|
||
|
|
// Create site + token + register agent (same flow as above)
|
||
|
|
var siteResponse = await client.PostAsJsonAsync("api/sites", new
|
||
|
|
{
|
||
|
|
name = "Heartbeat Test Site",
|
||
|
|
timezone = "UTC"
|
||
|
|
});
|
||
|
|
var siteContent = await siteResponse.Content.ReadFromJsonAsync<JsonElement>();
|
||
|
|
var siteId = siteContent.GetProperty("id").GetGuid();
|
||
|
|
|
||
|
|
var tokenResponse = await client.PostAsync($"api/sites/{siteId}/tokens", null);
|
||
|
|
var tokenContent = await tokenResponse.Content.ReadFromJsonAsync<JsonElement>();
|
||
|
|
var token = tokenContent.GetProperty("token").GetString()!;
|
||
|
|
|
||
|
|
var registerResponse = await client.PostAsJsonAsync("api/agents/register", new
|
||
|
|
{
|
||
|
|
token,
|
||
|
|
hostname = "heartbeat-test-host",
|
||
|
|
version = "1.0.0-test"
|
||
|
|
});
|
||
|
|
var agentContent = await registerResponse.Content.ReadFromJsonAsync<JsonElement>();
|
||
|
|
var agentId = agentContent.GetProperty("agentId").GetGuid();
|
||
|
|
var apiKey = agentContent.GetProperty("apiKey").GetString()!;
|
||
|
|
|
||
|
|
// Send heartbeat with API key auth
|
||
|
|
var heartbeatClient = _factory.CreateClient();
|
||
|
|
heartbeatClient.DefaultRequestHeaders.Authorization =
|
||
|
|
new AuthenticationHeaderValue("Bearer", apiKey);
|
||
|
|
|
||
|
|
var heartbeatResponse = await heartbeatClient.PostAsJsonAsync(
|
||
|
|
$"api/agents/{agentId}/heartbeat",
|
||
|
|
new { publicIp = "1.2.3.4" });
|
||
|
|
|
||
|
|
heartbeatResponse.StatusCode.ShouldBe(System.Net.HttpStatusCode.OK);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task RegisterAgent_WithInvalidToken_ReturnsUnauthorized()
|
||
|
|
{
|
||
|
|
var client = _factory.CreateClient();
|
||
|
|
|
||
|
|
var response = await client.PostAsJsonAsync("api/agents/register", new
|
||
|
|
{
|
||
|
|
token = "invalid-token-that-does-not-exist",
|
||
|
|
hostname = "bad-agent",
|
||
|
|
version = "1.0.0"
|
||
|
|
});
|
||
|
|
|
||
|
|
response.StatusCode.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|