adds tests to url/archive endpoint

This commit is contained in:
msramalho
2025-02-04 16:18:36 +00:00
parent 809438fbb9
commit a9aae77726
5 changed files with 38 additions and 12 deletions

View File

@@ -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,

View File

@@ -1 +0,0 @@
Generic single-database configuration.

View File

@@ -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

View File

@@ -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")

View File

@@ -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"