public void Put_ReturnsOk() { // Get a list of bids so we can grab the correct id that is stored in // the (in)memory db var username = TestSeedData.TestUsername; var getResources = _client.AuthorizeRequest().GetAsync(APIROUTE + "username/" + username).Result; getResources.EnsureSuccessStatusCode(); var resourceRaw = getResources.Content.ReadAsStringAsync().Result; var resource = JsonConvert.DeserializeObject <UserResource>(resourceRaw); // Verify we have something to look up Assert.NotNull(resource); // Let's just change the name since other tests rely on having this // username & password var user = new EditUserResource { FullName = "Test Name Change", UserName = "******", Password = "******", PasswordConfirm = "test1234!", Role = "Tester" }; // Act var response = _client.AuthorizeRequest().PutAsync(APIROUTE + resource.Id, ClientHelper.EncodeContent(user)).Result; // Assert Ok response.EnsureSuccessStatusCode(); }
public void Post_WhenUnAuthorized_ReturnsOk_WithNewUserResource() { // Has to be a unique username var user = new EditUserResource { FullName = "Testing Tests", UserName = "******", Password = "******", PasswordConfirm = "a1234!", Role = "Tester" }; var response = _client.PostAsync(APIROUTE, ClientHelper.EncodeContent(user)).Result; response.EnsureSuccessStatusCode(); var json = response.Content.ReadAsStringAsync().Result; var result = JsonConvert.DeserializeObject <UserResource>(json); Assert.NotNull(result); Assert.IsType <UserResource>(result); // Do cleanup CleanUp(); }
public void Put_WhenUserResourceInvalid_ReturnsWithValidationErrors() { // Get a list of bids so we can grab the correct id that is stored in // the (in)memory db var username = TestSeedData.TestUsername; var getResources = _client.AuthorizeRequest().GetAsync(APIROUTE + "username/" + username).Result; getResources.EnsureSuccessStatusCode(); var resourceRaw = getResources.Content.ReadAsStringAsync().Result; var resource = JsonConvert.DeserializeObject <UserResource>(resourceRaw); // Verify we have something to look up Assert.NotNull(resource); var user = new EditUserResource { FullName = "Testing Tests", UserName = "******", Password = "******", // Passwords do not match PasswordConfirm = "test1234!zzzz", Role = "Tester" }; // Act var response = _client.AuthorizeRequest().PutAsync(APIROUTE + resource.Id, ClientHelper.EncodeContent(user)).Result; var json = response.Content.ReadAsStringAsync().Result; Assert.Equal(System.Net.HttpStatusCode.BadRequest, response.StatusCode); Assert.Contains("validation errors", json); }
public ValidationResult ValidateResource(EditUserResource resource, bool isUpdate) { var result = new ValidationResult(); if (resource != null) { var validator = new UserValidator(isUpdate); var vr = validator.Validate(resource); if (vr.IsValid) { result.IsValid = true; return(result); } if (vr.Errors.Any()) { foreach (var error in vr.Errors) { result.ErrorMessages.Add(error.PropertyName, error.ErrorMessage); } } } return(result); }
public async Task <IActionResult> updateUserByInstructor( string phoneNumber, [FromBody] EditUserResource userResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await userRepository.GetOne(phoneNumber); if (user == null) { return(NotFound()); } user.LastUpdate = DateTime.Now; mapper.Map <EditUserResource, User>(userResource, user); userRepository.CreateLogs(phoneNumber, userResource.ByWho); await unitOfWork.CompleteAsync(); user = await userRepository.GetOne(phoneNumber); var response = mapper.Map <User, UserResource>(user); return(Ok(response)); }
public async Task <IActionResult> SetUserInfo([FromBody] EditUserResource model) { if (ModelState.IsValid) { return(BadRequest()); } await _userService.EditUser(model); return(Ok()); }
public void UpdateUser(int id, EditUserResource resource) { var userToUpdate = _userRepo.FindById(id); if (userToUpdate != null && resource != null) { // Hash any passwords resource.Password = AppSecurity.HashPassword(resource.Password).HashedPassword; _userRepo.Update(id, _mapper.Map(resource, userToUpdate)); _userRepo.SaveChanges(); } }
public async Task <IActionResult> UpdateUser([FromBody] EditUserResource resource) { var user = _mapper.Map <ApplicationUser>(resource); var userToUpdate = await _userService.GetUserByID(User.GetUserId()); if (userToUpdate == null) { return(NotFound(_response.Error("The user was not found"))); } userToUpdate = await _userService.UpdateUser(userToUpdate, user); var userResource = _mapper.Map <UserResource>(userToUpdate); return(Ok(_response.Ok(userResource))); }
public void Put_WhenNull_ReturnsNotFound() { // Make sure is valid or won't pass validation and return a 500 - json problem var user = new EditUserResource { FullName = "Testing Tests", UserName = "******", Password = "******", PasswordConfirm = "a1234!", Role = "Tester" }; // Act var response = _client.AuthorizeRequest().PutAsync(APIROUTE + 99999, ClientHelper.EncodeContent(user)).Result; // Assert Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode); }
public void Post_WhenResourceIsInvalied_ReturnsBadRequestWithValidationErrors() { // Arrange - Passwords do not match, username taken to be sure for validation failure var user = new EditUserResource { FullName = "Testing Tests", UserName = TestSeedData.TestUsername, // username should already be taken so fails here Password = "******", // password not strong enough fails here PasswordConfirm = "12341zxzxc", // password doesn't match fails here Role = "Tester" }; var response = _client.AuthorizeRequest().PostAsync(APIROUTE, ClientHelper.EncodeContent(user)).Result; var json = response.Content.ReadAsStringAsync().Result; Assert.Equal(System.Net.HttpStatusCode.BadRequest, response.StatusCode); Assert.Contains("validation errors", json); }
public UserResource CreateUser(EditUserResource resource) { if (resource == null) { throw new ArgumentNullException("createUserResource cannot be null"); } var user = _mapper.Map <User>(resource); var hashResult = AppSecurity.HashPassword(resource.Password); user.Password = hashResult.HashedPassword; _userRepo.Add(user); _userRepo.SaveChanges(); return(_mapper.Map <UserResource>(user)); }
public IActionResult Update(int id, [FromBody] EditUserResource resource) { AppLogger.LogResourceRequest(nameof(Update), base.GetUsernameForRequest()); try { var result = _userService.ValidateResource(resource, isUpdate: true); if (!result.IsValid) { GetErrorsForModelState(result.ErrorMessages); } if (ModelState.IsValid) { var user = _userService.GetUserById(id); if (user == null) { return(NotFound(AppConfig.ResourceNotFoundById + id)); } // Only allow a user to update their own account if (user.Id != base.GetCurrentUserId()) { return(Unauthorized()); } _userService.UpdateUser(id, resource); return(Ok()); } return(ValidationProblem()); } catch (Exception ex) { return(BadRequestExceptionHandler(ex, nameof(Update))); } }
public IActionResult Create([FromBody] EditUserResource resource) { try { var result = _userService.ValidateResource(resource, isUpdate: false); if (!result.IsValid) { GetErrorsForModelState(result.ErrorMessages); } if (ModelState.IsValid) { var user = _userService.CreateUser(resource); return(CreatedAtAction(nameof(Create), user)); } return(ValidationProblem()); } catch (Exception ex) { return(BadRequestExceptionHandler(ex, nameof(Create))); } }
public async Task EditUser(EditUserResource model) { await(await unitOfWork.GetRepository <UserRepository, User, Guid>()).Update(mapper.Map <User>(model)); await unitOfWork.Commit(); }