|
| 1 | +namespace VaultSharp.Extensions.Configuration.Test |
| 2 | +{ |
| 3 | + using System.Collections.Generic; |
| 4 | + using System.Threading.Tasks; |
| 5 | + using DotNet.Testcontainers.Containers.Builders; |
| 6 | + using DotNet.Testcontainers.Containers.Modules; |
| 7 | + using DotNet.Testcontainers.Containers.WaitStrategies; |
| 8 | + using FluentAssertions; |
| 9 | + using Microsoft.Extensions.Configuration; |
| 10 | + using VaultSharp.V1.AuthMethods.Token; |
| 11 | + using Xunit; |
| 12 | + |
| 13 | + public class IntegrationTests |
| 14 | + { |
| 15 | + private TestcontainersContainer PrepareVaultContainer() |
| 16 | + { |
| 17 | + var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>() |
| 18 | + .WithImage("vault") |
| 19 | + .WithName("vaultsharp_test") |
| 20 | + .WithPortBinding(8200, 8200) |
| 21 | + .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8200)) |
| 22 | + .WithEnvironment("VAULT_DEV_ROOT_TOKEN_ID", "root") |
| 23 | + .WithEnvironment("VAULT_DEV_LISTEN_ADDRESS", "0.0.0.0:8200"); |
| 24 | + |
| 25 | + return testcontainersBuilder.Build(); |
| 26 | + } |
| 27 | + |
| 28 | + private async Task LoadDataAsync(Dictionary<string, string> values) |
| 29 | + { |
| 30 | + var authMethod = new TokenAuthMethodInfo("root"); |
| 31 | + |
| 32 | + var vaultClientSettings = new VaultClientSettings("http://localhost:8200", authMethod); |
| 33 | + IVaultClient vaultClient = new VaultClient(vaultClientSettings); |
| 34 | + |
| 35 | + foreach (var pair in values) |
| 36 | + { |
| 37 | + var data = new Dictionary<string, object>() { ["value"] = pair.Value }; |
| 38 | + await vaultClient.V1.Secrets.KeyValue.V2.WriteSecretAsync(pair.Key, data).ConfigureAwait(false); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task Success_Test_TokenAuth() |
| 44 | + { |
| 45 | + // arrange |
| 46 | + Dictionary<string, string> values = new Dictionary<string, string>(); |
| 47 | + values.Add("data/test/option1", "value1"); |
| 48 | + values.Add("data/test/subsection/option2", "value2"); |
| 49 | + |
| 50 | + var container = this.PrepareVaultContainer(); |
| 51 | + try |
| 52 | + { |
| 53 | + await container.StartAsync().ConfigureAwait(false); |
| 54 | + await this.LoadDataAsync(values).ConfigureAwait(false); |
| 55 | + |
| 56 | + // act |
| 57 | + ConfigurationBuilder builder = new ConfigurationBuilder(); |
| 58 | + builder.AddVaultConfiguration( |
| 59 | + () => new VaultOptions("http://localhost:8200", "root"), "test", "secret"); |
| 60 | + var configurationRoot = builder.Build(); |
| 61 | + |
| 62 | + // assert |
| 63 | + configurationRoot.GetValue<string>("option1").Should().Be(values["data/test/option1"]); |
| 64 | + configurationRoot.GetSection("subsection").GetValue<string>("option2").Should() |
| 65 | + .Be(values["data/test/subsection/option2"]); |
| 66 | + } |
| 67 | + finally |
| 68 | + { |
| 69 | + await container.DisposeAsync().ConfigureAwait(false); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments