Skip to content
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

fix: invalid certificate uuid should raise 404 #2990

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ def program_certificate(
certificate = ProgramCertificate.objects.get(uuid=uuid)
except ProgramCertificate.DoesNotExist:
raise Http404 # noqa: B904
except ValidationError:
raise Http404 # noqa: B904

# Get a CertificatePage to serve this request
certificate_page = (
Expand Down Expand Up @@ -693,6 +695,8 @@ def course_certificate(
certificate = CourseRunCertificate.objects.get(uuid=uuid)
except CourseRunCertificate.DoesNotExist:
raise Http404 # noqa: B904
except ValidationError:
raise Http404 # noqa: B904

# Get a CertificatePage to serve this request
certificate_page = (
Expand Down
29 changes: 28 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,25 @@ 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_course_run_certificate_get(user_client):
"""Test that course run certificate get is successful for a valid UUID and raises 404 for invalid UUID"""
certificate = CourseRunCertificateFactory.create()
resp = user_client.get(f"/certificate/{certificate.uuid}/")
assert resp.status_code == 200

invalid_uuid = str(certificate.uuid)[0:-12]
resp = user_client.get(f"/certificate/{invalid_uuid}/")
assert resp.status_code == 404


def test_program_certificate_get(user_client):
"""Test that program certificate get is successful for a valid UUID and raises 404 for invalid UUID"""
certificate = ProgramCertificateFactory.create()
resp = user_client.get(f"/certificate/program/{certificate.uuid}/")
assert resp.status_code == 200

invalid_uuid = str(certificate.uuid)[0:-12]
resp = user_client.get(f"/certificate/program/{invalid_uuid}/")
assert resp.status_code == 404
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: We can refactor this in this way:

  1. A test only to check the valid certificate for both course and program certificate
  2. A parametrized test to check invalid certificates for both course & program certificate

We should check on multiple invalid UUIDs. Ideally making these tests parameterized for invalid e.g.

  1. Short string
  2. Empty String
  3. String longer than the length of UUID

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There were no existing tests so I just tried to put in some basic ones quickly but sure, I will add some more as you suggested.

Loading