|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.Extensions.Primitives; |
| 12 | +using Moq; |
| 13 | +using NuGet.Services.KeyVault; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace NuGet.Services.Configuration.Tests |
| 17 | +{ |
| 18 | + public class SecretInjectedConfigurationFacts |
| 19 | + { |
| 20 | + private SecretInjectedConfiguration _target; |
| 21 | + private Mock<IConfiguration> _configurationMock; |
| 22 | + private Mock<ICachingSecretInjector> _secretInjectorMock; |
| 23 | + private Mock<ILogger> _loggerMock; |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void InjectsSecretsWhenUsingIndexer() |
| 27 | + { |
| 28 | + _configurationMock |
| 29 | + .SetupGet(c => c[It.IsAny<string>()]) |
| 30 | + .Returns("SomeString"); |
| 31 | + var expectedString = "InjectedString"; |
| 32 | + _secretInjectorMock |
| 33 | + .Setup(si => si.TryInjectCached("SomeString", It.IsAny<ILogger>(), out expectedString)) |
| 34 | + .Returns(true); |
| 35 | + |
| 36 | + var result = _target["SomeString"]; |
| 37 | + Assert.Equal(expectedString, result); |
| 38 | + _secretInjectorMock.Verify(si => si.Inject(It.IsAny<string>()), Times.Never); |
| 39 | + _secretInjectorMock.Verify(si => si.Inject(It.IsAny<string>(), It.IsAny<ILogger>()), Times.Never); |
| 40 | + _secretInjectorMock.Verify(si => si.InjectAsync(It.IsAny<string>()), Times.Never); |
| 41 | + _secretInjectorMock.Verify(si => si.InjectAsync(It.IsAny<string>(), It.IsAny<ILogger>()), Times.Never); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void FallsBackToInjectWhenNotCached() |
| 46 | + { |
| 47 | + _configurationMock |
| 48 | + .SetupGet(c => c[It.IsAny<string>()]) |
| 49 | + .Returns("SomeString"); |
| 50 | + string outValue = null; |
| 51 | + _secretInjectorMock |
| 52 | + .Setup(si => si.TryInjectCached("SomeString", out outValue)) |
| 53 | + .Returns(false); |
| 54 | + _secretInjectorMock |
| 55 | + .Setup(si => si.Inject("SomeString", It.IsAny<ILogger>())) |
| 56 | + .Returns("InjectedString"); |
| 57 | + |
| 58 | + var result = _target["SomeString"]; |
| 59 | + Assert.Equal("InjectedString", result); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void ChildrenAreSecretInjectedSections() |
| 64 | + { |
| 65 | + _configurationMock |
| 66 | + .Setup(c => c.GetChildren()) |
| 67 | + .Returns([Mock.Of<IConfigurationSection>(), Mock.Of<IConfigurationSection>()]); |
| 68 | + |
| 69 | + var children = _target.GetChildren(); |
| 70 | + Assert.All(children, child => Assert.True(child is SecretInjectedConfigurationSection)); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void GetSectionReturnsInjectedSection() |
| 75 | + { |
| 76 | + _configurationMock |
| 77 | + .Setup(c => c.GetSection("TestSection")) |
| 78 | + .Returns(Mock.Of<IConfigurationSection>()); |
| 79 | + |
| 80 | + var section = _target.GetSection("TestSection"); |
| 81 | + Assert.IsType<SecretInjectedConfigurationSection>(section); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void PassesThroughChangeToken() |
| 86 | + { |
| 87 | + // Technically, we actually need is that when the original configuration |
| 88 | + // section change token notifies about a change returned token should |
| 89 | + // notify as well. But the implementation just passes it through, and it is |
| 90 | + // easier to test that instead. In an unlikely case that this behavior |
| 91 | + // changes we'd need to rewrite this test with proper assumptions in mind. |
| 92 | + |
| 93 | + var changeToken = Mock.Of<IChangeToken>(); |
| 94 | + _configurationMock |
| 95 | + .Setup(c => c.GetReloadToken()) |
| 96 | + .Returns(changeToken); |
| 97 | + |
| 98 | + var result = _target.GetReloadToken(); |
| 99 | + Assert.Same(changeToken, result); |
| 100 | + } |
| 101 | + |
| 102 | + public SecretInjectedConfigurationFacts() |
| 103 | + { |
| 104 | + _configurationMock = new Mock<IConfiguration>(); |
| 105 | + _secretInjectorMock = new Mock<ICachingSecretInjector>(); |
| 106 | + _loggerMock = new Mock<ILogger>(); |
| 107 | + |
| 108 | + _target = new SecretInjectedConfiguration(_configurationMock.Object, _secretInjectorMock.Object, _loggerMock.Object); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments