|
| 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 Microsoft.Extensions.Configuration; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace NuGet.Services.Configuration.Tests |
| 10 | +{ |
| 11 | + public class ConfigurationRootSecretReaderFactoryFacts |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void ConstructorThrowsWhenKeyConfigIsNull() |
| 15 | + { |
| 16 | + Assert.Throws<ArgumentNullException>(() => new ConfigurationRootSecretReaderFactory(null)); |
| 17 | + } |
| 18 | + |
| 19 | + public static IEnumerable<object[]> InvalidConfigs |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + yield return new object[] { |
| 24 | + new Dictionary<string, string> { |
| 25 | + { Constants.KeyVaultVaultNameKey, "KeyVaultName" }, |
| 26 | + { Constants.KeyVaultUseManagedIdentity, "true" }, |
| 27 | + { Constants.KeyVaultClientIdKey, "KeyVaultClientId" }, |
| 28 | + { Constants.KeyVaultCertificateThumbprintKey, "KeyVaultThumbprint" }, |
| 29 | + { Constants.KeyVaultStoreNameKey, "StoreName"}, |
| 30 | + { Constants.KeyVaultStoreLocationKey, "StoreLocation" }, |
| 31 | + { Constants.KeyVaultValidateCertificateKey, "true" }, |
| 32 | + { Constants.KeyVaultSendX5c, "false" } |
| 33 | + } |
| 34 | + }; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static IEnumerable<object[]> ValidConfigs |
| 39 | + { |
| 40 | + get |
| 41 | + { |
| 42 | + yield return new object[] { |
| 43 | + new Dictionary<string, string> { |
| 44 | + { Constants.KeyVaultVaultNameKey, "KeyVaultName" }, |
| 45 | + { Constants.KeyVaultUseManagedIdentity, "true" }, |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + yield return new object[] { |
| 50 | + new Dictionary<string, string> { |
| 51 | + { Constants.KeyVaultClientIdKey, "KeyVaultClientId" }, |
| 52 | + { Constants.KeyVaultCertificateThumbprintKey, "KeyVaultThumbprint" }, |
| 53 | + { Constants.KeyVaultStoreNameKey, "StoreName"}, |
| 54 | + { Constants.KeyVaultStoreLocationKey, "StoreLocation" }, |
| 55 | + { Constants.KeyVaultValidateCertificateKey, "true" }, |
| 56 | + { Constants.KeyVaultSendX5c, "false" } |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + yield return new object[] { |
| 61 | + new Dictionary<string, string> { |
| 62 | + { Constants.KeyVaultVaultNameKey, "KeyVaultName" }, |
| 63 | + { Constants.KeyVaultUseManagedIdentity, "false" }, |
| 64 | + { Constants.KeyVaultClientIdKey, "KeyVaultClientId" }, |
| 65 | + { Constants.KeyVaultCertificateThumbprintKey, "KeyVaultThumbprint" }, |
| 66 | + { Constants.KeyVaultStoreNameKey, "StoreName"}, |
| 67 | + { Constants.KeyVaultStoreLocationKey, "StoreLocation" }, |
| 68 | + { Constants.KeyVaultValidateCertificateKey, "true" }, |
| 69 | + { Constants.KeyVaultSendX5c, "false" } |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + yield return new object[] { |
| 74 | + new Dictionary<string, string> { |
| 75 | + { Constants.KeyVaultVaultNameKey, "KeyVaultName" }, |
| 76 | + { Constants.KeyVaultUseManagedIdentity, "false" }, |
| 77 | + { Constants.KeyVaultClientIdKey, "" }, |
| 78 | + { Constants.KeyVaultCertificateThumbprintKey, "" }, |
| 79 | + { Constants.KeyVaultStoreNameKey, ""}, |
| 80 | + { Constants.KeyVaultStoreLocationKey, "" }, |
| 81 | + { Constants.KeyVaultValidateCertificateKey, "" }, |
| 82 | + { Constants.KeyVaultSendX5c, "" } |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [Theory] |
| 89 | + [MemberData(nameof(InvalidConfigs))] |
| 90 | + public void ConstructorThrowsWhenKeyVaultConfigSpecifiesManagedIdentityAndCertificate(IDictionary<string, string> config) |
| 91 | + { |
| 92 | + Assert.Throws<ArgumentException>(() => new ConfigurationRootSecretReaderFactory(CreateTestConfiguration(config))); |
| 93 | + } |
| 94 | + |
| 95 | + [Theory] |
| 96 | + [MemberData(nameof(ValidConfigs))] |
| 97 | + public void CreatesSecretReaderFactoryForValidConfiguration(IDictionary<string, string> config) |
| 98 | + { |
| 99 | + var secretReaderFacotry = new ConfigurationRootSecretReaderFactory(CreateTestConfiguration(config)); |
| 100 | + Assert.NotNull(secretReaderFacotry); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + private IConfigurationRoot CreateTestConfiguration(IDictionary<string, string> config) |
| 105 | + { |
| 106 | + return new ConfigurationBuilder() |
| 107 | + .AddInMemoryCollection(config) |
| 108 | + .Build(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments