コード例 #1
0
        public void GivenExternalReturnUrl_ReturnsConfirmation()
        {
            var model = new Register
            {
                Username = GetRandom.String(20),
                Email = GetRandom.Email(),
                Password = "******",
                ReturnUrl = "http://google.com"
            };

            var result = Controller.Register(model) as ViewResult;
            result.Should().Not.Be.Null();
            result.ViewName.Should().Equal("RegisterConfirmation");
        }
コード例 #2
0
        public void GivenDuplicateUsername_ReturnsRegisterView()
        {
            var model = new Register
            {
                Username = User.Username,
                Password = "******"
            };

            var result = Controller.Register(model) as ViewResult;
            result.Should().Not.Be.Null();

            result.Model.Should().Be.OfType<Register>();
            result.ViewName.Should().Equal("");
        }
コード例 #3
0
        public void GivenDuplicateEmail_ReturnsRegisterView()
        {
            var model = new Register
            {
                Username = GetRandom.String(20),
                Email = User.Email,
                Password = "******"
            };

            var result = Controller.Register(model) as ViewResult;
            result.Should().Not.Be.Null();

            result.Model.Should().Be.OfType<Register>();
            result.ViewName.Should().Equal("");
        }
コード例 #4
0
        public void GivenInvalidModelState_ReturnsRegisterView()
        {
            var model = new Register
            {
                Username = GetRandom.String(20),
                Email = GetRandom.Email(),
                Password = "******"
            };
            Controller.ModelState.AddModelError("Fake error", "error");

            var result = Controller.Register(model) as ViewResult;
            result.Should().Not.Be.Null();

            result.Model.Should().Be.OfType<Register>();
            var typedModel = result.Model as Register;
            typedModel.Email.Should().Equal(model.Email);
            typedModel.Password.Should().Be.NullOrEmpty();
            typedModel.ReturnUrl.Should().Equal(model.ReturnUrl);
            typedModel.Username.Should().Equal(model.Username);
            result.ViewName.Should().Equal("");

            var modelState = result.ViewData.ModelState;
            modelState.ContainsKey("Fake error").Should().Be.True();
        }
コード例 #5
0
        public ActionResult Register(Register model)
        {
            if (ModelState.IsValid)
            {
                if (_authenticationService.ReservedUsernames.Any(x => model.Username.Equals(x, StringComparison.OrdinalIgnoreCase)))
                {
                    ModelState.AddModelErrorFor<Register>(x => x.Username, "Username is unavailable");
                }
                else
                {
                    var user = Db.Query<User>("select top 1 * from [{0}] where IsDeleted=0 AND (Username=@Username OR Email=@Email)".Fmt(Db.GetTableName<User>()), new
                        {
                            model.Username,
                            model.Email
                        }
                    ).SingleOrDefault();

                    if (user != null)
                    {
                        if (user.Username.Equals(model.Username, StringComparison.OrdinalIgnoreCase))
                        {
                            ModelState.AddModelErrorFor<Register>(x => x.Username, "Username is already in use");
                        }
                        if (user.Email.Equals(model.Email, StringComparison.OrdinalIgnoreCase))
                        {
                            ModelState.AddModelErrorFor<Register>(x => x.Email, "A user with that email exists");
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var newUser = Mapper.Map<User>(model);
                newUser.Password = model.Password.ToSHAHash();

                _userService.Save(newUser);
                Metrics.Increment(Metric.Users_Register);

                _mailController.Welcome(new ViewModels.Mail.Welcome
                    {
                        Username = newUser.Username,
                        To = newUser.Email
                    }).Deliver();

                // Auto login the user
                _authenticationService.SetLoginCookie(newUser, false);

                NotifySuccess("Your account has been created and you have been logged in. Enjoy!");
                return Redirect(Url.Home().Index());
            }

            // If we got this far, something failed, redisplay form
            model.Password = null; //clear the password so they have to re-enter it
            return View(model);
        }
コード例 #6
0
        public ActionResult Register(Register model)
        {
            if (_authenticationService.ReservedUsernames.Any(x => model.Username.Equals(x, StringComparison.OrdinalIgnoreCase)))
            {
                ModelState.AddModelError("Username", "Username is unavailable");
            }
            else
            {
                using (RavenSession.GetCachingContext())
                {
                    var existingUsername = RavenSession.Query<User>().SingleOrDefault(x => x.Username == model.Username && !x.IsDeleted);
                    if (existingUsername != null)
                    {
                        ModelState.AddModelError("Duplicate username", "Username is already in use");
                    }
                    else
                    {
                        var existingEmail = RavenSession.Query<User>().SingleOrDefault(x => x.Email == model.Email && !x.IsDeleted);
                        if (existingEmail != null)
                        {
                            ModelState.AddModelError("Duplicate email", "A user with that email exists");
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var newUser = Mapper.Map<User>(model);
                newUser.LastActivity = DateTime.UtcNow;
                newUser.Password = model.Password.ToSHAHash();

                RavenSession.Store(newUser);
                RavenSession.SaveChanges();
                Metrics.Increment(Metric.Users_Register);
                _authenticationService.SetLoginCookie(newUser, true);

                // TODO: Send welcome email?

                return View("RegisterConfirmation", model);
            }

            // If we got this far, something failed, redisplay form
            model.Password = null; //clear the password so they have to re-enter it
            return View(model);
        }
コード例 #7
0
        public void GivenReservedUsername_ReturnsRegisterView()
        {
            var model = new Register
            {
                Username = "******",
                Email = GetRandom.Email(),
                Password = "******"
            };

            var result = Controller.Register(model) as ViewResult;
            result.Should().Not.Be.Null();

            result.Model.Should().Be.OfType<Register>();
            result.ViewName.Should().Equal("");
        }
コード例 #8
0
        public void GivenValidPostRequest_SendsEmail()
        {
            var model = new Register
            {
                Username = GetRandom.String(20),
                Email = GetRandom.Email(),
                Password = "******"
            };

            Controller.Register(model);

            MailController.Verify(x => x.Welcome(It.Is<ViewModels.Mail.Welcome>(m => m.To == model.Email && m.Username == model.Username)), Times.Once());
        }
コード例 #9
0
        public void GivenValidModelState_ReturnsRedirect()
        {
            var model = new Register
            {
                Username = GetRandom.String(20),
                Email = GetRandom.Email(),
                Password = "******",
                ReturnUrl = "http://google.com"
            };

            var result = Controller.Register(model) as RedirectResult;
            result.Should().Not.Be.Null();
            Controller.TempData[ViewDataConstants.Notification].Should().Not.Be.Null();
            result.Url.Should().Equal(Controller.Url.Home().Index());
        }
コード例 #10
0
        public void GivenValidModelState_ReturnsConfirmation()
        {
            var model = new Register
            {
                Username = GetRandom.String(20),
                Email = GetRandom.Email(),
                Password = "******"
            };

            var result = Controller.Register(model) as ViewResult;
            result.Should().Not.Be.Null();
            result.ViewName.Should().Equal("RegisterConfirmation");
            var viewModel = result.Model as Register;
            viewModel.Should().Not.Be.Null();
        }
コード例 #11
0
        public void GivenLocalReturnUrl_ReturnsConfirmationWithReturnUrl()
        {
            var model = new Register
            {
                Username = GetRandom.String(20),
                Email = GetRandom.Email(),
                Password = "******",
                ReturnUrl = "/home/index"
            };

            var result = Controller.Register(model) as ViewResult;
            result.Should().Not.Be.Null();
            result.ViewName.Should().Equal("RegisterConfirmation");
            var viewModel = result.Model as Register;
            viewModel.Should().Not.Be.Null();
            viewModel.ReturnUrl.Should().Equal(model.ReturnUrl);
        }