public virtual async Task <bool> SendEmailAsync(ContactUsRequest sendEmailRequest, EmailTemplate template = null)
        {
            if (simulateEmailResponsesService.IsThisSimulationRequest(sendEmailRequest.Email))
            {
                return(simulateEmailResponsesService.SimulateEmailResponse(sendEmailRequest.Email));
            }

            if (template == null)
            {
                template = emailTemplateRepository.GetByTemplateName(sendEmailRequest.TemplateName);
            }

            if (template == null)
            {
                return(false);
            }

            var from             = new EmailAddress(sendEmailRequest.Email, $"{sendEmailRequest.FirstName} {sendEmailRequest.LastName}");
            var subject          = template.Subject;
            var to               = template.To?.Split(';').Select(toEmail => new EmailAddress(toEmail.Trim(), toEmail.Trim())).ToList();
            var plainTextContent = mergeEmailContentService.MergeTemplateBodyWithContent(sendEmailRequest, template.BodyNoHtml);
            var htmlContent      = mergeEmailContentService.MergeTemplateBodyWithContent(sendEmailRequest, template.Body);
            var msg              = MailHelper.CreateSingleEmailToMultipleRecipients(@from, to, subject, plainTextContent, htmlContent);
            var clientResponse   = await sendGridClient.SendEmailAsync(msg);

            var auditResponse = mapper.Map <SendEmailResponse>(clientResponse);
            var result        = clientResponse.StatusCode.Equals(HttpStatusCode.Accepted);

            auditRepository.CreateAudit(sendEmailRequest, template, auditResponse);
            return(result);
        }
        public FamSendGridEmailServiceTests()
        {
            var defaultTemplate = new EmailTemplate
            {
                To      = "*****@*****.**",
                Body    = "Body",
                Subject = DefaultSubject,
            };

            fakeEmailTemplateRepository = A.Fake <IEmailTemplateRepository>();
            A.CallTo(() => fakeEmailTemplateRepository.GetByTemplateName(A <string> .Ignored)).Returns(defaultTemplate);

            fakeMergeEmailContentService = A.Fake <IMergeEmailContent <ContactUsRequest> >();
            fakeSendGridClient           = A.Fake <ISendGridClient>();
            var defaultResponse = new Response(HttpStatusCode.Accepted, A.Fake <HttpContent>(), null);

            A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> .Ignored, A <CancellationToken> .Ignored)).Returns(defaultResponse);

            fakeSimulateEmailResponsesService = A.Fake <ISimulateEmailResponses>();
            fakeAuditRepository   = A.Fake <IAuditNoncitizenEmailRepository <ContactUsRequest> >();
            fakeHttpClientService = A.Fake <IHttpClientService <INoncitizenEmailService <ContactUsRequest> > >();

            var areaRoutingApiResponse = new AreaRoutingApiResponse {
                EmailAddress = EmailAddressFromAreaRouting
            };
            var httpResponseMessage = new HttpResponseMessage {
                Content = new StringContent(JsonConvert.SerializeObject(areaRoutingApiResponse), Encoding.Default, "application/json")
            };

            A.CallTo(() => fakeHttpClientService.GetAsync(A <string> .Ignored, A <FaultToleranceType> .Ignored)).Returns(httpResponseMessage);
            fakeMapper = A.Fake <IMapper>();
            fakeConfigurationProvider = A.Fake <IConfigurationProvider>();
            fakeApplicationLogger     = A.Fake <IApplicationLogger>();
        }
コード例 #3
0
        public async Task SendEmailAsyncTest(ContactUsRequest contactUsRequest, bool isThisSimulation, bool expectation)
        {
            //Assign
            var sendEmailService = new SendGridEmailService(fakeEmailTemplateRepository, fakeMergeEmailContentService, fakeAuditRepository, fakeSimulateEmailResponsesService, fakeSendGridClient, fakeMapper);

            A.CallTo(() => fakeEmailTemplateRepository.GetByTemplateName(A <string> ._)).Returns(string.IsNullOrEmpty(contactUsRequest.TemplateName) ? null : goodEmailTemplate);
            A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).Returns(nameof(IMergeEmailContent <ContactUsRequest> .MergeTemplateBodyWithContent));
            A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).Returns(new Response(HttpStatusCode.Accepted, null, null));
            A.CallTo(() => fakeConfiguration.GetConfig <string>(A <string> ._)).Returns(string.Empty);
            A.CallTo(() => fakeSimulateEmailResponsesService.IsThisSimulationRequest(A <string> ._)).Returns(isThisSimulation);
            A.CallTo(() => fakeSimulateEmailResponsesService.SimulateEmailResponse(A <string> ._)).Returns(expectation);
            A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).DoesNothing();
            A.CallTo(() => fakeMapper.Map <SendEmailResponse>(A <object> ._)).Returns(A.Dummy <SendEmailResponse>());

            //Act
            var result = await sendEmailService.SendEmailAsync(contactUsRequest);

            //Assert
            result.Should().Be(expectation);
            if (isThisSimulation)
            {
                A.CallTo(() => fakeEmailTemplateRepository.GetByTemplateName(A <string> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).MustNotHaveHappened();
                A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).MustNotHaveHappened();
            }
            else
            {
                A.CallTo(() => fakeEmailTemplateRepository.GetByTemplateName(A <string> ._)).MustHaveHappened();
                if (expectation)
                {
                    A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).MustHaveHappened(Repeated.Exactly.Twice);
                    A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).MustHaveHappened();
                    A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).MustHaveHappened();
                }
                else
                {
                    A.CallTo(() => fakeSendGridClient.SendEmailAsync(A <SendGridMessage> ._, A <CancellationToken> ._)).MustNotHaveHappened();
                    A.CallTo(() => fakeMergeEmailContentService.MergeTemplateBodyWithContent(A <ContactUsRequest> ._, A <string> ._)).MustNotHaveHappened();
                    A.CallTo(() => fakeAuditRepository.CreateAudit(A <ContactUsRequest> ._, A <EmailTemplate> ._, A <SendEmailResponse> ._)).MustNotHaveHappened();
                }
            }
        }
コード例 #4
0
        public override async Task <bool> SendEmailAsync(ContactUsRequest sendEmailRequest, EmailTemplate template = null)
        {
            if (sendEmailRequest.ContactOption == nameof(ContactOption.ContactAdviser))
            {
                if (template == null)
                {
                    template = emailTemplateRepository.GetByTemplateName(sendEmailRequest.TemplateName);
                }

                try
                {
                    var url       = $"{configuration.GetConfig<string>(Constants.AreaRoutingApiServiceUrl)}?location={sendEmailRequest.Postcode}";
                    var accessKey = configuration.GetConfig <string>(Constants.AreaRoutingApiSubscriptionKey);

                    httpClientService.AddHeader(Constants.OcpApimSubscriptionKey, accessKey);
                    var response = await this.httpClientService.GetAsync(url);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        var areaRoutingApiResponse = await response.Content.ReadAsAsync <AreaRoutingApiResponse>();

                        template.To = areaRoutingApiResponse.EmailAddress;
                    }
                }
                catch (LoggedException)
                {
                    return(await base.SendEmailAsync(sendEmailRequest, template).ConfigureAwait(false));
                }

                return(await base.SendEmailAsync(sendEmailRequest, template).ConfigureAwait(false));
            }
            else
            {
                return(await base.SendEmailAsync(sendEmailRequest, null).ConfigureAwait(false));
            }
        }