From 4b52e996fddfb0117a48a1bef986c23aee38014d Mon Sep 17 00:00:00 2001 From: Kaustav Banerjee Date: Wed, 20 Sep 2023 17:51:39 +0530 Subject: [PATCH] fix: fetch org list only for course creator granted status and course admin can create new library (#588) * feat: fetch organizations list only for granted course creators * fix: add ability for course instructors to create libraries same as course staff --- .../tests/test_course_create_rerun.py | 7 +++++++ cms/djangoapps/contentstore/views/course.py | 2 +- cms/djangoapps/contentstore/views/library.py | 3 ++- .../contentstore/views/tests/test_library.py | 21 +++++++++++++++---- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_course_create_rerun.py b/cms/djangoapps/contentstore/tests/test_course_create_rerun.py index 1d1b1177d0dc..0ae56105680a 100644 --- a/cms/djangoapps/contentstore/tests/test_course_create_rerun.py +++ b/cms/djangoapps/contentstore/tests/test_course_create_rerun.py @@ -207,6 +207,10 @@ def test_course_creation_when_user_not_in_org(self): self.assertEqual(response.status_code, 403) @override_settings(FEATURES={'ENABLE_CREATOR_GROUP': True}) + @mock.patch( + 'cms.djangoapps.course_creators.admin.render_to_string', + mock.Mock(side_effect=mock_render_to_string, autospec=True) + ) def test_course_creation_when_user_in_org_with_creator_role(self): """ Tests course creation with user having the organization content creation role. @@ -217,6 +221,9 @@ def test_course_creation_when_user_in_org_with_creator_role(self): 'description': 'Testing Organization Description', }) update_org_role(self.global_admin, OrgContentCreatorRole, self.user, [self.source_course_key.org]) + self.course_creator_entry.all_organizations = True + self.course_creator_entry.state = CourseCreator.GRANTED + self.creator_admin.save_model(self.request, self.course_creator_entry, None, True) self.assertIn(self.source_course_key.org, get_allowed_organizations(self.user)) response = self.client.ajax_post(self.course_create_rerun_url, { 'org': self.source_course_key.org, diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index 702308807688..2ae685801f9c 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -1834,7 +1834,7 @@ def get_organizations(user): Returns the list of organizations for which the user is allowed to create courses. """ course_creator = CourseCreator.objects.filter(user=user).first() - if not course_creator: + if not course_creator or course_creator.state != CourseCreator.GRANTED: return [] elif course_creator.all_organizations: organizations = Organization.objects.all().values_list('short_name', flat=True) diff --git a/cms/djangoapps/contentstore/views/library.py b/cms/djangoapps/contentstore/views/library.py index 328c92687b2c..fbfed7292d9a 100644 --- a/cms/djangoapps/contentstore/views/library.py +++ b/cms/djangoapps/contentstore/views/library.py @@ -83,8 +83,9 @@ def user_can_create_library(user, org=None): is_course_creator = get_course_creator_status(user) == 'granted' has_org_staff_role = OrgStaffRole().get_orgs_for_user(user).exists() has_course_staff_role = UserBasedRole(user=user, role=CourseStaffRole.ROLE).courses_with_role().exists() + has_course_admin_role = UserBasedRole(user=user, role=CourseInstructorRole.ROLE).courses_with_role().exists() - return is_course_creator or has_org_staff_role or has_course_staff_role + return is_course_creator or has_org_staff_role or has_course_staff_role or has_course_admin_role else: # EDUCATOR-1924: DISABLE_LIBRARY_CREATION overrides DISABLE_COURSE_CREATION, if present. disable_library_creation = settings.FEATURES.get('DISABLE_LIBRARY_CREATION', None) diff --git a/cms/djangoapps/contentstore/views/tests/test_library.py b/cms/djangoapps/contentstore/views/tests/test_library.py index bac450b3bd26..f6b7a48a68e1 100644 --- a/cms/djangoapps/contentstore/views/tests/test_library.py +++ b/cms/djangoapps/contentstore/views/tests/test_library.py @@ -19,7 +19,7 @@ from cms.djangoapps.contentstore.tests.utils import AjaxEnabledTestClient, CourseTestCase, parse_json from cms.djangoapps.contentstore.utils import reverse_course_url, reverse_library_url from cms.djangoapps.course_creators.views import add_user_with_status_granted as grant_course_creator_status -from common.djangoapps.student.roles import LibraryUserRole, CourseStaffRole +from common.djangoapps.student.roles import LibraryUserRole, CourseStaffRole, CourseInstructorRole from xmodule.modulestore.tests.factories import LibraryFactory # lint-amnesty, pylint: disable=wrong-import-order from cms.djangoapps.course_creators.models import CourseCreator @@ -101,6 +101,14 @@ def test_library_creator_status_with_course_staff_role_for_enabled_creator_group auth.add_users(self.user, CourseStaffRole(self.course.id), nostaff_user) self.assertEqual(user_can_create_library(nostaff_user), True) + # When creator groups are enabled, course instructor members can create libraries + @mock.patch("cms.djangoapps.contentstore.views.library.LIBRARIES_ENABLED", True) + def test_library_creator_status_with_course_instructor_role_for_enabled_creator_group_setting(self): + _, nostaff_user = self.create_non_staff_authed_user_client() + with mock.patch.dict('django.conf.settings.FEATURES', {"ENABLE_CREATOR_GROUP": True}): + auth.add_users(self.user, CourseInstructorRole(self.course.id), nostaff_user) + self.assertEqual(user_can_create_library(nostaff_user), True) + @ddt.data( (False, False, True), (False, True, False), @@ -480,9 +488,14 @@ def test_allowed_organizations_for_library(self): # Assert that the method returned the expected value self.assertEqual(organizations, []) with mock.patch.dict('django.conf.settings.FEATURES', {"ENABLE_CREATOR_GROUP": True}): - organizations = get_allowed_organizations_for_libraries(self.user) - # Assert that the method returned the expected value - self.assertEqual(organizations, ['org1', 'org2']) + # Assert that correct org values are returned based on course creator state + for course_creator_state in CourseCreator.STATES: + course_creator.state = course_creator_state + organizations = get_allowed_organizations_for_libraries(self.user) + if course_creator_state != CourseCreator.GRANTED: + self.assertEqual(organizations, []) + else: + self.assertEqual(organizations, ['org1', 'org2']) with mock.patch.dict( 'django.conf.settings.FEATURES', {"ENABLE_ORGANIZATION_STAFF_ACCESS_FOR_CONTENT_LIBRARIES": True}