using Heimdall.Infrastructure.Security; using Shouldly; namespace Heimdall.Infrastructure.Tests; public class ApiKeyAuthenticationHandlerTests { [Fact] public void GenerateApiKey_ReturnsValidComponents() { var (plainKey, prefix, hash) = ApiKeyAuthenticationHandler.GenerateApiKey(); plainKey.ShouldNotBeNullOrEmpty(); plainKey.Length.ShouldBe(64); prefix.ShouldBe(plainKey[..8]); hash.ShouldNotBeNullOrEmpty(); hash.Length.ShouldBe(64); plainKey.ShouldMatch(@"^[0-9a-f]{64}$"); hash.ShouldMatch(@"^[0-9a-f]{64}$"); } [Fact] public void GenerateApiKey_ProducesUniqueKeys() { var (key1, _, _) = ApiKeyAuthenticationHandler.GenerateApiKey(); var (key2, _, _) = ApiKeyAuthenticationHandler.GenerateApiKey(); key1.ShouldNotBe(key2); } [Fact] public void GenerateApiKey_PrefixMatchesFirst8Chars() { var (plainKey, prefix, _) = ApiKeyAuthenticationHandler.GenerateApiKey(); prefix.ShouldBe(plainKey[..8]); } [Fact] public void GenerateApiKey_HashIsDeterministic() { var (plainKey, _, hash1) = ApiKeyAuthenticationHandler.GenerateApiKey(); hash1.ShouldMatch(@"^[0-9a-f]{64}$"); } }