|
| 1 | +""" |
| 2 | +Tests for LTI Advantage Assignments and Grades Service views. |
| 3 | +""" |
| 4 | +from datetime import datetime |
| 5 | +from unittest.mock import patch, Mock |
| 6 | + |
| 7 | +from django.test import TestCase |
| 8 | +from opaque_keys.edx.keys import UsageKey |
| 9 | + |
| 10 | +from lti_consumer.models import LtiConfiguration, LtiAgsLineItem, LtiAgsScore |
| 11 | + |
| 12 | + |
| 13 | +class PublishGradeOnScoreUpdateTest(TestCase): |
| 14 | + """ |
| 15 | + Test the `publish_grade_on_score_update` signal. |
| 16 | + """ |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + """ |
| 20 | + Set up resources for signal testing. |
| 21 | + """ |
| 22 | + super().setUp() |
| 23 | + |
| 24 | + self.location = UsageKey.from_string( |
| 25 | + "block-v1:course+test+2020+type@problem+block@test" |
| 26 | + ) |
| 27 | + |
| 28 | + # Create configuration |
| 29 | + self.lti_config = LtiConfiguration.objects.create( |
| 30 | + location=self.location, |
| 31 | + version=LtiConfiguration.LTI_1P3, |
| 32 | + ) |
| 33 | + |
| 34 | + # Patch internal method to avoid calls to modulestore |
| 35 | + self._block_mock = Mock() |
| 36 | + compat_mock = patch("lti_consumer.signals.compat") |
| 37 | + self.addCleanup(compat_mock.stop) |
| 38 | + self._compat_mock = compat_mock.start() |
| 39 | + self._compat_mock.get_user_from_external_user_id.return_value = Mock() |
| 40 | + self._compat_mock.load_block_as_anonymous_user.return_value = self._block_mock |
| 41 | + |
| 42 | + def test_grade_publish_not_done_when_wrong_line_item(self): |
| 43 | + """ |
| 44 | + Test grade publish after for a different UsageKey than set on |
| 45 | + `lti_config.location`. |
| 46 | + """ |
| 47 | + # Create LineItem with `resource_link_id` != `lti_config.id` |
| 48 | + line_item = LtiAgsLineItem.objects.create( |
| 49 | + lti_configuration=self.lti_config, |
| 50 | + resource_id="test", |
| 51 | + resource_link_id=UsageKey.from_string( |
| 52 | + "block-v1:course+test+2020+type@problem+block@different" |
| 53 | + ), |
| 54 | + label="test label", |
| 55 | + score_maximum=100 |
| 56 | + ) |
| 57 | + |
| 58 | + # Save score and check that LMS method wasn't called. |
| 59 | + LtiAgsScore.objects.create( |
| 60 | + line_item=line_item, |
| 61 | + score_given=1, |
| 62 | + score_maximum=1, |
| 63 | + activity_progress=LtiAgsScore.COMPLETED, |
| 64 | + grading_progress=LtiAgsScore.FULLY_GRADED, |
| 65 | + user_id="test", |
| 66 | + timestamp=datetime.now(), |
| 67 | + ) |
| 68 | + |
| 69 | + # Check that methods to save grades are not called |
| 70 | + self._block_mock.set_user_module_score.assert_not_called() |
| 71 | + self._compat_mock.get_user_from_external_user_id.assert_not_called() |
| 72 | + self._compat_mock.load_block_as_anonymous_user.assert_not_called() |
| 73 | + |
| 74 | + def test_grade_publish(self): |
| 75 | + """ |
| 76 | + Test grade publish after if the UsageKey is equal to |
| 77 | + the one on `lti_config.location`. |
| 78 | + """ |
| 79 | + # Create LineItem with `resource_link_id` != `lti_config.id` |
| 80 | + line_item = LtiAgsLineItem.objects.create( |
| 81 | + lti_configuration=self.lti_config, |
| 82 | + resource_id="test", |
| 83 | + resource_link_id=self.location, |
| 84 | + label="test label", |
| 85 | + score_maximum=100 |
| 86 | + ) |
| 87 | + |
| 88 | + # Save score and check that LMS method wasn't called. |
| 89 | + LtiAgsScore.objects.create( |
| 90 | + line_item=line_item, |
| 91 | + score_given=1, |
| 92 | + score_maximum=1, |
| 93 | + activity_progress=LtiAgsScore.COMPLETED, |
| 94 | + grading_progress=LtiAgsScore.FULLY_GRADED, |
| 95 | + user_id="test", |
| 96 | + timestamp=datetime.now(), |
| 97 | + ) |
| 98 | + |
| 99 | + # Check that methods to save grades are called |
| 100 | + self._block_mock.set_user_module_score.assert_called_once() |
| 101 | + self._compat_mock.get_user_from_external_user_id.assert_called_once() |
| 102 | + self._compat_mock.load_block_as_anonymous_user.assert_called_once() |
0 commit comments