Skip to content

Commit

Permalink
test: add same v1 test with adjustments for new output format
Browse files Browse the repository at this point in the history
  • Loading branch information
mariajgrimaldi committed Mar 1, 2024
1 parent 8697e15 commit 6972dc5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
75 changes: 50 additions & 25 deletions cms/djangoapps/contentstore/rest_api/v2/views/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ddt
from django.conf import settings
from django.urls import reverse
from collections import OrderedDict
from edx_toggles.toggles.testutils import (
override_waffle_switch,
)
Expand All @@ -13,57 +14,81 @@
from cms.djangoapps.contentstore.views.course import ENABLE_GLOBAL_STAFF_OPTIMIZATION
from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory
from xmodule.modulestore.tests.factories import CourseFactory
from cms.djangoapps.contentstore.utils import get_lms_link_for_item, reverse_course_url


@ddt.ddt
class HomePageCoursesViewTest(CourseTestCase):
class HomePageCoursesViewV2Test(CourseTestCase):
"""
Tests for HomePageView.
Tests for HomePageView view version 2.
"""

def setUp(self):
super().setUp()
self.url = reverse("cms.djangoapps.contentstore:v2:courses")
CourseOverviewFactory.create(
id=self.course.id,
org=self.course.org,
run=self.course.id.run,
display_name=self.course.display_name,
)

def test_home_page_response(self):
"""Check successful response content"""
"""Get list of courses available to the logged in user.
Expected result:
- A list of courses available to the logged in user.
- A paginated response.
"""
response = self.client.get(self.url)
course_id = str(self.course.id)

expected_response = {
"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}',
}],
expected_data = {
"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}'),
("cms_link", f'//{settings.CMS_BASE}{reverse_course_url("course_handler", self.course.id)}'),
("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}'),
("is_active", True),
])],
"in_process_course_actions": [],
}
expected_response = OrderedDict([
('count', 1),
('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)

@override_waffle_switch(ENABLE_GLOBAL_STAFF_OPTIMIZATION, True)
def test_org_query_if_passed(self):
"""Test home page when org filter passed as a query param"""
foo_course = self.store.make_course_key('foo-org', 'bar-number', 'baz-run')
test_course = CourseFactory.create(
org=foo_course.org,
number=foo_course.course,
run=foo_course.run
)
CourseOverviewFactory.create(id=test_course.id, org='foo-org')
response = self.client.get(self.url, {"org": "foo-org"})
self.assertEqual(len(response.data['courses']), 1)
"""Get list of courses when org filter passed as a query param.
Expected result:
- A list of courses available to the logged in user for the specified org.
"""
demo_course_key = self.store.make_course_key('demo-org', 'demo-number', 'demo-run')
CourseOverviewFactory.create(id=demo_course_key, org=demo_course_key.org)

response = self.client.get(self.url, {"org": "demo-org"})

self.assertEqual(len(response.data['results']['courses']), 1)
self.assertEqual(response.status_code, status.HTTP_200_OK)

@override_waffle_switch(ENABLE_GLOBAL_STAFF_OPTIMIZATION, True)
def test_org_query_if_empty(self):
"""Test home page with an empty org query param"""
response = self.client.get(self.url)
self.assertEqual(len(response.data['courses']), 0)

self.assertEqual(len(response.data['results']['courses']), 0)
self.assertEqual(response.status_code, status.HTTP_200_OK)
1 change: 1 addition & 0 deletions cms/djangoapps/contentstore/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ def course_filter(course_summary):
return False

return has_studio_read_access(request.user, course_summary.id)

if org is not None:
courses_summary = [] if org == '' else CourseOverview.get_all_courses(orgs=[org])
else:
Expand Down

0 comments on commit 6972dc5

Please sign in to comment.