public ActionResult Register(Volunteer model) { if (!ModelState.IsValid) { return View(model); } var existingVolunteer = volunteerDataService.SelectOne(v => v.Username == model.Username); if (existingVolunteer != null) { ModelState.AddModelError("Username", "That username already exists."); return View(); } // Initially the account is inactive. An administrator has to activate the account in order // for the volunteer to access the site. // This is a HACK until we get role based authentication working. model.Active = false; model.Id = Guid.NewGuid(); model.LastSignInDate = DateTime.Now; model.Password = volunteerDataService.HashPassword(model.Password, model.Id); model.CreateByVolunteerId = model.Id; model.CreateDate = DateTime.Now; model.LastModByVolunteerId = model.Id; model.LastModDate = DateTime.Now; volunteerDataService.Insert(model, model.Id); // Do not sign the new registration into the website until we have roles in place! // That will be explained in the Welcome page. return RedirectToAction("Welcome"); }
public void AddPostValidModel_ReturnsRedirect() { var volunteerFake = new Volunteer { Id = Guid.NewGuid(), }; var recipientFake = new Recipient { Active = true, FirstName = "Dan", Id = Guid.NewGuid(), }; var dataServiceMock = new Mock<IDataService<Recipient>>(); dataServiceMock.Setup(d => d.Insert(It.IsAny<Recipient>(), It.IsAny<Guid>())).Verifiable(); var formsAuthenticationServiceMock = new Mock<IFormsAuthenticationService>(); formsAuthenticationServiceMock.Setup(f => f.GetVolunteerID(null)) .Returns(volunteerFake.Id); var controller = new RecipientController(dataServiceMock.Object, new Mock<IDataService<Volunteer>>().Object, formsAuthenticationServiceMock.Object); ActionResult result = controller.Add(recipientFake); dataServiceMock.VerifyAll(); Assert.IsInstanceOf(typeof (RedirectToRouteResult), result); }
public void Register_FailedSave_ReturnsRedirect() { // arrange var accountController = new AccountController( new Mock<IFormsAuthenticationService>().Object, new Mock<IDataService<Volunteer>>().Object); accountController.ModelState.AddModelError("testRequired", "dummy required field missing"); var fakeVolunteer = new Volunteer { FirstName = "Firstname", LastName = "LNameTest", Username = "******", Password = "******" }; ActionResult result = accountController.Register(fakeVolunteer); Assert.IsInstanceOf<ViewResult>(result); var view = (ViewResult) result; Assert.IsInstanceOf<Volunteer>(view.Model); }
public ActionResult Edit(Volunteer model) { if (!ModelState.IsValid) { return View(model); } Volunteer originalModel = volunteerDataService.SelectOne(v => v.Id == formsAuthenticationService.GetVolunteerID(User)); model.Id = originalModel.Id; // This is a HACK. We should have a separate view to change a password, or a better method. model.Password = UpdatedPassword(model, originalModel); model.LastSignInDate = DateTime.Now; model.CreateByVolunteerId = originalModel.CreateByVolunteerId; model.CreateDate = originalModel.CreateDate; model.LastModByVolunteerId = originalModel.Id; model.LastModDate = DateTime.Now; model.Active = originalModel.Active; volunteerDataService.Update(model); return View("Index"); }
public void SetAuthCookie(Volunteer volunteer, bool createPersistentCookie) { FormsAuthentication.SetAuthCookie(getUserName(volunteer), createPersistentCookie); BakeCookie(volunteer, FormsAuthentication.GetAuthCookie(FormsAuthentication.FormsCookieName, false), createPersistentCookie); }
public ActionResult Register(Volunteer volunteer) { if (!ModelState.IsValid) { return View(volunteer); } volunteerDataService.Save(volunteer); return new RedirectResult("Welcome"); }
public void Save_WasCalled() { var fakeVolunteer = new Volunteer(); var repository = new Mock<IRepository<Volunteer>>(); repository.Setup(r => r.Insert(It.IsAny<Volunteer>(), It.IsAny<object>())).Verifiable(); var dataService = new DataService<Volunteer>(repository.Object); dataService.Insert(fakeVolunteer, fakeVolunteer.Id); repository.Verify(); }
public void FindOne_ReturnsEntity() { var fakeVolunteer = new Volunteer { Id = Guid.NewGuid(), Username = "******", Password = "******" }; var repository = new Mock<IRepository<Volunteer>>(); repository.Setup(r => r.SelectOne(v => v.Username == fakeVolunteer.Username && v.Password == fakeVolunteer.Password)) .Returns(fakeVolunteer); }
/// <summary> /// Baking the cookie involves adding the volunteer ID and encrypting it. /// </summary> /// <param name="volunteer"></param> /// <param name="cookie"></param> /// <param name="createPersistentCookie"></param> private void BakeCookie(Volunteer volunteer, HttpCookie cookie, bool createPersistentCookie) { cookie.Value = FormsAuthentication.Encrypt(new FormsAuthenticationTicket( 1, getUserName(volunteer), DateTime.Now, DateTime.Now.AddTicks(FormsAuthentication.Timeout.Ticks), createPersistentCookie, volunteer.Id.ToString(), FormsAuthentication.FormsCookiePath)); cookie.Expires = DateTime.Now.AddTicks(FormsAuthentication.Timeout.Ticks); cookie.HttpOnly = true; HttpContext.Current.Response.Cookies.Set(cookie); }
public void Register_Save_ReturnsRedirect() { var fakeVolunteer = new Volunteer { FirstName = "Firstname", LastName = "LNameTest", Username = "******", Password = "******", PrimaryPhone = "3334445555", Email = "*****@*****.**" }; var dataService = new Mock<IDataService<Volunteer>>(); dataService.Setup(d => d.Insert(fakeVolunteer, It.IsAny<Guid>())).Verifiable("Save was not called."); var accountController = new AccountController( new Mock<IFormsAuthenticationService>().Object, dataService.Object); ActionResult result = accountController.Register(fakeVolunteer); dataService.VerifyAll(); Assert.IsInstanceOf<RedirectToRouteResult>(result); }
public void SignInPost_ValidUser_Redirects() { var model = new SignInViewModel { Username = "******", Password = "******", RememberMe = false, }; var fakeVolunteer = new Volunteer { Username = model.Username, Password = model.Password, Active = true, }; var formsAuthenticationServiceMock = new Mock<IFormsAuthenticationService>(); formsAuthenticationServiceMock.Setup(f => f.SetAuthCookie(It.IsAny<Volunteer>(), It.IsAny<bool>())) .Verifiable(); var volunteerDataServiceMock = new Mock<IDataService<Volunteer>>(); volunteerDataServiceMock.Setup(v => v.SelectOne(It.IsAny<Expression<Func<Volunteer, bool>>>())) .Returns(fakeVolunteer); volunteerDataServiceMock.Setup(v => v.HashPassword(It.IsAny<string>())) .Returns(model.Password); volunteerDataServiceMock.Setup(v => v.VerifyPassword(It.IsAny<string>(), It.IsAny<string>())) .Returns(true); var accountController = new AccountController( formsAuthenticationServiceMock.Object, volunteerDataServiceMock.Object); ActionResult result = accountController.SignIn(model, "home/index"); Assert.IsInstanceOf<RedirectResult>(result); }
/// <summary> /// Checks to see if the volunteer updated their password on the form. /// </summary> /// <param name="model">The updated model.</param> /// <param name="originalModel">The original model.</param> /// <returns>The new hashed password or the original password.</returns> private string UpdatedPassword(Volunteer model, Volunteer originalModel) { return model.Password != PasswordChars ? volunteerDataService.HashPassword(model.Password) : originalModel.Password; }
private string getUserName(Volunteer volunteer) { return string.Format("{0} {1}", volunteer.FirstName ?? "", volunteer.LastName ?? "").Trim(); }