예제 #1
0
        public void DoubleRegisterFails()
        {
            // Initialization stuff
            var controller = this.Controller;
            var firstModel = new RegisterViewModel
                { Login = "******", Email = "*****@*****.**", Password = "******", ConfirmPassword = "******" };
            var secondModel = new RegisterViewModel
                { Login = "******", Email = "*****@*****.**", Password = "******", ConfirmPassword = "******" };

            // Register two users with same login
            controller.Register(firstModel);
            var result = controller.Register(secondModel);

            // The result must be the view with the same argument as the controller's, and the ModelState must be invalid.
            result.AssertViewRendered().WithViewData<RegisterViewModel>().ShouldEqual(secondModel, "Unexpected model value.");
            Assert.IsFalse(controller.ModelState.IsValid);
            Assert.IsFalse(controller.ModelState.IsValidField("Login"));
        }
예제 #2
0
        public void Register()
        {
            // Initialization stuff
            var controller = this.Controller;
            var storage = this.Storage;
            var model = new RegisterViewModel { Login = "******", Email = "*****@*****.**", Password = "******", ConfirmPassword = "******" };

            // Actually call the method
            var result = controller.Register(model);

            // Register must redirect to the "Welcome" action on success
            result.AssertActionRedirect().ToAction("Welcome");

            // User must be created and correctly populated
            Assert.DoesNotThrow(() => storage.Users.Find("Elarnon"));
            var user = storage.Users.Find("Elarnon");
            Assert.That(user.Login, Is.EqualTo("Elarnon"));
            Assert.That(user.Email, Is.EqualTo("*****@*****.**"));

            // Verify a cookie is set
            var cookie = this.Builder.HttpContext.Response.Cookies[FormsAuthentication.FormsCookieName];
            Assert.NotNull(cookie);
            Assert.DoesNotThrow(() => FormsAuthentication.Decrypt(cookie.Value));
        }
예제 #3
0
        public ActionResult Register(RegisterViewModel registerViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var hashedPass = Auth.PasswordAuth.HashPassword(registerViewModel.Password);

                    // IUserRepository::Create creates a main account
                    var newUser = this.Storage.Users.Create(registerViewModel.Login, registerViewModel.Email, hashedPass);
                    this.AuthenticateUser(newUser, registerViewModel.RememberMe);
                    this.CurrentAccount = newUser.MainAccount;
                    IListModel list = this.Storage.Lists.Create(CurrentAccount, CurrentAccount.Name, "Personal list of " + CurrentAccount.Name, false);
                    list.Members.Add(CurrentAccount);
                    var tigwiAccount = this.Storage.Accounts.Find("Tigwi");

                    IListModel listTigwi = this.Storage.Lists.Create(CurrentAccount, "Tigwi", "Welcome !", true);
                    listTigwi.Members.Add(tigwiAccount);
                    listTigwi.Followers.Add(CurrentAccount);
                    this.Storage.SaveChanges();
                    return this.RedirectToAction("Index", "Home");
                }
                catch (DuplicateUserException ex)
                {
                    // TODO: We need more granularity (login failed ? email failed ? propositions ?)
                    ModelState.AddModelError("Login", ex.Message);
                }
                catch (DuplicateAccountException ex)
                {
                    ModelState.AddModelError("Login", ex.Message);
                }
            }

            // Somthing went wrong, display register page again
            return this.View(registerViewModel);
        }