def update_user_by_id(user_id: int): if request.json: user = User.query.filter(User.id == user_id).first() if user: if common.valid_user_params(request.json): for request_parameter in user_params: if request_parameter in request.json: setattr( user, request_parameter, request.json[request_parameter], ) try: experience_ids = request.json["experiences"] if not isinstance(experience_ids, list): return ( jsonify( message="Experiences Is Not A List"), 400, ) for experience_id in experience_ids: if not isinstance(experience_id, int): return jsonify( message=f"Experience Ids Must Be Int" f". {experience_id} Is Not An " f"Int") experiences = Experience.query.filter( Experience.id.in_(experience_ids)).all() user.experiences.append(experiences) except KeyError: pass db.session.add(user) db.session.commit() return jsonify(id=f"{request.host_url}api/user/" f"{user.id}") else: jsonify( message="Invalid Data", reasons=[ "Email Must Be Valid," "Password Must Be" "Alphanumeric and 4-" "15 Characters Long" ], ) else: jsonify(message="User does not exist"), 404 else: return ( jsonify(message="Request Parameters Must Be Submitted In JSON"), 400, )
def create_user(): if request.json: if common.valid_user_params(request.json): if common.parameters_in_request_json(request.json, user_params): first_name = request.json["first_name"] last_name = request.json["last_name"] city_state = request.json["city_state"] phone_number = request.json["phone_number"] email = request.json["email_address"] password = request.json["password"] user = User( first_name=first_name, last_name=last_name, city_state=city_state, phone_number=phone_number, email_address=email, password=password, ) db.session.add(user) db.session.commit() return jsonify(id=f"{request.host_url}api/user/{user.id}") else: return ( jsonify( message="One Or More Parameters Are Missing", required_parameters=user_params, ), 400, ) else: return ( jsonify( message="Invalid Data", reasons=[ "Email Must Be Valid, Password Must Be " "Alphanumeric and 4-8 Characters Long" ], ), 400, ) else: return ( jsonify(message="Request Parameters Must Be Submitted In JSON"), 400, )
def test_invalid_password_no_numbers_user_params(): request_json = {"password": "******"} assert not common.valid_user_params(request_json)
def test_invalid_password_greater_than_15_chars_user_params(): request_json = {"password": "******"} assert not common.valid_user_params(request_json)
def test_invalid_password_less_than_4_chars_user_params(): request_json = {"password": "******"} assert not common.valid_user_params(request_json)
def test_invalid_email_no_user_user_params(): request_json = {"email_address": "@lol"} assert not common.valid_user_params(request_json)
def test_invalid_email_no_top_level_domain_user_params(): request_json = {"email_address": "ron@lol"} assert not common.valid_user_params(request_json)
def test_valid_user_params(): request_json = {"email_address": "*****@*****.**", "number": 12345} assert common.valid_user_params(request_json)