diff --git a/src/xapi/tasks.py b/src/xapi/tasks.py index 7751a55261d1c2e91d63fd90203885ba6004578c..2040180df340e1bd3a99c47305d9283132e595d0 100644 --- a/src/xapi/tasks.py +++ b/src/xapi/tasks.py @@ -7,4 +7,4 @@ def retry_forward_statements(self, data, token_type, token, url): headers = {"Authorization": token_type + " " + token} res = requests.post(url, json=data, headers=headers) if not res: - raise RuntimeError(f"Could not submit {len(data)} statements to {url}.") \ No newline at end of file + raise RuntimeError(f"Could not forward {len(data)} statements to {url}.") \ No newline at end of file diff --git a/src/xapi/tests/tests.py b/src/xapi/tests/tests.py index 273eb68daab607566dbd1a2377ab6f861aca96f1..c555f41177ee1686b803182527a2a94e1aa8d6c3 100644 --- a/src/xapi/tests/tests.py +++ b/src/xapi/tests/tests.py @@ -896,6 +896,7 @@ class TestxAPIObjectMatchingDefinitiondId(BaseTestCase): response.json()["message"], "xAPI statements couldn't be stored in LRS" ) + class TextxAPIAdditionalLrs(BaseTestCase): provider_schema = { "id": "h5p-0", @@ -1016,15 +1017,16 @@ class TextxAPIAdditionalLrs(BaseTestCase): client = APIClient() client.credentials(HTTP_AUTHORIZATION="Basic " + access_token_h5p) - # side effect to immediately create a copy of the object to preserve original properties (since, apparently, the magic mock passes the json object by reference) - def capture_args(*args, **kwargs): - self.captured_json = copy.deepcopy(kwargs.get('json')) - self.captured_headers = copy.deepcopy(kwargs.get('headers')) - # create mock response for external LRS mock_response = MagicMock() mock_response.status_code = 200 mock_response.json.return_value = {'message': "xAPI statements successfully stored in LRS"} + mock_response.__bool__.return_value = True # important since we check "if not res" in xapi.views + # side effect to immediately create a copy of the object to preserve original properties (since, apparently, the magic mock passes the json object by reference) + def capture_args(*args, **kwargs): + self.captured_json = copy.deepcopy(kwargs.get('json')) + self.captured_headers = copy.deepcopy(kwargs.get('headers')) + return mock_response mock_post.return_value = mock_response mock_post.side_effect = capture_args @@ -1039,6 +1041,7 @@ class TextxAPIAdditionalLrs(BaseTestCase): response.json()["message"], "xAPI statements successfully stored in LRS" ) mock_post.assert_called_once() + assert mock_response.status_code == 200 if isinstance(self.captured_json, list): self.assertEqual(self.captured_json, [self.statement]) else: self.assertEqual(self.captured_json, self.statement) self.assertEqual(self.captured_headers, self.additional_lrs_auth_headers) diff --git a/src/xapi/views.py b/src/xapi/views.py index 5de3b36e5be9fa83aaed05009c1edb97884d38f4..b127c46ae943c047932d7459fad592fcbbbd1f39 100644 --- a/src/xapi/views.py +++ b/src/xapi/views.py @@ -327,18 +327,17 @@ class CreateStatement(APIView): if latest_schema.additional_lrs and isinstance(latest_schema.additional_lrs, list) and len(latest_schema.additional_lrs) > 0: for additional_lrs in latest_schema.additional_lrs: headers = {"Authorization": additional_lrs["token_type"] + " " + additional_lrs["token"]} - res = None try: res = requests.post(additional_lrs["url"], json=x_api_statements, headers=headers) + if res.status_code != 200: + raise RuntimeError("Returned status code other than 200") + if settings.DEBUG: + print("Forwarded statement to ", additional_lrs["url"], ":", res.reason, + "({})".format(res.status_code)) except Exception as e: if settings.DEBUG: print("Could not forward to ", additional_lrs["url"], ":", e) - if not res or res.status_code != 200: retry_forward_statements.delay(x_api_statements, additional_lrs["token_type"], additional_lrs["token"], additional_lrs["url"]) - if settings.DEBUG: - print("Could not forward to ", additional_lrs["url"], ":", res.reason if res is not None else "URL could not be reached", "({})".format(res.status_code) if res is not None else "") - elif res and settings.DEBUG: - print("Forwarded statement to ", additional_lrs["url"], ":", res.reason, "({})".format(res.status_code)) if settings.SHOW_XAPI_STATEMENTS: print(x_api_statements)