Exemple #1
0
        public void POST_Providing_A_Valid_New_Email_Address_Results_In_A_Verification_Email_Being_Sent()
        {
            // Arrange
            User user = new UserBuilder().WithEmailAddress("*****@*****.**").Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_NewEmailAddress", "*****@*****.**");

            var controllerBuilder = new ControllerBuilder <ChangeEmailController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(user)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(user)
                                    .WithMockUriHelper()
                                    .Build();

            // Act
            controller.ChangeEmailPost(new ChangeEmailViewModel());

            // Assert
            Assert.AreEqual(1, controllerBuilder.EmailsSent.Count);

            var email = controllerBuilder.EmailsSent.FirstOrDefault();

            Assert.NotNull(email);
            Assert.AreEqual(EmailTemplates.SendChangeEmailPendingVerificationEmail, email.TemplateId);
            Assert.AreEqual("*****@*****.**", email.EmailAddress);
        }
Exemple #2
0
        public void POST_Cannot_Update_Email_Address_For_Non_Active_User()
        {
            // Arrange
            User user = new UserBuilder().DefaultRetiredUser().WithEmailAddress("*****@*****.**").WithPassword("password").Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_Password", "password");

            var controllerBuilder = new ControllerBuilder <ChangeEmailController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(user)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(user)
                                    .WithMockUriHelper()
                                    .Build();

            var emailVerificationCode = Encryption.EncryptModel(
                new ChangeEmailVerificationToken
            {
                UserId          = user.UserId,
                NewEmailAddress = "*****@*****.**".ToLower(),
                TokenTimestamp  = VirtualDateTime.Now
            });

            var viewModel = new VerifyEmailChangeViewModel {
                NewEmailAddress = "*****@*****.**", Code = emailVerificationCode, User = user
            };

            // Act & Assert
            Assert.Throws <ArgumentException>(() => controller.VerifyEmailPost(viewModel));
        }
        public void POST_Email_Is_Sent_When_Password_Is_Successfully_Updated()
        {
            // Arrange
            User user = new UserBuilder().WithPassword("password").Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_CurrentPassword", "password");
            requestFormValues.Add("GovUk_Text_NewPassword", "NewPassword1");
            requestFormValues.Add("GovUk_Text_ConfirmNewPassword", "NewPassword1");

            var controllerBuilder = new ControllerBuilder <ChangePasswordController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(user)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(user)
                                    .WithMockUriHelper()
                                    .Build();

            // Act
            controller.ChangePasswordPost(new ChangePasswordViewModel());

            // Assert
            // Assert that exactly one email is sent
            Assert.AreEqual(1, controllerBuilder.EmailsSent.Count);

            NotifyEmail userEmail = controllerBuilder.EmailsSent.FirstOrDefault();

            // Assert that the email sent has the correct email address and template
            Assert.NotNull(userEmail);
            Assert.AreEqual(user.EmailAddress, userEmail.EmailAddress);
            Assert.AreEqual(EmailTemplates.SendChangePasswordCompletedEmail, userEmail.TemplateId, $"Expected the correct templateId to be in the email send queue, expected {EmailTemplates.SendChangePasswordCompletedEmail}");
        }
        public void POST_Audit_Log_Item_Is_Saved_When_Password_Is_Successfully_Updated()
        {
            // Arrange
            User user = new UserBuilder().WithPassword("password").Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_CurrentPassword", "password");
            requestFormValues.Add("GovUk_Text_NewPassword", "NewPassword1");
            requestFormValues.Add("GovUk_Text_ConfirmNewPassword", "NewPassword1");

            var controllerBuilder = new ControllerBuilder <ChangePasswordController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(user)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(user)
                                    .WithMockUriHelper()
                                    .Build();

            // Act
            controller.ChangePasswordPost(new ChangePasswordViewModel());

            // Assert
            // Assert that exactly one audit log entry is added
            List <AuditLog> auditLogEntries = controllerBuilder.DataRepository.GetAll <AuditLog>().ToList();

            Assert.AreEqual(1, auditLogEntries.Count);

            // Assert that the audit log entry audits the correct action
            Assert.AreEqual(AuditedAction.UserChangePassword, auditLogEntries.First().Action);
        }
Exemple #5
0
        public void POST_Closing_Account_Removes_User_From_Organisations_And_Emails_GEO_For_Orphans()
        {
            // Arrange
            Organisation organisation1 = new OrganisationBuilder().WithOrganisationId(1).Build();
            Organisation organisation2 = new OrganisationBuilder().WithOrganisationId(2).Build();

            User standardUser = new UserBuilder()
                                .WithUserId(1)
                                .WithOrganisation(organisation1)
                                .Build();

            User userToDelete = new UserBuilder()
                                .WithUserId(2)
                                .WithPassword("password")
                                .WithOrganisation(organisation1)
                                .WithOrganisation(organisation2)
                                .Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_Password", "password");

            var controllerBuilder = new ControllerBuilder <WebUI.Controllers.Account.CloseAccountController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(userToDelete)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(organisation1, organisation2, standardUser, userToDelete)
                                    .Build();

            // Act
            controller.CloseAccountPost(new CloseAccountViewModel());

            // Assert
            // Assert that organisation1 doesn't have userToDelete associated with it, but is not an orphan
            Assert.IsEmpty(organisation1.UserOrganisations.Where(uo => uo.User.Equals(userToDelete)));
            Assert.IsFalse(organisation1.GetIsOrphan());

            // Assert that organisation2 is now an orphan
            Assert.IsTrue(organisation2.GetIsOrphan());

            // Assert that there are two emails: 1 'Close Account' email to the user, 1 'Orphan Organisation' email to GEO
            Assert.AreEqual(2, controllerBuilder.EmailsSent.Count);

            NotifyEmail userEmail = controllerBuilder.EmailsSent.SingleOrDefault(ne => ne.EmailAddress == userToDelete.EmailAddress);

            Assert.NotNull(userEmail);
            Assert.AreEqual(EmailTemplates.SendCloseAccountCompletedEmail, userEmail.TemplateId, $"Expected the correct templateId to be in the email send queue, expected {EmailTemplates.SendCloseAccountCompletedEmail}");

            NotifyEmail geoEmail = controllerBuilder.EmailsSent.SingleOrDefault(ne => ne.EmailAddress == Global.GeoDistributionList[0]);

            Assert.NotNull(geoEmail);
            Assert.AreEqual(EmailTemplates.SendGeoOrphanOrganisationEmail, geoEmail.TemplateId, $"Expected the correct templateId to be in the email send queue, expected {EmailTemplates.SendGeoOrphanOrganisationEmail}");
        }
