Files
guilan-ace-backend/apps/users/tests/integration/test_meta.py
Amirhossein Khalili ff99130938 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.
2026-09-26 16:50:29 +03:30

43 lines
2.6 KiB
Python

import json
import pytest
from apps.users.models import Major, University
pytestmark = [pytest.mark.integration, pytest.mark.django_db]
@pytest.mark.parametrize("name,model", [("majors", Major), ("universities", University)])
def test_meta_public_search_and_admin_lifecycle(client, user_factory, auth_headers, name, model):
staff = user_factory(is_staff=True)
admin = user_factory(is_staff=True, is_superuser=True)
member = user_factory()
path = f"/api/meta/{name}"
admin_path = f"/api/meta/admin/{name}"
assert client.get(path).status_code == 200
assert client.post(admin_path, data=json.dumps({"code": "TEST_META_CX", "name": "Unique Meta Choice"}),
content_type="application/json", **auth_headers(member)).status_code == 403
created = client.post(admin_path, data=json.dumps({"code": "TEST_META_CX", "name": "Unique Meta Choice"}),
content_type="application/json", **auth_headers(staff))
assert created.status_code == 201, created.content
item = model.objects.get(pk=created.json()["id"])
assert client.get(path, {"search": "Unique Meta Choice"}).json()["results"][0]["id"] == item.pk
assert client.get(path, {"search": "missing"}).json()["count"] == 0
assert any(row["id"] == item.pk for row in client.get(admin_path, {"search": "Unique Meta Choice"}, **auth_headers(staff)).json()["results"])
duplicate = client.post(admin_path, data=json.dumps({"code": "TEST_META_CX", "name": "Duplicate"}),
content_type="application/json", **auth_headers(staff))
assert duplicate.status_code == 400
updated = client.put(f"{admin_path}/{item.pk}", data=json.dumps({"code": "TEST_META_CX", "name": "Updated Unique Meta Choice", "is_active": False}),
content_type="application/json", **auth_headers(staff))
assert updated.status_code == 200, updated.content
item.refresh_from_db()
assert item.name == "Updated Unique Meta Choice" and not item.is_active
assert client.get(path, {"search": "TEST_META_CX"}).json()["results"] == []
assert client.get(admin_path, {"search": "TEST_META_CX"}, **auth_headers(staff)).json()["results"][0]["id"] == item.pk
assert client.delete(f"{admin_path}/{item.pk}", **auth_headers(staff)).status_code == 403
assert client.delete(f"{admin_path}/{item.pk}", **auth_headers(admin)).status_code == 200
assert model.deleted_objects.filter(pk=item.pk).exists()
assert client.post(f"{admin_path}/{item.pk}/restore", **auth_headers(admin)).status_code == 200
assert model.objects.filter(pk=item.pk).exists()