public void LoadMethodSadPath()
        {
            var exception = new Exception();

            var mockSecret1 = MockSecret.Get("foo", "abc");
            var mockSecret2 = MockSecret.Get("bar", exception);

            var caughtExceptions = new List <Exception>();

            void OnSecretException(SecretExceptionContext context)
            {
                caughtExceptions.Add(context.Exception);
            }

            var source = new SecretsConfigurationSource
            {
                Secrets            = { mockSecret1.Object, mockSecret2.Object },
                ReloadMilliseconds = Timeout.Infinite,
                OnSecretException  = OnSecretException
            };

            var provider = new SecretsConfigurationProvider(source);

            provider.Load();

            provider.TryGet("foo", out var fooValue).Should().BeTrue();
            fooValue.Should().Be("abc");

            provider.TryGet("bar", out var barValue).Should().BeTrue();
            barValue.Should().BeNull();

            caughtExceptions.Should().HaveCount(1);
            caughtExceptions[0].Should().BeSameAs(exception);
        }
Example #2
0
        public void BuildMethodHappyPath3()
        {
            var secret = MockSecret.Get("key", "value").Object;

            var source = new SecretsConfigurationSource
            {
                Secrets = { secret }
            };

            var builder = new ConfigurationBuilder()
                          .AddInMemoryCollection(new Dictionary <string, string>
            {
                ["RockLib.Secrets:Type"] = typeof(CustomSecret).AssemblyQualifiedName
            });

            // Before building, source only contains the single secret that was added directly to it.
            source.Secrets.Should().ContainSingle(s => ReferenceEquals(s, secret));

            source.Build(builder);

            // After building the first time, the secret defined in configuration has been added to the source.
            source.Secrets.Should().HaveCount(2);
            source.Secrets[0].Should().BeSameAs(secret);
            source.Secrets[1].Should().BeOfType <CustomSecret>();

            source.Build(builder);

            // After building a second time, source hasn't changed.
            source.Secrets.Should().HaveCount(2);
            source.Secrets[0].Should().BeSameAs(secret);
            source.Secrets[1].Should().BeOfType <CustomSecret>();
        }
Example #3
0
        public void BuildMethodHappyPath1()
        {
            var secret1 = MockSecret.Get("key1", "value1").Object;
            var secret2 = MockSecret.Get("key2", "value2").Object;

            var source = new SecretsConfigurationSource
            {
                Secrets = { secret1, secret2 }
            };

            var builder = new ConfigurationBuilder()
                          .AddInMemoryCollection(new Dictionary <string, string>
            {
                ["RockLib.Secrets:Type"] = typeof(CustomSecret).AssemblyQualifiedName
            });

            var provider = (SecretsConfigurationProvider)source.Build(builder);

            provider.Source.Should().BeSameAs(source);

            provider.Secrets.Should().NotBeSameAs(source.Secrets);
            provider.Secrets.Should().BeEquivalentTo(source.Secrets);

            provider.Secrets[0].Should().BeSameAs(secret1);
            provider.Secrets[1].Should().BeSameAs(secret2);
            provider.Secrets[2].Should().BeOfType <CustomSecret>();
        }
Example #4
0
        public void AddSecretMethodHappyPath()
        {
            var source = new SecretsConfigurationSource();

            var builder = new SecretsConfigurationBuilder(source);

            var secret = MockSecret.Get("foo", "bar").Object;

            builder.AddSecret(secret);

            source.Secrets.Should().ContainSingle()
            .Which.Should().BeSameAs(secret);
        }
        public void ConstructorSadPath5()
        {
            var secret1 = MockSecret.Get("key1", "value1").Object;
            var secret2 = MockSecret.Get("key1", "value2").Object;

            var source = new SecretsConfigurationSource
            {
                Secrets = { secret1, secret2 }
            };

            Action act = () => new SecretsConfigurationProvider(source);

            act.Should().ThrowExactly <ArgumentException>().WithMessage("The SecretsConfigurationSource cannot contain any secrets with duplicate Keys.*source*");
        }
        public void ConstructorHappyPath()
        {
            var secret1 = MockSecret.Get("key1", "value1").Object;
            var secret2 = MockSecret.Get("key2", "value2").Object;

            var source = new SecretsConfigurationSource
            {
                Secrets = { secret1, secret2 }
            };

            var provider = new SecretsConfigurationProvider(source);

            provider.Source.Should().BeSameAs(source);

            provider.Secrets.Should().NotBeSameAs(source.Secrets);
            provider.Secrets.Should().BeEquivalentTo(source.Secrets);
        }
Example #7
0
        public void AddRockLibSecretsMethod2HappyPath()
        {
            var builder = new ConfigurationBuilder();

            builder.Sources.Should().BeEmpty();

            var secret = MockSecret.Get("foo", "bar").Object;

            Action <SecretsConfigurationSource> configureSource = source => source.Secrets.Add(secret);

            var secretsBuilder = builder.AddRockLibSecrets(configureSource);

            builder.Sources.Should().ContainSingle();
            builder.Sources[0].Should().BeOfType <SecretsConfigurationSource>()
            .Which.Secrets.Should().ContainSingle(s => ReferenceEquals(s, secret));

            secretsBuilder.Should().BeOfType <SecretsConfigurationBuilder>()
            .Which.Source.Should().BeSameAs(builder.Sources[0]);
        }
Example #8
0
        public void BuildMethodHappyPath2()
        {
            Action <SecretExceptionContext> onSecretException = context => { };

            var source = new SecretsConfigurationSource
            {
                Secrets = { MockSecret.Get("key", "value").Object }
            };

            var builder = new ConfigurationBuilder();

            builder.SetSecretExceptionHandler(onSecretException);

            var provider = source.Build(builder);

            var secretsConfigurationProvider = provider.Should().BeOfType <SecretsConfigurationProvider>().Subject;

            secretsConfigurationProvider.Source.Should().BeSameAs(source);
            source.OnSecretException.Should().BeSameAs(onSecretException);
        }
        public void LoadMethodHappyPath()
        {
            var secret1 = MockSecret.Get("foo", "abc").Object;
            var secret2 = MockSecret.Get("bar", (string)null).Object;

            var source = new SecretsConfigurationSource
            {
                Secrets            = { secret1, secret2 },
                ReloadMilliseconds = Timeout.Infinite
            };

            var provider = new SecretsConfigurationProvider(source);

            provider.Load();

            provider.TryGet("foo", out var fooValue).Should().BeTrue();
            fooValue.Should().Be("abc");

            provider.TryGet("bar", out var barValue).Should().BeTrue();
            barValue.Should().BeNull();
        }