Skip to content

Commit 68d2080

Browse files
committed
squash!: Refactor test to not use contentstore imports
1 parent 276576e commit 68d2080

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

  • openedx/core/djangoapps/content_tagging/rest_api/v1/tests

openedx/core/djangoapps/content_tagging/rest_api/v1/tests/test_views.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ddt
1414
from django.contrib.auth import get_user_model
1515
from django.core.files.uploadedfile import SimpleUploadedFile
16+
from django.urls import reverse
1617
from edx_django_utils.cache import RequestCache
1718
from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator, LibraryCollectionLocator, LibraryContainerLocator
1819
from openedx_authz.constants.roles import COURSE_STAFF
@@ -24,7 +25,6 @@
2425
from rest_framework.test import APITestCase, APIClient
2526

2627

27-
from cms.djangoapps.contentstore.api.tests.base import BaseCourseViewTest
2828
from common.djangoapps.student.auth import add_users, update_org_role
2929
from common.djangoapps.student.roles import (
3030
CourseInstructorRole,
@@ -34,8 +34,10 @@
3434
OrgLibraryUserRole,
3535
OrgStaffRole
3636
)
37-
from common.djangoapps.student.tests.factories import UserFactory
37+
from common.djangoapps.student.tests.factories import StaffFactory, UserFactory
3838
from openedx.core.djangoapps.authz.tests.mixins import CourseAuthzTestMixin
39+
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
40+
from xmodule.modulestore.tests.factories import CourseFactory
3941
from openedx.core.djangoapps.content_libraries.api import AccessLevel, create_library, set_library_user_permissions
4042
from openedx.core.djangoapps.content_tagging import api as tagging_api
4143
from openedx.core.djangoapps.content_tagging.models import TaxonomyOrg
@@ -2056,51 +2058,52 @@ def test_export_course_invalid_id(self) -> None:
20562058
assert response.status_code == status.HTTP_403_FORBIDDEN
20572059

20582060
@skip_unless_cms
2059-
class TestContentObjectChildrenExportViewWithAuthz(CourseAuthzTestMixin, BaseCourseViewTest):
2061+
class TestContentObjectChildrenExportViewWithAuthz(CourseAuthzTestMixin, SharedModuleStoreTestCase, APITestCase):
20602062
"""
20612063
Tests Tags Export in Course authorization using openedx-authz.
20622064
"""
20632065

2064-
view_name = 'content_tagging:taxonomy-object-tag-export'
2065-
course_key_arg_name = 'context_id'
20662066
authz_roles_to_assign = [COURSE_STAFF.external_key]
20672067

2068+
@classmethod
2069+
def setUpClass(cls):
2070+
super().setUpClass()
2071+
cls.password = 'test'
2072+
cls.course = CourseFactory.create()
2073+
cls.course_key = cls.course.id
2074+
cls.staff = StaffFactory(course_key=cls.course_key, password=cls.password)
2075+
2076+
def get_url(self, course_key):
2077+
return reverse('content_tagging:taxonomy-object-tag-export', kwargs={'context_id': course_key})
2078+
20682079
def test_authorized_user_can_access(self):
20692080
"""User with COURSE_STAFF role can access."""
2070-
self.authorized_client.login(username=self.authorized_user.username, password=self.password)
20712081
resp = self.authorized_client.get(self.get_url(self.course_key))
20722082
self.assertEqual(resp.status_code, status.HTTP_200_OK)
20732083

20742084
def test_unauthorized_user_cannot_access(self):
20752085
"""User without role cannot access."""
2076-
self.unauthorized_client.login(username=self.unauthorized_user.username, password=self.password)
20772086
resp = self.unauthorized_client.get(self.get_url(self.course_key))
20782087
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
20792088

20802089
def test_role_scoped_to_course(self):
20812090
"""Authorization should only apply to the assigned course."""
20822091
other_course = self.store.create_course("OtherOrg", "OtherCourse", "Run", self.staff.id)
20832092

2084-
self.authorized_client.login(username=self.authorized_user.username, password=self.password)
20852093
resp = self.authorized_client.get(self.get_url(other_course.id))
20862094
self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
20872095

20882096
def test_staff_user_allowed_via_legacy(self):
2089-
"""
2090-
Staff users should still pass through legacy fallback.
2091-
"""
2092-
self.client.login(username=self.staff.username, password=self.password)
2093-
2097+
"""Staff users should still pass through legacy fallback."""
2098+
self.client.force_authenticate(user=self.staff)
20942099
resp = self.client.get(self.get_url(self.course_key))
20952100
self.assertEqual(resp.status_code, status.HTTP_200_OK)
20962101

20972102
def test_superuser_allowed(self):
20982103
"""Superusers should always be allowed."""
2099-
superuser = UserFactory(is_superuser=True, username='superuser', password=self.password)
2100-
2104+
superuser = UserFactory(is_superuser=True)
21012105
client = APIClient()
2102-
client.login(username=superuser.username, password=self.password)
2103-
2106+
client.force_authenticate(user=superuser)
21042107
resp = client.get(self.get_url(self.course_key))
21052108
self.assertEqual(resp.status_code, status.HTTP_200_OK)
21062109

0 commit comments

Comments
 (0)