Ejemplo n.º 1
0
        public async Task ActivateUserAccount_SendsCompetentAuthorityEmail_WhenUserStatusIsPendingOnly(int userStatus)
        {
            // Arrange
            var builder = new UnauthenticatedUserControllerBuilder();

            A.CallTo(() => builder.UserManager.ConfirmEmailAsync(A <string> ._, A <string> ._))
            .Returns(IdentityResult.Success);

            var    userId       = Guid.NewGuid();
            string emailAddress = "*****@*****.**";

            var competentAuthority = new UKCompetentAuthority(
                A.Dummy <Guid>(),
                "EA",
                "EA",
                A.Dummy <Country>(),
                emailAddress,
                0);

            var competentAuthorityUser = A.Fake <CompetentAuthorityUser>();

            A.CallTo(() => competentAuthorityUser.CompetentAuthority)
            .Returns(competentAuthority);
            A.CallTo(() => competentAuthorityUser.UserStatus)
            .Returns(Enumeration.FromValue <UserStatus>(userStatus));

            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(userId))
            .Returns(competentAuthorityUser);

            var viewUserRoute            = A.Fake <ViewCompetentAuthorityUserRoute>();
            var activatedUserAccountData = new ActivatedUserAccountData
            {
                Id            = userId,
                ViewUserRoute = viewUserRoute
            };

            var controller = builder.Build();

            // Act
            await controller.ActivateUserAccount(activatedUserAccountData);

            // Assert
            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(A <Guid> ._))
            .MustHaveHappened();
            A.CallTo(() => builder.EmailService.SendInternalUserAccountActivated(A <string> ._, A <string> ._, A <string> ._, A <string> ._))
            .MustNotHaveHappened();
        }
Ejemplo n.º 2
0
        public async Task ResetPasswordRequest_InvalidEmailAddress_DoesNotReturnToken()
        {
            var builder     = new UnauthenticatedUserControllerBuilder();
            var controller  = builder.Build();
            var userManager = builder.UserManager;

            A.CallTo(() => userManager.FindByEmailAsync(A <string> ._)).Returns((ApplicationUser)null);

            var result = (OkNegotiatedContentResult <PasswordResetRequestResult>) await controller.ResetPasswordRequest(A.Fake <PasswordResetRequest>());

            var passwordResetRequestResult = result.Content;

            A.CallTo(() => userManager.FindByEmailAsync(A <string> ._)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => userManager.GeneratePasswordResetTokenAsync(A <string> ._)).MustNotHaveHappened();
            Assert.False(passwordResetRequestResult.ValidEmail);
            Assert.Null(passwordResetRequestResult.PasswordResetToken);
        }
Ejemplo n.º 3
0
        public async Task ResetPasswordRequest_ValidEmailAddress_ReturnsToken()
        {
            var builder     = new UnauthenticatedUserControllerBuilder();
            var controller  = builder.Build();
            var userManager = builder.UserManager;

            string resetToken = "Test token";

            A.CallTo(() => userManager.FindByEmailAsync(A <string> ._)).Returns(A.Fake <ApplicationUser>());
            A.CallTo(() => userManager.GeneratePasswordResetTokenAsync(A <string> ._)).Returns(resetToken);

            var result = (OkNegotiatedContentResult <PasswordResetRequestResult>) await controller.ResetPasswordRequest(A.Fake <PasswordResetRequest>());

            var passwordResetRequestResult = result.Content;

            Assert.True(passwordResetRequestResult.ValidEmail);
            Assert.Equal(resetToken, passwordResetRequestResult.PasswordResetToken);
        }
