Files
Entkube/tests/EntKube.Clusters.Tests/Features/CleuraClientTests.cs
2026-05-13 14:01:32 +02:00

153 lines
5.3 KiB
C#

using EntKube.Clusters.Infrastructure.Cleura;
using FluentAssertions;
namespace EntKube.Clusters.Tests.Features;
public class CleuraClientTests
{
[Fact]
public void ParseAuthResponse_ValidJson_ReturnsToken()
{
// The Cleura API returns a JSON body with result and token fields
// after a successful authentication POST.
string json = """{"result": "login_ok", "token": "vahkie7EiDaij7chegaitee2zohsh1oh"}""";
CleuraAuthResponse? response = System.Text.Json.JsonSerializer.Deserialize<CleuraAuthResponse>(json);
response.Should().NotBeNull();
response!.Result.Should().Be("login_ok");
response.Token.Should().Be("vahkie7EiDaij7chegaitee2zohsh1oh");
}
[Fact]
public void ParseDomainsResponse_ValidJson_ReturnsRegions()
{
// The domains endpoint returns a list of domains, each with a region identifier.
string json = """
{
"domains": [
{"name": "my-domain", "region": "Sto2", "id": "abc123"},
{"name": "my-domain", "region": "Fra1", "id": "def456"}
]
}
""";
CleuraDomainsResponse? response = System.Text.Json.JsonSerializer.Deserialize<CleuraDomainsResponse>(json,
new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
response.Should().NotBeNull();
response!.Domains.Should().HaveCount(2);
response.Domains[0].Region.Should().Be("Sto2");
response.Domains[1].Region.Should().Be("Fra1");
}
[Fact]
public void BuildAuthRequest_FormatsCorrectly()
{
// Verify the authentication request body matches Cleura's expected format.
CleuraAuthRequest request = new("my-user", "my-pass");
string json = System.Text.Json.JsonSerializer.Serialize(request);
json.Should().Contain("\"auth\"");
json.Should().Contain("\"login\"");
json.Should().Contain("\"password\"");
json.Should().Contain("my-user");
}
[Fact]
public void BuildAuthRequest_WithTwofaMethod_IncludesTwofaMethod()
{
// When a 2FA method is specified, the auth body should include
// the twofa_method field so Cleura selects that method.
CleuraAuthRequest request = new("my-user", "my-pass", "sms");
string json = System.Text.Json.JsonSerializer.Serialize(request);
json.Should().Contain("\"twofa_method\"");
json.Should().Contain("sms");
}
[Fact]
public void BuildAuthRequest_WithoutTwofaMethod_OmitsTwofaMethod()
{
// Without a 2FA method, the twofa_method field should not be present.
CleuraAuthRequest request = new("my-user", "my-pass");
string json = System.Text.Json.JsonSerializer.Serialize(request);
json.Should().NotContain("twofa_method");
}
[Fact]
public void ParseAuthResponse_TwoFactorOptions_IndicatesNeed()
{
// When 2FA is enabled but no code is provided, Cleura responds with
// twofactor_options listing available methods (sms, webauthn).
string json = """{"result": "twofactor_options", "token": "", "options": ["sms", "webauthn"]}""";
CleuraAuthResponse? response = System.Text.Json.JsonSerializer.Deserialize<CleuraAuthResponse>(json);
response.Should().NotBeNull();
response!.Result.Should().Be("twofactor_options");
response.Options.Should().Contain("sms");
response.Options.Should().Contain("webauthn");
}
[Fact]
public void ParseAuthResponse_TwoFactorRequired_ReturnsVerification()
{
// When a 2FA method is selected, Cleura responds with twofactor_required
// and a verification token for subsequent steps.
string json = """{"result": "twofactor_required", "token": "", "verification": "xe9hik8q6r"}""";
CleuraAuthResponse? response = System.Text.Json.JsonSerializer.Deserialize<CleuraAuthResponse>(json);
response.Should().NotBeNull();
response!.Result.Should().Be("twofactor_required");
response.Verification.Should().Be("xe9hik8q6r");
}
[Fact]
public void BuildRequest2faCode_FormatsCorrectly()
{
// The request2facode endpoint expects a specific JSON structure
// with the login and verification token from the prior step.
CleuraRequest2faCodeRequest request = new("JohnDoe", "xe9hik8q6r");
string json = System.Text.Json.JsonSerializer.Serialize(request);
json.Should().Contain("\"request2fa\"");
json.Should().Contain("\"login\"");
json.Should().Contain("\"verification\"");
json.Should().Contain("JohnDoe");
json.Should().Contain("xe9hik8q6r");
}
[Fact]
public void BuildVerify2fa_FormatsCorrectly()
{
// The verify2fa endpoint expects the login, verification, and integer code.
CleuraVerify2faRequest request = new("JohnDoe", "xe9hik8q6r", 123456);
string json = System.Text.Json.JsonSerializer.Serialize(request);
json.Should().Contain("\"verify2fa\"");
json.Should().Contain("\"login\"");
json.Should().Contain("\"verification\"");
json.Should().Contain("\"code\"");
json.Should().Contain("JohnDoe");
json.Should().Contain("xe9hik8q6r");
json.Should().Contain("123456");
}
}