Esempio n. 1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "lmi/webhook")] HttpRequest?request)
        {
            try
            {
                logger.LogInformation("Received webhook request");

                using var streamReader = new StreamReader(request?.Body !);
                var requestBody = await streamReader.ReadToEndAsync().ConfigureAwait(false);

                if (string.IsNullOrEmpty(requestBody))
                {
                    logger.LogError($"{nameof(request)} body is null");
                    return(new BadRequestResult());
                }

                var webhookRequestModel = lmiWebhookReceiverService.ExtractEvent(requestBody);
                switch (webhookRequestModel.WebhookCommand)
                {
                case WebhookCommand.SubscriptionValidation:
                    return(new OkObjectResult(webhookRequestModel.SubscriptionValidationResponse));

                case WebhookCommand.ReportDeltaForAll:
                    if (!environmentValues.IsDraftEnvironment)
                    {
                        return(new BadRequestResult());
                    }

                    return(new StatusCodeResult((int)await lmiWebhookReceiverService.ReportAll().ConfigureAwait(false)));

                case WebhookCommand.ReportDeltaForSoc:
                    if (!environmentValues.IsDraftEnvironment)
                    {
                        return(new BadRequestResult());
                    }

                    return(new StatusCodeResult((int)await lmiWebhookReceiverService.ReportSoc(webhookRequestModel.ContentId).ConfigureAwait(false)));

                default:
                    return(new BadRequestResult());
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex.ToString());
                return(new StatusCodeResult((int)HttpStatusCode.InternalServerError));
            }
        }
        public async Task LmiWebhookHttpTriggerPostForSubscriptionValidationReturnsOk()
        {
            // Arrange
            var expectedResult      = new StatusCodeResult((int)HttpStatusCode.OK);
            var function            = new LmiWebhookHttpTrigger(fakeLogger, draftEnvironmentValues, fakeLmiWebhookReceiverService);
            var request             = BuildRequestWithValidBody("a request body");
            var webhookRequestModel = new WebhookRequestModel
            {
                WebhookCommand = WebhookCommand.SubscriptionValidation,
            };

            A.CallTo(() => fakeLmiWebhookReceiverService.ExtractEvent(A <string> .Ignored)).Returns(webhookRequestModel);

            // Act
            var result = await function.Run(request).ConfigureAwait(false);

            // Assert
            A.CallTo(() => fakeLmiWebhookReceiverService.ExtractEvent(A <string> .Ignored)).MustHaveHappenedOnceExactly();
            A.CallTo(() => fakeLmiWebhookReceiverService.ReportAll()).MustNotHaveHappened();
            A.CallTo(() => fakeLmiWebhookReceiverService.ReportSoc(A <Guid> .Ignored)).MustNotHaveHappened();
            var statusResult = Assert.IsType <OkObjectResult>(result);

            Assert.Equal(expectedResult.StatusCode, statusResult.StatusCode);
        }