Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ void IGlobalConfigurationExpression.Validator(Validator validator) =>
/// </summary>
int IGlobalConfigurationExpression.RecursiveQueriesMaxDepth { get; set; }

public string LicenseKey { get; set; }
private string _licenseKey;

public string LicenseKey
{
get => _licenseKey ?? Environment.GetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY");
set => _licenseKey = value;
}

public ServiceLifetime ServiceLifetime { get; set; } = ServiceLifetime.Transient;

Expand Down
208 changes: 208 additions & 0 deletions src/UnitTests/Licensing/LicenseKeyEnvironmentVariableTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using Microsoft.Extensions.DependencyInjection;

namespace AutoMapper.UnitTests.Licensing;

public class LicenseKeyEnvironmentVariableTests
{
private const string EnvVarName = "AUTOMAPPER_LICENSE_KEY";

#region Environment Variable Auto-Detection Tests

[Fact]
public void LicenseKey_ReadsFromEnvironmentVariable_WhenNotExplicitlySet()
{
const string expectedKey = "test-license-key-12345";
Environment.SetEnvironmentVariable(EnvVarName, expectedKey);

try
{
var config = new MapperConfigurationExpression();

config.LicenseKey.ShouldBe(expectedKey);
}
finally
{
Environment.SetEnvironmentVariable(EnvVarName, null);
}
}

[Fact]
public void LicenseKey_ReturnsNull_WhenEnvironmentVariableNotSet()
{
Environment.SetEnvironmentVariable(EnvVarName, null);

var config = new MapperConfigurationExpression();

config.LicenseKey.ShouldBeNull();
}

#endregion

#region Backward Compatibility - Old Way Tests

[Fact]
public void LicenseKey_SupportsOldWay_DirectAssignment()
{
const string licenseKey = "old-way-explicit-key";
var config = new MapperConfigurationExpression();

config.LicenseKey = licenseKey;

config.LicenseKey.ShouldBe(licenseKey);
}

[Fact]
public void LicenseKey_PrioritizesExplicitValue_OverEnvironmentVariable()
{
const string envKey = "env-license-key";
const string explicitKey = "explicit-license-key";
Environment.SetEnvironmentVariable(EnvVarName, envKey);

try
{
var config = new MapperConfigurationExpression();

config.LicenseKey = explicitKey;

config.LicenseKey.ShouldBe(explicitKey);
config.LicenseKey.ShouldNotBe(envKey);
}
finally
{
Environment.SetEnvironmentVariable(EnvVarName, null);
}
}

[Fact]
public void LicenseKey_OldWayOverridesEnvironmentVariable_InConfigAction()
{
const string envKey = "env-license-key";
const string explicitKey = "explicit-override-key";
Environment.SetEnvironmentVariable(EnvVarName, envKey);

try
{
var config = new MapperConfigurationExpression();

config.LicenseKey = explicitKey;

config.LicenseKey.ShouldBe(explicitKey);
}
finally
{
Environment.SetEnvironmentVariable(EnvVarName, null);
}
}

#endregion

#region Integration Tests - Old Way with DI

[Fact]
public void AddAutoMapper_SupportsOldWay_ExplicitLicenseKey()
{
const string licenseKey = "old-way-integration-key";
MapperConfigurationExpression capturedCfg = null;

var services = new ServiceCollection();
services.AddLogging();
services.AddAutoMapper(cfg =>
{
cfg.LicenseKey = licenseKey;
cfg.CreateMap<TestSource, TestDestination>();
capturedCfg = (MapperConfigurationExpression)cfg;
});

var serviceProvider = services.BuildServiceProvider();
var mapper = serviceProvider.GetRequiredService<IMapper>();

var result = mapper.Map<TestDestination>(new TestSource { Name = "Test" });

result.ShouldNotBeNull();
result.Name.ShouldBe("Test");
capturedCfg.LicenseKey.ShouldBe(licenseKey);
}

[Fact]
public void AddAutoMapper_UsesEnvironmentVariable_WhenNoExplicitKeySet()
{
const string licenseKey = "env-integration-key";
Environment.SetEnvironmentVariable(EnvVarName, licenseKey);
MapperConfigurationExpression capturedCfg = null;

try
{
var services = new ServiceCollection();
services.AddLogging();
services.AddAutoMapper(cfg =>
{
cfg.CreateMap<TestSource, TestDestination>();
capturedCfg = (MapperConfigurationExpression)cfg;
});

var serviceProvider = services.BuildServiceProvider();
var mapper = serviceProvider.GetRequiredService<IMapper>();

var result = mapper.Map<TestDestination>(new TestSource { Name = "Test" });

result.ShouldNotBeNull();
result.Name.ShouldBe("Test");
capturedCfg.LicenseKey.ShouldBe(licenseKey);
}
finally
{
Environment.SetEnvironmentVariable(EnvVarName, null);
}
}

[Fact]
public void AddAutoMapper_OldWayTakesPrecedence_OverEnvironmentVariable()
{
const string envKey = "env-license-key";
const string explicitKey = "explicit-license-key-from-old-way";
Environment.SetEnvironmentVariable(EnvVarName, envKey);
MapperConfigurationExpression capturedCfg = null;

try
{
var services = new ServiceCollection();
services.AddLogging();
services.AddAutoMapper(cfg =>
{
cfg.LicenseKey = explicitKey;
cfg.CreateMap<TestSource, TestDestination>();
capturedCfg = (MapperConfigurationExpression)cfg;
});

var serviceProvider = services.BuildServiceProvider();
var mapper = serviceProvider.GetRequiredService<IMapper>();

var result = mapper.Map<TestDestination>(new TestSource { Name = "Test" });

result.ShouldNotBeNull();
result.Name.ShouldBe("Test");
capturedCfg.LicenseKey.ShouldBe(explicitKey);
capturedCfg.LicenseKey.ShouldNotBe(envKey);
}
finally
{
Environment.SetEnvironmentVariable(EnvVarName, null);
}
}

#endregion

#region Test Helper Classes

private class TestSource
{
public string Name { get; set; }
}

private class TestDestination
{
public string Name { get; set; }
}

#endregion
}
Loading