Skip to content

Commit

Permalink
🐛(backend) api status not found messages
Browse files Browse the repository at this point in the history
DRF verson 3.15 adds more descriptive not found messages.
encode/django-rest-framework#8051
  • Loading branch information
kernicPanel committed Mar 18, 2024
1 parent 2136e4b commit bfee0d1
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/backend/joanie/tests/core/api/order/test_read_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,6 @@ def test_api_order_read_detail_authenticated_not_owner(self):
)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

self.assertDictEqual(response.json(), {"detail": "Not found."})
self.assertDictEqual(
response.json(), {"detail": "No Order matches the given query."}
)
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_api_order_submit_for_signature_user_is_not_owner_of_the_order_to_be_sub
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

content = response.json()
self.assertEqual(content["detail"], "Not found.")
self.assertEqual(content["detail"], "No Order matches the given query.")

def test_api_order_submit_for_signature_authenticated_but_order_is_not_validate(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ def test_api_organizations_contracts_retrieve_with_accesses_and_canceled_order(
HTTP_AUTHORIZATION=f"Bearer {token}",
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Contract matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_api_organizations_contracts_retrieve_with_accesses_and_organization_code(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def test_api_organization_retrieve_authenticated_no_access(self):
)

self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
self.assertEqual(response.json(), {"detail": "Not found."})
self.assertEqual(
response.json(), {"detail": "No Organization matches the given query."}
)

@mock.patch.object(
fields.ThumbnailDetailField,
Expand Down
12 changes: 10 additions & 2 deletions src/backend/joanie/tests/core/test_api_admin_course_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ def test_admin_api_course_accesses_request_create_with_unknown_course_id(self):
},
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Course matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_admin_api_course_accesses_request_create_with_invalid_role(self):
"""
Expand Down Expand Up @@ -231,7 +235,11 @@ def test_admin_api_course_accesses_request_update_with_unknown_course_id(self):
},
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No CourseAccess matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_admin_api_course_accesses_request_update_with_partial_payload(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ def test_admin_api_organization_accesses_request_create_with_unknown_course_id(
},
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Organization matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_admin_api_organization_accesses_request_create_with_invalid_role(self):
"""
Expand Down Expand Up @@ -243,7 +247,11 @@ def test_admin_api_organization_accesses_request_update_with_unknown_course_id(
},
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No OrganizationAccess matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_admin_api_organization_accesses_request_update_with_partial_payload(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ def test_admin_api_organization_addresses_request_update_with_unknown_address_id
},
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Address matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_admin_api_organization_addresses_request_update_with_partial_payload(self):
"""
Expand All @@ -255,7 +259,10 @@ def test_admin_api_organization_addresses_request_update_with_partial_payload(se
)

self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
self.assertEqual(response.json(), {"detail": "Not found."})
self.assertEqual(
response.json(),
{"detail": "No OrganizationAccess matches the given query."},
)

def test_admin_api_organization_addresses_request_update_with_fake_organization_id(
self,
Expand Down
8 changes: 6 additions & 2 deletions src/backend/joanie/tests/core/test_api_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ def test_api_certificate_read_authenticated_from_an_order(self, _mock_thumbnail)
)

self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
self.assertDictEqual(response.json(), {"detail": "Not found."})
self.assertDictEqual(
response.json(), {"detail": "No Certificate matches the given query."}
)

# - Try to retrieve an owned certificate should return the certificate id
response = self.client.get(
Expand Down Expand Up @@ -535,7 +537,9 @@ def test_api_certificate_read_authenticated_from_an_enrollment(
)

self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
self.assertDictEqual(response.json(), {"detail": "Not found."})
self.assertDictEqual(
response.json(), {"detail": "No Certificate matches the given query."}
)

# - Try to retrieve an owned certificate should return the certificate id
response = self.client.get(
Expand Down
18 changes: 15 additions & 3 deletions src/backend/joanie/tests/core/test_api_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,11 @@ def test_api_contracts_retrieve_with_owner_and_canceled_order(self):
HTTP_AUTHORIZATION=f"Bearer {token}",
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Contract matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_api_contracts_create_anonymous(self):
"""Anonymous user cannot create a contract."""
Expand Down Expand Up @@ -1015,7 +1019,11 @@ def test_api_contract_download_authenticated_with_not_validate_order(self):
HTTP_AUTHORIZATION=f"Bearer {token}",
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Contract matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_api_contract_download_authenticated_cannot_create(self):
"""
Expand Down Expand Up @@ -1128,7 +1136,11 @@ def test_api_contract_download_authenticated_should_fail_if_owner_is_not_the_act
HTTP_AUTHORIZATION=f"Bearer {token}",
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Contract matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_api_contract_download_authenticated_should_fail_if_contract_is_not_signed(
self,
Expand Down
4 changes: 3 additions & 1 deletion src/backend/joanie/tests/core/test_api_course.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ def test_api_course_get_authenticated_no_access(self):
)

self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)
self.assertDictEqual(response.json(), {"detail": "Not found."})
self.assertDictEqual(
response.json(), {"detail": "No Course matches the given query."}
)

@mock.patch.object(
fields.ThumbnailDetailField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,9 @@ def test_api_course_product_relation_read_detail_no_organization(self):
response = self.client.get(
f"/api/v1.0/courses/{relation.course.id}/products/{relation.product.id}/",
)

self.assertContains(
response,
"Not found.",
"No CourseProductRelation matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

Expand All @@ -565,7 +564,7 @@ def test_api_course_product_relation_read_detail_no_organization(self):

self.assertContains(
response,
"Not found.",
"No CourseProductRelation matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

Expand All @@ -576,7 +575,7 @@ def test_api_course_product_relation_read_detail_no_organization(self):

self.assertContains(
response,
"Not found.",
"No CourseProductRelation matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

Expand Down
4 changes: 2 additions & 2 deletions src/backend/joanie/tests/core/test_api_course_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_api_course_run_read_detail_not_listed_anonymous(self):

self.assertContains(
response,
"Not found.",
"No CourseRun matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

Expand All @@ -239,7 +239,7 @@ def test_api_course_run_read_detail_not_listed_authenticated(self):

self.assertContains(
response,
"Not found.",
"No CourseRun matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/tests/core/test_api_course_wishes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_api_course_wish_unknown_course(self):

self.assertContains(
response,
"Not found.",
"No Course matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

Expand Down
6 changes: 5 additions & 1 deletion src/backend/joanie/tests/core/test_api_courses_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,11 @@ def test_api_courses_contracts_retrieve_with_accesses_and_canceled_order(self):
HTTP_AUTHORIZATION=f"Bearer {token}",
)

self.assertContains(response, "Not found.", status_code=HTTPStatus.NOT_FOUND)
self.assertContains(
response,
"No Contract matches the given query.",
status_code=HTTPStatus.NOT_FOUND,
)

def test_api_courses_contracts_retrieve_with_accesses_and_course_code(self):
"""
Expand Down
8 changes: 6 additions & 2 deletions src/backend/joanie/tests/core/test_api_enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,9 @@ def test_api_enrollment_read_detail_authenticated_not_owner(self, _mock_set):
)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

self.assertDictEqual(response.json(), {"detail": "Not found."})
self.assertDictEqual(
response.json(), {"detail": "No Enrollment matches the given query."}
)

def test_api_enrollment_create_anonymous(self):
"""Anonymous users should not be able to create an enrollment."""
Expand Down Expand Up @@ -1581,7 +1583,9 @@ def test_api_enrollment_update_detail_state_not_owner(self, _mock_set):
)
self.assertEqual(response.status_code, HTTPStatus.NOT_FOUND)

self.assertDictEqual(response.json(), {"detail": "Not found."})
self.assertDictEqual(
response.json(), {"detail": "No Enrollment matches the given query."}
)

@mock.patch.object(OpenEdXLMSBackend, "set_enrollment", return_value=True)
@mock.patch.object(
Expand Down

0 comments on commit bfee0d1

Please sign in to comment.