public async Task <IResponse <ActionResult> > UpdateUser([FromBody] UpdateUserModel user) { var validationResult = updateUserValidator.Validate(user); if (!validationResult.IsValid) { return(Invalid <ActionResult>(validationResult.Errors.Select(x => x.ErrorMessage).ToArray())); } try { var serviceUser = new Data.ServiceModels.User { Password = user.Password, Id = user.Id }; await userService.EditUser(serviceUser); return(Success <ActionResult>(null)); } catch (FqqException fex) { return(Error <ActionResult>(fex)); } }
public void CreateUser_ExistingUserName_ThrowsException() { mockUser.Setup(x => x.AnyAsync(It.IsAny <Expression <Func <User, bool> > >())).ReturnsAsync(true); userService = new UserService(mockUser.Object, null, null, null, null); var user = new Data.ServiceModels.User { UserName = "******", Password = "******", Role = Data.ServiceModels.Enums.UserRole.User }; Assert.That(() => userService.CreateUser(user), Throws.Exception.TypeOf <FqqException>()); }
/// <summary> /// edit user changes password /// </summary> /// <param name="userModel"></param> /// <returns></returns> public async Task EditUser(Data.ServiceModels.User userModel) { try { var dbUser = await user.SingleAsync(x => x.Id == userModel.Id && x.IsActive && !x.IsDeleted); dbUser.Password = EncriptPassword(userModel.Password); await user.Update(dbUser); await user.SaveChangesAsync(); } catch (InvalidOperationException iox) { throw new FqqException(FqqExceptionCode.UserNotFound, false, iox); } }
public async Task <IResponse <ActionResult> > DisableUser([FromBody] DeleteDisableUserModel user) { try { var serviceUser = new Data.ServiceModels.User { Id = user.Id }; await userService.DisableUser(serviceUser); return(Success <ActionResult>(null)); } catch (FqqException fex) { return(Error <ActionResult>(fex)); } }
public void EditUser_DeletedUser_ThrowsException() { mockDbContext = new BaseDbContext(options); mockDbContext.Users.Add(new User { Id = 1, UserName = "******", IsActive = true, IsDeleted = true, Password = "******", Mode = 1 }); mockDbContext.SaveChanges(); userService = new UserService(new Repository <User, Data.RepositoryModels.User>(mockDbContext, repoToEntity, entityToRepo), null, null, null, null); var user = new Data.ServiceModels.User { Id = 1, UserName = "******", Password = "******", Role = UserRole.User }; Assert.ThrowsAsync <FqqException>(() => userService.EditUser(user)); }
public async Task CreateUser_AddUser_PassSuccessfully() { mockDbContext = new BaseDbContext(options); userService = new UserService(new Repository <User, Data.RepositoryModels.User>(mockDbContext, repoToEntity, entityToRepo), null, null, null, null); var user = new Data.ServiceModels.User { UserName = "******", Password = "******", Role = UserRole.User }; await userService.CreateUser(user); var dbUser = await mockDbContext.Users.FirstOrDefaultAsync(); Assert.That(dbUser.UserName == user.UserName); }
/// <summary> /// delete user /// </summary> /// <param name="userModel"></param> /// <returns></returns> public async Task DeleteUser(Data.ServiceModels.User userModel) { try { var dbUser = await user.SingleAsync(x => x.Id == userModel.Id && !x.IsDeleted); dbUser.IsDeleted = true; await user.SaveChangesAsync(); } catch (InvalidOperationException iox) { throw new FqqException(FqqExceptionCode.UserNotFoundOrDeleted, false, iox); } catch (Exception ex) { throw new FqqException(FqqExceptionCode.GeneralError, "General error when deleting user", true, ex); } }
/// <summary> /// create user (make record in database) /// </summary> /// <param name="userModel"></param> /// <returns></returns> public async Task CreateUser(Data.ServiceModels.User userModel) { var userNameExists = await user.AnyAsync(x => x.UserName == userModel.UserName); if (userNameExists) { throw new FqqException(FqqExceptionCode.UserNameExists, false); } var dbUser = new User { UserName = userModel.UserName, RoleId = (int)userModel.Role, Password = EncriptPassword(userModel.Password), IsActive = true }; await user.AddAsync(dbUser); await user.SaveChangesAsync(); }
public async Task EditUser_ChangePassword_PassSuccessfully() { mockDbContext = new BaseDbContext(options); mockDbContext.Users.Add(new User { Id = 1, UserName = "******", IsActive = true, IsDeleted = false, Password = "******", Mode = 1 }); await mockDbContext.SaveChangesAsync(); userService = new UserService(new Repository <User, Data.RepositoryModels.User>(mockDbContext, repoToEntity, entityToRepo), null, null, null, null); var user = new Data.ServiceModels.User { Id = 1, UserName = "******", Password = "******", Role = UserRole.User }; await userService.EditUser(user); var dbUser = await mockDbContext.Users.FirstOrDefaultAsync(); Assert.That(user.Password != dbUser.Password && !string.IsNullOrEmpty(dbUser.Password) && dbUser.Password != "asdasdasd"); }