c7d3ec57c3
Commitd2217531(google-labs-jules SSRF fix) catastrophically deleted 11,500+ lines across 100+ files while fixing an unrelated IMAP issue. Restored from d2217531^ (pre-bad-commit state): Deleted files (fully restored): - app/api/{automation,classification_rules,comments,sharing}.py - app/middleware/upload_rate_limit.py - app/tasks/{automation_tasks,classify_document}.py - app/utils/{automation_hooks,classification_rules}.py - docs/AppleAppStoreCompliance.md - frontend/input.css, package.json, package-lock.json, tailwind.config.js - frontend/static/js/{annotations,claim,comments,sharing}.js - frontend/templates/{admin_connections,file_annotations,file_summary}.html - tests/{test_api_files_comprehensive,test_auth_extended,test_sharing, test_comments,test_connections,test_imap_profiles,test_api_sessions, test_automation,test_classification_rules,test_api_advanced_filters, test_api_classification_rules,test_upload_rate_limit,test_api_dropbox, test_classify_document,test_comments_ui,test_upload_to_icloud, test_api_onedrive_comprehensive,test_frontend_build,test_sentry, test_diagnostic,test_database,test_views_dropbox,test_local_auth}.py Truncated files (content restored): - app/{auth,config,main,models,celery_worker,database}.py - app/api/{__init__,api_tokens,diagnostic,dropbox,files,google_drive, integrations,local_auth,mobile,onedrive,pipelines,qr_auth, settings,url_upload}.py - app/middleware/upload_rate_limit.py - app/tasks/upload_to_nextcloud.py - app/utils/{allowed_types,settings_service,settings_sync,user_scope,webhook}.py - app/views/{base,dropbox,files,google_drive,onedrive,settings}.py - docs/{API,AuthenticationSetup,ConfigurationGuide,DatabaseConfiguration, DeploymentGuide,DropboxSetup,GoogleDriveSetup,KubernetesDeployment, MobileApp,OneDriveSetup,ProductionReadiness,SentrySetup, SocialLoginSetup,UserGuide}.md - frontend/static/{js/upload.js,styles.css} - frontend/templates/{api_tokens,base,devices,dropbox,dropbox_callback, file_view,files,google_drive,onedrive,onedrive_callback, signup}.html - frontend/translations/en.json - migrations/env.py - tests/{conftest,test_api_integrations,test_api_mobile,test_api_settings, test_api_tokens,test_audit_logs,test_duplicates,test_imap_tasks, test_setup_wizard,test_views_files_comprehensive}.py Security fixes kept from post-d2217531 commits: - app/utils/network.py: DNS SSRF fail-secure fix (06b0fced) - app/utils/file_operations.py: path traversal fix (1018ea17) - tests/test_imap_tasks.py: re-applied 4 is_private_ip mock patches Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> Agent-Logs-Url: https://github.com/christianlouis/DocuElevate/sessions/51133dd8-9bec-41ab-aa10-3de753634187
291 lines
9.6 KiB
Python
291 lines
9.6 KiB
Python
"""Tests for the classification rules API endpoints.
|
|
|
|
Covers CRUD operations, validation, and access control for
|
|
``/api/classification-rules``.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from app.models import ClassificationRuleModel
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _make_rule(db_session, owner_id="anonymous", **overrides):
|
|
"""Insert a ClassificationRuleModel and return it."""
|
|
defaults = {
|
|
"owner_id": owner_id,
|
|
"name": "test_rule",
|
|
"category": "invoice",
|
|
"rule_type": "filename_pattern",
|
|
"pattern": r"(?i)invoice",
|
|
"priority": 0,
|
|
"case_sensitive": False,
|
|
"enabled": True,
|
|
}
|
|
defaults.update(overrides)
|
|
rule = ClassificationRuleModel(**defaults)
|
|
db_session.add(rule)
|
|
db_session.commit()
|
|
db_session.refresh(rule)
|
|
return rule
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Categories & Rule Types endpoints
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestCategoriesEndpoint:
|
|
"""Tests for GET /api/classification-rules/categories."""
|
|
|
|
def test_list_categories(self, client):
|
|
"""Should return a dict of built-in categories."""
|
|
r = client.get("/api/classification-rules/categories")
|
|
assert r.status_code == 200
|
|
data = r.json()
|
|
assert isinstance(data, dict)
|
|
assert "invoice" in data
|
|
assert "contract" in data
|
|
assert "receipt" in data
|
|
assert "unknown" in data
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestRuleTypesEndpoint:
|
|
"""Tests for GET /api/classification-rules/rule-types."""
|
|
|
|
def test_list_rule_types(self, client):
|
|
"""Should return a list of valid rule types."""
|
|
r = client.get("/api/classification-rules/rule-types")
|
|
assert r.status_code == 200
|
|
data = r.json()
|
|
assert isinstance(data, list)
|
|
assert len(data) == 3
|
|
type_values = {item["type"] for item in data}
|
|
assert "filename_pattern" in type_values
|
|
assert "content_keyword" in type_values
|
|
assert "metadata_match" in type_values
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# CRUD Operations
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.integration
|
|
class TestClassificationRuleCRUD:
|
|
"""Full CRUD test-suite for classification rules."""
|
|
|
|
def test_list_rules_empty(self, client):
|
|
"""List returns an empty array when no rules exist."""
|
|
r = client.get("/api/classification-rules/")
|
|
assert r.status_code == 200
|
|
assert r.json() == []
|
|
|
|
def test_create_rule(self, client):
|
|
"""POST should create a new classification rule."""
|
|
r = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "My Invoice Rule",
|
|
"category": "invoice",
|
|
"rule_type": "filename_pattern",
|
|
"pattern": r"(?i)rechnung",
|
|
"priority": 10,
|
|
},
|
|
)
|
|
assert r.status_code == 201
|
|
data = r.json()
|
|
assert data["name"] == "My Invoice Rule"
|
|
assert data["category"] == "invoice"
|
|
assert data["rule_type"] == "filename_pattern"
|
|
assert data["priority"] == 10
|
|
assert data["enabled"] is True
|
|
assert data["id"] is not None
|
|
|
|
def test_create_rule_invalid_type_rejected(self, client):
|
|
"""Creating a rule with an invalid rule_type should be rejected."""
|
|
r = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "Bad Rule",
|
|
"category": "test",
|
|
"rule_type": "invalid_type",
|
|
"pattern": "test",
|
|
},
|
|
)
|
|
assert r.status_code == 400
|
|
|
|
def test_create_duplicate_name_rejected(self, client):
|
|
"""Creating two rules with the same name should be rejected."""
|
|
payload = {
|
|
"name": "Dupe Rule",
|
|
"category": "invoice",
|
|
"rule_type": "filename_pattern",
|
|
"pattern": "test",
|
|
}
|
|
r1 = client.post("/api/classification-rules/", json=payload)
|
|
assert r1.status_code == 201
|
|
r2 = client.post("/api/classification-rules/", json=payload)
|
|
assert r2.status_code == 409
|
|
|
|
def test_get_rule(self, client):
|
|
"""GET should return a specific rule by ID."""
|
|
create_resp = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "Get Test Rule",
|
|
"category": "contract",
|
|
"rule_type": "content_keyword",
|
|
"pattern": "agreement|terms",
|
|
},
|
|
)
|
|
rule_id = create_resp.json()["id"]
|
|
|
|
r = client.get(f"/api/classification-rules/{rule_id}")
|
|
assert r.status_code == 200
|
|
assert r.json()["name"] == "Get Test Rule"
|
|
assert r.json()["category"] == "contract"
|
|
|
|
def test_get_nonexistent_rule(self, client):
|
|
"""GET for a nonexistent rule should return 404."""
|
|
r = client.get("/api/classification-rules/99999")
|
|
assert r.status_code == 404
|
|
|
|
def test_update_rule(self, client):
|
|
"""PUT should update an existing rule."""
|
|
create_resp = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "Update Test",
|
|
"category": "receipt",
|
|
"rule_type": "filename_pattern",
|
|
"pattern": "receipt",
|
|
},
|
|
)
|
|
rule_id = create_resp.json()["id"]
|
|
|
|
r = client.put(
|
|
f"/api/classification-rules/{rule_id}",
|
|
json={"category": "invoice", "priority": 50},
|
|
)
|
|
assert r.status_code == 200
|
|
assert r.json()["category"] == "invoice"
|
|
assert r.json()["priority"] == 50
|
|
# Name should be unchanged
|
|
assert r.json()["name"] == "Update Test"
|
|
|
|
def test_update_nonexistent_rule(self, client):
|
|
"""PUT for a nonexistent rule should return 404."""
|
|
r = client.put("/api/classification-rules/99999", json={"category": "test"})
|
|
assert r.status_code == 404
|
|
|
|
def test_update_invalid_rule_type_rejected(self, client):
|
|
"""PUT with an invalid rule_type should be rejected."""
|
|
create_resp = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "Invalid Update",
|
|
"category": "test",
|
|
"rule_type": "filename_pattern",
|
|
"pattern": "test",
|
|
},
|
|
)
|
|
rule_id = create_resp.json()["id"]
|
|
|
|
r = client.put(
|
|
f"/api/classification-rules/{rule_id}",
|
|
json={"rule_type": "bad_type"},
|
|
)
|
|
assert r.status_code == 400
|
|
|
|
def test_delete_rule(self, client):
|
|
"""DELETE should remove the rule."""
|
|
create_resp = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "Delete Test",
|
|
"category": "test",
|
|
"rule_type": "content_keyword",
|
|
"pattern": "test",
|
|
},
|
|
)
|
|
rule_id = create_resp.json()["id"]
|
|
|
|
r = client.delete(f"/api/classification-rules/{rule_id}")
|
|
assert r.status_code == 204
|
|
|
|
# Verify it's gone
|
|
r2 = client.get(f"/api/classification-rules/{rule_id}")
|
|
assert r2.status_code == 404
|
|
|
|
def test_delete_nonexistent_rule(self, client):
|
|
"""DELETE for a nonexistent rule should return 404."""
|
|
r = client.delete("/api/classification-rules/99999")
|
|
assert r.status_code == 404
|
|
|
|
def test_list_rules_after_create(self, client):
|
|
"""List should return created rules."""
|
|
client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "List Rule 1",
|
|
"category": "invoice",
|
|
"rule_type": "filename_pattern",
|
|
"pattern": "test1",
|
|
},
|
|
)
|
|
client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "List Rule 2",
|
|
"category": "contract",
|
|
"rule_type": "content_keyword",
|
|
"pattern": "test2",
|
|
},
|
|
)
|
|
r = client.get("/api/classification-rules/")
|
|
assert r.status_code == 200
|
|
assert len(r.json()) == 2
|
|
|
|
def test_create_rule_with_all_fields(self, client):
|
|
"""Create a rule providing all optional fields."""
|
|
r = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "Full Rule",
|
|
"category": "tax_document",
|
|
"rule_type": "metadata_match",
|
|
"pattern": "department=finance",
|
|
"priority": 100,
|
|
"case_sensitive": True,
|
|
"enabled": False,
|
|
},
|
|
)
|
|
assert r.status_code == 201
|
|
data = r.json()
|
|
assert data["case_sensitive"] is True
|
|
assert data["enabled"] is False
|
|
assert data["priority"] == 100
|
|
|
|
def test_create_rule_defaults(self, client):
|
|
"""Create a rule with minimal fields to test defaults."""
|
|
r = client.post(
|
|
"/api/classification-rules/",
|
|
json={
|
|
"name": "Minimal Rule",
|
|
"category": "invoice",
|
|
"rule_type": "filename_pattern",
|
|
"pattern": "test",
|
|
},
|
|
)
|
|
assert r.status_code == 201
|
|
data = r.json()
|
|
assert data["priority"] == 0
|
|
assert data["case_sensitive"] is False
|
|
assert data["enabled"] is True
|