def test_create_user(self): with self.app_context(): self.user.save_to_db() user = UserModel.find_by_id(self.user.id) self.assertEqual(user.username, 'test_admin') self.assertEqual(user.password, '0123456789')
def test_update_user(self): with self.app_context(): self.user.username = '******' self.user.save_to_db() user = UserModel.find_by_id(self.user.id) self.assertEqual(user.username, 'administrator')
def put(self, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {'message': "User Not Found"}, 404 data = user_parser.parse_args() user.update_to_db(data['password']) return {'message': "User password updated"}, 200
def post(self, user_id): user = UserModel.find_by_id(user_id) if user: return user.to_json_score() else: return { 'message': "User with an user_id = {} " "does not exist.".format(user_id) }, 404
def post(self): data = UserRegister.parser.parse_args() if UserModel.find_by_id(data['username']): return {"message": "A user with that username already exists"}, 400 user = UserModel(**data) user.save_to_db() return {"message": "User created successfully."}, 201
def get_kitty_by_id(user_id, kitty_id): kitties_from_db = get_user_kitties(user_id) kitty_by_id = KittyModel.find_by_id(kitty_id) if kitty_by_id.name not in kitties_from_db: response = build_response({'error message': 'Kitty not found'}, 404) return response user = UserModel.find_by_id(user_id) kitty_dict = kitty_by_id.serialize_kitty() kitty_dict['user'] = {'id': user_id, 'email': user.email} response = build_response(kitty_dict) return response
def post(self): """ Create a new transaction. Check that the auth'd user is sending a positive amount of money to an existing user, and that they have enough money in their account to complete the transaction """ data = transaction_parser.parse_args() from_user_id = data["from_user_id"] to_user_id = data["to_user_id"] amount = data["amount"] if (current_user.id != from_user_id or from_user_id == to_user_id or amount <= 0): return {"message": "invalid transaction details"}, 400 from_user = UserModel.find_by_id(from_user_id) to_user = UserModel.find_by_id(to_user_id) if (from_user is None or to_user is None): return {"message": "invalid transaction details"}, 400 new_transaction = TransactionModel( from_user_id=from_user_id, to_user_id=to_user_id, amount=amount, ) try: new_transaction.save_to_db() return { "message": f"Sent {new_transaction.amount} from {from_user.username} to {to_user.username}", } except Exception as exception: print(exception) return {"message": "Something went wrong"}, 500
def put(self, id): data = User.parser.parse_args() user = UserModel.find_by_id(id) if user: if User.validOptions(self): user.username = data['username'] user.email = data['email'] user.phone = data['phone'] user.send_frequency = data['send_frequency'] user.send_method = data['send_method'] user.sending_status = data['sending_status'] user.save_to_db() return user.json(), 201
def put_user(user_data, user_id): user = UserModel.find_by_id(user_id) # Buscamos el user por id if user: # Si existe ese user # Comprobamos si existe ya un user con ese email user_email = UserModel.find_by_email(user_data['email']) if not user_email: # Si el email está disponible: machacamos los datos if not user_facade.email_validation(user_data['email']): response = build_response( {'error message': 'Email has an incorrect format'}, 400) return response user.email = user_data['email'] user.password = user_data['password'] else: # El email NO está disponible: devolvemos un error 409 response = build_response( {'error message': 'Email alrady exists'}, 409) return response else: # Si no existe ese user, devolvemos un error 404 response = build_response({'error message': 'User not found'}, 404) return response user.save() # Guardo la información response = build_response({'email': user.email}) return response
def test_crud(self): with self.app_context(): # Create user = UserModel('test_user', 'password', 'admin') user.save_to_db() # Read user = UserModel.find_by_username('test_user') self.assertIsNotNone(user) user = UserModel.find_by_id(1) self.assertIsNotNone(user) user = next(iter(UserModel.find_by_role('admin'))) self.assertIsNotNone(user) # Update user.username = '******' user.save_to_db() user = UserModel.find_by_username('administrator') self.assertIsNotNone(user) # Delete user.delete_from_db() self.assertIsNone(UserModel.find_by_username('administrator'))
def delete(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"message": USER_NOT_FOUND}, 400 user.delete_from_db() return {"message": USER_DELETED}
def delete(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"message": "User not found"}, 404 user.delete() return {"message", "User deleted"}, 200
def get(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"message": "User not found"}, 404 return user_schema.dump(user), 200
def delete(cls, user_id): user = UserModel.find_by_id(user_id) if not user: return {'message': 'User not found'}, 404 user.delete_from_db() return {'message': 'User deleted!'}
def get(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {'message': "User Not Found"}, 404 return user.to_json(), 200
def delete(self, id): user = UserModel.find_by_id(id) if user: user.delete_from_db() return {'message': 'user deleted.'} return {'message': 'user not found.'}, 404
def get(self, id): # data = UserModel.find_by_username(name) data = UserModel.find_by_id(id) if data: return data.json() return {'message': 'User not found'}, 404
def get(self, user_id): return UserModel.find_by_id(user_id)
def identity(payload): print('XXXXXXXXXX', payload) user_id = payload['identity'] return UserModel.find_by_id(user_id)
def identity(payload): user_id = payload['identity'] return UserModel.find_by_id( user_id) # returns user based on their id, or None if not found
def get(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"message": USER_NOT_FOUND}, 400 return user.json()
def identity(payload): user_id = payload[ 'identity'] # ['identity is the first element of the payload] return UserModel.find_by_id(user_id)
def test_delete_user(self): with self.app_context(): self.user.delete_from_db() user = UserModel.find_by_id(self.user.id) self.assertIsNone(user)
def get(cls, user_id): user = UserModel.find_by_id(user_id) if not user: return {'message': 'User not found'}, 404 return user.json()
def identity(payload): user_id = payload['identity'] return UserModel.find_by_id(user_id)
def delete(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {'message': "User Not Found"}, 404 user.delete_from_db() return {'message': "User deleted"}, 200