Ejemplo n.º 4
0
        public async Task ActivateUserAccount_SendsEmailWithViewUserUrl()
        {
            // Arrange
            var builder = new UnauthenticatedUserControllerBuilder();

            A.CallTo(() => builder.UserManager.ConfirmEmailAsync(A <string> ._, A <string> ._))
            .Returns(IdentityResult.Success);

            var userId = Guid.NewGuid();
            var user   = new User(userId.ToString(), "FirstName", "SecondName", "*****@*****.**");

            var competentAuthorityUser = A.Fake <CompetentAuthorityUser>();

            A.CallTo(() => competentAuthorityUser.User)
            .Returns(user);
            A.CallTo(() => competentAuthorityUser.UserStatus)
            .Returns(UserStatus.Pending);

            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(userId))
            .Returns(competentAuthorityUser);

            string viewUserUrl = "http://localhost/EditUser/abc";

            var viewUserRoute = A.Fake <ViewCompetentAuthorityUserRoute>();

            A.CallTo(() => viewUserRoute.GenerateUrl())
            .Returns(viewUserUrl);

            var activatedUserAccountData = new ActivatedUserAccountData
            {
                Id            = userId,
                ViewUserRoute = viewUserRoute
            };

            var controller = builder.Build();

            // Act
            await controller.ActivateUserAccount(activatedUserAccountData);

            // Assert
            A.CallTo(() => builder.EmailService.SendInternalUserAccountActivated(A <string> ._, A <string> ._, A <string> ._, viewUserUrl))
            .MustHaveHappened();
        }
