feat(AuthClientTests): Added GetpublicKey_ShouldReturnExpectedPublicKey to test GetPublicKeyAsync method of AuthHub

This commit is contained in:
Developer 02 2025-03-05 15:31:24 +01:00
parent 6664a1f342
commit 36891b5abb

View File

@ -1,6 +1,7 @@
using DigitalData.Auth.Abstractions;
using DigitalData.Auth.API.Hubs;
using DigitalData.Auth.Client;
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security;
using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.RSAKey;
@ -34,6 +35,17 @@ public class AuthClientTests
}
}
private readonly IEnumerable<RSATokenDescriptor> _tokenDescriptors =
[
new()
{
Issuer = "Foo",
Audience = "Bar",
Lifetime = new TimeSpan(1, 0, 0),
Content = Instance.RSAFactory.CreatePrivateKeyPem()
}
];
[SetUp]
public void Setup()
{
@ -55,7 +67,8 @@ public class AuthClientTests
builder.Services.AddCryptoFactory(new CryptoFactoryParams()
{
PemDirectory = "/",
Decryptors = [new RSADecryptor()]
Decryptors = [new RSADecryptor()],
TokenDescriptors = _tokenDescriptors
});
builder.Services.AddMemoryCache();
@ -149,4 +162,32 @@ public class AuthClientTests
Assert.That(rcv_key, Is.EqualTo(key));
});
}
[Test]
public async Task GetpublicKey_ShouldReturnExpectedPublicKey()
{
// Arrange
string? publicKey = null;
var provider = Build(opt =>
{
opt.Url = _hubUrl;
opt.Events.OnMessageReceived = (issuer, audience, key, logger) => publicKey = key;
});
var client = provider.GetRequiredService<IAuthClient>();
await client.StartAsync();
var expectedPublicKey = _tokenDescriptors.Get("Foo", "Bar").PublicKey.Content;
// Act
await client.GetPublicKeyAsync("Foo", "Bar");
// wait for network
await Task.Delay(2000);
// Assert
Assert.Multiple(() =>
{
Assert.That(publicKey, Is.Not.Null);
Assert.That(publicKey, Is.EqualTo(expectedPublicKey));
});
}
}