Skip to content

Commit

Permalink
fix: Handle empty course_overviews in get_courses_order_by function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Salman29 committed Dec 3, 2024
1 parent 1397673 commit 5b85fd0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions cms/djangoapps/contentstore/rest_api/v2/views/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 @@ -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)
Expand Down

0 comments on commit 5b85fd0

Please sign in to comment.