Skip to content
Snippets Groups Projects
Commit 4ca2ccd5 authored by Lennard Strohmeyer's avatar Lennard Strohmeyer :penguin:
Browse files

hotfix for broken test case

parent 70f25678
No related branches found
Tags v1.0.1.rc8
No related merge requests found
Pipeline #1563341 passed
......@@ -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
......@@ -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)
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment