public void IndexPostAction_InvalidModel_ReturnsConfiguredViewWithSameModel() { var controller = new DummyRegistrationController(); controller.TemplateName = "TestTemplate"; var viewModel = new RegistrationViewModel(); controller.ModelState.AddModelError("TestError", "Test error message"); var result = controller.Index(viewModel); Assert.IsNotNull(result, "The action result is null."); Assert.IsInstanceOfType(result, typeof(ViewResult), "The action result is not of the expected type."); var viewResult = (ViewResult)result; Assert.AreEqual("Registration.TestTemplate", viewResult.ViewName, "The Index did not return the configured view according to its convention."); Assert.IsNotNull(viewResult.Model, "The Index action did not assign a view model."); Assert.IsInstanceOfType(viewResult.Model, typeof(RegistrationViewModel), "The Index action did not assign a view model of the expected type."); Assert.AreSame(viewModel, viewResult.Model, "The model in the result is not the same as the one passed to the action."); }
public void IndexPostAction_ValidModelFailedRegistration_ReturnsConfiguredViewWithSameModelAndErrorMessage() { var controller = new DummyRegistrationController(); controller.TemplateName = "TestTemplate"; var viewModel = new RegistrationViewModel(); // "Fail" is a magical UserName that tells the Dummy Model to fail the registration. viewModel.UserName = "******"; var result = controller.Index(viewModel); Assert.IsNotNull(result, "The action result is null."); Assert.IsInstanceOfType(result, typeof(ViewResult), "The action result is not of the expected type."); Assert.AreEqual(MembershipCreateStatus.InvalidUserName.ToString(), controller.ViewBag.Error, "ViewBag did not containg the expected error message."); var viewResult = (ViewResult)result; Assert.AreEqual("Registration.TestTemplate", viewResult.ViewName, "The Index did not return the configured view according to its convention."); Assert.IsNotNull(viewResult.Model, "The Index action did not assign a view model."); Assert.IsInstanceOfType(viewResult.Model, typeof(RegistrationViewModel), "The Index action did not assign a view model of the expected type."); Assert.AreSame(viewModel, viewResult.Model, "The model in the result is not the same as the one passed to the action."); }
/// <summary> /// Renders appropriate list view depending on the <see cref="TemplateName" /> /// </summary> /// <returns> /// The <see cref="ActionResult" />. /// </returns> public ActionResult Index() { var fullTemplateName = this.templateNamePrefix + this.TemplateName; var viewModel = new RegistrationViewModel(); viewModel.EmailAddressShouldBeTheUsername = this.EmailAddressShouldBeTheUsername; this.Model.InitializeViewModel(viewModel); this.ViewBag.ShowSuccessfulRegistrationMsg = false; this.ViewBag.Error = this.Model.GetError(); return this.View(fullTemplateName, viewModel); }
public ActionResult Index(RegistrationViewModel viewModel) { viewModel.EmailAddressShouldBeTheUsername = this.EmailAddressShouldBeTheUsername; if(this.EmailAddressShouldBeTheUsername){ ModelState.Remove("UserName"); } if (ModelState.IsValid) { var status = this.Model.RegisterUser(viewModel); if (status == MembershipCreateStatus.Success) { if (this.Model.ActivationMethod == ActivationMethod.AfterConfirmation) { this.ViewBag.ShowActivationMsg = true; } if (this.Model.SuccessfulRegistrationAction == SuccessfulRegistrationAction.ShowMessage) { this.ViewBag.ShowSuccessfulRegistrationMsg = true; } else if (this.Model.SuccessfulRegistrationAction == SuccessfulRegistrationAction.RedirectToPage) { return this.Redirect(this.Model.GetPageUrl(this.Model.SuccessfulRegistrationPageId)); } } else { this.ViewBag.Error = this.Model.ErrorMessage(status); } } this.Model.InitializeViewModel(viewModel); var fullTemplateName = this.templateNamePrefix + this.TemplateName; return this.View(fullTemplateName, viewModel); }
public MembershipCreateStatus RegisterUser(RegistrationViewModel model) { if (model.UserName != "Fail") return MembershipCreateStatus.Success; else return MembershipCreateStatus.InvalidUserName; }
public void InitializeViewModel(RegistrationViewModel viewModel) { }
public void IndexPostAction_ValidModelRedirectAction_Redirects() { var controller = new DummyRegistrationController(); controller.TemplateName = "TestTemplate"; var viewModel = new RegistrationViewModel(); controller.Model.SuccessfulRegistrationPageId = new Guid("3bf29da0-1074-4a71-bb23-7ae43f36d8f9"); controller.Model.SuccessfulRegistrationAction = SuccessfulRegistrationAction.RedirectToPage; var result = controller.Index(viewModel); Assert.IsNotNull(result, "The action result is null."); Assert.IsInstanceOfType(result, typeof(RedirectResult), "The action result is not of the expected type."); var redirectResult = (RedirectResult)result; Assert.AreEqual("http://3bf29da0-1074-4a71-bb23-7ae43f36d8f9", redirectResult.Url, true, "The action did not redirect to the expected URL."); }
public void IndexPostAction_ValidModelShowMessageAction_ShowsSuccessMessage() { var controller = new DummyRegistrationController(); controller.TemplateName = "TestTemplate"; var viewModel = new RegistrationViewModel(); controller.Model.SuccessfulRegistrationAction = SuccessfulRegistrationAction.ShowMessage; var result = controller.Index(viewModel); Assert.IsNotNull(result, "The action result is null."); Assert.IsInstanceOfType(result, typeof(ViewResult), "The action result is not of the expected type."); Assert.AreEqual(controller.ViewBag.ShowSuccessfulRegistrationMsg, true, "The result did not contain the expected message."); }
/// <summary> /// Attempt to create user. Returns true if the creation was successful, otherwise false. /// </summary> /// <param name="manager">The manager.</param> /// <param name="user">The user.</param> /// <param name="status">The status that will be set depending on the creation outcome.</param> protected virtual bool TryCreateUser(UserManager manager, RegistrationViewModel userData, out User user, out MembershipCreateStatus status) { string username = (userData.EmailAddressShouldBeTheUsername) ? userData.Email : userData.UserName; user = manager.CreateUser(username, userData.Password, userData.Email, null, null, this.ActivationMethod == ActivationMethod.Immediately, null, out status); return status == MembershipCreateStatus.Success; }
/// <inheritdoc /> public virtual MembershipCreateStatus RegisterUser(RegistrationViewModel viewModel) { var userManager = UserManager.GetManager(this.MembershipProviderName); User user; MembershipCreateStatus status; using (new ElevatedModeRegion(userManager)) { if (this.TryCreateUser(userManager, viewModel, out user, out status)) { userManager.SaveChanges(); this.CreateUserProfiles(user, viewModel.Profile); this.AssignRolesToUser(user); this.ConfirmRegistration(userManager, user); //this.ExecuteUserProfileSuccessfullUpdateActions(); } } return status; }
/// <inheritDoc/> public void InitializeViewModel(RegistrationViewModel viewModel) { if(viewModel != null) { viewModel.LoginPageUrl = this.GetLoginPageUrl(); viewModel.MembershipProviderName = this.MembershipProviderName; viewModel.CssClass = this.CssClass; viewModel.SuccessfulRegistrationPageUrl = this.GetPageUrl(this.SuccessfulRegistrationPageId); } }