public async Task ValidCasesAsync(WebHook validWebHook)
        {
            var schema = new OpenApiSchema
            {
                Properties = new Dictionary <string, OpenApiSchema>
                {
                    ["param1"] = new OpenApiSchema {
                        Type = "integer"
                    },
                    ["param2"] = new OpenApiSchema {
                        Type = "string"
                    },
                }
            };
            var triggersById = new Dictionary <string, WebHookTrigger>
            {
                ["valid"] = new WebHookTrigger {
                    Id = "valid", Schema = schema
                },
            };

            var triggers = new Mock <IWebHookTriggerProvider>();

            triggers.Setup(s => s.GetAvailableTriggers()).Returns(triggersById);
            var logger  = new Mock <ILogger <DefaultWebHookValidator> >();
            var client  = HttpClientMocker.ReturnQueryParam("echo");
            var service = new DefaultWebHookValidator(triggers.Object, logger.Object, client);

            await service.ValidateAsync(validWebHook);

            Assert.NotEqual(default, validWebHook.Id);
        public async Task ArgNullAsync()
        {
            var triggers = new Mock <IWebHookTriggerProvider>();
            var logger   = new Mock <ILogger <DefaultWebHookValidator> >();
            var client   = new Mock <HttpClient>();

            Assert.Throws <ArgumentNullException>(() => new DefaultWebHookValidator(null, logger.Object, client.Object));
            Assert.Throws <ArgumentNullException>(() => new DefaultWebHookValidator(triggers.Object, null, client.Object));
            Assert.Throws <ArgumentNullException>(() => new DefaultWebHookValidator(triggers.Object, logger.Object, null));

            var service = new DefaultWebHookValidator(triggers.Object, logger.Object, client.Object);
            await Assert.ThrowsAsync <ArgumentNullException>(() => service.ValidateAsync(null));
        }
        public async Task ValidCasesAsync(WebHook validWebHook)
        {
            var triggersById = new Dictionary <string, WebHookTrigger>
            {
                ["valid"] = new WebHookTrigger("valid"),
            };

            var triggers = new Mock <IWebHookTriggerProvider>();

            triggers.Setup(s => s.GetAvailableTriggers()).Returns(triggersById);
            var logger  = new Mock <ILogger <DefaultWebHookValidator> >();
            var client  = HttpClientMocker.ReturnQueryParam("echo");
            var service = new DefaultWebHookValidator(triggers.Object, logger.Object, client);

            await service.ValidateAsync(validWebHook);

            Assert.NotEqual(default, validWebHook.Id);
        public async Task InvalidCasesAsync()
        {
            var triggersById = new Dictionary <string, WebHookTrigger>
            {
                ["valid"] = new WebHookTrigger("valid")
            };
            var triggers = new Mock <IWebHookTriggerProvider>();

            triggers.Setup(s => s.GetAvailableTriggers()).Returns(triggersById);
            var logger  = new Mock <ILogger <DefaultWebHookValidator> >();
            var client  = HttpClientMocker.Static(System.Net.HttpStatusCode.NotFound, "fail");
            var service = new DefaultWebHookValidator(triggers.Object, logger.Object, client);

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Secret = "too short"
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Secret = "toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_"
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook()));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter>()
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "invalid"
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = "c:/data"
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = "ftp://data"
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = "http://www.example.com"
            }));

            service = new DefaultWebHookValidator(triggers.Object, logger.Object, HttpClientMocker.AlwaysFail(new Exception()));
            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = "http://www.example.com"
            }));
        }
        public async Task InvalidCasesAsync()
        {
            var schema = new OpenApiSchema
            {
                Properties = new Dictionary <string, OpenApiSchema>
                {
                    ["param1"] = new OpenApiSchema {
                        Type = "integer"
                    },
                    ["param2"] = new OpenApiSchema {
                        Type = "string"
                    },
                    ["param3"] = new OpenApiSchema {
                        Type = "array", Items = new OpenApiSchema {
                            Type = "integer"
                        }
                    },
                    ["param4"] = new OpenApiSchema {
                        Type = "array", Reference = new OpenApiReference()
                    },
                    ["number"] = new OpenApiSchema {
                        Type = "number"
                    },
                    ["integer"] = new OpenApiSchema {
                        Type = "integer"
                    },
                    ["bool"] = new OpenApiSchema {
                        Type = "boolean"
                    },
                    ["object"] = new OpenApiSchema {
                        Type = "object"
                    },
                }
            };
            var triggersById = new Dictionary <string, WebHookTrigger>
            {
                ["valid"] = new WebHookTrigger {
                    Id = "valid", Schema = schema
                },
            };
            var triggers = new Mock <IWebHookTriggerProvider>();

            triggers.Setup(s => s.GetAvailableTriggers()).Returns(triggersById);
            var logger  = new Mock <ILogger <DefaultWebHookValidator> >();
            var client  = HttpClientMocker.Static(System.Net.HttpStatusCode.NotFound, "fail");
            var service = new DefaultWebHookValidator(triggers.Object, logger.Object, client);

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Secret = "tooshort"
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Secret = "toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_toolong_"
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook()));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter>()
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "invalid"
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            [""] = ""
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["invalid"] = ""
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["param3"] = ""
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["param4.next"] = ""
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["param1"] = "string"
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["param1"] = null
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["number"] = true
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["integer"] = 2.3
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["boolean"] = 345
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid", Parameters = new Dictionary <string, object> {
                            ["object"] = 23
                        }
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = new Uri("c:/data")
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = new Uri("ftp://data")
            }));

            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = new Uri("http://www.example.com")
            }));

            service = new DefaultWebHookValidator(triggers.Object, logger.Object, HttpClientMocker.AlwaysFail(new Exception()));
            await Assert.ThrowsAsync <ArgumentException>(() => service.ValidateAsync(new WebHook {
                Filters = new List <WebHookFilter> {
                    new WebHookFilter {
                        Trigger = "valid"
                    }
                }, Callback = new Uri("http://www.example.com")
            }));
        }