diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index dcee04480885..609f6989a880 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -31,7 +31,7 @@ from openedx.core.djangoapps.waffle_utils import WaffleSwitchNamespace from openedx.features.course_experience.waffle import waffle as course_experience_waffle from openedx.features.course_experience.waffle import ENABLE_COURSE_ABOUT_SIDEBAR_HTML -from openedx.features.edly.utils import get_edx_org_from_cookie, get_enabled_organizations +from openedx.features.edly.utils import filter_courses_based_on_org, get_edx_org_from_cookie, get_enabled_organizations from six import text_type from contentstore.course_group_config import ( @@ -514,6 +514,7 @@ def filter_ccx(course_access): staff_courses = UserBasedRole(request.user, CourseStaffRole.ROLE).courses_with_role() site_courses = UserBasedRole(request.user, GlobalCourseCreatorRole.ROLE).courses_with_role() all_courses = filter(filter_ccx, instructor_courses | staff_courses | site_courses) + all_courses = filter_courses_based_on_org(request, all_courses) courses_list = [] course_keys = {} diff --git a/openedx/features/edly/tests/test_utils.py b/openedx/features/edly/tests/test_utils.py index 5a75fa38840d..60e71bd795a5 100644 --- a/openedx/features/edly/tests/test_utils.py +++ b/openedx/features/edly/tests/test_utils.py @@ -33,13 +33,19 @@ user_has_edly_organization_access, user_belongs_to_edly_sub_organization, user_can_login_on_requested_edly_organization, + filter_courses_based_on_org, ) +from organizations.tests.factories import OrganizationFactory from student import auth from student.roles import ( CourseCreatorRole, GlobalCourseCreatorRole, ) from student.tests.factories import UserFactory, GroupFactory +from xmodule.modulestore.django import modulestore +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory + def mock_render_to_string(template_name, context): """ @@ -47,7 +53,8 @@ def mock_render_to_string(template_name, context): """ return str((template_name, context)) -class UtilsTests(TestCase): + +class UtilsTests(ModuleStoreTestCase): """ Tests for utility methods. """ @@ -297,3 +304,34 @@ def test_edly_panel_user_has_edly_org_access(self): edly_panel_user_group = GroupFactory(name=settings.EDLY_PANEL_USERS_GROUP) self.request.user.groups.add(edly_panel_user_group) assert edly_panel_user_has_edly_org_access(self.request) + + def test_filter_courses_based_on_org(self): + """ + Test course are filtered properly on current site organization. + """ + edx_org_1 = OrganizationFactory() + edx_org_2 = OrganizationFactory() + courses_of_org_1 = CourseFactory.create_batch(2, org=edx_org_1.short_name) + CourseFactory.create(org=edx_org_2.short_name) + + assert len(modulestore().get_courses()) == 3 + + EdlySubOrganizationFactory( + edx_organization=edx_org_1, + lms_site=self.request.site, + studio_site=self.request.site + ) + create_user_link_with_edly_sub_organization(self.request, self.request.user) + response = cookies_api.set_logged_in_edly_cookies( + self.request, HttpResponse(), + self.user, + cookie_settings(self.request) + ) + self._copy_cookies_to_request(response, self.request) + + filtered_courses = filter_courses_based_on_org(self.request, courses_of_org_1) + assert len(filtered_courses) == 2 + + edx_orgs_of_filterd_courses = [course.org for course in filtered_courses] + for org in edx_orgs_of_filterd_courses: + assert org == edx_org_1.short_name diff --git a/openedx/features/edly/utils.py b/openedx/features/edly/utils.py index 524ef05ecab6..45c05a1bd600 100644 --- a/openedx/features/edly/utils.py +++ b/openedx/features/edly/utils.py @@ -290,3 +290,26 @@ def user_can_login_on_requested_edly_organization(request, user): return True return False + + +def filter_courses_based_on_org(request, all_courses): + """ + Filter courses based on the requested URL site. + + Most of our LMS based roles are not organization based we would + need to filter courses manually based on org of current site. + + Arguments: + request: HTTP request object, + all_courses (iterator): Iterator object. + + Returns: + list: Returns List of courses filtered based on current site organization. + """ + + edly_user_info_cookie = request.COOKIES.get(settings.EDLY_USER_INFO_COOKIE_NAME, None) + edx_org = get_edx_org_from_cookie(edly_user_info_cookie) + + filtered_courses = [course for course in list(all_courses) if course.org == edx_org] + + return filtered_courses