|
| 1 | +"""Tests for the video xAPI statement sent from LTI.""" |
| 2 | + |
| 3 | +import io |
| 4 | +import json |
| 5 | +import logging |
| 6 | + |
| 7 | +from django.core.cache import cache |
| 8 | +from django.test import TestCase |
| 9 | + |
| 10 | +from logging_ldp.formatters import LDPGELFFormatter |
| 11 | + |
| 12 | +from marsha.core.factories import ( |
| 13 | + ConsumerSiteFactory, |
| 14 | + OrganizationFactory, |
| 15 | + PlaylistFactory, |
| 16 | + VideoFactory, |
| 17 | +) |
| 18 | +from marsha.core.simple_jwt.factories import StudentLtiTokenFactory |
| 19 | + |
| 20 | + |
| 21 | +class XAPIVideoFromLTITest(TestCase): |
| 22 | + """Tests for the video xAPI statement sent from LTI.""" |
| 23 | + |
| 24 | + maxDiff = None |
| 25 | + |
| 26 | + def setUp(self): |
| 27 | + self.logger = logging.getLogger("xapi.lti.example.com") |
| 28 | + self.logger.setLevel(logging.INFO) |
| 29 | + self.log_stream = io.StringIO() |
| 30 | + |
| 31 | + handler = logging.StreamHandler(self.log_stream) |
| 32 | + handler.setFormatter(LDPGELFFormatter(token="foo", null_character=False)) |
| 33 | + self.logger.addHandler(handler) |
| 34 | + |
| 35 | + # Clear cache |
| 36 | + cache.clear() |
| 37 | + |
| 38 | + super().setUp() |
| 39 | + |
| 40 | + def test_send_xapi_statement_from_lti_request(self): |
| 41 | + """ |
| 42 | + A video xAPI statement should be sent when the video has been created in a LTI context. |
| 43 | + """ |
| 44 | + video = VideoFactory( |
| 45 | + id="7b18195e-e183-4bbf-b8ef-5145ef64ae19", |
| 46 | + title="Video 000", |
| 47 | + playlist__consumer_site__domain="lti.example.com", |
| 48 | + ) |
| 49 | + jwt_token = StudentLtiTokenFactory( |
| 50 | + playlist=video.playlist, |
| 51 | + context_id="cf253c93-3738-496b-8c8f-1e8a1b09a6b1", |
| 52 | + ) |
| 53 | + |
| 54 | + data = { |
| 55 | + "verb": { |
| 56 | + "id": "http://adlnet.gov/expapi/verbs/initialized", |
| 57 | + "display": {"en-US": "initialized"}, |
| 58 | + }, |
| 59 | + "context": { |
| 60 | + "extensions": {"https://w3id.org/xapi/video/extensions/volume": 1} |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + response = self.client.post( |
| 65 | + f"/xapi/video/{video.id}/", |
| 66 | + HTTP_AUTHORIZATION=f"Bearer {jwt_token}", |
| 67 | + data=json.dumps(data), |
| 68 | + content_type="application/json", |
| 69 | + ) |
| 70 | + |
| 71 | + self.assertEqual(response.status_code, 200) |
| 72 | + log = json.loads(self.log_stream.getvalue()) |
| 73 | + self.assertIn("short_message", log) |
| 74 | + message = json.loads(log["short_message"]) |
| 75 | + self.assertEqual( |
| 76 | + message.get("verb"), |
| 77 | + { |
| 78 | + "id": "http://adlnet.gov/expapi/verbs/initialized", |
| 79 | + "display": {"en-US": "initialized"}, |
| 80 | + }, |
| 81 | + ) |
| 82 | + self.assertEqual( |
| 83 | + message.get("context"), |
| 84 | + { |
| 85 | + "extensions": {"https://w3id.org/xapi/video/extensions/volume": 1}, |
| 86 | + "contextActivities": { |
| 87 | + "category": [{"id": "https://w3id.org/xapi/video"}], |
| 88 | + "parent": [ |
| 89 | + { |
| 90 | + "id": "cf253c93-3738-496b-8c8f-1e8a1b09a6b1", |
| 91 | + "objectType": "Activity", |
| 92 | + "definition": { |
| 93 | + "type": "http://adlnet.gov/expapi/activities/course" |
| 94 | + }, |
| 95 | + } |
| 96 | + ], |
| 97 | + }, |
| 98 | + }, |
| 99 | + ) |
| 100 | + self.assertEqual( |
| 101 | + message.get("object"), |
| 102 | + { |
| 103 | + "definition": { |
| 104 | + "type": "https://w3id.org/xapi/video/activity-type/video", |
| 105 | + "name": {"en-US": "Video 000"}, |
| 106 | + }, |
| 107 | + "id": "uuid://7b18195e-e183-4bbf-b8ef-5145ef64ae19", |
| 108 | + "objectType": "Activity", |
| 109 | + }, |
| 110 | + ) |
| 111 | + |
| 112 | + def test_send_xapi_statement_from_lti_request_video_no_consumer_site(self): |
| 113 | + """ |
| 114 | + A video xAPI statement should be sent when the video has not been created in a LTI context. |
| 115 | + """ |
| 116 | + organization = OrganizationFactory() |
| 117 | + playlist = PlaylistFactory( |
| 118 | + organization=organization, consumer_site=None, lti_id=None |
| 119 | + ) |
| 120 | + consumer_site = ConsumerSiteFactory(domain="lti.example.com") |
| 121 | + video = VideoFactory( |
| 122 | + id="7b18195e-e183-4bbf-b8ef-5145ef64ae19", |
| 123 | + title="Video 000", |
| 124 | + playlist=playlist, |
| 125 | + ) |
| 126 | + jwt_token = StudentLtiTokenFactory( |
| 127 | + playlist=video.playlist, |
| 128 | + context_id="cf253c93-3738-496b-8c8f-1e8a1b09a6b1", |
| 129 | + consumer_site=str(consumer_site.id), |
| 130 | + ) |
| 131 | + self.assertIsNotNone(video.playlist.organization) |
| 132 | + self.assertIsNone(video.playlist.consumer_site) |
| 133 | + |
| 134 | + data = { |
| 135 | + "verb": { |
| 136 | + "id": "http://adlnet.gov/expapi/verbs/initialized", |
| 137 | + "display": {"en-US": "initialized"}, |
| 138 | + }, |
| 139 | + "context": { |
| 140 | + "extensions": {"https://w3id.org/xapi/video/extensions/volume": 1} |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + response = self.client.post( |
| 145 | + f"/xapi/video/{video.id}/", |
| 146 | + HTTP_AUTHORIZATION=f"Bearer {jwt_token}", |
| 147 | + data=json.dumps(data), |
| 148 | + content_type="application/json", |
| 149 | + ) |
| 150 | + |
| 151 | + self.assertEqual(response.status_code, 200) |
| 152 | + log = json.loads(self.log_stream.getvalue()) |
| 153 | + self.assertIn("short_message", log) |
| 154 | + message = json.loads(log["short_message"]) |
| 155 | + self.assertEqual( |
| 156 | + message.get("verb"), |
| 157 | + { |
| 158 | + "id": "http://adlnet.gov/expapi/verbs/initialized", |
| 159 | + "display": {"en-US": "initialized"}, |
| 160 | + }, |
| 161 | + ) |
| 162 | + self.assertEqual( |
| 163 | + message.get("context"), |
| 164 | + { |
| 165 | + "extensions": {"https://w3id.org/xapi/video/extensions/volume": 1}, |
| 166 | + "contextActivities": { |
| 167 | + "category": [{"id": "https://w3id.org/xapi/video"}], |
| 168 | + "parent": [ |
| 169 | + { |
| 170 | + "id": "cf253c93-3738-496b-8c8f-1e8a1b09a6b1", |
| 171 | + "objectType": "Activity", |
| 172 | + "definition": { |
| 173 | + "type": "http://adlnet.gov/expapi/activities/course" |
| 174 | + }, |
| 175 | + } |
| 176 | + ], |
| 177 | + }, |
| 178 | + }, |
| 179 | + ) |
| 180 | + self.assertEqual( |
| 181 | + message.get("object"), |
| 182 | + { |
| 183 | + "definition": { |
| 184 | + "type": "https://w3id.org/xapi/video/activity-type/video", |
| 185 | + "name": {"en-US": "Video 000"}, |
| 186 | + }, |
| 187 | + "id": "uuid://7b18195e-e183-4bbf-b8ef-5145ef64ae19", |
| 188 | + "objectType": "Activity", |
| 189 | + }, |
| 190 | + ) |
0 commit comments