Example #1
0
        public override void Init(IServiceContainer serviceContainer, IDictionary <string, string> jobArgsDictionary)
        {
            base.Init(serviceContainer, jobArgsDictionary);

            InitializationConfiguration = _serviceProvider.GetRequiredService <IOptionsSnapshot <InitializationConfiguration> >().Value;

            var serializer  = new ServiceBusMessageSerializer();
            var topicClient = new TopicClientWrapper(InitializationConfiguration.EmailPublisherConnectionString, InitializationConfiguration.EmailPublisherTopicName);
            var enqueuer    = new EmailMessageEnqueuer(topicClient, serializer, LoggerFactory.CreateLogger <EmailMessageEnqueuer>());

            EmailService = new AsynchronousEmailMessageService(
                enqueuer,
                LoggerFactory.CreateLogger <AsynchronousEmailMessageService>(),
                InitializationConfiguration);

            FromAddress = new MailAddress(InitializationConfiguration.MailFrom);

            var storageAccount = CloudStorageAccount.Parse(InitializationConfiguration.DataStorageAccount);
            var storageFactory = new AzureStorageFactory(
                storageAccount,
                InitializationConfiguration.ContainerName,
                LoggerFactory.CreateLogger <AzureStorage>());

            Storage = storageFactory.Create();
        }
Example #2
0
            public void ThrowsArgumentExceptionWhenSenderNull()
            {
                var recipients = new EmailRecipients(
                    to: new[] { new MailAddress("*****@*****.**") });
                var emailBuilder = new Mock <IEmailBuilder>();

                emailBuilder
                .Setup(m => m.GetRecipients())
                .Returns(recipients)
                .Verifiable();
                emailBuilder
                .Setup(m => m.Sender)
                .Returns <MailAddress>(null)
                .Verifiable();

                var emailMessageEnqueuerMock = new Mock <IEmailMessageEnqueuer>();
                var messageService           = new AsynchronousEmailMessageService(
                    emailMessageEnqueuerMock.Object,
                    Mock.Of <ILogger <AsynchronousEmailMessageService> >(),
                    Mock.Of <IMessageServiceConfiguration>());

                Assert.ThrowsAsync <ArgumentException>(() => messageService.SendMessageAsync(emailBuilder.Object, false, false));

                emailBuilder.Verify();
                emailMessageEnqueuerMock.Verify(
                    m => m.SendEmailMessageAsync(It.IsAny <EmailMessageData>()),
                    Times.Never);
            }
            public void ThrowsArgumentNullExceptionForNullEmailBuilder()
            {
                var emailMessageEnqueuer = new Mock <IEmailMessageEnqueuer>().Object;
                var messageService       = new AsynchronousEmailMessageService(
                    Configuration,
                    emailMessageEnqueuer);

                Assert.ThrowsAsync <ArgumentNullException>(() => messageService.SendMessageAsync(null, It.IsAny <bool>(), It.IsAny <bool>()));
            }
            public void DoesNotEnqueueMessageWhenRecipientsToListEmpty()
            {
                var emailBuilder = new Mock <IEmailBuilder>();

                emailBuilder
                .Setup(m => m.GetRecipients())
                .Returns(EmailRecipients.None)
                .Verifiable();

                var emailMessageEnqueuerMock = new Mock <IEmailMessageEnqueuer>();
                var messageService           = new AsynchronousEmailMessageService(
                    Configuration,
                    emailMessageEnqueuerMock.Object);

                messageService.SendMessageAsync(emailBuilder.Object, false, false);

                emailBuilder.Verify();
                emailMessageEnqueuerMock.Verify(
                    m => m.SendEmailMessageAsync(It.IsAny <EmailMessageData>()),
                    Times.Never);
            }
Example #5
0
            public async Task DoesNotEnqueueMessageWhenRecipientsToListEmpty()
            {
                var emailBuilder = new Mock <IEmailBuilder>();

                emailBuilder
                .Setup(m => m.GetRecipients())
                .Returns(new EmailRecipients(to: new MailAddress[0]))
                .Verifiable();

                var emailMessageEnqueuerMock = new Mock <IEmailMessageEnqueuer>();
                var messageService           = new AsynchronousEmailMessageService(
                    emailMessageEnqueuerMock.Object,
                    Mock.Of <ILogger <AsynchronousEmailMessageService> >(),
                    Mock.Of <IMessageServiceConfiguration>());

                await messageService.SendMessageAsync(emailBuilder.Object, false, false);

                emailBuilder.Verify();
                emailMessageEnqueuerMock.Verify(
                    m => m.SendEmailMessageAsync(It.IsAny <EmailMessageData>()),
                    Times.Never);
            }
            public void ThrowsArgumentExceptionWhenPlainTextAndHtmlBodyEmpty()
            {
                var recipients = new EmailRecipients(
                    to: new[] { new MailAddress("*****@*****.**") });
                var emailBuilder = new Mock <IEmailBuilder>();

                emailBuilder
                .Setup(m => m.Sender)
                .Returns(new MailAddress("*****@*****.**"))
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetRecipients())
                .Returns(recipients)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetSubject())
                .Returns("subject")
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetBody(EmailFormat.Html))
                .Returns <string>(null)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetBody(EmailFormat.PlainText))
                .Returns <string>(null)
                .Verifiable();

                var emailMessageEnqueuerMock = new Mock <IEmailMessageEnqueuer>();
                var messageService           = new AsynchronousEmailMessageService(
                    Configuration,
                    emailMessageEnqueuerMock.Object);

                Assert.ThrowsAsync <ArgumentException>(() => messageService.SendMessageAsync(emailBuilder.Object, false, false));

                emailBuilder.Verify();
                emailMessageEnqueuerMock.Verify(
                    m => m.SendEmailMessageAsync(It.IsAny <EmailMessageData>()),
                    Times.Never);
            }
Example #7
0
            public async Task WillSendCopyToSenderIfAsked()
            {
                var subject       = "subject";
                var htmlBody      = "html body";
                var plainTextBody = "plain-text body";
                var toAddress     = "*****@*****.**";
                var ccAddress     = "*****@*****.**";
                var bccAddress    = "*****@*****.**";
                var fromAddress   = "*****@*****.**";
                var senderAddress = "*****@*****.**";

                var recipients = new EmailRecipients(
                    to: new[] { new MailAddress(toAddress) },
                    cc: new[] { new MailAddress(ccAddress) },
                    bcc: new[] { new MailAddress(bccAddress) },
                    replyTo: new[] { new MailAddress(fromAddress) });
                var emailBuilder = new Mock <IEmailBuilder>();

                emailBuilder
                .Setup(m => m.GetRecipients())
                .Returns(recipients)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetSubject())
                .Returns(subject)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetBody(EmailFormat.Html))
                .Returns(htmlBody)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetBody(EmailFormat.PlainText))
                .Returns(plainTextBody)
                .Verifiable();
                emailBuilder
                .Setup(m => m.Sender)
                .Returns(new MailAddress(senderAddress))
                .Verifiable();

                var messageServiceConfiguration = new TestMessageServiceConfiguration();
                var emailMessageEnqueuerMock    = new Mock <IEmailMessageEnqueuer>();
                var messageService = new AsynchronousEmailMessageService(
                    emailMessageEnqueuerMock.Object,
                    Mock.Of <ILogger <AsynchronousEmailMessageService> >(),
                    messageServiceConfiguration);

                // We want to copy the sender but not disclose the sender address
                await messageService.SendMessageAsync(
                    emailBuilder.Object,
                    copySender : true,
                    discloseSenderAddress : false);

                emailBuilder.Verify();

                // Verify the original email is sent (not disclosing the sender address)
                emailMessageEnqueuerMock.Verify(
                    m => m.SendEmailMessageAsync(It.Is <EmailMessageData>(
                                                     d =>
                                                     d.HtmlBody == htmlBody &&
                                                     d.PlainTextBody == plainTextBody &&
                                                     d.Subject == subject &&
                                                     d.Sender == senderAddress &&
                                                     d.To.Contains(toAddress) &&
                                                     d.CC.Contains(ccAddress) &&
                                                     d.Bcc.Contains(bccAddress) &&
                                                     d.ReplyTo.Contains(fromAddress) &&
                                                     d.MessageTrackingId != Guid.Empty &&
                                                     d.DeliveryCount == 0)),
                    Times.Once);

                // Verify a copy is sent to the sender
                var expectedPlainTextBody = string.Format(
                    CultureInfo.CurrentCulture,
                    "You sent the following message via {0}: {1}{1}{2}",
                    messageServiceConfiguration.GalleryOwner.DisplayName,
                    Environment.NewLine,
                    plainTextBody);

                var expectedHtmlBody = string.Format(
                    CultureInfo.CurrentCulture,
                    "You sent the following message via {0}: {1}{1}{2}",
                    messageServiceConfiguration.GalleryOwner.DisplayName,
                    Environment.NewLine,
                    htmlBody);

                emailMessageEnqueuerMock.Verify(
                    m => m.SendEmailMessageAsync(It.Is <EmailMessageData>(
                                                     d =>
                                                     d.HtmlBody == expectedHtmlBody &&
                                                     d.PlainTextBody == expectedPlainTextBody &&
                                                     d.Subject == subject + " [Sender Copy]" &&
                                                     d.Sender == messageServiceConfiguration.GalleryOwner.Address &&
                                                     d.To.Single() == fromAddress &&
                                                     !d.CC.Any() &&
                                                     !d.Bcc.Any() &&
                                                     d.ReplyTo.Single() == fromAddress &&
                                                     d.MessageTrackingId != Guid.Empty &&
                                                     d.DeliveryCount == 0)),
                    Times.Once);

                emailMessageEnqueuerMock.Verify(m => m.SendEmailMessageAsync(It.IsAny <EmailMessageData>()), Times.Exactly(2));
            }
Example #8
0
            public async Task CreatesAndSendsExpectedMessageOnce()
            {
                var subject        = "subject";
                var htmlBody       = "html body";
                var plainTextBody  = "plain-text body";
                var toAddress      = "*****@*****.**";
                var ccAddress      = "*****@*****.**";
                var bccAddress     = "*****@*****.**";
                var replyToAddress = "*****@*****.**";
                var senderAddress  = "*****@*****.**";

                var recipients = new EmailRecipients(
                    to: new[] { new MailAddress(toAddress) },
                    cc: new[] { new MailAddress(ccAddress) },
                    bcc: new[] { new MailAddress(bccAddress) },
                    replyTo: new[] { new MailAddress(replyToAddress) });
                var emailBuilder = new Mock <IEmailBuilder>();

                emailBuilder
                .Setup(m => m.GetRecipients())
                .Returns(recipients)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetSubject())
                .Returns(subject)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetBody(EmailFormat.Html))
                .Returns(htmlBody)
                .Verifiable();
                emailBuilder
                .Setup(m => m.GetBody(EmailFormat.PlainText))
                .Returns(plainTextBody)
                .Verifiable();
                emailBuilder
                .Setup(m => m.Sender)
                .Returns(new MailAddress(senderAddress))
                .Verifiable();

                var emailMessageEnqueuerMock = new Mock <IEmailMessageEnqueuer>();
                var messageService           = new AsynchronousEmailMessageService(
                    emailMessageEnqueuerMock.Object,
                    Mock.Of <ILogger <AsynchronousEmailMessageService> >(),
                    Mock.Of <IMessageServiceConfiguration>());

                await messageService.SendMessageAsync(emailBuilder.Object, false, false);

                emailBuilder.Verify();
                emailMessageEnqueuerMock.Verify(
                    m => m.SendEmailMessageAsync(It.Is <EmailMessageData>(
                                                     d =>
                                                     d.HtmlBody == htmlBody &&
                                                     d.PlainTextBody == plainTextBody &&
                                                     d.Subject == subject &&
                                                     d.Sender == senderAddress &&
                                                     d.To.Contains(toAddress) &&
                                                     d.CC.Contains(ccAddress) &&
                                                     d.Bcc.Contains(bccAddress) &&
                                                     d.ReplyTo.Contains(replyToAddress) &&
                                                     d.MessageTrackingId != Guid.Empty &&
                                                     d.DeliveryCount == 0)),
                    Times.Once);

                emailMessageEnqueuerMock.Verify(m => m.SendEmailMessageAsync(It.IsAny <EmailMessageData>()), Times.Once);
            }
        protected SupportRequestsNotificationScheduledTask(
            InitializationConfiguration configuration,
            Func <Task <SqlConnection> > openSupportRequestSqlConnectionAsync,
            ILoggerFactory loggerFactory)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            var serializer     = new ServiceBusMessageSerializer();
            var topicClient    = new TopicClientWrapper(configuration.EmailPublisherConnectionString, configuration.EmailPublisherTopicName);
            var enqueuer       = new EmailMessageEnqueuer(topicClient, serializer, loggerFactory.CreateLogger <EmailMessageEnqueuer>());
            var messageService = new AsynchronousEmailMessageService(
                enqueuer,
                loggerFactory.CreateLogger <AsynchronousEmailMessageService>(),
                configuration);

            _messagingService = new MessagingService(messageService, loggerFactory.CreateLogger <MessagingService>());

            _supportRequestRepository = new SupportRequestRepository(loggerFactory, openSupportRequestSqlConnectionAsync);
        }