|
4 | 4 | from django.urls import reverse |
5 | 5 | from rest_framework import status |
6 | 6 |
|
| 7 | +from openedx_authz.constants.roles import COURSE_STAFF, COURSE_EDITOR |
| 8 | + |
7 | 9 | from cms.djangoapps.contentstore.tests.utils import CourseTestCase |
8 | 10 | from cms.djangoapps.contentstore.views.tests.test_certificates import HelperMethods |
| 11 | +from openedx.core.djangoapps.authz.tests.mixins import CourseAuthoringAuthzTestMixin |
9 | 12 |
|
10 | 13 | from ...mixins import PermissionAccessMixin |
11 | 14 |
|
@@ -36,3 +39,35 @@ def test_success_response(self): |
36 | 39 | self.assertEqual(response_data["course_number_override"], self.course.display_coursenumber) |
37 | 40 | self.assertEqual(response_data["course_title"], self.course.display_name_with_default) |
38 | 41 | self.assertEqual(response_data["course_number"], self.course.number) |
| 42 | + |
| 43 | + |
| 44 | +class CourseCertificatesAuthzViewTest( |
| 45 | + CourseAuthoringAuthzTestMixin, CourseTestCase, PermissionAccessMixin, HelperMethods |
| 46 | + ): |
| 47 | + """ |
| 48 | + Tests for CourseCertificatesView with AuthZ enabled. |
| 49 | + """ |
| 50 | + |
| 51 | + def setUp(self): |
| 52 | + super().setUp() |
| 53 | + self.url = reverse( |
| 54 | + "cms.djangoapps.contentstore:v1:certificates", |
| 55 | + kwargs={"course_id": self.course.id}, |
| 56 | + ) |
| 57 | + |
| 58 | + def test_authorized_user_can_access(self): |
| 59 | + """User with COURSE_STAFF role can access.""" |
| 60 | + self._add_course_certificates(count=2, signatory_count=2) |
| 61 | + self.add_user_to_role_in_course(self.authorized_user, COURSE_STAFF.external_key, self.course.id) |
| 62 | + resp = self.authorized_client.get(self.url) |
| 63 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 64 | + |
| 65 | + def test_non_staff_user_cannot_access(self): |
| 66 | + """ |
| 67 | + User without permissions should be denied. |
| 68 | + This case validates that a non-staff user cannot access. |
| 69 | + """ |
| 70 | + self._add_course_certificates(count=2, signatory_count=2) |
| 71 | + self.add_user_to_role_in_course(self.authorized_user, COURSE_EDITOR.external_key, self.course.id) |
| 72 | + resp = self.authorized_client.get(self.url) |
| 73 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
0 commit comments