public async Task PublishEventToTopic_ShouldReturnFalse_WhenBadTopicKey()
        {
            // Arrange
            var gridEvent = new EventGridEvent();

            // Arrange Mocks
            Mock.Get(_settingsProvider)
            .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundKeySettingName))
            .Returns((string)null);

            // Act
            var result = await _eventGridPublisher.PublishEventToTopic(gridEvent).ConfigureAwait(true);

            // Assert
            result.ShouldBe(false);
            Mock.Get(_logger).Verify(x =>
                                     x.LogExceptionObject(LogEventIds.ExceptionPublishingEvent,
                                                          It.IsAny <ArgumentException>(),
                                                          It.IsAny <object>()),
                                     Times.Once,
                                     "An exception should be logged when the handler fails publishing the event");
        }
Esempio n. 2
0
        private async Task <bool> PublishEventToTopicAsync(EventGridEvent outputEvent, EventGridEvent inputEvent)
        {
            bool eventPublished;

            try
            {
                eventPublished = await _eventPublisher.PublishEventToTopic(outputEvent).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Log.LogExceptionObject(LogEventIds.FailedCriticallyToPublishEvent, ex, new { inputEvent, outputEvent });
                return(false);
            }

            if (!eventPublished)
            {
                Log.LogEventObject(LogEventIds.FailedToPublishEvent, new { inputEvent, outputEvent });
            }

            return(eventPublished);
        }
Esempio n. 3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequest req,
            CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogEvent(LogEventIds.FunctionAppShuttingDown, LogEventIds.FunctionAppShuttingDown.Name);
                throw new OperationCanceledException("Function invoked with a canceled cancellation token.");
            }

            try
            {
                cancellationToken.Register(() =>
                {
                    _logger.LogEvent(LogEventIds.FunctionAppShuttingDown, LogEventIds.FunctionAppShuttingDown.Name);
                });

                _logger.LogEvent(LogEventIds.CallbackFunctionTriggered, $"C# HTTP trigger function processed a request. Path={req?.Path}");

                // TODO: not sure why we have to get the byte array.. might be legacy code
                // MemoryStream stream = new MemoryStream();
                // await req.Body.CopyToAsync(stream);
                // byte[] bodyByteArray = stream.ToArray();

                string requestBody;
                using (var sr = new StreamReader(req.Body))
                {
                    requestBody = await sr.ReadToEndAsync().ConfigureAwait(false);
                }

                if (req.Headers.TryGetValue("ms-signature", out _))
                {
                    // TODO: need to verify headers here. However not sure how to get the signing key.
                    MediaServicesV2NotificationMessage notificationMessage = JsonConvert.DeserializeObject <MediaServicesV2NotificationMessage>(requestBody);

                    var eventGridEventId = System.Guid.NewGuid().ToString();
                    var eventToPublish   = new Microsoft.Azure.EventGrid.Models.EventGridEvent
                    {
                        Id          = eventGridEventId,
                        Data        = notificationMessage,
                        EventTime   = System.DateTime.UtcNow,
                        EventType   = CustomEventTypes.ResponseEncodeMediaServicesV2TranslateCallback,
                        Subject     = $"/{CustomEventTypes.ResponseEncodeMediaServicesV2TranslateCallback}/{eventGridEventId}",
                        DataVersion = "1.0",
                    };

                    await _publisher.PublishEventToTopic(eventToPublish).ConfigureAwait(false);

                    _logger.LogEvent(LogEventIds.CallbackFunctionNotificationMessageProcessed, "processed notification message");
                }
                else
                {
                    _logger.LogEvent(LogEventIds.RequestIsMissingVerifyWebHookRequestSignature, "VerifyWebHookRequestSignature failed.");
                    return(new BadRequestObjectResult("VerifyWebHookRequestSignature failed."));
                }

                return(new OkObjectResult("OK"));
            }
            catch (OperationCanceledException oce)
            {
                _logger.LogEventObject(LogEventIds.OperationCancelException, oce);
                throw;
            }
        }