Exemplo n.º 1
0
        public async void ChangePassword_SuccessfulChange()
        {

            ChangePasswordViewModel passwordModel = new ChangePasswordViewModel()
            {
                OldPassword = "******",
                NewPassword = "******",
            };

            User user = new User()
            {
                Id = memberId,
                Email = "*****@*****.**",
                NewEmail = "*****@*****.**"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<IAuthenticationManager> authenticationManagerStub = new Mock<IAuthenticationManager>();
            Mock<IUserStore<User, Guid>> userStoreStub = new Mock<IUserStore<User, Guid>>();
            Mock<VeilUserManager> userManagerStub = new Mock<VeilUserManager>(dbStub.Object, null /*messageService*/,
                null /*dataProtectionProvider*/);

            userManagerStub.Setup(um => um.FindByIdAsync(memberId)).ReturnsAsync(user);
            userManagerStub.Setup(um => um.ChangePasswordAsync(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>()))
                .ReturnsAsync(IdentityResult.Success);

            dbStub.Setup(db => db.UserStore).Returns(userStoreStub.Object);

            Mock<VeilSignInManager> signInManagerMock = new Mock<VeilSignInManager>(userManagerStub.Object, authenticationManagerStub.Object);
            signInManagerMock.Setup(sm => sm.SignInAsync(It.IsAny<User>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(Task.FromResult(0));

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null);
            context.Setup(c => c.HttpContext.User.Identity.IsAuthenticated).Returns(true);

            Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>();
            idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(memberId);

            ManageController controller = new ManageController(userManagerStub.Object, signInManagerMock.Object, dbStub.Object,
                idGetterStub.Object, null)
            {
                ControllerContext = context.Object
            };

            var result = await controller.ChangePassword(passwordModel) as RedirectToRouteResult;

            Assert.That(result != null);
            Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));
        }
Exemplo n.º 2
0
        public async void ChangePassword_InvalidModelState()
        {
            ChangePasswordViewModel passwordModel = new ChangePasswordViewModel();
            ManageController controller = new ManageController(null, null, null, null, null);

            controller.ModelState.AddModelError("error", "this is an error");

            var result = await controller.ChangePassword(passwordModel);

            Assert.That(result != null);
        }
Exemplo n.º 3
0
        public async void ChangePassword_ThrowsDbEntityValidationException()
        {

            ChangePasswordViewModel passwordModel = new ChangePasswordViewModel()
            {
                NewPassword = "******",
                OldPassword = "******"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<IUserStore<User, Guid>> userStoreStub = new Mock<IUserStore<User, Guid>>();
            Mock<VeilUserManager> userManagerStub = new Mock<VeilUserManager>(dbStub.Object, null /*messageService*/,
                null /*dataProtectionProvider*/);

            userManagerStub.Setup(um => um.ChangePasswordAsync(It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>()))
                .ThrowsAsync(new DbEntityValidationException());

            dbStub.Setup(db => db.UserStore).Returns(userStoreStub.Object);

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null);
            context.Setup(c => c.HttpContext.User.Identity.IsAuthenticated).Returns(true);

            Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>();
            idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(memberId);

            ManageController controller = new ManageController(userManagerStub.Object, null, dbStub.Object, idGetterStub.Object, null)
            {
                ControllerContext = context.Object
            };

            var result = await controller.ChangePassword(passwordModel);

            Assert.That(result != null);
        }
Exemplo n.º 4
0
        public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            IdentityResult result;

            try
            {
                result = await userManager.
                    ChangePasswordAsync(GetUserId(), model.OldPassword, model.NewPassword);
            }
            catch (DbEntityValidationException ex)
            {
                this.AddAlert(AlertType.Error, ex.Message);

                return View(model);
            }

            if (result.Succeeded)
            {
                var user = await userManager.FindByIdAsync(GetUserId());

                if (user != null)
                {
                    await signInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                }

                return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
            }

            AddErrors(result);

            return View(model);
        }