From c64518c8a742998f9b90eb9e3514bf27558b78ee Mon Sep 17 00:00:00 2001 From: Kaustav Banerjee Date: Mon, 18 Sep 2023 19:44:00 +0530 Subject: [PATCH] feat: fetch organizations list only for granted course creators --- .../contentstore/tests/test_course_create_rerun.py | 7 +++++++ cms/djangoapps/contentstore/views/course.py | 2 +- .../contentstore/views/tests/test_library.py | 11 ++++++++--- 3 files changed, 16 insertions(+), 4 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 be83c164cc02..341e51dd1986 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -1999,7 +1999,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/tests/test_library.py b/cms/djangoapps/contentstore/views/tests/test_library.py index bac450b3bd26..2db838953f04 100644 --- a/cms/djangoapps/contentstore/views/tests/test_library.py +++ b/cms/djangoapps/contentstore/views/tests/test_library.py @@ -480,9 +480,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}