Esempio n. 1
0
        public async Task InvokeAsyncInvalidShouldLogError()
        {
            var testOptions = new Mock <IOptions <TwitterOptions> >();

            testOptions.SetupGet(x => x.Value).Returns(_twitterOptions.Object);

            var adapter = new TwitterAdapter(testOptions.Object);

            var httpResponse = new Mock <HttpResponse>();

            httpResponse.SetupAllProperties();
            httpResponse.Object.Body = new MemoryStream();

            var request = new Mock <HttpRequest>();

            request.SetupAllProperties();
            request.Object.Method = HttpMethods.Get;
            request.Setup(req => req.Query.TryGetValue("crc_token", out _values)).Returns(false);

            var context = new Mock <HttpContext>();

            context.SetupGet(x => x.Response).Returns(httpResponse.Object);
            context.SetupGet(x => x.Request).Returns(request.Object);

            var middleware = new WebhookMiddleware(testOptions.Object, _testLogger.Object, _testBot.Object, adapter);

            await middleware.InvokeAsync(context.Object, _testDelegate.Object);

            _testLogger.Verify(x => x.Log(LogLevel.Error, 0, It.IsAny <FormattedLogValues>(), null, It.IsAny <Func <object, Exception, string> >()), Times.Once);
        }
Esempio n. 2
0
        public async Task InvokeAsyncValidShouldSucceed()
        {
            var testOptions = new Mock <IOptions <TwitterOptions> >();

            testOptions.SetupGet(x => x.Value).Returns(_twitterOptions.Object);

            var adapter = new TwitterAdapter(testOptions.Object);

            var httpResponse = new Mock <HttpResponse>();

            httpResponse.SetupAllProperties();
            httpResponse.Object.Body = new MemoryStream();

            var request = new Mock <HttpRequest>();

            request.SetupAllProperties();
            request.Object.Method = HttpMethods.Get;
            request.Setup(req => req.Query.TryGetValue("crc_token", out _values)).Returns(true);

            var context = new Mock <HttpContext>();

            context.SetupGet(x => x.Response).Returns(httpResponse.Object);
            context.SetupGet(x => x.Request).Returns(request.Object);

            var middleware = new WebhookMiddleware(testOptions.Object, _testLogger.Object, _testBot.Object, adapter);

            await middleware.InvokeAsync(context.Object, _testDelegate.Object);

            Assert.AreEqual(httpResponse.Object.StatusCode, (int)HttpStatusCode.OK);
        }
Esempio n. 3
0
        public async Task RegisteredConsumerShouldDeserializePayloadCorrectlyForMultipleConsumers()
        {
            //Arrange
            var consumerBuilder = new ConsumerBuilder();
            var called          = false;
            var secondCalled    = false;
            var hookEntry       = new Entry <dynamic>();
            var secondEntry     = new Entry <dynamic>();

            consumerBuilder.AddConsumer <Entry <dynamic> >("Test", SystemWebhookTopics.Entry, SystemWebhookActions.Publish, (s) => { called = true; hookEntry = s; return(new { Result = "Ok" }); });
            consumerBuilder.AddConsumer <Entry <dynamic> >("Test", SystemWebhookTopics.Entry, SystemWebhookActions.Publish, (s) => { secondCalled = true; secondEntry = s; return(new { Result = "Ok" }); });
            var middleware = new WebhookMiddleware(null, consumerBuilder.Build());
            var context    = new DefaultHttpContext();

            var entry = new Entry <dynamic>()
            {
                SystemProperties = new SystemProperties
                {
                    Id = "123"
                },
                Fields = new
                {
                    Title = new Dictionary <string, string>
                    {
                        { "en-US", "Hello" },
                        { "sv-SE", "Hallå" }
                    }
                }
            };

            string jsonObject = JsonConvert.SerializeObject(entry);

            var memoryStream = new MemoryStream();
            var writer       = new StreamWriter(memoryStream);

            writer.Write(jsonObject);
            writer.Flush();
            memoryStream.Position = 0;

            context.Request.Body = memoryStream;
            context.Request.Headers.Add("X-Contentful-Topic", new Microsoft.Extensions.Primitives.StringValues("ContentManagement.Entry.publish"));
            context.Request.Headers.Add("X-Contentful-Webhook-Name", new Microsoft.Extensions.Primitives.StringValues("Test"));
            context.Request.ContentType = "application/vnd.contentful.management.v1+json";
            //Act
            await middleware.Invoke(context);

            //Assert
            Assert.True(called);
            Assert.Equal("123", hookEntry.SystemProperties.Id);
            Assert.Equal("Hello", hookEntry.Fields.Title["en-US"].ToString());
            Assert.True(secondCalled);
            Assert.Equal("123", secondEntry.SystemProperties.Id);
            Assert.Equal("Hello", secondEntry.Fields.Title["en-US"].ToString());
        }
Esempio n. 4
0
        public async Task RegisteredConsumerShouldFireForCorrectTopic()
        {
            //Arrange
            var consumerBuilder = new ConsumerBuilder();
            var called          = false;

            consumerBuilder.AddConsumer <Entry <dynamic> >("Test", SystemWebhookTopics.Entry, SystemWebhookActions.Publish, (s) => { called = true; return(new { Result = "Ok" }); });
            var middleware = new WebhookMiddleware(null, consumerBuilder.Build());
            var context    = new DefaultHttpContext();

            context.Request.Headers.Add("X-Contentful-Topic", new Microsoft.Extensions.Primitives.StringValues("ContentManagement.Entry.publish"));
            context.Request.Headers.Add("X-Contentful-Webhook-Name", new Microsoft.Extensions.Primitives.StringValues("Test"));
            context.Request.ContentType = "application/vnd.contentful.management.v1+json";
            //Act
            await middleware.Invoke(context);

            //Assert
            Assert.True(called);
        }