Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:updating an user with empty username and name gives an error message #695

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
return messages.USER_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

username = data.get("username", None)

if username:
user_with_same_username = UserModel.find_by_username(username)

Expand All @@ -223,8 +224,17 @@ def update_user_profile(user_id: int, data: Dict[str, str]):

user.username = username

if "name" in data and data["name"]:
user.name = data["name"]
if "name" in data:
if data["name"]:
user.name = data["name"]
else:
return messages.NAME_FIELD_IS_MISSING,HTTPStatus.OK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rewrite this as:

try:
    user.name = data["name"]
except KeyError:
    return ....


if "username" in data:
if data["username"]:
user.username = data["username"]
else:
return messages.USERNAME_FIELD_IS_MISSING,HTTPStatus.OK

if "bio" in data:
if data["bio"]:
Expand Down Expand Up @@ -291,7 +301,7 @@ def update_user_profile(user_id: int, data: Dict[str, str]):

if "available_to_mentor" in data:
user.available_to_mentor = data["available_to_mentor"]

user.save_to_db()

return messages.USER_SUCCESSFULLY_UPDATED, HTTPStatus.OK
Expand Down
1 change: 0 additions & 1 deletion app/api/validations/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ def validate_update_profile_request_data(data):

if "available_to_mentor" in data and data["available_to_mentor"] is None:
return messages.FIELD_AVAILABLE_TO_MENTOR_IS_INVALID

return {}


Expand Down
1 change: 1 addition & 0 deletions app/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
DESCRIPTION_FIELD_IS_MISSING = {"message": "Description field is missing."}
COMMENT_FIELD_IS_MISSING = {"message": "Comment field is missing."}
USERNAME_FIELD_IS_EMPTY = {"message": f"The username field has to be longer than {USERNAME_MIN_LENGTH} characters and shorter than {USERNAME_MAX_LENGTH} characters."}
USER_AND_USERNAME_FIELD_IS_MISSING ={"message":"The name and username fields are missing."}

# Admin
USER_IS_ALREADY_AN_ADMIN = {"message": "User is already an Admin."}
Expand Down
57 changes: 57 additions & 0 deletions tests/users/test_api_update_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,63 @@ def test_update_username_not_taken(self):
self.assertDictEqual(expected_response, json.loads(actual_response.data))
self.assertEqual(user1_new_username, self.first_user.username)

def test_update_missing_username(self):

self.first_user = UserModel(
name=user1["name"],
email=user1["email"],
username=user1["username"],
password=user1["password"],
terms_and_conditions_checked=user1["terms_and_conditions_checked"],
)
self.first_user.is_email_verified = True

db.session.add(self.first_user)
db.session.commit()

user1_new_username = ""
auth_header = get_test_request_header(self.first_user.id)
expected_response = messages.USERNAME_FIELD_IS_MISSING
actual_response = self.client.put(
"/user",
follow_redirects=True,
headers=auth_header,
data=json.dumps(dict(username=user1_new_username)),
content_type="application/json",
)

self.assertEqual(200, actual_response.status_code)
self.assertDictEqual(expected_response, json.loads(actual_response.data))

def test_update_missing_name(self):

self.first_user = UserModel(
name=user1["name"],
email=user1["email"],
username=user1["username"],
password=user1["password"],
terms_and_conditions_checked=user1["terms_and_conditions_checked"],
)
self.first_user.is_email_verified = True

db.session.add(self.first_user)
db.session.commit()

user1_new_username = "new_username"
user1_new_name = ""
auth_header = get_test_request_header(self.first_user.id)
expected_response = messages.NAME_FIELD_IS_MISSING
actual_response = self.client.put(
"/user",
follow_redirects=True,
headers=auth_header,
data=json.dumps(dict(name=user1_new_name)),
content_type="application/json",
)

self.assertEqual(200, actual_response.status_code)
self.assertDictEqual(expected_response, json.loads(actual_response.data))

def test_update_username_invalid_length(self):

self.first_user = UserModel(
Expand Down