|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | from unittest import mock |
| 6 | +from unittest.mock import patch |
6 | 7 |
|
7 | 8 | from ccx_keys.locator import CCXLocator |
8 | 9 | from django.utils.timezone import now |
@@ -229,3 +230,48 @@ def test_ccx_course_passing_status_updated_emitted(self): |
229 | 230 | }, |
230 | 231 | event_receiver.call_args.kwargs, |
231 | 232 | ) |
| 233 | + |
| 234 | + |
| 235 | +class GradeEventContextFilterTest(SharedModuleStoreTestCase): |
| 236 | + """ |
| 237 | + Tests that course_grade_passed_first_time invokes the GradeEventContextRequested |
| 238 | + filter instead of the old enterprise_support import. |
| 239 | + """ |
| 240 | + |
| 241 | + @classmethod |
| 242 | + def setUpClass(cls): |
| 243 | + super().setUpClass() |
| 244 | + |
| 245 | + def setUp(self): |
| 246 | + super().setUp() |
| 247 | + self.user = UserFactory.create() |
| 248 | + self.course = CourseFactory.create() |
| 249 | + |
| 250 | + @patch('lms.djangoapps.grades.events.GradeEventContextRequested.run_filter') |
| 251 | + def test_filter_called_with_context(self, mock_run_filter): |
| 252 | + """ |
| 253 | + course_grade_passed_first_time should call GradeEventContextRequested.run_filter |
| 254 | + and merge the returned context. |
| 255 | + """ |
| 256 | + enriched = {"org": "test_org", "enterprise_uuid": "abc-123"} |
| 257 | + mock_run_filter.return_value = (enriched, self.user.id, self.course.id) |
| 258 | + |
| 259 | + from lms.djangoapps.grades.events import course_grade_passed_first_time |
| 260 | + with patch('lms.djangoapps.grades.events.tracker'): |
| 261 | + course_grade_passed_first_time(self.user.id, self.course.id) |
| 262 | + |
| 263 | + mock_run_filter.assert_called_once() |
| 264 | + call_kwargs = mock_run_filter.call_args.kwargs |
| 265 | + assert call_kwargs['user_id'] == self.user.id |
| 266 | + assert str(call_kwargs['course_id']) == str(self.course.id) |
| 267 | + |
| 268 | + @patch('lms.djangoapps.grades.events.GradeEventContextRequested.run_filter') |
| 269 | + def test_filter_none_return_leaves_context_intact(self, mock_run_filter): |
| 270 | + """ |
| 271 | + If run_filter returns None (fail_silently path), context is not overwritten. |
| 272 | + """ |
| 273 | + mock_run_filter.return_value = (None, self.user.id, self.course.id) |
| 274 | + from lms.djangoapps.grades.events import course_grade_passed_first_time |
| 275 | + with patch('lms.djangoapps.grades.events.tracker'): |
| 276 | + # Should not raise even when filter returns None |
| 277 | + course_grade_passed_first_time(self.user.id, self.course.id) |
0 commit comments