Skip to content

Commit cb5990f

Browse files
authored
Merge pull request #189 from simplify9/hamza/fix/rebex-license-key-config
refactor: move Rebex license key into BitweenOptions
2 parents 204552e + 1bfb2b2 commit cb5990f

5 files changed

Lines changed: 28 additions & 11 deletions

File tree

SW.Bitween.Api/Services/BitweenOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public BitweenOptions()
6767
public string RabbitMqManagementUsername { get; set; }
6868
public string RabbitMqManagementPassword { get; set; }
6969

70+
/// <summary>
71+
/// License key for the Rebex POP3 library. When not set, the native Rebex POP3 receiver adapter is not registered.
72+
/// </summary>
73+
public string RebexLicenseKey { get; set; }
74+
7075
/// <summary>
7176
/// Allowed CORS origins for credential-bearing requests (cookies).
7277
/// When set, enables AllowCredentials() on the CORS policy.

SW.Bitween.NativeAdapters/RebexPop3Receiver/NativeRebexPop3Receiver.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SW.Bitween.NativeAdapters.RebexPop3Receiver;
66

77
public class NativeRebexPop3Receiver : INativeInfolinkReceiver
88
{
9-
public const string LicenseKeyEnvironmentVariable = "REBEX_LICENSE_KEY";
9+
private readonly string? _licenseKey;
1010

1111
private RebexPop3ReceiverInput _options = new();
1212
private Pop3 _pop3 = new();
@@ -16,9 +16,14 @@ public class NativeRebexPop3Receiver : INativeInfolinkReceiver
1616
internal int Port { get; set; } = 995;
1717
internal bool UseSsl { get; set; } = true;
1818

19+
public NativeRebexPop3Receiver(string? licenseKey = null)
20+
{
21+
_licenseKey = licenseKey;
22+
}
23+
1924
public async Task Initialize()
2025
{
21-
Rebex.Licensing.Key = Environment.GetEnvironmentVariable(LicenseKeyEnvironmentVariable);
26+
Rebex.Licensing.Key = _licenseKey;
2227
_pop3 = new Pop3();
2328
var sslMode = UseSsl ? SslMode.Implicit : SslMode.None;
2429
await _pop3.ConnectAsync(_options.Host, Port, sslMode);

SW.Bitween.NativeAdapters/ServiceCollectionExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace SW.Bitween.NativeAdapters;
77

88
public static class ServiceCollectionExtensions
99
{
10-
public static void AddNativeAdapters(this IServiceCollection serviceCollection)
10+
public static void AddNativeAdapters(this IServiceCollection serviceCollection, string? rebexLicenseKey = null)
1111
{
1212
serviceCollection.ConfigureHttpClientDefaults(builder =>
1313
{
@@ -36,10 +36,10 @@ public static void AddNativeAdapters(this IServiceCollection serviceCollection)
3636
serviceCollection.AddScoped<INativeInfolinkReceiver, NativePop3Receiver>();
3737
serviceCollection.AddScoped<INativeAdapter, NativePop3Receiver>();
3838

39-
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(NativeRebexPop3Receiver.LicenseKeyEnvironmentVariable)))
39+
if (!string.IsNullOrEmpty(rebexLicenseKey))
4040
{
41-
serviceCollection.AddScoped<INativeInfolinkReceiver, NativeRebexPop3Receiver>();
42-
serviceCollection.AddScoped<INativeAdapter, NativeRebexPop3Receiver>();
41+
serviceCollection.AddScoped<INativeInfolinkReceiver>(_ => new NativeRebexPop3Receiver(rebexLicenseKey));
42+
serviceCollection.AddScoped<INativeAdapter>(_ => new NativeRebexPop3Receiver(rebexLicenseKey));
4343
}
4444
}
4545
}

SW.Bitween.UnitTests/NativeRebexPop3ReceiverTests.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,31 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Configuration;
67
using Microsoft.VisualStudio.TestTools.UnitTesting;
78
using SW.Bitween.NativeAdapters.RebexPop3Receiver;
89

910
namespace SW.Bitween.UnitTests;
1011

1112
/// <summary>
12-
/// Requires a real Rebex license via the REBEX_LICENSE_KEY environment variable.
13+
/// Requires a real Rebex license via the Bitween:RebexLicenseKey configuration value
14+
/// (settable through the Bitween__RebexLicenseKey environment variable).
1315
/// Tests report Inconclusive (not Failed) when it's not set, so CI/dev machines
1416
/// without a license don't fail the build.
1517
/// </summary>
1618
[TestClass]
1719
public class NativeRebexPop3ReceiverTests
1820
{
21+
private static readonly string LicenseKey = new ConfigurationBuilder()
22+
.AddEnvironmentVariables()
23+
.Build()
24+
.GetSection(BitweenOptions.ConfigurationSection)[nameof(BitweenOptions.RebexLicenseKey)];
25+
1926
[TestInitialize]
2027
public void SkipIfNoLicense()
2128
{
22-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable(NativeRebexPop3Receiver.LicenseKeyEnvironmentVariable)))
23-
Assert.Inconclusive($"{NativeRebexPop3Receiver.LicenseKeyEnvironmentVariable} is not set.");
29+
if (string.IsNullOrEmpty(LicenseKey))
30+
Assert.Inconclusive($"{BitweenOptions.ConfigurationSection}:{nameof(BitweenOptions.RebexLicenseKey)} is not set.");
2431
}
2532

2633
private static Dictionary<string, string> Settings(string encoding = "utf8", int batchSize = 50) => new()
@@ -34,7 +41,7 @@ public void SkipIfNoLicense()
3441

3542
private static NativeRebexPop3Receiver CreateReceiver(FakePop3Server server, string encoding = "utf8", int batchSize = 50)
3643
{
37-
var receiver = new NativeRebexPop3Receiver { Port = server.Port, UseSsl = false };
44+
var receiver = new NativeRebexPop3Receiver(LicenseKey) { Port = server.Port, UseSsl = false };
3845
receiver.InitializeStartupValues(Settings(encoding, batchSize));
3946
return receiver;
4047
}

SW.Bitween.Web/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void ConfigureServices(IServiceCollection services)
289289
});
290290
});
291291

292-
services.AddNativeAdapters();
292+
services.AddNativeAdapters(bitweenOptions.RebexLicenseKey);
293293

294294
// services.AddScoped<INativeInfolinkHandler, NativeUpdatePartnerPropsHandler>();
295295
// services.AddScoped<INativeAdapter, NativeUpdatePartnerPropsHandler>();

0 commit comments

Comments
 (0)