Exemple #6
0
        public void POST_Cannot_Update_Email_Address_To_Email_Associated_With_Another_Account()
        {
            // Arrange
            User user  = new UserBuilder().WithEmailAddress("*****@*****.**").WithPassword("password").Build();
            User user2 = new UserBuilder().WithEmailAddress("*****@*****.**").Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_Password", "password");

            var controllerBuilder = new ControllerBuilder <ChangeEmailController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(user)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(user, user2)
                                    .WithMockUriHelper()
                                    .Build();

            var emailVerificationCode = Encryption.EncryptModel(
                new ChangeEmailVerificationToken
            {
                UserId          = user.UserId,
                NewEmailAddress = "*****@*****.**".ToLower(),
                TokenTimestamp  = VirtualDateTime.Now
            });

            var viewModel = new VerifyEmailChangeViewModel {
                NewEmailAddress = "*****@*****.**", Code = emailVerificationCode, User = user
            };

            // Act
            controller.VerifyEmailPost(viewModel);

            // Assert
            Assert.AreEqual("*****@*****.**", user.EmailAddress);

            var auditLogs = controllerBuilder.DataRepository.GetAll <AuditLog>();

            Assert.AreEqual(0, auditLogs.Count());
        }
Exemple #7
0
        public void POST_Trying_To_Change_Email_Address_To_Current_Email_Address_Does_Not_Send_Verification_Email()
        {
            // Arrange
            User user = new UserBuilder().WithEmailAddress("*****@*****.**").Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_NewEmailAddress", "*****@*****.**");

            var controllerBuilder = new ControllerBuilder <ChangeEmailController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(user)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(user)
                                    .WithMockUriHelper()
                                    .Build();

            // Act
            controller.ChangeEmailPost(new ChangeEmailViewModel());

            // Assert
            Assert.AreEqual(0, controllerBuilder.EmailsSent.Count);
        }
Exemple #8
0
        public void POST_User_Can_Verify_Their_Email_Address_And_Confirm_Password_To_Change_Email_Address()
        {
            // Arrange
            User user = new UserBuilder().WithEmailAddress("*****@*****.**").WithPassword("password").Build();

            var requestFormValues = new Dictionary <string, StringValues>();

            requestFormValues.Add("GovUk_Text_Password", "password");

            var controllerBuilder = new ControllerBuilder <ChangeEmailController>();
            var controller        = controllerBuilder
                                    .WithLoggedInUser(user)
                                    .WithRequestFormValues(requestFormValues)
                                    .WithDatabaseObjects(user)
                                    .WithMockUriHelper()
                                    .Build();

            var emailVerificationCode = Encryption.EncryptModel(
                new ChangeEmailVerificationToken
            {
                UserId          = user.UserId,
                NewEmailAddress = "*****@*****.**".ToLower(),
                TokenTimestamp  = VirtualDateTime.Now
            });

            var viewModel = new VerifyEmailChangeViewModel {
                NewEmailAddress = "*****@*****.**", Code = emailVerificationCode, User = user
            };

            // Act
            controller.VerifyEmailPost(viewModel);

            // Assert
            Assert.AreEqual("*****@*****.**", user.EmailAddress);

            var auditLogs = controllerBuilder.DataRepository.GetAll <AuditLog>();

            Assert.AreEqual(1, auditLogs.Count());

            var auditLog = auditLogs.FirstOrDefault();

            Assert.NotNull(auditLog);
            Assert.AreEqual(AuditedAction.UserChangeEmailAddress, auditLog.Action);

            Assert.AreEqual(2, controllerBuilder.EmailsSent.Count);

            var oldEmailNotifications = controllerBuilder.EmailsSent.Where(e => e.EmailAddress == "*****@*****.**").ToList();

            Assert.AreEqual(1, oldEmailNotifications.Count);

            var oldEmailNotification = oldEmailNotifications.FirstOrDefault();

            Assert.AreEqual(EmailTemplates.SendChangeEmailCompletedNotificationEmail, oldEmailNotification.TemplateId);

            var newEmailNotifications = controllerBuilder.EmailsSent.Where(e => e.EmailAddress == "*****@*****.**").ToList();

            Assert.AreEqual(1, newEmailNotifications.Count);

            var newEmailNotification = newEmailNotifications.FirstOrDefault();

            Assert.AreEqual(EmailTemplates.SendChangeEmailCompletedVerificationEmail, newEmailNotification.TemplateId);
        }