Ejemplo n.º 5
0
        public async Task ActivateUserAccount_DoesNotSendCompetentAuthorityEmail_WhenEmailConfirmationFails()
        {
            // Arrange
            var builder = new UnauthenticatedUserControllerBuilder();

            A.CallTo(() => builder.UserManager.ConfirmEmailAsync(A <string> ._, A <string> ._))
            .Returns(IdentityResult.Failed());

            var controller = builder.Build();

            // Act
            await controller.ActivateUserAccount(A.Dummy <ActivatedUserAccountData>());

            // Assert
            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(A <Guid> ._))
            .MustNotHaveHappened();
            A.CallTo(() => builder.EmailService.SendInternalUserAccountActivated(A <string> ._, A <string> ._, A <string> ._, A <string> ._))
            .MustNotHaveHappened();
        }
        public async Task ActivateUserAccount_SendsEmailWithViewUserUrl()
        {
            // Arrange
            var builder = new UnauthenticatedUserControllerBuilder();

            A.CallTo(() => builder.UserManager.ConfirmEmailAsync(A<string>._, A<string>._))
                .Returns(IdentityResult.Success);

            var userId = Guid.NewGuid();
            var user = new User(userId.ToString(), "FirstName", "SecondName", "*****@*****.**");

            var competentAuthorityUser = A.Fake<CompetentAuthorityUser>();
            A.CallTo(() => competentAuthorityUser.User)
                .Returns(user);
            A.CallTo(() => competentAuthorityUser.UserStatus)
                .Returns(UserStatus.Pending);

            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(userId))
                .Returns(competentAuthorityUser);

            string viewUserUrl = "http://localhost/EditUser/abc";

            var viewUserRoute = A.Fake<ViewCompetentAuthorityUserRoute>();
            A.CallTo(() => viewUserRoute.GenerateUrl())
                .Returns(viewUserUrl);

            var activatedUserAccountData = new ActivatedUserAccountData
            {
                Id = userId,
                ViewUserRoute = viewUserRoute
            };

            var controller = builder.Build();

            // Act
            await controller.ActivateUserAccount(activatedUserAccountData);

            // Assert
            A.CallTo(() => builder.EmailService.SendInternalUserAccountActivated(A<string>._, A<string>._, A<string>._, viewUserUrl))
                .MustHaveHappened();
        }
        public async Task ActivateUserAccount_SendsEmailToCompetentAuthorityEmailAddress()
        {
            // Arrange
            var builder = new UnauthenticatedUserControllerBuilder();

            A.CallTo(() => builder.UserManager.ConfirmEmailAsync(A<string>._, A<string>._))
                .Returns(IdentityResult.Success);

            var userId = Guid.NewGuid();
            string emailAddress = "*****@*****.**";

            var competentAuthority = new UKCompetentAuthority(
                A.Dummy<Guid>(),
                "EA",
                "EA",
                A.Dummy<Country>(),
                emailAddress);

            var competentAuthorityUser = A.Fake<CompetentAuthorityUser>();
            A.CallTo(() => competentAuthorityUser.CompetentAuthority)
                .Returns(competentAuthority);
            A.CallTo(() => competentAuthorityUser.UserStatus)
                .Returns(UserStatus.Pending);

            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(userId))
                .Returns(competentAuthorityUser);

            var viewUserRoute = A.Fake<ViewCompetentAuthorityUserRoute>();
            var activatedUserAccountData = new ActivatedUserAccountData
            {
                Id = userId,
                ViewUserRoute = viewUserRoute
            };

            var controller = builder.Build();

            // Act
            await controller.ActivateUserAccount(activatedUserAccountData);

            // Assert
            A.CallTo(() => builder.EmailService.SendInternalUserAccountActivated(emailAddress, A<string>._, A<string>._, A<string>._))
                .MustHaveHappened();
        }
        public async Task ActivateUserAccount_DoesNotSendCompetentAuthorityEmail_WhenUserIsNotInternalUser()
        {
            // Arrange
            var builder = new UnauthenticatedUserControllerBuilder();

            A.CallTo(() => builder.UserManager.ConfirmEmailAsync(A<string>._, A<string>._))
                .Returns(IdentityResult.Success);

            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(A<Guid>._))
                .Returns((CompetentAuthorityUser)null);

            var controller = builder.Build();

            // Act
            await controller.ActivateUserAccount(A.Dummy<ActivatedUserAccountData>());

            // Assert
            A.CallTo(() => builder.GetAdminUserDataAccess.GetAdminUserOrDefault(A<Guid>._))
                .MustHaveHappened();
            A.CallTo(() => builder.EmailService.SendInternalUserAccountActivated(A<string>._, A<string>._, A<string>._, A<string>._))
                .MustNotHaveHappened();
        }
        public async Task ResetPasswordRequest_ValidEmailAddress_ReturnsToken()
        {
            var builder = new UnauthenticatedUserControllerBuilder();
            var controller = builder.Build();
            var userManager = builder.UserManager;

            string resetToken = "Test token";

            A.CallTo(() => userManager.FindByEmailAsync(A<string>._)).Returns(A.Fake<ApplicationUser>());
            A.CallTo(() => userManager.GeneratePasswordResetTokenAsync(A<string>._)).Returns(resetToken);

            var result = (OkNegotiatedContentResult<PasswordResetRequestResult>)await controller.ResetPasswordRequest(A.Fake<PasswordResetRequest>());
            var passwordResetRequestResult = result.Content;

            Assert.True(passwordResetRequestResult.ValidEmail);
            Assert.Equal(resetToken, passwordResetRequestResult.PasswordResetToken);
        }
        public async Task ResetPasswordRequest_InvalidEmailAddress_DoesNotReturnToken()
        {
            var builder = new UnauthenticatedUserControllerBuilder();
            var controller = builder.Build();
            var userManager = builder.UserManager;

            A.CallTo(() => userManager.FindByEmailAsync(A<string>._)).Returns((ApplicationUser)null);

            var result = (OkNegotiatedContentResult<PasswordResetRequestResult>)await controller.ResetPasswordRequest(A.Fake<PasswordResetRequest>());
            var passwordResetRequestResult = result.Content;

            A.CallTo(() => userManager.FindByEmailAsync(A<string>._)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => userManager.GeneratePasswordResetTokenAsync(A<string>._)).MustNotHaveHappened();
            Assert.False(passwordResetRequestResult.ValidEmail);
            Assert.Null(passwordResetRequestResult.PasswordResetToken);
        }