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: support numeric usernames on get_user method #372

Merged
merged 2 commits into from
Nov 28, 2023

Conversation

andrey-canon
Copy link
Contributor

Description:
When a user sets their username as a numeric value, for example the local id (1022547854), the current code handles this as the user id therefore the right user is not found and that returns None, This changes add support for that specific case and includes some basic unit tests which validates the basic behavior

Testing instructions:

  1. Create a user with an username like "1100865375"
  2. Set a problem, example multiple choices
  3. Attempt previous problem
  4. check logs, the message User with username "1100865375" does not exist. shouldn't appear

or directly call the method in a shell

Merge checklist:

  • All reviewers approved
  • CI build is green
  • Version bumped
  • Changelog record added
  • Documentation updated (not only docstrings)
  • Commits are squashed

Post merge:

  • Create a tag
  • Check new version is pushed to PyPI after tag-triggered build is
    finished.

Author concerns: List any concerns about this PR - inelegant
solutions, hacks, quick-and-dirty implementations, concerns about
migrations, etc.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Nov 21, 2023
@openedx-webhooks
Copy link

Thanks for the pull request, @andrey-canon! Please note that it may take us up to several weeks or months to complete a review and merge your PR.

Feel free to add as much of the following information to the ticket as you can:

  • supporting documentation
  • Open edX discussion forum threads
  • timeline information ("this must be merged by XX date", and why that is)
  • partner information ("this is a course on edx.org")
  • any other information that can help Product understand the context for the PR

All technical communication about the code itself will be done via the GitHub pull request interface. As a reminder, our process documentation is here.

Please let us know once your PR is ready for our review and all tests are green.


if username:
try:
user = User.objects.get(id=int(username_or_id))
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the username is equal to a user ID in the database?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the first thing, that current implementation makes, is to try to set the user_id here and then look for the user with that id , so if there is a user with username=5 and another user with id=5, this will return the user with id=5, and this basically keeps that logic. The modification implemented here considers the case when there is no a user with id=5 instead of returning None this will return the user with username =5.

Finally my comment is that there is not an easy way to identify the right case, the method uses username_or_id parameter but that is too ambiguous and the method can not identify that by itself that needs some criteria like first id and then username or limit the method scope to username or id, not both, but that requires to standard the events to use just one of them.

Copy link
Contributor

Choose a reason for hiding this comment

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

What if we verify if the user_id is already an integer (all events send this param as int) then we query by ID, in another case query by username?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's the same with an extra step

Comment on lines +91 to +111

@data("edx", "10228945687")
def test_get_user_by_username(self, username):
"""Test that the method get_user returns the right user based on given username parameter.

Expected behavior:
- Returned user corresponds to the username.
"""
user = get_user(username)

self.assertEqual(username, user.username)

def test_get_user_by_id(self):
""" Test that the method get_user returns the right user based on the user id.

Expected behavior:
- Returned user is the edx_user
"""
user = get_user(self.edx_user.id)

self.assertEqual(self.edx_user, user)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a new test with two users, the first user with a hardcoded ID and the second one with the same ID as the username, verify that getting the user by the user_id as str returns the second user

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@Ian2012
Copy link
Contributor

Ian2012 commented Nov 24, 2023

@bmtcril @ziafazal would you like to review this one too?

Copy link
Contributor

@ziafazal ziafazal left a comment

Choose a reason for hiding this comment

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

👍

@Ian2012
Copy link
Contributor

Ian2012 commented Nov 27, 2023

@andrey-canon Can you add a changelog entry and bump version?

@andrey-canon andrey-canon force-pushed the and/support_numeric_usernames branch from fd688f9 to 2e7756c Compare November 27, 2023 22:24
@Ian2012 Ian2012 merged commit 2013ee0 into openedx:master Nov 28, 2023
9 checks passed
@openedx-webhooks
Copy link

@andrey-canon 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants