|
| 1 | +import uuid |
| 2 | +from datetime import datetime, timedelta |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import ddt |
| 6 | +import pytest |
| 7 | +import pytz |
| 8 | +from rest_framework import status |
| 9 | + |
| 10 | +from enterprise_catalog.apps.api.base.tests.enterprise_catalog_views import ( |
| 11 | + BaseEnterpriseCatalogViewSetTests, |
| 12 | +) |
| 13 | +from enterprise_catalog.apps.catalog.constants import ( |
| 14 | + COURSE, |
| 15 | + COURSE_RUN, |
| 16 | + RESTRICTED_RUNS_ALLOWED_KEY, |
| 17 | +) |
| 18 | +from enterprise_catalog.apps.catalog.tests.factories import ( |
| 19 | + ContentMetadataFactory, |
| 20 | + EnterpriseCatalogFactory, |
| 21 | + RestrictedCourseMetadataFactory, |
| 22 | + RestrictedRunAllowedForRestrictedCourseFactory, |
| 23 | +) |
| 24 | +from enterprise_catalog.apps.catalog.utils import localized_utcnow |
| 25 | + |
| 26 | + |
| 27 | +@ddt.ddt |
| 28 | +class EnterpriseCatalogContainsContentItemsTests(BaseEnterpriseCatalogViewSetTests): |
| 29 | + """ |
| 30 | + Tests for the EnterpriseCatalogViewSetV2, which is permissive of restricted course/run metadata. |
| 31 | + """ |
| 32 | + VERSION = 'v2' |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + super().setUp() |
| 36 | + |
| 37 | + self.customer_details_patcher = mock.patch( |
| 38 | + 'enterprise_catalog.apps.catalog.models.EnterpriseCustomerDetails' |
| 39 | + ) |
| 40 | + self.mock_customer_details = self.customer_details_patcher.start() |
| 41 | + self.NOW = localized_utcnow() |
| 42 | + self.mock_customer_details.return_value.last_modified_date = self.NOW |
| 43 | + |
| 44 | + self.addCleanup(self.customer_details_patcher.stop) |
| 45 | + |
| 46 | + def test_contains_content_items_unauthorized_non_catalog_learner(self): |
| 47 | + """ |
| 48 | + Verify the contains_content_items endpoint rejects users that are not catalog learners |
| 49 | + """ |
| 50 | + self.set_up_invalid_jwt_role() |
| 51 | + self.remove_role_assignments() |
| 52 | + url = self._get_contains_content_base_url() + '?course_run_ids=fakeX' |
| 53 | + response = self.client.get(url) |
| 54 | + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
| 55 | + |
| 56 | + def test_contains_content_items_unauthorized_incorrect_jwt_context(self): |
| 57 | + """ |
| 58 | + Verify the contains_content_items endpoint rejects users that are catalog learners |
| 59 | + with an incorrect JWT context (i.e., enterprise uuid) |
| 60 | + """ |
| 61 | + other_customer_catalog = EnterpriseCatalogFactory(enterprise_uuid=uuid.uuid4()) |
| 62 | + |
| 63 | + base_url = self._get_contains_content_base_url(other_customer_catalog.uuid) |
| 64 | + url = base_url + '?course_run_ids=fakeX' |
| 65 | + response = self.client.get(url) |
| 66 | + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
| 67 | + |
| 68 | + def test_contains_content_items_implicit_access(self): |
| 69 | + """ |
| 70 | + Verify the contains_content_items endpoint responds with 200 OK for |
| 71 | + user with implicit JWT access |
| 72 | + """ |
| 73 | + self.remove_role_assignments() |
| 74 | + url = self._get_contains_content_base_url() + '?program_uuids=fakeX' |
| 75 | + self.assert_correct_contains_response(url, False) |
| 76 | + |
| 77 | + def test_contains_content_items_no_params(self): |
| 78 | + """ |
| 79 | + Verify the contains_content_items endpoint errors if no parameters are provided |
| 80 | + """ |
| 81 | + response = self.client.get(self._get_contains_content_base_url()) |
| 82 | + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
| 83 | + |
| 84 | + def test_contains_content_items_not_in_catalogs(self): |
| 85 | + """ |
| 86 | + Verify the contains_content_items endpoint returns False if the content is not in any associated catalog |
| 87 | + """ |
| 88 | + self.add_metadata_to_catalog(self.enterprise_catalog, [ContentMetadataFactory()]) |
| 89 | + |
| 90 | + url = self._get_contains_content_base_url() + '?program_uuids=this-is-not-the-uuid-youre-looking-for' |
| 91 | + self.assert_correct_contains_response(url, False) |
| 92 | + |
| 93 | + def test_contains_content_items_in_catalogs(self): |
| 94 | + """ |
| 95 | + Verify the contains_content_items endpoint returns True if the content is in any associated catalog |
| 96 | + """ |
| 97 | + content_key = 'fake-key+101x' |
| 98 | + relevant_content = ContentMetadataFactory(content_key=content_key) |
| 99 | + self.add_metadata_to_catalog(self.enterprise_catalog, [relevant_content]) |
| 100 | + |
| 101 | + url = self._get_contains_content_base_url() + '?course_run_ids=' + content_key |
| 102 | + self.assert_correct_contains_response(url, True) |
| 103 | + |
| 104 | + def _create_restricted_course_and_run(self, catalog): |
| 105 | + """ |
| 106 | + Helper to setup restricted course and run. |
| 107 | + """ |
| 108 | + content_one = ContentMetadataFactory(content_key='org+key1', content_type=COURSE) |
| 109 | + restricted_course = RestrictedCourseMetadataFactory.create( |
| 110 | + content_key='org+key1', |
| 111 | + content_type=COURSE, |
| 112 | + unrestricted_parent=content_one, |
| 113 | + catalog_query=catalog.catalog_query, |
| 114 | + _json_metadata=content_one.json_metadata, |
| 115 | + ) |
| 116 | + restricted_run = ContentMetadataFactory.create( |
| 117 | + content_key='course-v1:org+key1+restrictedrun', |
| 118 | + parent_content_key=restricted_course.content_key, |
| 119 | + content_type=COURSE_RUN, |
| 120 | + ) |
| 121 | + restricted_course.restricted_run_allowed_for_restricted_course.set( |
| 122 | + [restricted_run], clear=True, |
| 123 | + ) |
| 124 | + catalog.catalog_query.content_filter[RESTRICTED_RUNS_ALLOWED_KEY] = { |
| 125 | + content_one.content_key: [restricted_run.content_key], |
| 126 | + } |
| 127 | + catalog.catalog_query.save() |
| 128 | + return content_one, restricted_course, restricted_run |
| 129 | + |
| 130 | + def test_contains_catalog_key_restricted_runs_allowed(self): |
| 131 | + """ |
| 132 | + Tests that a catalog is considered to contain a restricted run, |
| 133 | + and that a different catalog that does *not* allow the restricted run |
| 134 | + is not considered to contain it. |
| 135 | + """ |
| 136 | + other_catalog = EnterpriseCatalogFactory(enterprise_uuid=self.enterprise_uuid) |
| 137 | + content_one, _, restricted_run = self._create_restricted_course_and_run(other_catalog) |
| 138 | + |
| 139 | + self.add_metadata_to_catalog(self.enterprise_catalog, [content_one]) |
| 140 | + self.add_metadata_to_catalog(other_catalog, [content_one]) |
| 141 | + |
| 142 | + url = self._get_contains_content_base_url(other_catalog.uuid) + \ |
| 143 | + f'?course_run_ids={restricted_run.content_key}' |
| 144 | + |
| 145 | + response = self.client.get(url) |
| 146 | + response_payload = response.json() |
| 147 | + |
| 148 | + self.assertTrue(response_payload.get('contains_content_items')) |
| 149 | + |
| 150 | + # self.enterprise_catalog does not contain the restricted run. |
| 151 | + url = self._get_contains_content_base_url(self.enterprise_catalog) + \ |
| 152 | + f'?course_run_ids={restricted_run.content_key}' |
| 153 | + |
| 154 | + response = self.client.get(url) |
| 155 | + response_payload = response.json() |
| 156 | + |
| 157 | + self.assertFalse(response_payload.get('contains_content_items')) |
0 commit comments