Inheritance: IUserProfileValidation
        public void RegisterUser_Returns_Redirect_If_ModelState_Is_Invalid()
        {
            RegisterModel model = new RegisterModel
                                      {
                                          Email = "goodEmail",
                                          Password = "******",
                                          ConfirmPassword = "******",
                                          FirstName = "Jason",
                                          LastName = "Powers",
                                          Birthdate = DateTime.Parse("1/1/1970"),
                                          Gender = "M",
                                          ZipCode = "85213",
                                          Consent = true
                                      };

            controller.ModelState.AddModelError("", "Dummy error message.");

            ActionResult result = controller.RegisterUser(model);
            Assert.IsInstanceOf(typeof(RedirectToRouteResult), result);
            Assert.IsNotNull(controller.TempData["RegisterModel"]);
        }
        public void RegisterUser_Returns_Redirect_On_Success()
        {
            RegisterModel model = new RegisterModel
                                      {
                                          Email = "goodEmail",
                                          Password = "******",
                                          ConfirmPassword = "******",
                                          FirstName = "Jason",
                                          LastName = "Powers",
                                          Birthdate = DateTime.Parse("1/1/1970"),
                                          Gender = "M",
                                          ZipCode = "85213",
                                          Consent = true
                                      };

            ActionResult result = controller.RegisterUser(model);
            Assert.IsInstanceOf(typeof(RedirectToRouteResult), result);
            Assert.IsNull(controller.TempData["RegisterModel"]);
        }
Esempio n. 3
0
        public ActionResult RegisterUser(RegisterModel model, string returnUrl = "")
        {
            if (ModelState.IsValid)
            {
                MembershipCreateStatus status;
                UserProfile userProfile;
                Organization organization;

                using (new UnitOfWorkScope())
                using (var transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    // This should ensure best compatiblity through a variety of SQL database environments
                    // (e.g. - SQL Server, MySQL, SQL Azure).
                    userProfile = Mapper.Map<RegisterModel, UserProfile>(model);
                    organization = OrganizationRepository.GetDefaultOrganization(readOnly: false);

                    if (organization.UserProfiles == null)
                    {
                        organization.UserProfiles = new List<UserProfile>();
                    }

                    userProfile.Active = true;
                    userProfile.IsActivated = false;
                    var service = new GrassrootsMembershipService();
                    userProfile.ActivationHash = service.GetUserAuthorizationHash();
                    userProfile.LastActivationAttempt = DateTime.Now;
                    organization.UserProfiles.Add(userProfile);
                    OrganizationRepository.Save();
                    status = MembershipService.CreateUser(model.Email, model.Password, model.Email);
                    transactionScope.Complete();
                }

                if (status == MembershipCreateStatus.Success)
                {
                    accountMailer.Authorize(MapAuthorizeModel(userProfile, organization, returnUrl)).SendAsync();

                    return RedirectToAction("AwaitingActivation", "Account");
                }
            }

            var url = returnUrl;
            TempData["RegisterModel"] = model;
            TempData["ModelErrors"] = FindModelErrors();
            return RedirectToAction("Register", "Account", new { returnUrl = url });
        }