Example #1
0
        public void CanRelayMessageAndPersistResult()
        {
            // setup
            var messageId = GetData().First().Id;

            server.TryRelayMessage(Arg.Any <DbModel.Message>(), Arg.Any <MailboxAddress[]>()).Returns(new RelayResult(GetData().First())
            {
                RelayRecipients = new List <RelayRecipientResult>()
                {
                    new RelayRecipientResult {
                        Email = "*****@*****.**", RelayDate = DateTime.UtcNow
                    }
                }
            });

            // act
            var result = controller.RelayMessage(messageId,
                                                 new MessageRelayOptions()
            {
                OverrideRecipientAddresses = new[] { "*****@*****.**" }
            });

            // expect ok result
            result.Should().BeOfType <OkResult>();

            var relay = context.MessageRelays.FirstOrDefault(mr => mr.MessageId == messageId);

            //expect MessageRelay persisted.
            relay.Should().NotBeNull();
        }
Example #2
0
        public IActionResult RelayMessage(Guid id, [FromBody] MessageRelayOptions options)
        {
            var message     = GetDbMessage(id);
            var relayResult = server.TryRelayMessage(message,
                                                     options?.OverrideRecipientAddresses?.Length > 0
                    ? options?.OverrideRecipientAddresses.Select(a => MailboxAddress.Parse(a)).ToArray()
                    : null);

            if (relayResult.Exceptions.Any())
            {
                var relayErrorSummary = string.Join(". ", relayResult.Exceptions.Select(e => e.Key.Address + ": " + e.Value.Message));
                return(Problem("Failed to relay to recipients: " + relayErrorSummary));
            }
            if (relayResult.WasRelayed)
            {
                foreach (var relay in relayResult.RelayRecipients)
                {
                    message.AddRelay(new MessageRelay {
                        SendDate = relay.RelayDate, To = relay.Email
                    });
                }
                messagesRepository.DbContext.SaveChanges();
            }
            return(Ok());
        }