diff --git a/src/AutoMapper/Configuration/MapperConfigurationExpression.cs b/src/AutoMapper/Configuration/MapperConfigurationExpression.cs index 6bfebeadfe..c71ba333fb 100644 --- a/src/AutoMapper/Configuration/MapperConfigurationExpression.cs +++ b/src/AutoMapper/Configuration/MapperConfigurationExpression.cs @@ -127,7 +127,13 @@ void IGlobalConfigurationExpression.Validator(Validator validator) => /// 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; diff --git a/src/UnitTests/AutoMapper.UnitTests.csproj b/src/UnitTests/AutoMapper.UnitTests.csproj index 4dc25a1377..f343ea1a65 100644 --- a/src/UnitTests/AutoMapper.UnitTests.csproj +++ b/src/UnitTests/AutoMapper.UnitTests.csproj @@ -15,6 +15,8 @@ + + diff --git a/src/UnitTests/LicenseKeyEnvironmentVariableTests.cs b/src/UnitTests/LicenseKeyEnvironmentVariableTests.cs new file mode 100644 index 0000000000..e0c77098e0 --- /dev/null +++ b/src/UnitTests/LicenseKeyEnvironmentVariableTests.cs @@ -0,0 +1,229 @@ +using Xunit; +using AutoMapper; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace AutoMapper.UnitTests.Licensing; + +public class LicenseKeyEnvironmentVariableTests +{ + #region Environment Variable Auto-Detection Tests + + [Fact] + public void LicenseKey_ReadsFromEnvironmentVariable_WhenNotExplicitlySet() + { + // Arrange + const string expectedKey = "test-license-key-12345"; + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", expectedKey); + + try + { + var config = new MapperConfigurationExpression(); + + // Act + var actualKey = config.LicenseKey; + + // Assert + Assert.Equal(expectedKey, actualKey); + } + finally + { + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", null); + } + } + + [Fact] + public void LicenseKey_ReturnsNull_WhenEnvironmentVariableNotSet() + { + // Arrange + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", null); + var config = new MapperConfigurationExpression(); + + // Act + var actualKey = config.LicenseKey; + + // Assert + Assert.Null(actualKey); + } + + #endregion + + #region Backward Compatibility - Old Way Tests + + [Fact] + public void LicenseKey_SupportsOldWay_DirectAssignment() + { + // Arrange + const string licenseKey = "old-way-explicit-key"; + var config = new MapperConfigurationExpression(); + + // Act + config.LicenseKey = licenseKey; + var actualKey = config.LicenseKey; + + // Assert + Assert.Equal(licenseKey, actualKey); + } + + [Fact] + public void LicenseKey_PrioritizesExplicitValue_OverEnvironmentVariable() + { + // Arrange + const string envKey = "env-license-key"; + const string explicitKey = "explicit-license-key"; + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", envKey); + + try + { + var config = new MapperConfigurationExpression(); + + // Act + config.LicenseKey = explicitKey; + var actualKey = config.LicenseKey; + + // Assert + Assert.Equal(explicitKey, actualKey); + Assert.NotEqual(envKey, actualKey); + } + finally + { + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", null); + } + } + + [Fact] + public void LicenseKey_OldWayOverridesEnvironmentVariable_InConfigAction() + { + // Arrange + const string envKey = "env-license-key"; + const string explicitKey = "explicit-override-key"; + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", envKey); + + try + { + var config = new MapperConfigurationExpression(); + + // Act - Old way: set it in the config action + config.LicenseKey = explicitKey; + + // Assert + Assert.Equal(explicitKey, config.LicenseKey); + } + finally + { + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", null); + } + } + + #endregion + + #region Integration Tests - Old Way with DI + + [Fact] + public void AddAutoMapper_SupportsOldWay_ExplicitLicenseKey() + { + // Arrange + const string licenseKey = "old-way-integration-key"; + + var services = new ServiceCollection(); + services.AddLogging(); + services.AddAutoMapper(cfg => + { + cfg.LicenseKey = licenseKey; + cfg.CreateMap(); + }); + + var provider = services.BuildServiceProvider(); + var mapper = provider.GetRequiredService(); + + // Act + var result = mapper.Map(new TestSource { Name = "Test" }); + + // Assert + Assert.NotNull(result); + Assert.Equal("Test", result.Name); + } + + [Fact] + public void AddAutoMapper_UsesEnvironmentVariable_WhenNoExplicitKeySet() + { + // Arrange + const string licenseKey = "env-integration-key"; + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", licenseKey); + + try + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddAutoMapper(cfg => + { + // No explicit LicenseKey assignment - should use env var + cfg.CreateMap(); + }); + + var provider = services.BuildServiceProvider(); + var mapper = provider.GetRequiredService(); + + // Act + var result = mapper.Map(new TestSource { Name = "Test" }); + + // Assert + Assert.NotNull(result); + Assert.Equal("Test", result.Name); + } + finally + { + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", null); + } + } + + [Fact] + public void AddAutoMapper_OldWayTakesPrecedence_OverEnvironmentVariable() + { + // Arrange + const string envKey = "env-license-key"; + const string explicitKey = "explicit-license-key-from-old-way"; + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", envKey); + + try + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddAutoMapper(cfg => + { + cfg.LicenseKey = explicitKey; + cfg.CreateMap(); + }); + + var provider = services.BuildServiceProvider(); + var mapper = provider.GetRequiredService(); + + // Act + var result = mapper.Map(new TestSource { Name = "Test" }); + + // Assert + Assert.NotNull(result); + Assert.Equal("Test", result.Name); + } + finally + { + Environment.SetEnvironmentVariable("AUTOMAPPER_LICENSE_KEY", null); + } + } + + #endregion + + #region Test Helper Classes + + private class TestSource + { + public string Name { get; set; } + } + + private class TestDestination + { + public string Name { get; set; } + } + + #endregion +} \ No newline at end of file