Ejemplo n.º 1
0
        public void Register_Post_ReturnsViewIfRegistrationFails()
        {
            // Arrange
            AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };

            // Act
            ActionResult result = controller.Register(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
            Assert.AreEqual("Username already exists. Please enter a different user name.", controller.ModelState[""].Errors[0].ErrorMessage);
            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
        }
Ejemplo n.º 2
0
        public void Register_Post_ReturnsViewIfModelStateIsInvalid()
        {
            // Arrange
            AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };
            controller.ModelState.AddModelError("", "Dummy error message.");

            // Act
            ActionResult result = controller.Register(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
        }
Ejemplo n.º 3
0
        public void Register_Post_ReturnsRedirectOnSuccess()
        {
            // Arrange
            AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };

            // Act
            ActionResult result = controller.Register(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
            Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
        }
Ejemplo n.º 4
0
        public void CreateInitialAdmin_Post_ReturnsRedirectOnSuccess_And_DoesNotCreateRole_And_DoesNotCreateUser()
        {
            // Arrange
            var model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };

            mockRoles.Stub(r => r.RoleExists("Administrator")).Return(true);
            mockMembership.Stub(r => r.UserExists(model.UserName)).Return(true);

            // Act
            ActionResult result = controller.CreateInitialAdmin(model);

            // Assert
            mockMembership.AssertWasNotCalled(m => m.CreateUser(Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            mockRoles.AssertWasNotCalled(r => r.CreateRole("Administrator"));
            mockRoles.AssertWasCalled(r => r.AddUsersToRoles(new[] { model.UserName }, new[] { "Administrator" }));
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            RedirectToRouteResult redirect = (RedirectToRouteResult)result;
            Assert.AreEqual("Home", redirect.RouteValues["controller"]);
            Assert.AreEqual("Index", redirect.RouteValues["action"]);
        }
Ejemplo n.º 5
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }
Ejemplo n.º 6
0
        public ActionResult CreateInitialAdmin(RegisterModel model)
        {
            MembershipCreateStatus status = MembershipCreateStatus.Success;

            if (ModelState.IsValid)
            {
                if (AccountRoleService.RoleExists(ROLE_Administrator) == false)
                    AccountRoleService.CreateRole(ROLE_Administrator);

                if (MembershipService.UserExists(model.UserName) == false)
                    status = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

                if (status == MembershipCreateStatus.Success)
                {
                    AccountRoleService.AddUsersToRoles(new[] { model.UserName }, new[] { ROLE_Administrator });

                    FormsService.SignIn(model.UserName, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(status));
            }

            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            return View(model);
        }