From a9aae77726c0aaa45873c3d957ca11ef72d13254 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Tue, 4 Feb 2025 16:18:36 +0000 Subject: [PATCH] adds tests to url/archive endpoint --- src/endpoints/url.py | 2 +- src/migrations/README | 1 - src/tests/conftest.py | 6 ++++-- src/tests/endpoints/test_url.py | 27 ++++++++++++++++++++++++++- src/tests/user-groups.test.yaml | 14 +++++++------- 5 files changed, 38 insertions(+), 12 deletions(-) delete mode 100644 src/migrations/README diff --git a/src/endpoints/url.py b/src/endpoints/url.py index d32e6f6..2578b5e 100644 --- a/src/endpoints/url.py +++ b/src/endpoints/url.py @@ -33,7 +33,7 @@ def archive_url( raise HTTPException(status_code=429, detail="User has reached their monthly MB quota.") if archive.group_id and not user.in_group(archive.group_id): raise HTTPException(status_code=403, detail="User does not have access to this group.") - + # TODO: deprecate ArchiveCreate backwards_compatible_archive = schemas.ArchiveCreate( url=archive.url, diff --git a/src/migrations/README b/src/migrations/README deleted file mode 100644 index 98e4f9c..0000000 --- a/src/migrations/README +++ /dev/null @@ -1 +0,0 @@ -Generic single-database configuration. \ No newline at end of file diff --git a/src/tests/conftest.py b/src/tests/conftest.py index 854bd20..89160c1 100644 --- a/src/tests/conftest.py +++ b/src/tests/conftest.py @@ -2,6 +2,7 @@ import os from fastapi.testclient import TestClient import pytest from unittest.mock import patch +from core.config import ALLOW_ANY_EMAIL from db.user_state import UserState from shared.settings import Settings @@ -91,8 +92,9 @@ def client_with_auth(app_with_auth): @pytest.fixture() def app_with_token(app): - from web.security import token_api_key_auth - app.dependency_overrides[token_api_key_auth] = lambda: "jerry@example.com" + from web.security import token_api_key_auth,get_token_or_user_auth + 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 diff --git a/src/tests/endpoints/test_url.py b/src/tests/endpoints/test_url.py index 282769d..ee4f134 100644 --- a/src/tests/endpoints/test_url.py +++ b/src/tests/endpoints/test_url.py @@ -1,5 +1,5 @@ import json -from unittest.mock import patch +from unittest.mock import MagicMock, patch 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] 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): test_no_auth(client.get, "/url/search") diff --git a/src/tests/user-groups.test.yaml b/src/tests/user-groups.test.yaml index 4e33cbd..80c96ac 100644 --- a/src/tests/user-groups.test.yaml +++ b/src/tests/user-groups.test.yaml @@ -73,12 +73,12 @@ groups: orchestrator: tests/orchestration.test.yaml orchestrator_sheet: tests/orchestration.test.yaml permissions: - read: [] + # read: [] archive_url: true - archive_sheet: false - sheet_frequency: [] - max_sheets: 0 - max_archive_lifespan_months: 12 - max_monthly_urls: 10 - max_monthly_mbs: 50 + # archive_sheet: false + # sheet_frequency: [] + # max_sheets: 0 + # max_archive_lifespan_months: 12 + max_monthly_urls: 1 + # max_monthly_mbs: 50 priority: "low" \ No newline at end of file