Skip to content

Conversation

saltie2193
Copy link
Contributor

Fix an issue where read_user_by_id would fail to return if the requested user ID did not exist.

  • Return 404 - Not Found when ID does not exist.
  • Request without sufficient permission will always result in 403 - Unauthorized.
  • Add tests to test requesting non-existing user IDs as superuser and normal user.

@alejsdev alejsdev changed the title 👷 Handle non-existing user IDs in read_user_by_id. 🐛 Handle non-existing user IDs in read_user_by_id. Oct 24, 2024
@alejsdev alejsdev added the bug Something isn't working label Oct 24, 2024
@alejsdev alejsdev changed the title 🐛 Handle non-existing user IDs in read_user_by_id. 🐛 Handle non-existing user IDs in read_user_by_id Oct 24, 2024
@berar
Copy link

berar commented Nov 7, 2024

Hello. I'd like to know why this pull request has not been approved. It is valid.

@jonbzt
Copy link

jonbzt commented Dec 28, 2024

Yeah pretty minor upgrade but it makes sens to merge IMO.

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

@saltie2193, thanks for working on this!

Please, take a look at my change suggestions.
Basically, I think we should leave existing tests untouched (only rename test_get_existing_user -> test_get_existing_user_as_superuser) and add 2 test for non-existing user (superuser - 404, regular user - 403)

Comment on lines 91 to 93
@pytest.mark.parametrize(
"is_superuser", (True, False), ids=lambda x: "superuser" if x else "normal user"
)
Copy link
Member

Choose a reason for hiding this comment

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

Case with is_superuser=True is already covered by test_get_existing_user_as_superuser.
No need to parameterize this test

Copy link
Contributor Author

@saltie2193 saltie2193 Sep 5, 2025

Choose a reason for hiding this comment

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

It should not be the same as in test_get_existing_user_as_superuser.

test_get_existing_user_as_superuser tests whether a user authenticated as superuser is able to access another user. (User A tries to access data of user B)

test_get_exiting_user_current_user however tests whether a user is allowed to access his own data, independent of them being a superuser or not. (User A tries to access data of user A)

That's at least what they are intended to test.

Copy link
Member

Choose a reason for hiding this comment

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

So, this is to ensure that superuser can access their own user information, not only other user's information, right?
I still think this is redundant. I believe the chance somebody writes the algorithm this way is extremely low..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It's to ensure superuser privileges don't influence it.
Sure might be excessive but it's just an additional simple case of the test, ensuring the correct function of the backend, and not even expensive.

Copy link
Member

Choose a reason for hiding this comment

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

I think I can come up with 10 more additional tests. And all of them will be hypothetically possible.
I think we should consider:

  • what is the probability that somebody writes this function this way (superuser can access random user, but can't access their own info. At the same time normal user can access their own info)?
  • every change is additional load for people who will review it and read this code later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean especially when talking about authentication and authorization it do think it's not unlikely to not think about something like this, that seems really easy ans stupid. Especially when looking at differences between superusers and non superuser. Often the most simple parts are overlooked because people thought it's obvious how it's supposed to work and nobody would ever make a mistake like that.
This just uses as simple test case in a preventive way ensuring the backend is working as intended and at the same time explicitly documents the intended behavior.

But I do understand and have to respect it if this project does not want to use test cases in this way.

So we only test if the current user (authenticated as non-superuser) is able to access his data and I drop the changes to test_get_existing_user_current_user?

Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep test_get_existing_user_current_user as it was before this PR

Fix an issue where `read_user_by_id` would fail to return if the requested user ID did not exist.
* Return `404 - Not Found` when ID does not exist.
* Request without sufficient permission will always result in `403 - Unauthorized`.
* Add tests to test requesting non-existing user IDs as superuser and normal user.
@saltie2193 saltie2193 force-pushed the backend-read-user-by-id-no-user branch from a2dd74f to e031948 Compare September 5, 2025 18:27
@saltie2193
Copy link
Contributor Author

@YuriiMotov please have a look at the adjustments I made. I hope I could address all your concerns.

Note, I rebased the original commits onto the master branch so test_get_existing_user_current_user does not show as code contributed by me, since I did not change it.

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

LGTM

I added a few suggestions that can reduce the diff a bit. Feel free to ignore

@saltie2193, thank you!

@saltie2193
Copy link
Contributor Author

From my side this is ready to merge.

@YuriiMotov, thank you for taking the time to review this pull request!

Copy link
Contributor

This pull request has a merge conflict that needs to be resolved.

@github-actions github-actions bot added conflicts Automatically generated when a PR has a merge conflict and removed conflicts Automatically generated when a PR has a merge conflict labels Sep 20, 2025
# Conflicts:
#	backend/tests/api/routes/test_users.py
@saltie2193 saltie2193 force-pushed the backend-read-user-by-id-no-user branch from 796afd5 to 568f0da Compare September 24, 2025 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants