Пример #1
0
        private async Task ChangePassword(string userId, string oldPassword, string newPassword, string confirmPassword, string username, bool useDependencyService = false, bool shouldReset = true, bool shouldReportSuccess = true)
        {
            var userManager   = MockHelpers.CreateMockUserManager();
            var signInManager = MockHelpers.CreateMockSignInManager();
            var httpContext   = MockHelpers.CreateMockHttpContext();
            var logService    = CreateMockLogService();

            var container = new UnityContainer();

            container.RegisterInstance(logService.Object);

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            var controller = new ManageController(userManager.Object, signInManager.Object, useDependencyService ? null : logService.Object);
            var context    = new ControllerContext {
                HttpContext = httpContext.Object
            };

            controller.ControllerContext = context;

            var model = new ChangePasswordViewModel
            {
                OldPassword     = oldPassword,
                NewPassword     = newPassword,
                ConfirmPassword = confirmPassword
            };

            MockHelpers.Validate(model, controller);

            var result = await controller.ChangePassword(model);

            if (shouldReportSuccess)
            {
                Assert.IsType <RedirectToRouteResult>(result);
                var view = (RedirectToRouteResult)result;
                Assert.Equal("Index", view.RouteValues["action"]);
            }
            else
            {
                Assert.IsType <ViewResult>(result);
            }

            if (shouldReset)
            {
                userManager.Verify(u => u.ChangePasswordAsync(userId, oldPassword, newPassword));
            }
            else
            {
                userManager.Verify(u => u.ChangePasswordAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
            }

            if (shouldReportSuccess && shouldReset)
            {
                logService.Verify(s => s.LogUserUpdated(username, username, It.Is <IEnumerable <string> >(i => i.Single() == "Password")));
            }
            else
            {
                logService.Verify(s => s.LogUserUpdated(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <string> >()), Times.Never);
            }
        }