public ActionResult Create(CompanyProfileEditModel viewModel) { if (ModelState.IsValid) { CompanyProfile toAdd = new CompanyProfile { Address1 = viewModel.Address1, Address2 = viewModel.Address2, BusinessType = viewModel.BusinessType.Value, City = viewModel.City, CompanyName = viewModel.CompanyName, OperatingDistance = viewModel.OperatingDistance, Phone = Util.ConvertPhoneForStorage(viewModel.Phone), PostalCode = viewModel.PostalCode, Published = viewModel.Published, StateId = viewModel.StateId, Website = viewModel.Website, SubscriptionStatus = viewModel.Subscribed ? SubscriptionStatus.Paid : SubscriptionStatus.Free }; if (_service.Create(toAdd)) { return RedirectToAction("Index"); } else { Util.MapValidationErrors(_service.ValidationDic, this.ModelState); } } rePopViewModel(viewModel); return View(viewModel); }
public void Post_CreateArchitect_CannotCreateCompany_Returns_ViewModelErrors() { // arrange string currentUserEmail = "*****@*****.**"; int currentUserId = 321; NewArchitectViewModel viewModel = new NewArchitectViewModel { CompanyName = "Some company", ContactEmail = "*****@*****.**", ContactFirstName = "bob", ContactLastName = "builder", ProjectNumber = "abc123", ProjectTitle = "the project" }; CompanyProfile company = new CompanyProfile { BusinessType = BusinessType.Architect, ContactEmail = viewModel.ContactEmail, CompanyName = viewModel.CompanyName }; CompanyProfile userCompany = new CompanyProfile { CompanyName = "another company" }; UserProfile theUser = new UserProfile { UserId = currentUserId, Email = currentUserEmail, FirstName = "jim", LastName = "Corn", Company = userCompany }; Mock<ICompanyProfileServiceLayer> service = new Mock<ICompanyProfileServiceLayer>(); service.Setup(s => s.Create(It.IsAny<CompanyProfile>())).Returns(false); service.SetupGet(s => s.ValidationDic).Returns(new Dictionary<string, string> { { "Error", "Cannot create company" } }); Mock<IWebSecurityWrapper> security = new Mock<IWebSecurityWrapper>(); security.Setup(s => s.UserExists(viewModel.ContactEmail)).Returns(false); Mock<IEmailSender> email = new Mock<IEmailSender>(); Mock<ControllerContext> context = new Mock<ControllerContext>(); context.SetupGet(c => c.HttpContext.User.Identity.Name).Returns(currentUserEmail); CompanyController controller = new CompanyController(service.Object, security.Object, email.Object); controller.ControllerContext = context.Object; // act var result = controller.CreateArchitect(viewModel); // assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public ActionResult CreateArchitect(NewArchitectViewModel viewModel) { CompanyProfile toCreate = new CompanyProfile { CompanyName = viewModel.CompanyName, ContactEmail = viewModel.ContactEmail, BusinessType = BusinessType.Architect }; // create record if user doesn't exist if (!_security.UserExists(viewModel.ContactEmail)) { if (_serviceLayer.Create(toCreate)) { Guid password = new Guid(); var token = _security.CreateUserAndAccount(viewModel.ContactEmail, password.ToString(), new { FirstName = viewModel.ContactFirstName, LastName = viewModel.ContactLastName, CompanyId = toCreate.Id } , true); _security.AddUserToRoles(viewModel.ContactEmail, new[] { "architect", "Manager" }); var user = _serviceLayer.GetUserProfile(_security.GetUserId(User.Identity.Name)); string invitingTagLine = string.Format("{0} {1} of {2}", user.FirstName, user.LastName, user.Company.CompanyName); string name = string.Format("{0} {1}", viewModel.ContactFirstName, viewModel.ContactLastName); //send email _email.InviteArchitect(viewModel.ContactEmail, name, viewModel.CompanyName, invitingTagLine, token); return RedirectToRoute("Default", new { controller = "Project", action = "CreateStepTwo", architectId = toCreate.Id, title = viewModel.ProjectTitle, number = viewModel.ProjectNumber }); } else { Util.MapValidationErrors(_serviceLayer.ValidationDic, this.ModelState); return View(viewModel); } } else { this.ModelState.AddModelError("ContactEmail", "There is already someone registered using that email"); return View(viewModel); } }
private CompanySearchResultItem companySearchResultItemMapper(CompanyProfile company) { CompanySearchResultItem result = new CompanySearchResultItem { CompanyId = company.Id, CompanyName = company.CompanyName, LinkPath = Url.Link("Default", new { controller = "Company", action = "Profile", id = company.Id }), BusinessType = company.BusinessType.ToDescription(), Area = company.City != null ? company.City + ", " + company.State.Abbr : "", ScopesOfWork = company.Scopes == null || company.Scopes.Count == 0 ? default(Dictionary<int, string>) : company.Scopes.Where(c => c.Scope.Children == null || c.Scope.Children.Count == 0).Select(s => s.Scope).ToDictionary(s => s.Id, s => s.CsiNumber + " " + s.Description) }; return result; }
public void Post_CreateArchitect_ValidModel_Returns_Redirect() { // arrange string currentUserEmail = "*****@*****.**"; int currentUserId = 321; int newArchitectId = 123; NewArchitectViewModel viewModel = new NewArchitectViewModel { CompanyName = "Some company", ContactEmail = "*****@*****.**", ContactFirstName = "bob", ContactLastName = "builder", ProjectNumber = "abc123", ProjectTitle = "the project" }; CompanyProfile company = new CompanyProfile { BusinessType = BusinessType.Architect, ContactEmail = viewModel.ContactEmail, CompanyName = viewModel.CompanyName }; CompanyProfile userCompany = new CompanyProfile { CompanyName = "another company" }; UserProfile theUser = new UserProfile { UserId = currentUserId, Email = currentUserEmail, FirstName = "jim", LastName = "Corn", Company = userCompany }; Mock<ICompanyProfileServiceLayer> service = new Mock<ICompanyProfileServiceLayer>(); service.Setup(s => s.Create(It.IsAny<CompanyProfile>())).Returns(true).Callback((CompanyProfile toCreate) => toCreate.Id = newArchitectId); service.Setup(s => s.GetUserProfile(321)).Returns(theUser); Mock<IWebSecurityWrapper> security = new Mock<IWebSecurityWrapper>(); security.Setup(s => s.GetUserId(currentUserEmail)).Returns(currentUserId); security.Setup(s => s.UserExists(viewModel.ContactEmail)).Returns(false); security.Setup(s => s.CreateUserAndAccount(viewModel.ContactEmail, It.IsAny<string>(), new { FirstName = viewModel.ContactFirstName, LastName = viewModel.ContactLastName, CompanyId = company.Id } , true)).Returns("abcderf"); Mock<IEmailSender> email = new Mock<IEmailSender>(); Mock<ControllerContext> context = new Mock<ControllerContext>(); context.SetupGet(c => c.HttpContext.User.Identity.Name).Returns(currentUserEmail); CompanyController controller = new CompanyController(service.Object, security.Object, email.Object); controller.ControllerContext = context.Object; // act var result = controller.CreateArchitect(viewModel); // assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); Assert.AreEqual("Default", ((RedirectToRouteResult)result).RouteName); Assert.AreEqual("Project", ((RedirectToRouteResult)result).RouteValues["controller"]); Assert.AreEqual("CreateStepTwo", ((RedirectToRouteResult)result).RouteValues["action"]); Assert.AreEqual(newArchitectId, ((RedirectToRouteResult)result).RouteValues["architect"]); Assert.AreEqual(viewModel.ProjectTitle, ((RedirectToRouteResult)result).RouteValues["title"]); Assert.AreEqual(viewModel.ProjectNumber, ((RedirectToRouteResult)result).RouteValues["number"]); }
public ActionResult Register(RegisterModel model) { RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper(); model.States = _serviceLayer.GetStates().Select(x => new SelectListItem { Text = x.Abbr, Value = x.Id.ToString() }); //model.BusinessTypes = _serviceLayer.GetBusinessTypes().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); if (String.IsNullOrEmpty(recaptchaHelper.Response)) { ModelState.AddModelError("", "Captcha answer cannot be empty."); return View(model); } RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse(); if (recaptchaResult != RecaptchaVerificationResult.Success) { ModelState.AddModelError("", "Incorrect captcha answer."); } int cpId; if (ModelState.IsValid) { // Attempt to register the user try { CompanyProfile cp = new CompanyProfile { Address1 = model.Address1, Address2 = model.Address2, BusinessType = model.BusinessType, City = model.City, CompanyName = model.CompanyName, OperatingDistance = model.OperatingDistance, Phone = Util.ConvertPhoneForStorage(model.Phone), PostalCode = model.PostalCode, Published = true, StateId = model.StateId }; GeoLocator locator = new GeoLocator(); string state = _serviceLayer.GetStates().Where(x => x.Id == model.StateId).FirstOrDefault().Abbr; if (model.Address1 == null || model.Address1 == string.Empty) { cp.GeoLocation = locator.GetFromCityStateZip(model.City, state, model.PostalCode); } else { cp.GeoLocation = locator.GetFromAddress(model.Address1, model.City, state, model.PostalCode); } // if we can create the company, create the user if (_serviceLayer.CreateCompany(cp)) { cpId = cp.Id; string confirmationToken = _security.CreateUserAndAccount( model.Email, model.Password, new { FirstName = model.FirstName, LastName = model.LastName, CompanyId = cp.Id }, true); _security.AddUserToRole(model.Email, "Manager"); //var businesstypes = _serviceLayer.GetBusinessTypes(); switch (model.BusinessType) { case BusinessType.GeneralContractor: _security.AddUserToRole(model.Email, "general_contractor"); break; case BusinessType.SubContractor: _security.AddUserToRole(model.Email, "subcontractor"); break; case BusinessType.Architect: _security.AddUserToRole(model.Email, "architect"); break; case BusinessType.Engineer: _security.AddUserToRole(model.Email, "engineer"); break; case BusinessType.Owner: _security.AddUserToRole(model.Email, "owner_client"); break; case BusinessType.MaterialsVendor: _security.AddUserToRole(model.Email, "materials_vendor"); break; case BusinessType.MaterialsMfg: _security.AddUserToRole(model.Email, "materials_manufacturer"); break; case BusinessType.Consultant: _security.AddUserToRole(model.Email, "consultant"); break; }; _emailer.SendConfirmationMail(model.FirstName, model.Email, confirmationToken); return RedirectToAction("RegisterStepTwo", "Account"); } } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } catch (Exception ex) { ModelState.AddModelError("Exception", ex.Message); } } // If we got this far, something failed, redisplay form return View(model); }