using ZB.MOM.WW.MxGateway.Server.Security.Authentication;

namespace ZB.MOM.WW.MxGateway.Tests.Security.Authentication;

public sealed class ApiKeyParserTests
{
    /// <summary>Verifies that TryParseAuthorizationHeader parses a valid Bearer token and returns the key ID and secret.</summary>
    [Fact]
    public void TryParseAuthorizationHeader_ValidBearerToken_ReturnsKeyIdAndSecret()
    {
        ApiKeyParser parser = new();

        bool parsed = parser.TryParseAuthorizationHeader(
            "Bearer mxgw_operator01_secret_value",
            out ParsedApiKey? apiKey);

        Assert.True(parsed);
        Assert.NotNull(apiKey);
        Assert.Equal("operator01", apiKey.KeyId);
        Assert.Equal("secret_value", apiKey.Secret);
    }

    /// <summary>Verifies that TryParseAuthorizationHeader returns false for malformed tokens.</summary>
    /// <param name="authorizationHeader">Malformed authorization header value.</param>
    [Theory]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("mxgw_operator01_secret")]
    [InlineData("Bearer not-a-gateway-key")]
    [InlineData("Bearer mxgw__secret")]
    [InlineData("Bearer mxgw_operator01_")]
    public void TryParseAuthorizationHeader_MalformedToken_ReturnsFalse(string? authorizationHeader)
    {
        ApiKeyParser parser = new();

        bool parsed = parser.TryParseAuthorizationHeader(authorizationHeader, out ParsedApiKey? apiKey);

        Assert.False(parsed);
        Assert.Null(apiKey);
    }
}
