From 557079042fbcb2622d3c8c78e891e1b127166fbe Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Fri, 12 Jun 2020 14:57:09 -0400 Subject: [PATCH] Guard highlights against null module Sometimes a course module will not exist and was causing exceptions in our weekly highlights mail code. This will hopefully guard against that a bit better. --- .../djangoapps/schedules/content_highlights.py | 10 +++++----- .../schedules/tests/test_content_highlights.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/openedx/core/djangoapps/schedules/content_highlights.py b/openedx/core/djangoapps/schedules/content_highlights.py index 3c4148f30f..f236c0a004 100644 --- a/openedx/core/djangoapps/schedules/content_highlights.py +++ b/openedx/core/djangoapps/schedules/content_highlights.py @@ -124,9 +124,12 @@ def _get_course_module(course_descriptor, user): field_data_cache = FieldDataCache.cache_for_descriptor_descendents( course_descriptor.id, user, course_descriptor, depth=1, read_only=True, ) - return get_module_for_descriptor( + course_module = get_module_for_descriptor( user, request, course_descriptor, field_data_cache, course_descriptor.id, course=course_descriptor, ) + if not course_module: + raise CourseUpdateDoesNotExist('Course module {} not found'.format(course_descriptor.id)) + return course_module def _section_has_highlights(section): @@ -134,10 +137,7 @@ def _section_has_highlights(section): def _get_sections_with_highlights(course_module): - if course_module: - return list(filter(_section_has_highlights, course_module.get_children())) - else: - return [] + return list(filter(_section_has_highlights, course_module.get_children())) def _get_highlights_for_week(sections, week_num, course_key): diff --git a/openedx/core/djangoapps/schedules/tests/test_content_highlights.py b/openedx/core/djangoapps/schedules/tests/test_content_highlights.py index 004b5b1533..a98c0d070a 100644 --- a/openedx/core/djangoapps/schedules/tests/test_content_highlights.py +++ b/openedx/core/djangoapps/schedules/tests/test_content_highlights.py @@ -157,3 +157,19 @@ class TestContentHighlights(ModuleStoreTestCase): ) with self.assertRaises(CourseUpdateDoesNotExist): get_next_section_highlights(self.user, self.course_key, yesterday, tomorrow.date()) + + @override_waffle_flag(COURSE_UPDATE_WAFFLE_FLAG, True) + @mock.patch('lms.djangoapps.courseware.module_render.get_module_for_descriptor') + def test_get_highlights_without_module(self, mock_get_module): + mock_get_module.return_value = None + + with self.store.bulk_operations(self.course_key): + self._create_chapter(highlights='Test highlight') + + with self.assertRaisesRegex(CourseUpdateDoesNotExist, 'Course module .* not found'): + get_week_highlights(self.user, self.course_key, 1) + + yesterday = datetime.datetime.utcnow() - datetime.timedelta(days=1) + today = datetime.datetime.utcnow() + with self.assertRaisesRegex(CourseUpdateDoesNotExist, 'Course module .* not found'): + get_next_section_highlights(self.user, self.course_key, yesterday, today.date())