private ActionResult RegiterWithAction(RegisterUserModel model, Func<ActionResult> successAction, Func<ActionResult> defaultAction)
        {
            if (!_auth.ValidateCaptcha()) {
                ModelState.AddModelError("", "Sorry, we failed to validate your captcha. Please try again.");                
                defaultAction.Invoke();
            }

            if (ModelState.IsValid)
            {
                var email = model.Email;
                var password = model.Password;

                if (_auth.RegisterNewUser(email, password))
                {
                    _notification.NotifyUserOnRegistration(email, password);
                    return successAction.Invoke();
                }
                else
                {
                    ModelState.AddModelError("", "Sorry, user with such email already exist. Please register with different email.");
                }
            }

            return defaultAction.Invoke();
        }
        public void Register_Post_Success_Redirected_To_Dashboard()
        {
            // arrange
            var auth = new Mock<IAuthenticationService>();
            var notification = new Mock<INotificationService>();
            var redirect = new RedirectService();
            var controller = new RegistrationController(auth.Object, notification.Object, redirect);

            var model = new RegisterUserModel()
            {
                Email = "*****@*****.**",
                Password = "******",
                ConfirmPassword = "******"
            };

            auth.Setup(a => a.ValidateCaptcha()).Returns(true);
            auth.Setup(a => a.RegisterNewUser("*****@*****.**", "password")).Returns(true);
            auth.Setup(a => a.Authenticate("*****@*****.**", "password")).Returns(true);

            //act
            var result = controller.Register(model) as RedirectResult;

            //assert (result)
            result.Url.Should().Be("~/user/[email protected]");
        }
        public void Register_Post_Fail_Already_Registered()
        {
            // arrange
            var auth = new Mock<IAuthenticationService>();
            var notification = new Mock<INotificationService>();
            var redirect = new RedirectService();
            var controller = new RegistrationController(auth.Object, notification.Object, redirect);
            var model = new RegisterUserModel()
            {
                Email = "*****@*****.**",
                Password = "******",
                ConfirmPassword = "******"
            };

            auth.Setup(a => a.ValidateCaptcha()).Returns(true);
            auth.Setup(a => a.RegisterNewUser("*****@*****.**", "password")).Returns(false);

            //act
            controller.Register(model);
            var result = controller.Register(model) as ViewResult;

            //assert
            Assert.That(model, Is.EqualTo(result.ViewData.Model));
            Assert.That(controller.ModelState[""].Errors[0].ErrorMessage, Is.EqualTo("Sorry, user with such email already exist. Please register with different email."));
        }
        public void RegisterMobile_Post_RedirectedToSuccess()
        {
            // arrange
            var auth = new Mock<IAuthenticationService>();
            var notification = new Mock<INotificationService>();
            var redirect = new RedirectService();
            var controller = new RegistrationController(auth.Object, notification.Object, redirect);

            // act
            var model = new RegisterUserModel()
            {
                Email = "*****@*****.**",
                Password = "******",
                ConfirmPassword = "******"
            };

            auth.Setup(a => a.ValidateCaptcha()).Returns(true);
            auth.Setup(a => a.RegisterNewUser("*****@*****.**", "password")).Returns(true);
            auth.Setup(a => a.Authenticate("*****@*****.**", "password")).Returns(true);

            var result = controller.RegisterMobile(model) as RedirectResult;

            // assert
            Assert.That(result.Url, Is.EqualTo("~/registration/success"));
        }
        public void Register_NewUserRegistered_EmailSent()
        {
            // arrange
            var auth = new Mock<IAuthenticationService>();
            var notification = new Mock<INotificationService>();
            var redirect = new RedirectService();
            var controller = new RegistrationController(auth.Object, notification.Object, redirect);
            var user = new RegisterUserModel
            {
                Email = "*****@*****.**",
                Password = "******"
            };

            auth.Setup(a => a.ValidateCaptcha()).Returns(true);
            auth.Setup(a => a.RegisterNewUser("*****@*****.**", "111111")).Returns(true);

            //act
            var result = controller.Register(user) as RedirectResult;

            //post
            notification.Verify(n => n.NotifyUserOnRegistration("*****@*****.**", "111111"));
        }
        public void Register_Post_Fail_Unknown_Reason()
        {
            // arrange
            var auth = new Mock<IAuthenticationService>();
            var notification = new Mock<INotificationService>();
            var redirect = new RedirectService();
            var controller = new RegistrationController(auth.Object, notification.Object, redirect);
            var model = new RegisterUserModel()
            {
                Email = "*****@*****.**",
                Password = "******",
                ConfirmPassword = "******"
            };

            auth.Setup(a => a.ValidateCaptcha()).Returns(true);
            auth.Setup(a => a.RegisterNewUser("*****@*****.**", "password")).Throws(new Exception());

            //act / post
            var result = controller.Register(model) as ViewResult;
        }
 public ActionResult RegisterMobile(RegisterUserModel model)
 {
     return RegiterWithAction(model, () => { return _redirect.ToRegistrationSuccess(); }, () => { return View("mobile", model); });
 }
 public ActionResult Register(RegisterUserModel model)
 {
     return RegiterWithAction(model, () => { return _redirect.ToDashboard(model.Email); }, () => { return View("index", model); });
 }