Beispiel #1
0
        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_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_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_Succeeds_IfValidSlashPostRequest(string id)
        {
            // Arrange
            Initialize(GetConfigValue(id, TestSecret));
            _postRequest.Content = new StringContent(TestSlashContent, Encoding.UTF8, "application/x-www-form-urlencoded");

            var actions = new List <string> {
                "hello!"
            };

            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();
        }
Beispiel #5
0
        public async Task ReceiveAsync_Succeeds_CallingHandler_IfTestIdAndTestMode()
        {
            // Arrange
            Initialize(TestSecret, inTestMode: true);
            _postRequest.Content = new StringContent("{ \"type\": \"action\", \"id\": \"" + StripeWebHookReceiver.TestId + "\" }", Encoding.UTF8, "application/json");
            List <string> actions = new List <string> {
                "action"
            };

            ReceiverMock.Protected()
            .Setup <Task <HttpResponseMessage> >("ExecuteWebHookAsync", string.Empty, RequestContext, _postRequest, actions, ItExpr.IsAny <object>())
            .ReturnsAsync(new HttpResponseMessage())
            .Verifiable();

            // Act
            await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest);

            // Assert
            Assert.Equal(0, _handlerMock.Counter);
            ReceiverMock.Verify();
        }
        public async Task ReceiveAsync_ReturnsError_IfPostHasNoEventParameter()
        {
            // Arrange
            Initialize(TestSecret);
            ReceiverMock.Protected()
            .Setup <bool>("ValidateReceivedEvent", RequestContext, ItExpr.IsAny <NameValueCollection>(), "{ }")
            .Returns(true)
            .Verifiable();
            _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 'event_type' property.", 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_Throws_IfPostIsNotJson()
        {
            // Arrange
            Initialize(TestSecret);
            ReceiverMock.Protected()
            .Setup <bool>("ValidateReceivedEvent", RequestContext, ItExpr.IsAny <NameValueCollection>(), TestContent)
            .Returns(true)
            .Verifiable();
            _postRequest.Content = new StringContent(TestContent, Encoding.UTF8, "text/plain");

            // 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 JSON.", 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_Throws_IfNoRequestBody()
        {
            // Arrange
            Initialize(TestSecret);
            ReceiverMock.Protected()
            .Setup <bool>("ValidateReceivedEvent", RequestContext, ItExpr.IsAny <NameValueCollection>(), string.Empty)
            .Returns(true)
            .Verifiable();
            _postRequest.Content = null;

            // Act
            HttpResponseException ex = await Assert.ThrowsAsync <HttpResponseException>(() => ReceiverMock.Object.ReceiveAsync(string.Empty, RequestContext, _postRequest));

            // Assert
            HttpError error = await ex.Response.Content.ReadAsAsync <HttpError>();

            Assert.Equal("The WebHook request entity body cannot be empty.", 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_Succeeds_IfValidPostRequest()
        {
            // Arrange
            Initialize(TestSecret);
            List <string> actions = new List <string> {
                "action1"
            };

            ReceiverMock.Protected()
            .Setup <bool>("ValidateReceivedEvent", RequestContext, ItExpr.IsAny <NameValueCollection>(), TestContent)
            .Returns(true)
            .Verifiable();
            ReceiverMock.Protected()
            .Setup <Task <HttpResponseMessage> >("ExecuteWebHookAsync", TestId, RequestContext, _postRequest, actions, ItExpr.IsAny <object>())
            .ReturnsAsync(new HttpResponseMessage())
            .Verifiable();

            // Act
            await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest);

            // Assert
            ReceiverMock.Verify();
        }
Beispiel #10
0
        public async Task ReceiveAsync_Succeeds_IfValidPostRequest_WithExtraSignatureHeaderContent()
        {
            // Arrange
            Initialize(TestSecret);
            var header = $" {TestSignatureHeader} , {ExtraHeaderContent} ";

            _postRequest.Headers.Add(StripeWebHookReceiver.SignatureHeaderName, header);

            var actions = new List <string> {
                "action"
            };

            ReceiverMock.Protected()
            .Setup <Task <HttpResponseMessage> >("ExecuteWebHookAsync", TestId, RequestContext, _postRequest, actions, ItExpr.IsAny <object>())
            .ReturnsAsync(new HttpResponseMessage())
            .Verifiable();

            // Act
            await ReceiverMock.Object.ReceiveAsync(TestId, RequestContext, _postRequest);

            // Assert
            ReceiverMock.Verify();
        }