public CreateBuildingTests() { facade = new Mock <IFacade>(); var username = "******"; var identity = new GenericIdentity(username, ""); var nameIdentityClaim = new Claim(ClaimTypes.NameIdentifier, username); identity.AddClaim(nameIdentityClaim); var principal = new Mock <IPrincipal>(); principal.Setup(p => p.Identity).Returns(identity); principal.Setup(p => p.IsInRole("Tenant")).Returns(true); //var userStore = new Mock<IUserStore<ApplicationUser>>(); //userManager = new ApplicationUserManager(userStore.Object); model = new RegisterBuildingModel() { Address = "32 Francis st, Sydney, 2000", BuildingName = "Metro Apartments" }; controllerContext = new HttpControllerContext { RequestContext = { Principal = principal.Object } }; }
public async Task <IHttpActionResult> CreateBuilding(RegisterBuildingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); model.Password = ""; if (result.Succeeded) { try { Cad.CreateBuilding(model.FirstName, model.LastName, model.DateOfBirth, model.Phone, user.Id, model.BuildingName, model.Address); UserManager.AddToRole(user.Id, "BuildingManager"); } catch (ConnectedApartmentsException e) { UserManager.Delete(user); return(BadRequest(e.Message)); } catch (Exception) { return(InternalServerError()); } var emailService = new EmailService(); emailService.SendBuildingCreationEmail(model.Email); return(GetResponse()); } else { return(GetErrorResult(result)); } }