|
16 | 16 | from django.urls import reverse |
17 | 17 | from edx_django_utils.cache import RequestCache |
18 | 18 | from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator, LibraryCollectionLocator, LibraryContainerLocator |
19 | | -from openedx_authz.constants.roles import COURSE_STAFF |
| 19 | +from openedx_authz.constants.roles import COURSE_ADMIN, COURSE_STAFF |
20 | 20 | from openedx_tagging.models import Tag, Taxonomy |
21 | 21 | from openedx_tagging.models.system_defined import SystemDefinedTaxonomy |
22 | 22 | from openedx_tagging.rest_api.v1.serializers import TaxonomySerializer |
@@ -2106,6 +2106,166 @@ def test_superuser_allowed(self): |
2106 | 2106 | resp = client.get(self.get_url(self.course_key)) |
2107 | 2107 | self.assertEqual(resp.status_code, status.HTTP_200_OK) |
2108 | 2108 |
|
| 2109 | +@skip_unless_cms |
| 2110 | +class TestTaxonomyEndpointsWithAuthz(CourseAuthzTestMixin, SharedModuleStoreTestCase, APITestCase): |
| 2111 | + """ |
| 2112 | + Tests taxonomy management endpoints with openedx-authz. |
| 2113 | +
|
| 2114 | + When the AUTHZ_COURSE_AUTHORING_FLAG is globally enabled, taxonomy CRUD |
| 2115 | + endpoints should enforce courses.manage_taxonomies / courses.manage_tags |
| 2116 | + permissions via openedx-authz. |
| 2117 | +
|
| 2118 | + Uses COURSE_ADMIN role because only course_admin has manage_taxonomies permission. |
| 2119 | + """ |
| 2120 | + |
| 2121 | + authz_roles_to_assign = [COURSE_ADMIN.external_key] |
| 2122 | + |
| 2123 | + @classmethod |
| 2124 | + def setUpClass(cls): |
| 2125 | + super().setUpClass() |
| 2126 | + cls.password = 'test' |
| 2127 | + cls.course = CourseFactory.create() |
| 2128 | + cls.course_key = cls.course.id |
| 2129 | + cls.staff = StaffFactory(course_key=cls.course_key, password=cls.password) |
| 2130 | + |
| 2131 | + def setUp(self): |
| 2132 | + super().setUp() |
| 2133 | + self.orgA = Organization.objects.create(name="Organization A", short_name="orgA") |
| 2134 | + self.taxonomy = tagging_api.create_taxonomy(name="Test Taxonomy") |
| 2135 | + tagging_api.set_taxonomy_orgs(self.taxonomy, all_orgs=False, orgs=[self.orgA]) |
| 2136 | + |
| 2137 | + def test_delete_taxonomy_authorized(self): |
| 2138 | + """Authorized user can delete a taxonomy.""" |
| 2139 | + url = TAXONOMY_ORG_DETAIL_URL.format(pk=self.taxonomy.id) |
| 2140 | + resp = self.authorized_client.delete(url) |
| 2141 | + self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) |
| 2142 | + |
| 2143 | + def test_delete_taxonomy_unauthorized(self): |
| 2144 | + """Unauthorized user cannot delete a taxonomy.""" |
| 2145 | + url = TAXONOMY_ORG_DETAIL_URL.format(pk=self.taxonomy.id) |
| 2146 | + resp = self.unauthorized_client.delete(url) |
| 2147 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 2148 | + |
| 2149 | + def test_update_orgs_authorized(self): |
| 2150 | + """Authorized user can update taxonomy orgs.""" |
| 2151 | + url = TAXONOMY_ORG_UPDATE_ORG_URL.format(pk=self.taxonomy.id) |
| 2152 | + resp = self.authorized_client.put(url, {"all_orgs": True}, format="json") |
| 2153 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 2154 | + |
| 2155 | + def test_update_orgs_unauthorized(self): |
| 2156 | + """Unauthorized user cannot update taxonomy orgs.""" |
| 2157 | + url = TAXONOMY_ORG_UPDATE_ORG_URL.format(pk=self.taxonomy.id) |
| 2158 | + resp = self.unauthorized_client.put(url, {"all_orgs": True}, format="json") |
| 2159 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 2160 | + |
| 2161 | + def test_create_import_authorized(self): |
| 2162 | + """Authorized user can create a taxonomy via import.""" |
| 2163 | + url = TAXONOMY_CREATE_IMPORT_URL |
| 2164 | + file = SimpleUploadedFile( |
| 2165 | + "taxonomy.csv", |
| 2166 | + b"id,value\ntag_1,Tag 1\n", |
| 2167 | + content_type="text/csv", |
| 2168 | + ) |
| 2169 | + resp = self.authorized_client.post( |
| 2170 | + url, |
| 2171 | + {"taxonomy_name": "Imported", "taxonomy_description": "", "file": file}, |
| 2172 | + format="multipart", |
| 2173 | + ) |
| 2174 | + self.assertEqual(resp.status_code, status.HTTP_201_CREATED) |
| 2175 | + |
| 2176 | + def test_create_import_unauthorized(self): |
| 2177 | + """Unauthorized user cannot create a taxonomy via import.""" |
| 2178 | + url = TAXONOMY_CREATE_IMPORT_URL |
| 2179 | + file = SimpleUploadedFile( |
| 2180 | + "taxonomy.csv", |
| 2181 | + b"id,value\ntag_1,Tag 1\n", |
| 2182 | + content_type="text/csv", |
| 2183 | + ) |
| 2184 | + resp = self.unauthorized_client.post( |
| 2185 | + url, |
| 2186 | + {"taxonomy_name": "Imported", "taxonomy_description": "", "file": file}, |
| 2187 | + format="multipart", |
| 2188 | + ) |
| 2189 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 2190 | + |
| 2191 | + def test_taxonomy_list_permission_fields_authorized(self): |
| 2192 | + """Authorized user sees correct permission fields in taxonomy list.""" |
| 2193 | + url = TAXONOMY_ORG_LIST_URL |
| 2194 | + resp = self.authorized_client.get(url) |
| 2195 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 2196 | + for taxonomy in resp.data["results"]: |
| 2197 | + self.assertTrue(taxonomy["can_change_taxonomy"]) |
| 2198 | + self.assertTrue(taxonomy["can_delete_taxonomy"]) |
| 2199 | + self.assertTrue(taxonomy["can_tag_object"]) |
| 2200 | + |
| 2201 | + def test_taxonomy_list_permission_fields_unauthorized(self): |
| 2202 | + """Unauthorized user sees restricted permission fields in taxonomy list.""" |
| 2203 | + url = TAXONOMY_ORG_LIST_URL |
| 2204 | + resp = self.unauthorized_client.get(url) |
| 2205 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 2206 | + for taxonomy in resp.data["results"]: |
| 2207 | + self.assertFalse(taxonomy["can_change_taxonomy"]) |
| 2208 | + self.assertFalse(taxonomy["can_delete_taxonomy"]) |
| 2209 | + self.assertFalse(taxonomy["can_tag_object"]) |
| 2210 | + |
| 2211 | + |
| 2212 | +@skip_unless_cms |
| 2213 | +class TestObjectTagUpdateWithAuthz(CourseAuthzTestMixin, SharedModuleStoreTestCase, APITestCase): |
| 2214 | + """ |
| 2215 | + Tests object tag update endpoint with openedx-authz. |
| 2216 | +
|
| 2217 | + When the AUTHZ_COURSE_AUTHORING_FLAG is enabled for a course, |
| 2218 | + PUT /object_tags/{course_id}/ should enforce courses.manage_tags. |
| 2219 | + """ |
| 2220 | + |
| 2221 | + authz_roles_to_assign = [COURSE_STAFF.external_key] |
| 2222 | + |
| 2223 | + @classmethod |
| 2224 | + def setUpClass(cls): |
| 2225 | + super().setUpClass() |
| 2226 | + cls.password = 'test' |
| 2227 | + cls.course = CourseFactory.create() |
| 2228 | + cls.course_key = cls.course.id |
| 2229 | + cls.staff = StaffFactory(course_key=cls.course_key, password=cls.password) |
| 2230 | + |
| 2231 | + def setUp(self): |
| 2232 | + super().setUp() |
| 2233 | + self.taxonomy = tagging_api.create_taxonomy(name="Test Taxonomy") |
| 2234 | + tagging_api.set_taxonomy_orgs(self.taxonomy, all_orgs=True, orgs=[]) |
| 2235 | + Tag.objects.create(taxonomy=self.taxonomy, value="Tag 1") |
| 2236 | + |
| 2237 | + def test_update_object_tags_authorized(self): |
| 2238 | + """Authorized user can update object tags.""" |
| 2239 | + url = OBJECT_TAG_UPDATE_URL.format(object_id=self.course_key) |
| 2240 | + resp = self.authorized_client.put( |
| 2241 | + url, |
| 2242 | + {"tagsData": [{"taxonomy": self.taxonomy.id, "tags": ["Tag 1"]}]}, |
| 2243 | + format="json", |
| 2244 | + ) |
| 2245 | + self.assertEqual(resp.status_code, status.HTTP_200_OK) |
| 2246 | + |
| 2247 | + def test_update_object_tags_unauthorized(self): |
| 2248 | + """Unauthorized user cannot update object tags.""" |
| 2249 | + url = OBJECT_TAG_UPDATE_URL.format(object_id=self.course_key) |
| 2250 | + resp = self.unauthorized_client.put( |
| 2251 | + url, |
| 2252 | + {"tagsData": [{"taxonomy": self.taxonomy.id, "tags": ["Tag 1"]}]}, |
| 2253 | + format="json", |
| 2254 | + ) |
| 2255 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 2256 | + |
| 2257 | + def test_update_object_tags_scoped_to_course(self): |
| 2258 | + """Authorization should only apply to the assigned course.""" |
| 2259 | + other_course = self.store.create_course("OtherOrg", "OtherCourse", "Run", self.staff.id) |
| 2260 | + url = OBJECT_TAG_UPDATE_URL.format(object_id=other_course.id) |
| 2261 | + resp = self.authorized_client.put( |
| 2262 | + url, |
| 2263 | + {"tagsData": [{"taxonomy": self.taxonomy.id, "tags": ["Tag 1"]}]}, |
| 2264 | + format="json", |
| 2265 | + ) |
| 2266 | + self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) |
| 2267 | + |
| 2268 | + |
2109 | 2269 | @skip_unless_cms |
2110 | 2270 | @ddt.ddt |
2111 | 2271 | class TestDownloadTemplateView(APITestCase): |
|
0 commit comments