public async Task ReceiveAsync_ReturnsError_IfPostHasNoPayloadTypeProperty(string id) { // Arrange Initialize(GetConfigValue(id, TestSecret)); _postRequest.Content = CreateRequestContent("Microsoft.AspNet.WebHooks.Messages.InvalidMessage.json"); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(id, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook request must contain a 'PayloadType' JSON property indicating the type of event.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), id, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasWrongCodeParameter() { // Arrange Initialize(TestSecret); _postRequest.RequestUri = new Uri("https://some.no.ssl.host?code=invalid"); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The 'code' query parameter provided in the HTTP request did not match the expected value.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasNoEventHeader(string id, string header) { // Arrange Initialize(GetConfigValue(id, TestSecret)); _postRequest.Headers.Add(GitHubWebHookReceiver.SignatureHeaderName, header); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(id, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook request must contain a 'X-Github-Event' HTTP header indicating the type of event.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), id, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasNoAction() { // Arrange Initialize(TestSecret); _postRequest.Content = new StringContent("{ }", Encoding.UTF8, "application/json"); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The HTTP request body did not contain a required 'status' property.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasNoCodeParameter(string query) { // Arrange Initialize(TestSecret); _postRequest.RequestUri = new Uri("https://some.no.ssl.host?" + query); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook verification request must contain a 'code' query parameter.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostIsNotXml() { // Arrange Initialize(TestSecret); _postRequest.Content = new StringContent("{ }", Encoding.UTF8, "application/json"); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook request must contain an entity body formatted as XML.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasNoAction() { // Arrange Initialize(TestSecret); _postRequest.Content = new StringContent("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body><notifications xmlns='http://soap.sforce.com/2005/09/outbound'><OrganizationId>123456789012345</OrganizationId></notifications></soapenv:Body></soapenv:Envelope>", Encoding.UTF8, "application/xml"); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The HTTP request body did not contain a required 'ActionId' property.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasNoEventTypeProperty(string id) { // Arrange Initialize(GetConfigValue(id, TestSecret)); _postRequest.Content = CreateRequestContent("Microsoft.AspNet.WebHooks.Messages.bad.noEventType.json"); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(id, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("No property 'eventType' was found in root of the object.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), id, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasInvalidSignatureHeader(string header) { // Arrange Initialize(TestSecret); _postRequest.Headers.Add(CustomWebHookReceiver.SignatureHeaderName, header); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("Invalid 'ms-signature' header value. Expecting a value of 'sha256=<value>'.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasInvalidSignatureHeaderEncoding() { // Arrange Initialize(TestSecret); _postRequest.Headers.Add(TrelloWebHookReceiver.SignatureHeaderName, "你好世界"); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The 'x-trello-webhook' header value is invalid. It must be a valid base64-encoded string.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasNoIdHeader() { // Arrange Initialize(TestSecret); _postRequest.Headers.Remove(BitbucketWebHookReceiver.UUIDHeaderName); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook request must contain a 'X-Hook-UUID' HTTP header indicating the ID of the WebHook.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasInvalidToken() { // Arrange Initialize(TestSecret); _postRequest.Content = new StringContent("token=invalid", Encoding.UTF8, "application/x-www-form-urlencoded"); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The 'token' parameter provided in the HTTP request did not match the expected value.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasNoAction() { // Arrange Initialize(TestSecret); _postRequest.Content = new StringContent("token=" + TestSecret, Encoding.UTF8, "application/x-www-form-urlencoded"); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The HTTP request body did not contain a required 'command' property indicating a slash command or a 'trigger_word' property indicating an outgoing WebHook.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostIsNotUsingHttps() { // Arrange Initialize(TestSecret); _postRequest.RequestUri = new Uri("http://some.no.ssl.host"); // Act var ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert var error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook receiver 'SalesforceSoapWebHookReceiverProxy' requires HTTPS in order to be secure. Please register a WebHook URI of type 'https'.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Succeeds_WithoutCallingHandler_IfTestId() { // Arrange var content = "{ \"type\": \"action\", \"id\": \"" + StripeWebHookReceiver.TestId + "\" }"; Initialize(TestSecret); _postRequest.Content = new StringContent(content, Encoding.UTF8, "application/json"); _postRequest.Headers.Add(StripeWebHookReceiver.SignatureHeaderName, GetSignatureHeader(content)); // Act var actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert Assert.Equal(HttpStatusCode.OK, actual.StatusCode); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostIsNotJson() { // Arrange Initialize(TestSecret); _postRequest.Headers.Add(TrelloWebHookReceiver.SignatureHeaderName, _signature); _postRequest.Content = new StringContent(TestContent, Encoding.UTF8, "text/plain"); // Act var ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert var error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook request must contain an entity body formatted as JSON.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasTwoSignatureHeaders() { // Arrange Initialize(TestSecret); _postRequest.Headers.Add(DropboxWebHookReceiver.SignatureHeaderName, "value1"); _postRequest.Headers.Add(DropboxWebHookReceiver.SignatureHeaderName, "value2"); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("Expecting exactly one 'X-Dropbox-Signature' header field in the WebHook request but found 2. Please ensure that the request contains exactly one 'X-Dropbox-Signature' header field.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasNoSignatureHeader() { // Arrange Initialize(TestSecret); var expectedMessage = "Expecting exactly one 'Stripe-Signature' header field in the WebHook request " + "but found 0. Please ensure that the request contains exactly one 'Stripe-Signature' header field."; // Act var exception = await Assert.ThrowsAsync <HttpResponseException>( () => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert var error = await exception.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal(expectedMessage, error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfPostHasInvalidKeyHeader() { // Arrange Initialize(TestSecret); _postRequest.Headers.Add(PusherWebHookReceiver.SignatureHeaderName, _testSignature); _postRequest.Headers.Remove(PusherWebHookReceiver.KeyHeaderName); _postRequest.Headers.Add(PusherWebHookReceiver.KeyHeaderName, "invalid"); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The HTTP header 'X-Pusher-Key' value of 'invalid' is not recognized as a valid application key. Please ensure that the right application keys and secrets have been configured.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasInvalidSignature() { // Arrange Initialize(TestSecret); string invalid = EncodingUtilities.ToHex(Encoding.UTF8.GetBytes("invalid")); _postRequest.Headers.Add(CustomWebHookReceiver.SignatureHeaderName, "sha256=" + invalid); // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook signature provided by the 'ms-signature' header field does not match the value expected by the 'CustomWebHookReceiverProxy' receiver. WebHook request is invalid.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfInvalidMethod(string method) { // Arrange Initialize(TestSecret); HttpRequestMessage req = new HttpRequestMessage { Method = new HttpMethod(method) }; req.SetRequestContext(RequestContext); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, req); // Assert Assert.Equal(HttpStatusCode.MethodNotAllowed, actual.StatusCode); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, req, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnError_IfPostHasInvalidSignature() { // Arrange Initialize(TestSecret); string invalid = EncodingUtilities.ToHex(Encoding.UTF8.GetBytes("你好世界")); _postRequest.Headers.Add(DropboxWebHookReceiver.SignatureHeaderName, invalid); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The WebHook signature provided by the 'X-Dropbox-Signature' header field does not match the value expected by the 'DropboxWebHookReceiverProxy' receiver. WebHook request is invalid.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Succeeds_IfValidPostRequest(string id) { // Arrange Initialize(GetConfigValue(id, TestSecret)); List <string> actions = new List <string> { "abcde" }; ReceiverMock.Protected() .Setup <Task <HttpResponseMessage> >("ExecuteWebHookAsync", id, RequestContext, _postRequest, actions, ItExpr.IsAny <object>()) .ReturnsAsync(new HttpResponseMessage()) .Verifiable(); // Act await ReceiverMock.Object.ReceiveAsync(id, RequestContext, _postRequest); // Assert ReceiverMock.Verify(); }
public async Task ReceiveAsync_ReturnsError_IfPostHasNoId() { // Arrange var content = "{ \"type\": \"action\" }"; Initialize(TestSecret); _postRequest.Content = new StringContent(content, Encoding.UTF8, "application/json"); _postRequest.Headers.Add(StripeWebHookReceiver.SignatureHeaderName, GetSignatureHeader(content)); // Act var actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert var error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The HTTP request body did not contain a required 'id' property.", error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasInvalidSignatureHeader(string header) { // Arrange Initialize(TestSecret); _postRequest.Headers.Add(StripeWebHookReceiver.SignatureHeaderName, header); var expectedMessage = "The 'Stripe-Signature' header value is invalid. It must be formatted as " + "key=value pairs separated by commas."; // Act var exception = await Assert.ThrowsAsync <HttpResponseException>( () => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert var error = await exception.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal(expectedMessage, error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Throws_IfPostHasSignatureHeaderWithMissingValues(string header) { // Arrange Initialize(TestSecret); _postRequest.Headers.Add(StripeWebHookReceiver.SignatureHeaderName, header); var expectedMessage = $"The '{StripeWebHookReceiver.SignatureHeaderName}' header value is invalid. It " + $"must contain timestamp ('{StripeWebHookReceiver.TimestampKey}') and signature " + $"('{StripeWebHookReceiver.SignatureKey}') values."; // Act var exception = await Assert.ThrowsAsync <HttpResponseException>( () => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert var error = await exception.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal(expectedMessage, error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnsError_IfValidationFails() { // Arrange Initialize(TestSecret); ReceiverMock.Protected() .Setup <bool>("ValidateReceivedEvent", RequestContext, ItExpr.IsAny <NameValueCollection>(), TestContent) .Returns(false) .Verifiable(); // Act HttpResponseMessage actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert HttpError error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal("The received WebHook is not valid.", error.Message); ReceiverMock.Verify(); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_ReturnError_IfPostHasIncorrectSignature() { // Arrange Initialize(TestSecret); var header = $"{StripeWebHookReceiver.TimestampKey}={TestTimestamp}, {ExtraHeaderContent}"; _postRequest.Headers.Add(StripeWebHookReceiver.SignatureHeaderName, header); var expectedMessage = "The WebHook signature provided by the 'Stripe-Signature' header field does not " + "match the value expected by the 'StripeWebHookReceiverProxy' receiver. WebHook request is invalid."; // Act var actual = await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest); // Assert var error = await actual.Content.ReadAsAsync <HttpError>(); Assert.Equal(expectedMessage, error.Message); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task ReceiveAsync_Succeeds_IfValidPostRequest(string id, string header) { // Arrange Initialize(GetConfigValue(id, TestSecret)); List <string> actions = new List <string> { "action1" }; _postRequest.Headers.Add(GitHubWebHookReceiver.SignatureHeaderName, header); _postRequest.Headers.Add(GitHubWebHookReceiver.EventHeaderName, "action1"); ReceiverMock.Protected() .Setup <Task <HttpResponseMessage> >("ExecuteWebHookAsync", id, RequestContext, _postRequest, actions, ItExpr.IsAny <object>()) .ReturnsAsync(new HttpResponseMessage()) .Verifiable(); // Act await ReceiverMock.Object.ReceiveAsync(id, RequestContext, _postRequest); // Assert ReceiverMock.Verify(); }
public async Task ReceiveAsync_Throws_IfPostHasUnknownId() { // Arrange Initialize(TestSecret); _postRequest.Content = new StringContent("{ \"type\": \"action\", \"id\": \"" + TestStripeId + "\" }", Encoding.UTF8, "application/json"); _handlerMock.Handler = (req, counter) => { return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound))); }; // Act HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>(); Assert.Equal("The ID '12345' could not be resolved for an actual event.", error.Message); Assert.Equal(1, _handlerMock.Counter); ReceiverMock.Protected() .Verify <Task <HttpResponseMessage> >("ExecuteWebHookAsync", Times.Never(), TestId, RequestContext, _postRequest, ItExpr.IsAny <IEnumerable <string> >(), ItExpr.IsAny <object>()); }
public async Task GetSecretLookupTable_Throws_IfInvalidSecret(string invalid) { // Arrange ReceiverMock mock = new ReceiverMock(); _settings["MS_WebHookReceiverSecret_Pusher"] = invalid; // Act HttpResponseException ex = Assert.Throws<HttpResponseException>(() => mock.GetSecretLookupTable(_postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync<HttpError>(); Assert.Equal("The application setting 'MS_WebHookReceiverSecret_Pusher' must have a comma separated list of one or more values of the form '<appKey>_<appSecret>'.", error.Message); }
public void GetSecretLookupTable_BuildsLookupTable(string secret, IDictionary<string, string> expected) { // Arrange ReceiverMock mock = new ReceiverMock(); _settings["MS_WebHookReceiverSecret_Pusher"] = secret; // Act IDictionary<string, string> actual = mock.GetSecretLookupTable(_postRequest); // Assert Assert.Equal(expected, actual); }
public void GetSecretLookupTable_ReturnsSameInstance() { // Arrange ReceiverMock mock = new ReceiverMock(); // Act IDictionary<string, string> actual1 = mock.GetSecretLookupTable(_postRequest); IDictionary<string, string> actual2 = mock.GetSecretLookupTable(_postRequest); // Assert Assert.Same(actual1, actual2); }
public void GetActions_ExtractsActions(string valid, IEnumerable<string> actions) { // Arrange ReceiverMock mock = new ReceiverMock(_config); JObject data = JObject.Parse(valid); // Act IEnumerable<string> actual = mock.GetActions(_postRequest, data); // Assert Assert.Equal(actions, actual); }
public async Task GetActions_Throws_IfInvalidData(string invalid) { ReceiverMock mock = new ReceiverMock(_config); JObject data = JObject.Parse(invalid); // Act HttpResponseException ex = Assert.Throws<HttpResponseException>(() => mock.GetActions(_postRequest, data)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync<HttpError>(); Assert.StartsWith("Could not parse WebHook data: ", error.Message); }
public async Task GetSecretLookupTable_Throws_IfNoSecrets() { // Arrange ReceiverMock mock = new ReceiverMock(); _settings["MS_WebHookReceiverSecret_Pusher"] = " "; // Act HttpResponseException ex = Assert.Throws<HttpResponseException>(() => mock.GetSecretLookupTable(_postRequest)); // Assert HttpError error = await ex.Response.Content.ReadAsAsync<HttpError>(); Assert.Equal("Did not find any applications settings of the form 'MS_WebHookReceiverSecret_Pusher'. To receive WebHooks from the 'PusherWebHookReceiver' receiver, please add corresponding applications settings.", error.Message); }