mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-08 03:28:35 +03:00
testing task endpoints
This commit is contained in:
@@ -60,6 +60,7 @@ def test_endpoint_groups(m1):
|
||||
assert r.status_code == 200
|
||||
j = r.json()
|
||||
assert j == ["group1", "group2"]
|
||||
app.dependency_overrides = {}
|
||||
|
||||
|
||||
def test_no_serve_local_archive_by_default():
|
||||
|
||||
64
src/tests/endpoints/test_task.py
Normal file
64
src/tests/endpoints/test_task.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
|
||||
def setup_client():
|
||||
from main import app
|
||||
from security import get_token_or_user_auth
|
||||
async def mock_get_token_or_user_auth(): return "example@email.com"
|
||||
app.dependency_overrides[get_token_or_user_auth] = mock_get_token_or_user_auth
|
||||
return TestClient(app), app
|
||||
|
||||
@patch("endpoints.task.AsyncResult")
|
||||
def test_get_status_success(mock_async_result):
|
||||
client, app = setup_client()
|
||||
|
||||
mock_async_result.return_value.status = "SUCCESS"
|
||||
mock_async_result.return_value.result = {"data": "some result"}
|
||||
|
||||
response = client.get("/task/test-task-id")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"id": "test-task-id",
|
||||
"status": "SUCCESS",
|
||||
"result": {"data": "some result"}
|
||||
}
|
||||
app.dependency_overrides = {}
|
||||
|
||||
|
||||
@patch("endpoints.task.AsyncResult")
|
||||
def test_get_status_failure(mock_async_result):
|
||||
client, app = setup_client()
|
||||
|
||||
mock_async_result.return_value.status = "FAILURE"
|
||||
mock_async_result.return_value.result = Exception("Some error")
|
||||
|
||||
response = client.get("/task/test-task-id")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"id": "test-task-id",
|
||||
"status": "FAILURE",
|
||||
"result": {"error": "Some error"}
|
||||
}
|
||||
app.dependency_overrides = {}
|
||||
|
||||
|
||||
@patch("endpoints.task.AsyncResult")
|
||||
def test_get_status_pending(mock_async_result):
|
||||
client, app = setup_client()
|
||||
|
||||
mock_async_result.return_value.status = "PENDING"
|
||||
mock_async_result.return_value.result = None
|
||||
|
||||
response = client.get("/task/test-task-id")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"id": "test-task-id",
|
||||
"status": "PENDING",
|
||||
"result": None
|
||||
}
|
||||
app.dependency_overrides = {}
|
||||
Reference in New Issue
Block a user