From 5b85fd04e24c45602ecacc4a4b3a6192d49c25bd Mon Sep 17 00:00:00 2001 From: Ali-Salman29 Date: Mon, 2 Dec 2024 14:29:09 +0100 Subject: [PATCH] fix: Handle empty course_overviews in get_courses_order_by function --- .../rest_api/v2/views/tests/test_home.py | 25 +++++++++++++++++++ cms/djangoapps/contentstore/views/course.py | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cms/djangoapps/contentstore/rest_api/v2/views/tests/test_home.py b/cms/djangoapps/contentstore/rest_api/v2/views/tests/test_home.py index 6905de254f3e..d7186570d88b 100644 --- a/cms/djangoapps/contentstore/rest_api/v2/views/tests/test_home.py +++ b/cms/djangoapps/contentstore/rest_api/v2/views/tests/test_home.py @@ -46,6 +46,31 @@ def setUp(self): end=(datetime.now() - timedelta(days=365)).replace(tzinfo=pytz.UTC), ) + def test_no_courses_non_staff_user(self): + """Get list of courses when there are no courses available. + + Expected result: + - An empty list of courses available to the logged in user. + """ + client, _ = self.create_non_staff_authed_user_client() + CourseOverviewFactory._meta.model.objects.all().delete() + response = client.get(self.api_v2_url, {"order": "display_name"}) + + expected_data = { + "courses": [], + "in_process_course_actions": [], + } + expected_response = OrderedDict([ + ('count', 0), + ('num_pages', 1), + ('next', None), + ('previous', None), + ('results', expected_data), + ]) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertDictEqual(expected_response, response.data) + def test_home_page_response(self): """Get list of courses available to the logged in user. diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index 9f6cfb7c430e..487faf13adcf 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -621,7 +621,7 @@ def get_courses_order_by(order_query, course_overviews): order_query (str): any string used to order Course Overviews. course_overviews (Course Overview objects): queryset to be ordered. """ - if not order_query: + if not (order_query and course_overviews): return course_overviews try: return course_overviews.order_by(order_query)