Skip to content

Commit

Permalink
fix: address test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
mariajgrimaldi committed Mar 8, 2024
1 parent 4fec61b commit 5d53a1e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
28 changes: 18 additions & 10 deletions cms/djangoapps/contentstore/rest_api/v1/views/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Unit tests for home page view.
"""
import ddt
from collections import OrderedDict
from django.conf import settings
from django.urls import reverse
from edx_toggles.toggles.testutils import (
Expand Down Expand Up @@ -83,6 +84,11 @@ class HomePageCoursesViewTest(CourseTestCase):
def setUp(self):
super().setUp()
self.url = reverse("cms.djangoapps.contentstore:v1:courses")
CourseOverviewFactory.create(
id=self.course.id,
org=self.course.org,
display_name=self.course.display_name,
)

def test_home_page_response(self):
"""Check successful response content"""
Expand All @@ -91,16 +97,18 @@ def test_home_page_response(self):

expected_response = {
"archived_courses": [],
"courses": [{
"course_key": course_id,
"display_name": self.course.display_name,
"lms_link": f'//{settings.LMS_BASE}/courses/{course_id}/jump_to/{self.course.location}',
"number": self.course.number,
"org": self.course.org,
"rerun_link": f'/course_rerun/{course_id}',
"run": self.course.id.run,
"url": f'/course/{course_id}',
}],
"courses": [
OrderedDict([
("course_key", course_id),
("display_name", self.course.display_name),
("lms_link", f'//{settings.LMS_BASE}/courses/{course_id}/jump_to/{self.course.location}'),
("number", self.course.number),
("org", self.course.org),
("rerun_link", f'/course_rerun/{course_id}'),
("run", self.course.id.run),
("url", f'/course/{course_id}'),
]),
],
"in_process_course_actions": [],
}

Expand Down
1 change: 1 addition & 0 deletions cms/djangoapps/contentstore/rest_api/v2/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


class HomePageCoursesPaginator(PageNumberPagination):
"""Custom paginator for the home page courses view version 2."""

def get_paginated_response(self, data):
"""Return a paginated style `Response` object for the given output data."""
Expand Down
31 changes: 21 additions & 10 deletions cms/djangoapps/contentstore/rest_api/v2/views/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_home_page_response(self):
"""
response = self.client.get(self.url)
course_id = str(self.course.id)
archived_course_id = str(self.archived_course.id)

expected_data = {
"courses": [
Expand All @@ -67,8 +68,14 @@ def test_home_page_response(self):
OrderedDict([
("course_key", str(self.archived_course.id)),
("display_name", self.archived_course.display_name),
("lms_link", f'//{settings.LMS_BASE}/courses/{str(self.archived_course.id)}/jump_to/{self.archived_course.location}'),
("cms_link", f'//{settings.CMS_BASE}{reverse_course_url("course_handler", self.archived_course.id)}'),
(
"lms_link",
f'//{settings.LMS_BASE}/courses/{archived_course_id}/jump_to/{self.archived_course.location}'
),
(
"cms_link",
f'//{settings.CMS_BASE}{reverse_course_url("course_handler", self.archived_course.id)}',
),
("number", self.archived_course.number),
("org", self.archived_course.org),
("rerun_link", f'/course_rerun/{str(self.archived_course.id)}'),
Expand All @@ -83,7 +90,7 @@ def test_home_page_response(self):
('count', 2),
('num_pages', 1),
('next', None),
('previous',None),
('previous', None),
('results', expected_data),
])

Expand Down Expand Up @@ -149,7 +156,10 @@ def test_archived_only_query_if_passed(self):
self.assertEqual(response.data["results"]["courses"], [OrderedDict([
("course_key", str(self.archived_course.id)),
("display_name", self.archived_course.display_name),
("lms_link", f'//{settings.LMS_BASE}/courses/{str(self.archived_course.id)}/jump_to/{self.archived_course.location}'),
(
"lms_link",
f'//{settings.LMS_BASE}/courses/{str(self.archived_course.id)}/jump_to/{self.archived_course.location}',
),
("cms_link", f'//{settings.CMS_BASE}{reverse_course_url("course_handler", self.archived_course.id)}'),
("number", self.archived_course.number),
("org", self.archived_course.org),
Expand All @@ -172,7 +182,10 @@ def test_search_query_if_passed(self):
self.assertEqual(response.data["results"]["courses"], [OrderedDict([
("course_key", str(self.archived_course.id)),
("display_name", self.archived_course.display_name),
("lms_link", f'//{settings.LMS_BASE}/courses/{str(self.archived_course.id)}/jump_to/{self.archived_course.location}'),
(
"lms_link",
f'//{settings.LMS_BASE}/courses/{str(self.archived_course.id)}/jump_to/{self.archived_course.location}',
),
("cms_link", f'//{settings.CMS_BASE}{reverse_course_url("course_handler", self.archived_course.id)}'),
("number", self.archived_course.number),
("org", self.archived_course.org),
Expand All @@ -183,19 +196,17 @@ def test_search_query_if_passed(self):
])])
self.assertEqual(response.status_code, status.HTTP_200_OK)

@ddt.data(("org", "demo-org"), ("-org", "org.4"))
@ddt.unpack
def test_order_query_if_passed(self, order_query, expected_first_org):
def test_order_query_if_passed(self):
"""Get list of courses when order filter passed as a query param.
Expected result:
- A list of courses (active or inactive) available to the logged in user for the specified order.
"""
response = self.client.get(self.url, {"order": order_query})
response = self.client.get(self.url, {"order": "org"})

self.assertEqual(len(response.data["results"]["courses"]), 2)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["results"]["courses"][0]["org"], expected_first_org)
self.assertEqual(response.data["results"]["courses"][0]["org"], "demo-org")

def test_page_query_if_passed(self):
"""Get list of courses when page filter passed as a query param.
Expand Down
5 changes: 3 additions & 2 deletions cms/djangoapps/contentstore/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def course_filter(course_summary):
if org is not None:
courses_summary = [] if org == '' else CourseOverview.get_all_courses(orgs=[org])
else:
courses_summary = CourseOverview.get_all_courses()
courses_summary = courses_summary = modulestore().get_course_summaries()

search_query, order, active_only, archived_only = get_query_params_if_present(request)
courses_summary = get_filtered_and_ordered_courses(
Expand Down Expand Up @@ -447,7 +447,8 @@ def get_query_params_if_present(request):
archived_only (str): if not None, this value will limit the courses returned to archived courses.
The default value is None.
"""
if not request.GET:
allowed_query_params = ['search', 'order', 'active_only', 'archived_only']
if not any(param in request.GET for param in allowed_query_params):
return None, None, None, None
search_query = request.GET.get('search')
order = request.GET.get('order')
Expand Down

0 comments on commit 5d53a1e

Please sign in to comment.