test: migrate backend suites to pytest with module coverage gate
Split backend tests into unit and integration suites, add isolated fixtures and coverage enforcement, and update CI and test documentation. Cover current authentication, content, notification, payment, media, and admin flows; fix behaviors revealed by the new tests. Validate both suites and the per-module gate against local PostgreSQL.
This commit is contained in:
40
apps/analytics/tests/integration/test_analytics.py
Normal file
40
apps/analytics/tests/integration/test_analytics.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import pytest
|
||||
|
||||
|
||||
pytestmark = [pytest.mark.integration, pytest.mark.django_db]
|
||||
|
||||
|
||||
def test_analytics_requires_staff_and_reports_current_users(client, user_factory, auth_headers):
|
||||
member = user_factory()
|
||||
staff = user_factory(is_staff=True)
|
||||
path = "/api/analytics/admin/users"
|
||||
assert client.get(path).status_code == 401
|
||||
assert client.get(path, **auth_headers(member)).status_code == 403
|
||||
response = client.get(path, **auth_headers(staff))
|
||||
assert response.status_code == 200
|
||||
assert response.json()["summary"]["total_users"] == 2
|
||||
|
||||
|
||||
def test_staff_analytics_endpoints_return_empty_aggregate_shapes(client, user_factory, event_factory, auth_headers):
|
||||
staff = user_factory(is_staff=True)
|
||||
event = event_factory()
|
||||
headers = auth_headers(staff)
|
||||
options = client.get("/api/analytics/admin/events/options", {"search": "Test", "limit": 1}, **headers)
|
||||
assert options.status_code == 200
|
||||
assert options.json()["count"] == 1
|
||||
assert options.json()["results"][0]["value"] == str(event.pk)
|
||||
|
||||
for path in ("/api/analytics/admin/events", "/api/analytics/admin/blog", "/api/analytics/admin/dashboard"):
|
||||
response = client.get(path, **headers)
|
||||
assert response.status_code == 200, (path, response.content)
|
||||
assert response.json()
|
||||
|
||||
|
||||
def test_analytics_rejects_invalid_date_range(client, user_factory, auth_headers):
|
||||
staff = user_factory(is_staff=True)
|
||||
response = client.get(
|
||||
"/api/analytics/admin/users",
|
||||
{"date_from": "2026-12-31", "date_to": "2026-01-01"},
|
||||
**auth_headers(staff),
|
||||
)
|
||||
assert response.status_code == 400
|
||||
55
apps/analytics/tests/unit/test_helpers.py
Normal file
55
apps/analytics/tests/unit/test_helpers.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
from ninja.errors import HttpError
|
||||
|
||||
from apps.analytics.api.views import (
|
||||
_auto_granularity,
|
||||
_filters_payload,
|
||||
_normalize_entry_year,
|
||||
_parse_boundary,
|
||||
_point_group_from_rows,
|
||||
_require_staff,
|
||||
)
|
||||
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
[(None, "نامشخص"), ("۱۴۰۴", "1404"), ("404", "1404"), ("94", "1394"), ("bad", "نامشخص")],
|
||||
)
|
||||
def test_year_normalization(value, expected):
|
||||
assert _normalize_entry_year(value) == expected
|
||||
|
||||
|
||||
def test_date_filters_validate_and_choose_granularity():
|
||||
assert _parse_boundary(None) is None
|
||||
with pytest.raises(HttpError):
|
||||
_parse_boundary("not-a-date")
|
||||
start = _parse_boundary("2026-01-01")
|
||||
end = _parse_boundary("2026-01-31", is_end=True)
|
||||
assert start < end
|
||||
assert _auto_granularity(start, end) == "day"
|
||||
assert _auto_granularity(start, start + timedelta(days=90)) == "week"
|
||||
assert _auto_granularity(None, end) == "month"
|
||||
assert _filters_payload(start, end, include_granularity=True)["granularity"] == "day"
|
||||
|
||||
|
||||
def test_point_group_sorts_and_counts_remainder():
|
||||
result = _point_group_from_rows(
|
||||
[{"label": "B", "value": 1}, {"label": "A", "value": 3}, {"label": None, "value": 0}],
|
||||
limit=1,
|
||||
)
|
||||
assert result["top_items"] == [{"label": "A", "value": 3}]
|
||||
assert result["other_count"] == 2
|
||||
assert result["total_count"] == 3
|
||||
|
||||
|
||||
def test_staff_guard_rejects_anonymous_and_nonstaff():
|
||||
with pytest.raises(HttpError):
|
||||
_require_staff(None)
|
||||
with pytest.raises(HttpError):
|
||||
_require_staff(type("User", (), {"is_staff": False, "is_superuser": False})())
|
||||
_require_staff(type("User", (), {"is_staff": True, "is_superuser": False})())
|
||||
Reference in New Issue
Block a user