Provides a default IWebHookReceiverConfig implementation which manages IWebHookReceiver configuration using application settings. The name of the application setting is 'MS_WebHookReceiverSecret_<name>' where 'name' is the name of the receiver, for example github. The value is a comma-separated list of secrets, using an ID to differentiate between them. For example, 'secret0, id1=secret1, id2=secret2'. The corresponding WebHook URI is of the form 'https://<host>/api/webhooks/incoming/custom/{id}'.
Inheritance: IWebHookReceiverConfig
Exemple #1
0
        public virtual void Initialize(string name, string config)
        {
            if (ReceiverMock == null)
            {
                ReceiverMock = new Mock <T> {
                    CallBase = true
                };
            }

            Receiver = ReceiverMock.Object;
            Logger   = new Mock <ILogger>().Object;

            name     = name ?? Receiver.Name;
            Settings = new SettingsDictionary
            {
                [SecretPrefix + name] = config,
            };

            ReceiverConfig = new WebHookReceiverConfig(Settings, Logger);

            HttpConfig = HttpConfigurationMock.Create(new Dictionary <Type, object>
            {
                { typeof(IWebHookReceiverConfig), ReceiverConfig },
                { typeof(SettingsDictionary), Settings },
            });

            RequestContext = new HttpRequestContext {
                Configuration = HttpConfig
            };
        }
Exemple #2
0
        public void GetConfigKey(string receiver, string id, string expected)
        {
            // Act
            string actual = WebHookReceiverConfig.GetConfigKey(receiver, id);

            // Assert
            Assert.Equal(expected, actual);
        }
Exemple #3
0
        public void AddKey_AddsItem(string receiver, string id, string key)
        {
            // Arrange
            IDictionary <string, string> config = new Dictionary <string, string>();

            // Act
            WebHookReceiverConfig.AddKey(config, _logger, receiver, id, "Value");

            // Assert
            Assert.Equal("Value", config[key]);
        }
Exemple #4
0
        public void ReadSettings_Parses_ValidValues(IDictionary <string, string> input, IDictionary <string, string> expected)
        {
            // Arrange
            SettingsDictionary settings = GetSettings(input);

            // Act
            IDictionary <string, string> actual = WebHookReceiverConfig.ReadSettings(settings, logger: null);

            // Assert
            Assert.Equal(expected, actual);
        }
Exemple #5
0
        public async Task GetReceiverConfigAsync_Returns_ExpectedValue(IDictionary <string, string> input, string receiver, string id, string expected)
        {
            // Arrange
            SettingsDictionary    settings = GetSettings(input);
            WebHookReceiverConfig config   = new WebHookReceiverConfig(settings, _logger);

            // Act
            string actual = await config.GetReceiverConfigAsync(receiver, id);

            // Assert
            Assert.Equal(expected, actual);
        }
        public async Task GetReceiverConfigAsync_Returns_ExpectedValue(IDictionary<string, string> input, string receiver, string id, string expected)
        {
            // Arrange
            SettingsDictionary settings = GetSettings(input);
            WebHookReceiverConfig config = new WebHookReceiverConfig(settings, _logger);

            // Act
            string actual = await config.GetReceiverConfigAsync(receiver, id);

            // Assert
            Assert.Equal(expected, actual);
        }
Exemple #7
0
        public void AddKey_ThrowsOnDuplicateKey()
        {
            // Arrange
            IDictionary <string, string> config = new Dictionary <string, string>();

            WebHookReceiverConfig.AddKey(config, _logger, "Receiver", "Id", "Value");

            // Act
            InvalidOperationException ioex = Assert.Throws <InvalidOperationException>(() => WebHookReceiverConfig.AddKey(config, _logger, "Receiver", "Id", "Value"));

            // Assert
            Assert.Equal("Could not add configuration for receiver 'Receiver' and id 'Id': An item with the same key has already been added.", ioex.Message);
        }
Exemple #8
0
        /// <summary>
        /// Gets a default <see cref="IWebHookReceiverConfig"/> implementation which is used if none are registered with the
        /// Dependency Injection engine.
        /// </summary>
        /// <returns>A default <see cref="IWebHookReceiverConfig"/> instance.</returns>
        public static IWebHookReceiverConfig GetReceiverConfig(SettingsDictionary settings, ILogger logger)
        {
            if (_receiverConfig != null)
            {
                return _receiverConfig;
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            IWebHookReceiverConfig instance = new WebHookReceiverConfig(settings, logger);
            Interlocked.CompareExchange(ref _receiverConfig, instance, null);
            return _receiverConfig;
        }
        /// <summary>
        /// Gets a default <see cref="IWebHookReceiverConfig"/> implementation which is used if none are registered with the
        /// Dependency Injection engine.
        /// </summary>
        /// <returns>A default <see cref="IWebHookReceiverConfig"/> instance.</returns>
        public static IWebHookReceiverConfig GetReceiverConfig(SettingsDictionary settings, ILogger logger)
        {
            if (_receiverConfig != null)
            {
                return(_receiverConfig);
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            IWebHookReceiverConfig instance = new WebHookReceiverConfig(settings, logger);

            Interlocked.CompareExchange(ref _receiverConfig, instance, null);
            return(_receiverConfig);
        }
Exemple #10
0
        public void ReadSettings_Throws_OnInvalidValues()
        {
            // Arrange
            SettingsDictionary settings = GetSettings(new Dictionary <string, string> {
                { "MS_WebHookReceiverSecret_A", "a=b=c" }
            });

            // Act
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => WebHookReceiverConfig.ReadSettings(settings, logger: null));

            // Assert
            Assert.Equal("The 'MS_WebHookReceiverSecret_A' application setting must have a comma-separated value of one or more secrets of the form <secret> or <id>=<secret>.", ex.Message);
        }