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

Solved Issue #596 of updating user with current username #1159

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ def update_user_profile(user_id: int, data: Dict[str, str]):

# username should be unique
if user_with_same_username:
# If requested update username is current username of user, OK status, nothing updated
if user.username == username:
return (
messages.CURRENT_USERNAME_NO_FIELDS_UPDATED,
HTTPStatus.OK,
)
# Otherwise, error: cannot change username to existing one
return (
messages.USER_USES_A_USERNAME_THAT_ALREADY_EXISTS,
HTTPStatus.BAD_REQUEST,
Expand Down
3 changes: 3 additions & 0 deletions app/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@
USER_IS_NOT_REGISTERED_IN_THE_SYSTEM = {
"message": "You are not registered in the system."
}
CURRENT_USERNAME_NO_FIELDS_UPDATED = {
"message": "Updated username is the same as current username. No fields have been updated."
}
NAME_LENGTH_GREATER_THAN_MAX_LIMIT = {
"message": "The {field_name} field has"
" to be shorter than {max_limit}"
Expand Down
23 changes: 23 additions & 0 deletions tests/users/test_api_update_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,28 @@ def test_change_password_to_current_password(self):
self.assertDictEqual(expected_response, json.loads(actual_response.data))


def test_update_same_username(self):
self.first_user = UserModel(**user1)
self.first_user.is_email_verified = True

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

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

self.assertEqual(HTTPStatus.OK, actual_response.status_code)
self.assertDictEqual(expected_response, json.loads(actual_response.data))
self.assertEqual(new_username, self.first_user.username)


if __name__ == "__main__":
unittest.main()