Example #1
0
        public void ArgNullEfSender()
        {
            var logger     = new Mock <ILogger <EFWebHookSender <TestContext1> > >();
            var httpClient = HttpClientMocker.Static(System.Net.HttpStatusCode.OK, "");
            var signature  = new Mock <ISignatureService>();

            Assert.Throws <ArgumentNullException>(() => new EFWebHookSender <TestContext1>(httpClient, signature.Object, logger.Object, null));
        }
Example #2
0
        public async Task ArgNullAsync()
        {
            var logger     = new Mock <ILogger <DefaultWebHookSender> >();
            var httpClient = HttpClientMocker.Static(System.Net.HttpStatusCode.OK, "");
            var signature  = new Mock <ISignatureService>();

            Assert.Throws <ArgumentNullException>(() => new DefaultWebHookSender(null, signature.Object, logger.Object));
            Assert.Throws <ArgumentNullException>(() => new DefaultWebHookSender(httpClient, null, logger.Object));
            Assert.Throws <ArgumentNullException>(() => new DefaultWebHookSender(httpClient, signature.Object, null));

            var service = new DefaultWebHookSender(httpClient, signature.Object, logger.Object);
            await Assert.ThrowsAsync <ArgumentNullException>(() => service.SendAsync(null, CancellationToken.None));
        }
Example #3
0
        public async Task NotFoundScenarioAsync(System.Net.HttpStatusCode code)
        {
            var logger    = new Mock <ILogger <DefaultWebHookSender> >();
            var signature = new Mock <ISignatureService>();

            var webHook = new WebHook {
                Callback = new Uri("http://www.example.com")
            };
            var notif = new WebHookNotification {
                TriggerId = "noun.verb"
            };

            var httpClient = HttpClientMocker.Static(code, "");

            var service = new CounterDefaultWebHookSender(httpClient, signature.Object, logger.Object);
            await service.SendAsync(new WebHookWorkItem(notif, webHook), CancellationToken.None);

            Assert.Equal(1, service.NotFounds);
        }
Example #4
0
        public async Task NotFoundScenarioAsync(System.Net.HttpStatusCode code)
        {
            var logger    = new Mock <ILogger <EFWebHookSender <TestContext1> > >();
            var signature = new Mock <ISignatureService>();

            var context = _fixture.Provider.GetRequiredService <TestContext1>();
            var webHook = AddWebHook(context, Guid.NewGuid(), "myPrincipalxxx", "noun.verb", false, null);

            webHook.Callback = new Uri("http://example.org");
            await context.SaveChangesAsync();

            var notif = new WebHookNotification {
                TriggerId = "noun.verb"
            };

            var httpClient = HttpClientMocker.Static(code, "");

            var service = new EFWebHookSender <TestContext1>(httpClient, signature.Object, logger.Object, context);
            await service.SendAsync(new WebHookWorkItem(notif, webHook), CancellationToken.None);

            Assert.True((await context.WebHooks.FirstAsync(w => w.Id == webHook.Id)).IsPaused);
        }
        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")
            }));
        }