Esempio n. 1
0
        public async Task EmailNotification_NotActiveNotification_FailSend()
        {
            //Arrange
            var language     = "default";
            var subject      = "some subject";
            var body         = "some body";
            var notification = new RegistrationEmailNotification()
            {
                Templates = new List <NotificationTemplate>()
                {
                    new EmailNotificationTemplate()
                    {
                        Subject = subject,
                        Body    = body,
                    }
                },
                LanguageCode = language
            };

            //Act
            var result = await _sender.SendNotificationAsync(notification);

            //Assert
            Assert.False(result.IsSuccess);
        }
Esempio n. 2
0
        public async Task SendNotification_SetCustomValidationError_NotSend()
        {
            //Arrange
            var language     = "default";
            var subject      = "some subject";
            var body         = "some body";
            var notification = new RegistrationEmailNotification()
            {
                Templates = new List <NotificationTemplate>()
                {
                    new EmailNotificationTemplate()
                    {
                        Subject = subject,
                        Body    = body,
                    }
                },
                LanguageCode = language
            };

            notification.SetCustomValidationError("some error");

            var message = new EmailNotificationMessage()
            {
                Id       = "1",
                From     = "*****@*****.**",
                To       = "*****@*****.**",
                Subject  = subject,
                Body     = body,
                SendDate = DateTime.Now
            };

            _messageServiceMock.Setup(ms => ms.SaveNotificationMessagesAsync(new NotificationMessage[] { message }));
            _messageSenderMock.Setup(ms => ms.SendNotificationAsync(It.IsAny <NotificationMessage>())).Throws(new SmtpException());
            _messageServiceMock.Setup(ms => ms.GetNotificationsMessageByIds(It.IsAny <string[]>())).ReturnsAsync(new[] { message })
            .Callback(() =>
            {
                message.Status = NotificationMessageStatus.Error;
            });

            var sender = GetNotificationSender();

            //Act
            var sendResult = await sender.SendNotificationAsync(notification);

            //Assert
            Assert.False(sendResult.IsSuccess);
            Assert.Equal("Can't send notification message by . There are errors.", sendResult.ErrorMessage);
        }
Esempio n. 3
0
        public async Task RegistrationEmailNotification_SentNotification()
        {
            //Arrange
            var language     = "default";
            var subject      = "Your login - {{ login }}.";
            var body         = "Thank you for registration {{ firstname }} {{ lastname }}!!!";
            var notification = new RegistrationEmailNotification()
            {
                FirstName = "First Name",
                LastName  = "Last Name",
                Login     = "******",
                Templates = new List <NotificationTemplate>()
                {
                    new EmailNotificationTemplate()
                    {
                        Subject = subject,
                        Body    = body,
                    }
                },
                LanguageCode = language,
                IsActive     = true
            };
            var date    = new DateTime(2018, 02, 20, 10, 00, 00);
            var message = new EmailNotificationMessage()
            {
                Id       = "1",
                From     = "*****@*****.**",
                To       = "*****@*****.**",
                Subject  = subject,
                Body     = body,
                SendDate = date
            };

            _messageServiceMock.Setup(ms => ms.GetNotificationsMessageByIds(It.IsAny <string[]>()))
            .ReturnsAsync(new[] { message });

            //Act
            var result = await _sender.SendNotificationAsync(notification);

            //Assert
            Assert.True(result.IsSuccess);
        }
Esempio n. 4
0
        public async Task EmailNotification_SuccessSend()
        {
            //Arrange
            string language     = null;
            var    subject      = "some subject";
            var    body         = "some body";
            var    notification = new RegistrationEmailNotification()
            {
                Templates = new List <NotificationTemplate>()
                {
                    new EmailNotificationTemplate()
                    {
                        Subject = subject,
                        Body    = body,
                    }
                },
                TenantIdentity = new TenantIdentity(null, null),
                LanguageCode   = language,
                IsActive       = true
            };

            var message = new EmailNotificationMessage()
            {
                Id             = "1",
                From           = "*****@*****.**",
                To             = "*****@*****.**",
                Subject        = subject,
                Body           = body,
                SendDate       = DateTime.Now,
                TenantIdentity = new TenantIdentity(null, null)
            };

            _messageServiceMock.Setup(ms => ms.GetNotificationsMessageByIds(It.IsAny <string[]>()))
            .ReturnsAsync(new[] { message });

            //Act
            var result = await _sender.SendNotificationAsync(notification);

            //Assert
            Assert.True(result.IsSuccess);
        }
