private string GetBody(string template, INotificationTemplateData templateData)
        {
            var body = new StringBuilder(template);

            templateData.GetKeys().ForEach(key => body = body.Replace(key, templateData.GetValue(key)));
            return(body.ToString());
        }
        public async Task <NotificationDto> CreateNotification(INotificationTemplateData templateData)
        {
            if (templateData == null)
            {
                throw new ArgumentNullException(nameof(templateData));
            }

            var notificationTemplate = await
                                       _repository.SingleOrDefaultAsync(x => x.Abbreviation == templateData.TemplateAbbreviation);

            if (notificationTemplate == null)
            {
                throw new ArgumentException(
                          $"The notification template with abbreviation: {templateData.TemplateAbbreviation} does not exist in data store.");
            }

            return(new NotificationDto
            {
                Body = GetBody(notificationTemplate.Body, templateData), Subject = notificationTemplate.Subject
            });
        }
Ejemplo n.º 3
0
        public async Task Handle_WhenCalled_AttemptToSendProperNotificationExpected()
        {
            // ARRANGE
            var userEmail            = "*****@*****.**";
            var configuration        = Substitute.For <IConfiguration>();
            var configurationSection = Substitute.For <IConfigurationSection>();

            configurationSection.Value = "http://localhost:4200/AccountActivate/{token}";
            configuration.GetSection("AppSettings:AccountActivationUriFormat").Returns(configurationSection);

            var userService = Substitute.For <IUserService>();
            var user        = new UserForDetailedDto {
                Email = userEmail, ActivationToken = "activation_token"
            };

            userService.GetUser(default(string)).ReturnsForAnyArgs(user);

            var notificationService = Substitute.For <INotificationService>();
            INotificationTemplateData actualNotificationTemplateData = null;
            string actualRecipient = string.Empty;

            notificationService.When(x => x.SendAsync(Arg.Any <string>(), Arg.Any <INotificationTemplateData>())).Do(x =>
            {
                actualRecipient = x.ArgAt <string>(0);
                actualNotificationTemplateData = x.ArgAt <INotificationTemplateData>(1);
            });
            var expectedNotificationTemplateData =
                new UserRegisteredNotificationTemplateData($"http://localhost:4200/AccountActivate/{user.ActivationToken}",
                                                           user.Username);

            var sut = new UserRegisteredNotificationHandler(notificationService, userService, configuration);
            var userRegisteredNotificationEvent = new UserRegisteredNotificationEvent(userEmail);

            // ACT
            await sut.Handle(userRegisteredNotificationEvent, new CancellationToken());

            // ASSERT
            actualRecipient.Should().BeEquivalentTo(userEmail);
            actualNotificationTemplateData.Should().BeEquivalentTo(expectedNotificationTemplateData);
        }
Ejemplo n.º 4
0
        public async Task Handle_WhenCalled_AttemptToSendProperNotificationExpected()
        {
            // ARRANGE
            var userEmail            = "*****@*****.**";
            var configuration        = Substitute.For <IConfiguration>();
            var configurationSection = Substitute.For <IConfigurationSection>();

            configurationSection.Value = "http://server.com/ResetPassword/{token}";
            configuration.GetSection("AppSettings:ResetPasswordUriFormat").Returns(configurationSection);

            var userService = Substitute.For <IUserService>();
            var user        = new UserForDetailedDto {
                Email = userEmail, ResetPasswordToken = "reset_password_token", Username = "******"
            };

            userService.GetUser(default(string)).ReturnsForAnyArgs(user);

            var notificationService = Substitute.For <INotificationService>();
            INotificationTemplateData actualNotificationTemplateData = null;
            string actualRecipient = string.Empty;

            notificationService.When(x => x.SendAsync(Arg.Any <string>(), Arg.Any <INotificationTemplateData>())).Do(x =>
            {
                actualRecipient = x.ArgAt <string>(0);
                actualNotificationTemplateData = x.ArgAt <INotificationTemplateData>(1);
            });
            var expectedNotificationTemplateData =
                new ResetPasswordNotificationTemplateData("http://server.com/ResetPassword/reset_password_token",
                                                          user.Username);

            var sut = new ResetPasswordRequestNotificationHandler(notificationService, userService, configuration);
            var resetPasswordRequestNotificationEvent = new ResetPasswordRequestNotificationEvent(userEmail);

            // ACT
            await sut.Handle(resetPasswordRequestNotificationEvent, new CancellationToken());

            // ASSERT
            actualRecipient.Should().BeEquivalentTo(userEmail);
            actualNotificationTemplateData.Should().BeEquivalentTo(expectedNotificationTemplateData);
        }
Ejemplo n.º 5
0
        public async Task SendAsync(string recipient, INotificationTemplateData templateData)
        {
            if (templateData == null)
            {
                throw new ArgumentNullException(nameof(templateData));
            }

            MailAddress recipientMailAddress;

            try
            {
                recipientMailAddress = new MailAddress(recipient);
            }
            catch (FormatException e)
            {
                throw new ArgumentException($"Incorrect format for passed recipient email: `{recipient}`.", e);
            }

            var notification = await _notificationTemplateService.CreateNotification(templateData);

            // Send the email via gateway client
            await _emailClientProvider.SendAsync(recipientMailAddress, notification.Subject, notification.Body);
        }