Skip to content

Commit

Permalink
fix: invalid certificate uuid should raise 404 (#2990)
Browse files Browse the repository at this point in the history
* fix: invalid certificate uuid should raise 404

* fix: fix length certificate uuid regex match

* test: rewrite tests
  • Loading branch information
asadali145 committed May 27, 2024
1 parent 5a258b9 commit 98fe911
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 35 additions & 1 deletion cms/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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

0 comments on commit 98fe911

Please sign in to comment.