Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/MediatR/Licensing/LicenseAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ private License Initialize()
}

var key = _configuration?.LicenseKey
?? Mediator.LicenseKey
?? null;

if (key == null)
?? Mediator.LicenseKey;

if (string.IsNullOrWhiteSpace(key))
{
return new License();
}

var licenseClaims = ValidateKey(key);
var licenseClaims = ValidateKey(key!);
return licenseClaims.Any()
? new License(new ClaimsPrincipal(new ClaimsIdentity(licenseClaims)))
: new License();
Expand Down
59 changes: 59 additions & 0 deletions test/MediatR.Tests/Licensing/LicenseAccessorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using MediatR.Licensing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Testing;
using Shouldly;
using Xunit;

namespace MediatR.Tests.Licensing;

public class LicenseAccessorTests
{
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("\t")]
public void Should_return_unconfigured_license_for_missing_or_blank_key(string? key)
{
var factory = new LoggerFactory();
var provider = new FakeLoggerProvider();
factory.AddProvider(provider);

var config = new MediatRServiceConfiguration { LicenseKey = key };
var accessor = new LicenseAccessor(config, factory);

var license = accessor.Current;

license.IsConfigured.ShouldBeFalse();

var logMessages = provider.Collector.GetSnapshot();
logMessages.ShouldNotContain(log => log.Level == LogLevel.Critical);
}

[Fact]
public void Should_return_unconfigured_license_when_static_key_is_blank()
{
var factory = new LoggerFactory();
var provider = new FakeLoggerProvider();
factory.AddProvider(provider);

var original = Mediator.LicenseKey;
try
{
Mediator.LicenseKey = " ";
var accessor = new LicenseAccessor(factory);

var license = accessor.Current;

license.IsConfigured.ShouldBeFalse();

var logMessages = provider.Collector.GetSnapshot();
logMessages.ShouldNotContain(log => log.Level == LogLevel.Critical);
}
finally
{
Mediator.LicenseKey = original;
}
}
}
Loading