Commit 2b389a2d by zubiar-arbi

raise ItemNotFoundError if course not found in modulestore (course_listing)

STUD-1455
parent c3b8380d
......@@ -146,6 +146,43 @@ class TestCourseListing(ModuleStoreTestCase):
with self.assertRaises(ItemNotFoundError):
courses_list_by_groups = _accessible_courses_list_from_groups(request)
def test_get_course_list_with_invalid_course_location(self):
"""
Test getting courses with invalid course location (course deleted from modulestore but
location exists in loc_mapper).
"""
request = self.factory.get('/course')
request.user = self.user
course_location = Location('i4x', 'Org', 'Course', 'course', 'Run')
self._create_course_with_access_groups(course_location, 'group_name_with_dots', self.user)
# get courses through iterating all courses
courses_list = _accessible_courses_list(request)
self.assertEqual(len(courses_list), 1)
# get courses by reversing group name formats
courses_list_by_groups = _accessible_courses_list_from_groups(request)
self.assertEqual(len(courses_list_by_groups), 1)
# check both course lists have same courses
self.assertEqual(courses_list, courses_list_by_groups)
# now delete this course and re-add user to instructor group of this course
delete_course_and_groups(course_location.course_id, commit=True)
course_locator = loc_mapper().translate_location(course_location.course_id, course_location)
instructor_group_name = CourseInstructorRole(course_locator)._group_names[0] # pylint: disable=protected-access
group, __ = Group.objects.get_or_create(name=instructor_group_name)
self.user.groups.add(group)
# test that get courses through iterating all courses now returns no course
courses_list = _accessible_courses_list(request)
self.assertEqual(len(courses_list), 0)
# now test that get courses by reversing group name formats gives 'ItemNotFoundError'
with self.assertRaises(ItemNotFoundError):
_accessible_courses_list_from_groups(request)
def test_course_listing_performance(self):
"""
Create large number of courses and give access of some of these courses to the user and
......@@ -202,8 +239,11 @@ class TestCourseListing(ModuleStoreTestCase):
Test getting courses with same id but with different name case. Then try to delete one of them and
check that it is properly deleted and other one is accessible
"""
# create and log in a non-staff user
self.user = UserFactory()
request = self.factory.get('/course')
request.user = self.user
self.client.login(username=self.user.username, password='test')
course_location_caps = Location(['i4x', 'Org', 'COURSE', 'course', 'Run'])
self._create_course_with_access_groups(course_location_caps, 'group_name_with_dots', self.user)
......@@ -239,6 +279,15 @@ class TestCourseListing(ModuleStoreTestCase):
group, __ = Group.objects.get_or_create(name=instructor_group_name)
self.user.groups.add(group)
# test viewing the index page which creates missing courses loc_map entries
resp = self.client.get_html('/course')
self.assertContains(
resp,
'<h1 class="page-header">My Courses</h1>',
status_code=200,
html=True
)
# test that get courses through iterating all courses now returns one course
courses_list = _accessible_courses_list(request)
self.assertEqual(len(courses_list), 1)
......
......@@ -220,6 +220,9 @@ def _accessible_courses_list_from_groups(request):
raise ItemNotFoundError(course_id)
course = modulestore('direct').get_course(course_location.course_id)
if course is None:
raise ItemNotFoundError(course_id)
courses_list.append(course)
return courses_list
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment