Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/UnitTests/AutoMapper.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<ItemGroup>
<ProjectReference Include="..\AutoMapper\AutoMapper.csproj" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" Version="10.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="[10.0.0, )" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="[10.0.0, )" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="All" />
Expand Down
229 changes: 229 additions & 0 deletions src/UnitTests/LicenseKeyEnvironmentVariableTests.cs
Original file line number Diff line number Diff line change
@@ -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<TestSource, TestDestination>();
});

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

// Act
var result = mapper.Map<TestDestination>(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<TestSource, TestDestination>();
});

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

// Act
var result = mapper.Map<TestDestination>(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<TestSource, TestDestination>();
});

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

// Act
var result = mapper.Map<TestDestination>(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
}
Loading