Skip to content

Commit f91f407

Browse files
committed
fixup! only start background task on active forwarding configuration
1 parent cbe57d6 commit f91f407

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎src/ralph/api/routers/statements.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
status,
1616
)
1717

18-
from ralph.api.forwarding import forward_xapi_statements
18+
from ralph.api.forwarding import forward_xapi_statements, get_active_xapi_forwardings
1919
from ralph.backends.database.base import BaseDatabase, StatementParameters
2020
from ralph.conf import settings
2121
from ralph.exceptions import BackendException, BadFormatException
@@ -261,7 +261,10 @@ async def post(
261261
detail="Duplicate statement IDs in the list of statements",
262262
)
263263

264-
background_tasks.add_task(forward_xapi_statements, list(statements_dict.values()))
264+
if get_active_xapi_forwardings():
265+
background_tasks.add_task(
266+
forward_xapi_statements, list(statements_dict.values())
267+
)
265268

266269
try:
267270
statements_ids_result = DATABASE_CLIENT.query_statements_by_ids(statements_ids)

‎tests/api/test_statements_post.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,55 @@ def query_statements_by_ids_mock(*args, **kwargs):
342342
assert response.json() == {"detail": "xAPI statements query failed"}
343343

344344

345+
@pytest.mark.parametrize("backend", [get_es_test_backend, get_mongo_test_backend])
346+
def test_post_statements_list_without_statement_forwarding(
347+
backend, auth_credentials, monkeypatch, es, mongo
348+
):
349+
"""Tests the post statements API route, given an empty forwarding configuration,
350+
should not start the forwarding background task.
351+
"""
352+
# pylint: disable=invalid-name,unused-argument
353+
354+
spy = {}
355+
356+
def spy_mock_forward_xapi_statements(_):
357+
"""Mocks the forward_xapi_statements; spies over whether it has been called."""
358+
359+
spy["error"] = "forward_xapi_statements should not have been called!"
360+
361+
monkeypatch.setattr(
362+
"ralph.api.routers.statements.forward_xapi_statements",
363+
spy_mock_forward_xapi_statements,
364+
)
365+
monkeypatch.setattr(
366+
"ralph.api.routers.statements.get_active_xapi_forwardings", lambda: []
367+
)
368+
monkeypatch.setattr("ralph.api.routers.statements.DATABASE_CLIENT", backend())
369+
370+
statement = {
371+
"actor": {
372+
"account": {
373+
"homePage": "https://example.com/homepage/",
374+
"name": str(uuid4()),
375+
},
376+
"objectType": "Agent",
377+
},
378+
"id": str(uuid4()),
379+
"object": {"id": "https://example.com/object-id/1/"},
380+
"timestamp": "2022-06-22T08:31:38Z",
381+
"verb": {"id": "https://example.com/verb-id/1/"},
382+
}
383+
384+
response = client.post(
385+
"/xAPI/statements/",
386+
headers={"Authorization": f"Basic {auth_credentials}"},
387+
json=statement,
388+
)
389+
390+
assert response.status_code == 200
391+
assert "error" not in spy
392+
393+
345394
@pytest.mark.asyncio
346395
@pytest.mark.parametrize(
347396
"forwarding_backend", [get_es_test_backend, get_mongo_test_backend]

0 commit comments

Comments
 (0)