-
Notifications
You must be signed in to change notification settings - Fork 716
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
Consistent 400 Response for Invalid Input in Kolibri Public Content APIs #12818
Consistent 400 Response for Invalid Input in Kolibri Public Content APIs #12818
Conversation
Build Artifacts
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is totally valid from my point of view, pk being None return a 400 error code, while not finding a pk should return a 404 code.
This pr solves first case.
However, tests must be fixed to pass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests are not passing because pk is being provided, for this test to pass you either must set pk to none or change the code to return 400 when pk is not an UUID (which current code is not doing)
self = <kolibri.core.content.test.test_content_app.ContentNodeAPITestCase testMethod=test_contentnode_tree_bad_pk>
def test_contentnode_tree_bad_pk(self):
response = self.client.get(
reverse(
"kolibri:core:contentnode_tree-detail",
kwargs={"pk": "this is not a UUID"},
)
)
> self.assertEqual(response.status_code, 400)
E AssertionError: 404 != 400
I tried with pk as None but it gives me 404 error def test_import_metadata_bad_pk(self):
response = self.client.get(
reverse(
"kolibri:core:importmetadata-detail",
kwargs={"pk": None},
)
)
> self.assertEqual(response.status_code, 400)
E AssertionError: 404 != 400 kolibri/core/content/test/test_public_api.py:105: AssertionError
----------------------------------------------------------------------------- Captured stderr setup -----------------------------------------------------------------------------
ERROR 2024-11-13 01:19:35,987 No tree cache found for 3 levels and 5 children per level
------------------------------------------------------------------------------ Captured log setup -------------------------------------------------------------------------------
ERROR kolibri.core.content.test.test_channel_upgrade:test_channel_upgrade.py:110 No tree cache found for 3 levels and 5 children per level
----------------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------------
INFO 2024-11-13 01:19:36,823 127.0.0.1 - - "GET /api/public/v2/importmetadata/None/" 404 0 "" "unknown"
WARNING 2024-11-13 01:19:36,824 Not Found: /api/public/v2/importmetadata/None/
------------------------------------------------------------------------------- Captured log call -------------------------------------------------------------------------------
WARNING django.request:log.py:224 Not Found: /api/public/v2/importmetadata/None/
=============================================================================== warnings summary ================================================================================ but changing into the retrieve function as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks all and @manzil-infinity180 welcome and thanks for working on what seems to be your first contribution to Kolibri! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little more clarity on some of the thoughts mentioned earlier. Feel free to ask any more questions to clarify!
kolibri/core/content/api.py
Outdated
@@ -861,13 +860,8 @@ class ContentNodeViewset(InternalContentNodeMixin, RemoteMixin, ReadOnlyValuesVi | |||
pagination_class = OptionalContentNodePagination | |||
|
|||
def retrieve(self, request, pk=None): | |||
|
|||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @manzil-infinity180! We still want this functionality of checking if the pk is a valid UUID or not. We want both of the checks that if the UUID is invalid then we throw a 400 and if it is None then we would raise 404. so both the checks should simultaneously exist.
@ozer550 Hello, i implemented as you intended above def test_import_metadata_pk(self):
response = self.client.get(
reverse(
"kolibri:core:importmetadata-detail",
kwargs={"pk": "20f5484b88ae49b08af03a389b4917dd"},
)
)
self.assertEqual(response.status_code, 404) |
So the current tests that you have implemented are failing? |
Yeah only two test passing, for when pk is none (Content node Viewset) and when pk is invalid (ImportMetadataViewset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of corrections from myside over things that I overlooked! we are almost there.
@ozer550 done , review it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @manzil-infinity180! I know we iterated quite a lot of times on this but we both learnt new things. Congratulations on your first contribution to kolibri. Looking forward towards more!
@jredrejo was all your feedback addressed and do you feel good about merging? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. This ensures that if the PK is not available then the response must raise a 404(not found) as per requirements and if the pk is not a valid UUID then it should raise a ValueError 400(bad request). The tests also ensure the validity of the response perfectly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Summary
This pr is intended to be consistent for 400 response for retrieve function of ContentNodeTreeViewset and ImportMetadataViewset invalid input by returning a 404 response.
References
Closes: #12807
Reviewer guidance
…
Testing checklist
PR process
Reviewer checklist
yarn
andpip
)