public void AddUser_ExistingDepartmentInvalidUser_MissingProperties() { var dc = initializeTest(); var user = new GirafUserDTO() { }; var res = dc.AddUser(DEPARTMENT_ONE, user.Id).Result; Assert.Equal(res.ErrorCode, ErrorCode.MissingProperties); }
public async Task <Response <GirafUserDTO> > UpdateUser(string id, [FromBody] GirafUserDTO newUser) { if (newUser == null || newUser.Username == null || newUser.ScreenName == null) { return(new ErrorResponse <GirafUserDTO>(ErrorCode.MissingProperties)); } var user = _giraf._context.Users.FirstOrDefault(u => u.Id == id); // Get the roles the user is associated with var userRole = await _roleManager.findUserRole(_giraf._userManager, user); if (user == null) { return(new ErrorResponse <GirafUserDTO>(ErrorCode.UserNotFound)); } // check access rights if (!(await _authentication.HasEditOrReadUserAccess(await _giraf._userManager.GetUserAsync(HttpContext.User), user))) { return(new ErrorResponse <GirafUserDTO>(ErrorCode.NotAuthorized)); } // check whether user with that username already exist that does dot have the same id if (_giraf._context.Users.Any(u => u.UserName == newUser.Username && u.Id != user.Id)) { return(new ErrorResponse <GirafUserDTO>(ErrorCode.UserAlreadyExists)); } // update fields if they are not null if (!String.IsNullOrEmpty(newUser.Username)) { await _giraf._userManager.SetUserNameAsync(user, newUser.Username); } if (!String.IsNullOrEmpty(newUser.ScreenName)) { user.DisplayName = newUser.ScreenName; } // save and return _giraf._context.Users.Update(user); await _giraf._context.SaveChangesAsync(); return(new Response <GirafUserDTO>(new GirafUserDTO(user, userRole))); }