From 98fe91169c9b9f76260f366d76d6eb925b3762c7 Mon Sep 17 00:00:00 2001 From: Asad Ali Date: Mon, 27 May 2024 19:13:47 +0500 Subject: [PATCH] fix: invalid certificate uuid should raise 404 (#2990) * fix: invalid certificate uuid should raise 404 * fix: fix length certificate uuid regex match * test: rewrite tests --- cms/models.py | 4 ++-- cms/models_test.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cms/models.py b/cms/models.py index 45f758241..fd7938d4c 100644 --- a/cms/models.py +++ b/cms/models.py @@ -639,7 +639,7 @@ def can_create_at(cls, parent): and not parent.get_children().type(cls).exists() ) - @route(r"^program/([A-Fa-f0-9-]+)/?$") + @route(r"^program/([A-Fa-f0-9-]{36})/?$") def program_certificate( self, request, @@ -677,7 +677,7 @@ def program_certificate( certificate_page.certificate = certificate return certificate_page.serve(request) - @route(r"^([A-Fa-f0-9-]+)/?$") + @route(r"^([A-Fa-f0-9-]{36})/?$") def course_certificate( self, request, diff --git a/cms/models_test.py b/cms/models_test.py index 5892462af..6dd896216 100644 --- a/cms/models_test.py +++ b/cms/models_test.py @@ -64,7 +64,12 @@ UserTestimonialsPage, WhoShouldEnrollPage, ) -from courses.factories import CourseFactory, CourseRunFactory +from courses.factories import ( + CourseFactory, + CourseRunCertificateFactory, + CourseRunFactory, + ProgramCertificateFactory, +) pytestmark = [pytest.mark.django_db] @@ -1785,3 +1790,32 @@ def test_program_page_price_is_updated(superuser_client): resp = superuser_client.post(path, data_to_post) assert resp.status_code == 302 assert program.current_price == 999 + + +def test_certificate_request_with_valid_uuid(user_client): + """Test that certificate request is successful for course and program certificates.""" + course_run_certificate = CourseRunCertificateFactory.create() + resp = user_client.get(f"/certificate/{course_run_certificate.uuid}/") + assert resp.status_code == 200 + + program_certificate = ProgramCertificateFactory.create() + resp = user_client.get(f"/certificate/program/{program_certificate.uuid}/") + assert resp.status_code == 200 + + +@pytest.mark.parametrize( + "uuid_string", + [ + "", + "1bebd843-ebf0-40c0-850e", + "1bebd843-ebf0-40c0-850e-fe73baa31b944444", + "1bebd843-ebf0-40c0-850e-fe73baa31b94-4ab4", + ], +) +def test_certificate_request_with_invalid_uuid(user_client, uuid_string): + """Test that course run and program certificate request returns a 404 for invalid uuids.""" + program_certificate_resp = user_client.get(f"/certificate/program/{uuid_string}/") + assert program_certificate_resp.status_code == 404 + + course_certificate_resp = user_client.get(f"/certificate/{uuid_string}/") + assert course_certificate_resp.status_code == 404