public ActionResult Register(UserAccount.RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var result = _accountService.Register(model);

                if (result.IsSuccess)
                {
                    return(RedirectToAction("AccountCreated", "Account"));
                }

                foreach (var registrationError in result.Errors)
                {
                    if (registrationError.Exception != null && registrationError.Exception.IsDuplicateIndexError())
                    {
                        var user         = registrationError.Data.MapTo <User>();
                        var serviceError = registrationError.Exception.GetServiceErrorFromException(user);
                        ModelState.AddModelError("", serviceError.ErrorMessage);
                    }
                    else
                    {
                        ModelState.AddModelError(registrationError.FieldName, registrationError.ErrorMessage);
                    }
                }
            }

            return(View(model));
        }
        public void Register_Success()
        {
            var username = "******";
            var email    = "*****@*****.**";

            var model = new UserAccount.RegisterModel
            {
                UserName        = username,
                EmailAddress    = email,
                Password        = "******",
                ConfirmPassword = "******",
                FirstName       = "Joe",
                LastName        = "Bloggs",
                PhoneNumber     = "12323423",
                BirthDate       = new DateTime(2017, 3, 3)
            };

            var result = _service.Register(model);

            Assert.Equal(0, result.Errors.Count);
            _authentication.Verify(_ => _.CreateUserAndAccount(model.UserName, model.Password, It.IsAny <object>(), true), Times.Once);
            _mailer.Verify(_ => _.SendEmail(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), true),
                           Times.Once);
            Assert.True(result.IsSuccess);
        }
        public void Register_ThrowsException()
        {
            var username = "******";
            var email    = "*****@*****.**";

            var model = new UserAccount.RegisterModel
            {
                UserName        = username,
                EmailAddress    = email,
                Password        = "******",
                ConfirmPassword = "******"
            };

            _authentication.Setup(_ =>
                                  _.CreateUserAndAccount(model.UserName, model.Password, It.IsAny <object>(), It.IsAny <bool>()))
            .Throws(new Exception("Foo"));

            var result = _service.Register(model);

            Assert.Contains("Foo", result.Errors.Select(_ => _.ErrorMessage));
            Assert.False(result.IsSuccess);
        }
        public void Register_ShouldHaveErrors()
        {
            var username = "******";
            var email    = "*****@*****.**";

            var model = new UserAccount.RegisterModel
            {
                UserName        = username,
                EmailAddress    = email,
                Password        = "",
                ConfirmPassword = "******"
            };

            _userRepository.Setup(_ => _.Exists(It.IsAny <Expression <Func <User, bool> > >()))
            .Returns(true);

            var result = _service.Register(model);

            Assert.Contains("UserName", result.Errors.Select(_ => _.FieldName));
            Assert.Contains("EmailAddress", result.Errors.Select(_ => _.FieldName));
            Assert.Contains("InvalidPassword", result.Errors.Select(_ => _.FieldName));
            Assert.Contains("ConfirmPassword", result.Errors.Select(_ => _.FieldName));
            Assert.False(result.IsSuccess);
        }