mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-11 21:18:35 +03:00
refactoring with app_factory
This commit is contained in:
@@ -3,10 +3,7 @@ from fastapi.testclient import TestClient
|
||||
from core.config import VERSION
|
||||
|
||||
|
||||
def test_endpoint_home():
|
||||
from main import app
|
||||
client = TestClient(app)
|
||||
|
||||
def test_endpoint_home(client):
|
||||
r = client.get("/")
|
||||
assert r.status_code == 200
|
||||
j = r.json()
|
||||
@@ -18,10 +15,7 @@ def test_endpoint_home():
|
||||
@patch("endpoints.default.bearer_security", new_callable=AsyncMock)
|
||||
@patch("endpoints.default.get_user_auth", new_callable=AsyncMock, return_value="test@example.com")
|
||||
@patch("endpoints.default.crud.get_user_groups", return_value=["group1", "group2"])
|
||||
def test_endpoint_home_with_groups(m1, m2, m3):
|
||||
from main import app
|
||||
client = TestClient(app)
|
||||
|
||||
def test_endpoint_home_with_groups(m1, m2, m3, client):
|
||||
r = client.get("/")
|
||||
assert r.status_code == 200
|
||||
j = r.json()
|
||||
@@ -31,30 +25,24 @@ def test_endpoint_home_with_groups(m1, m2, m3):
|
||||
assert j["groups"] == ["group1", "group2"]
|
||||
|
||||
|
||||
def test_endpoint_health():
|
||||
from main import app
|
||||
client = TestClient(app)
|
||||
|
||||
def test_endpoint_health(client):
|
||||
r = client.get("/health")
|
||||
assert r.status_code == 200
|
||||
assert r.json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_endpoint_groups_403():
|
||||
from main import app
|
||||
client = TestClient(app)
|
||||
def test_endpoint_groups_403(client):
|
||||
r = client.get("/groups")
|
||||
assert r.status_code == 403
|
||||
|
||||
|
||||
@patch("endpoints.default.crud.get_user_groups", return_value=["group1", "group2"])
|
||||
def test_endpoint_groups(m1):
|
||||
def test_endpoint_groups(m1, app):
|
||||
async def mock_get_user_auth(): return True
|
||||
from main import app
|
||||
from security import get_user_auth
|
||||
app.dependency_overrides[get_user_auth] = mock_get_user_auth
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
r = client.get("/groups")
|
||||
|
||||
assert r.status_code == 200
|
||||
@@ -63,18 +51,12 @@ def test_endpoint_groups(m1):
|
||||
app.dependency_overrides = {}
|
||||
|
||||
|
||||
def test_no_serve_local_archive_by_default():
|
||||
from main import app
|
||||
client = TestClient(app)
|
||||
|
||||
def test_no_serve_local_archive_by_default(client):
|
||||
r = client.get("/app/local_archive_test/temp.txt")
|
||||
assert r.status_code == 404
|
||||
|
||||
|
||||
def test_favicon():
|
||||
from main import app
|
||||
client = TestClient(app)
|
||||
|
||||
def test_favicon(client):
|
||||
r = client.get("/favicon.ico")
|
||||
assert r.status_code == 200
|
||||
assert r.headers["content-type"] == "image/vnd.microsoft.icon"
|
||||
|
||||
@@ -2,18 +2,8 @@ from unittest.mock import 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()
|
||||
|
||||
def test_get_status_success(mock_async_result, client):
|
||||
mock_async_result.return_value.status = "SUCCESS"
|
||||
mock_async_result.return_value.result = {"data": "some result"}
|
||||
|
||||
@@ -25,12 +15,10 @@ def test_get_status_success(mock_async_result):
|
||||
"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()
|
||||
def test_get_status_failure(mock_async_result, client):
|
||||
|
||||
mock_async_result.return_value.status = "FAILURE"
|
||||
mock_async_result.return_value.result = Exception("Some error")
|
||||
@@ -43,13 +31,10 @@ def test_get_status_failure(mock_async_result):
|
||||
"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()
|
||||
|
||||
def test_get_status_pending(mock_async_result, client):
|
||||
mock_async_result.return_value.status = "PENDING"
|
||||
mock_async_result.return_value.result = None
|
||||
|
||||
@@ -61,4 +46,3 @@ def test_get_status_pending(mock_async_result):
|
||||
"status": "PENDING",
|
||||
"result": None
|
||||
}
|
||||
app.dependency_overrides = {}
|
||||
|
||||
6
src/tests/endpoints/test_url.py
Normal file
6
src/tests/endpoints/test_url.py
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
# def test_archive_url(client):
|
||||
# response = client.get("/archive/url")
|
||||
# assert response.status_code == 200
|
||||
# assert response.json() == {"message": "Archive URL"}
|
||||
|
||||
Reference in New Issue
Block a user