61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
using FluentAssertions;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using SlpModularCms.Modules.Availability.Services;
|
|
|
|
namespace SlpModularCms.Modules.Availability.Tests.Services;
|
|
|
|
public class MasterApiKeyProtectorTests
|
|
{
|
|
private static MasterApiKeyProtector CreateProtector()
|
|
{
|
|
var provider = new EphemeralDataProtectionProvider();
|
|
return new MasterApiKeyProtector(provider);
|
|
}
|
|
|
|
[Fact]
|
|
public void Protect_Unprotect_RoundTrip_ReturnsOriginalValue()
|
|
{
|
|
var protector = CreateProtector();
|
|
var plain = "my-secret-api-key";
|
|
|
|
var encrypted = protector.Protect(plain);
|
|
var decrypted = protector.Unprotect(encrypted);
|
|
|
|
decrypted.Should().Be(plain);
|
|
}
|
|
|
|
[Fact]
|
|
public void Protect_ReturnsNonEmptyString_DifferentFromInput()
|
|
{
|
|
var protector = CreateProtector();
|
|
var plain = "my-secret-api-key";
|
|
|
|
var encrypted = protector.Protect(plain);
|
|
|
|
encrypted.Should().NotBeNullOrEmpty();
|
|
encrypted.Should().NotBe(plain);
|
|
}
|
|
|
|
[Fact]
|
|
public void Unprotect_ReturnsNull_WhenGivenInvalidCiphertext()
|
|
{
|
|
var protector = CreateProtector();
|
|
|
|
var result = protector.Unprotect("this-is-not-valid-ciphertext");
|
|
|
|
result.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void Unprotect_ReturnsNull_WhenProtectedWithDifferentProvider()
|
|
{
|
|
var protector1 = CreateProtector();
|
|
var protector2 = CreateProtector();
|
|
var encrypted = protector1.Protect("key");
|
|
|
|
var result = protector2.Unprotect(encrypted);
|
|
|
|
result.Should().BeNull();
|
|
}
|
|
}
|