mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-13 05:58:35 +03:00
adds tests to url/archive endpoint
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
Generic single-database configuration.
|
|
||||||
@@ -2,6 +2,7 @@ import os
|
|||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
import pytest
|
import pytest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
from core.config import ALLOW_ANY_EMAIL
|
||||||
from db.user_state import UserState
|
from db.user_state import UserState
|
||||||
from shared.settings import Settings
|
from shared.settings import Settings
|
||||||
|
|
||||||
@@ -91,8 +92,9 @@ def client_with_auth(app_with_auth):
|
|||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def app_with_token(app):
|
def app_with_token(app):
|
||||||
from web.security import token_api_key_auth
|
from web.security import token_api_key_auth,get_token_or_user_auth
|
||||||
app.dependency_overrides[token_api_key_auth] = lambda: "jerry@example.com"
|
app.dependency_overrides[token_api_key_auth] = lambda: ALLOW_ANY_EMAIL
|
||||||
|
app.dependency_overrides[get_token_or_user_auth] = lambda: ALLOW_ANY_EMAIL
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from db.schemas import ArchiveCreate, TaskResult
|
from db.schemas import ArchiveCreate, TaskResult
|
||||||
|
|
||||||
@@ -38,6 +38,31 @@ def test_archive_url(m1, client_with_auth):
|
|||||||
called_val = m1.call_args.args[0]
|
called_val = m1.call_args.args[0]
|
||||||
assert json.loads(called_val)["group_id"] == "spaceship"
|
assert json.loads(called_val)["group_id"] == "spaceship"
|
||||||
|
|
||||||
|
def test_archive_url_quotas(client_with_auth):
|
||||||
|
m_user_state = MagicMock()
|
||||||
|
|
||||||
|
# misses on monthly URLs quota
|
||||||
|
m_user_state.has_quota_max_monthly_urls.return_value = False
|
||||||
|
with patch("endpoints.url.UserState", return_value=m_user_state):
|
||||||
|
response = client_with_auth.post("/url/archive", json={"url": "https://example.com"})
|
||||||
|
assert response.status_code == 429
|
||||||
|
assert response.json()["detail"] == "User has reached their monthly URL quota."
|
||||||
|
m_user_state.has_quota_max_monthly_urls.assert_called_once()
|
||||||
|
|
||||||
|
# misses on monthly MBs quota
|
||||||
|
m_user_state.has_quota_max_monthly_urls.return_value = True
|
||||||
|
m_user_state.has_quota_max_monthly_mbs.return_value = False
|
||||||
|
with patch("endpoints.url.UserState", return_value=m_user_state):
|
||||||
|
response = client_with_auth.post("/url/archive", json={"url": "https://example.com"})
|
||||||
|
assert response.status_code == 429
|
||||||
|
assert response.json()["detail"] == "User has reached their monthly MB quota."
|
||||||
|
m_user_state.has_quota_max_monthly_mbs.assert_called_once()
|
||||||
|
|
||||||
|
@patch("worker.main.create_archive_task.delay", return_value=TaskResult(id="123-456-789", status="PENDING", result=""))
|
||||||
|
def test_archive_url_with_api_token(m1, client_with_token):
|
||||||
|
response = client_with_token.post("/url/archive", json={"url": "https://example.com"})
|
||||||
|
assert response.status_code == 201
|
||||||
|
assert response.json() == {'id': '123-456-789'}
|
||||||
|
|
||||||
def test_search_by_url_unauthenticated(client, test_no_auth):
|
def test_search_by_url_unauthenticated(client, test_no_auth):
|
||||||
test_no_auth(client.get, "/url/search")
|
test_no_auth(client.get, "/url/search")
|
||||||
|
|||||||
@@ -73,12 +73,12 @@ groups:
|
|||||||
orchestrator: tests/orchestration.test.yaml
|
orchestrator: tests/orchestration.test.yaml
|
||||||
orchestrator_sheet: tests/orchestration.test.yaml
|
orchestrator_sheet: tests/orchestration.test.yaml
|
||||||
permissions:
|
permissions:
|
||||||
read: []
|
# read: []
|
||||||
archive_url: true
|
archive_url: true
|
||||||
archive_sheet: false
|
# archive_sheet: false
|
||||||
sheet_frequency: []
|
# sheet_frequency: []
|
||||||
max_sheets: 0
|
# max_sheets: 0
|
||||||
max_archive_lifespan_months: 12
|
# max_archive_lifespan_months: 12
|
||||||
max_monthly_urls: 10
|
max_monthly_urls: 1
|
||||||
max_monthly_mbs: 50
|
# max_monthly_mbs: 50
|
||||||
priority: "low"
|
priority: "low"
|
||||||
Reference in New Issue
Block a user