Esempio n. 5
0
        public async Task SendNotificationAsync_SentEmailNotification()
        {
            //Arrange
            var language     = "default";
            var subject      = "some subject";
            var body         = "some body";
            var notification = new RegistrationEmailNotification()
            {
                Templates = new List <NotificationTemplate>()
                {
                    new EmailNotificationTemplate()
                    {
                        Subject = subject,
                        Body    = body,
                    }
                },
                LanguageCode = language
            };

            var message = new EmailNotificationMessage()
            {
                Id       = "1",
                From     = "*****@*****.**",
                To       = "*****@*****.**",
                Subject  = subject,
                Body     = body,
                SendDate = DateTime.Now
            };

            _messageServiceMock.Setup(ms => ms.SaveNotificationMessagesAsync(new NotificationMessage[] { message }));
            _messageSenderMock.Setup(ms => ms.SendNotificationAsync(It.IsAny <NotificationMessage>())).Throws(new SmtpException());

            var sender = new NotificationSender(_templateRender, _messageServiceMock.Object, _senderFactoryMock.Object, _backgroundJobClient.Object);

            //Act
            var result = await sender.SendNotificationAsync(notification);

            //Assert
            Assert.False(result.IsSuccess);
        }
Esempio n. 6
0
        public async Task RegistrationEmailNotification_SentNotification()
        {
            //Arrange
            string language     = "default";
            string subject      = "Your login - {{ login }}.";
            string body         = "Thank you for registration {{ firstname }} {{ lastname }}!!!";
            var    notification = new RegistrationEmailNotification()
            {
                FirstName = "First Name",
                LastName  = "Last Name",
                Login     = "******",
                Templates = new List <NotificationTemplate>()
                {
                    new EmailNotificationTemplate()
                    {
                        Subject = subject,
                        Body    = body,
                    }
                }
            };
            var date    = new DateTime(2018, 02, 20, 10, 00, 00);
            var message = new EmailNotificationMessage()
            {
                Id       = "1",
                From     = "*****@*****.**",
                To       = "*****@*****.**",
                Subject  = subject,
                Body     = body,
                SendDate = date
            };

            _serviceMock.Setup(serv => serv.GetByTypeAsync(nameof(RegistrationEmailNotification), null, null, null)).ReturnsAsync(notification);
            _messageServiceMock.Setup(ms => ms.SaveNotificationMessagesAsync(new NotificationMessage[] { message }));

            //Act
            var result = await _sender.SendNotificationAsync(notification, language);

            //Assert
            Assert.True(result.IsSuccess);
        }
        public async Task <ActionResult> Register([FromForm] UserRegistration registration)
        {
            TryValidateModel(registration);

            // This required for populate fields on form on post-back
            WorkContext.Form = Form.FromObject(registration);

            if (ModelState.IsValid)
            {
                // Register user
                var user = registration.ToUser();
                user.Contact = registration.ToContact();
                user.StoreId = WorkContext.CurrentStore.Id;

                var result = await _signInManager.UserManager.CreateAsync(user, registration.Password);

                if (result.Succeeded)
                {
                    user = await _signInManager.UserManager.FindByNameAsync(user.UserName);

                    await _publisher.Publish(new UserRegisteredEvent(WorkContext, user, registration));

                    if (!_identityOptions.SignIn.RequireConfirmedEmail)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : true);

                        await _publisher.Publish(new UserLoginEvent(WorkContext, user));
                    }

                    // Send new user registration notification
                    var registrationEmailNotification = new RegistrationEmailNotification(WorkContext.CurrentStore.Id, WorkContext.CurrentLanguage)
                    {
                        FirstName = registration.FirstName,
                        LastName  = registration.LastName,
                        Login     = registration.UserName,
                        Sender    = WorkContext.CurrentStore.Email,
                        Recipient = GetUserEmail(user)
                    };
                    await SendNotificationAsync(registrationEmailNotification);

                    if (_options.SendAccountConfirmation)
                    {
                        var token = await _signInManager.UserManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { UserId = user.Id, Token = token }, protocol: Request.Scheme, host: WorkContext.CurrentStore.Host);
                        var emailConfirmationNotification = new EmailConfirmationNotification(WorkContext.CurrentStore.Id, WorkContext.CurrentLanguage)
                        {
                            Url       = callbackUrl,
                            Sender    = WorkContext.CurrentStore.Email,
                            Recipient = GetUserEmail(user)
                        };
                        var sendNotifcationResult = await SendNotificationAsync(emailConfirmationNotification);

                        if (sendNotifcationResult.IsSuccess == false)
                        {
                            WorkContext.Form.Errors.Add(SecurityErrorDescriber.ErrorSendNotification(sendNotifcationResult.ErrorMessage));
                            return(View("customers/register", WorkContext));
                        }
                    }

                    return(StoreFrontRedirect("~/account"));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        WorkContext.Form.Errors.Add(new FormError {
                            Code = error.Code?.PascalToKebabCase(), Description = error.Description
                        });
                    }
                }
            }

            return(View("customers/register", WorkContext));
        }
Esempio n. 8
0
 private async Task SendRegistrationEmailNotification(string userID)
 {
     RegistrationEmailNotification rn = new RegistrationEmailNotification();
     await rn.SendNewUserRegistrationNotification(userID);
 }