Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fetch org list only for course creator granted status and course admin can create new library #588

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cms/djangoapps/contentstore/tests/test_course_create_rerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cms/djangoapps/contentstore/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cms/djangoapps/contentstore/views/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions cms/djangoapps/contentstore/views/tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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}
Expand Down
Loading