|
| 1 | +import pytest |
| 2 | +from init import db |
| 3 | +from modules.contenttype.models import ContentType, ContentItem |
| 4 | +import os |
| 5 | + |
| 6 | +# We use fixtures from conftest.py: test_client, db, db_session |
| 7 | + |
| 8 | +def test_get_types_empty(test_client): |
| 9 | + response = test_client.get("/api/v1/types") |
| 10 | + assert response.status_code == 200 |
| 11 | + # Might not be empty if other tests or initialization added types |
| 12 | + # but in a clean testing.db it should be [] if we didn't add any in db fixture |
| 13 | + assert isinstance(response.json, list) |
| 14 | + |
| 15 | +def test_get_content_not_found(test_client): |
| 16 | + response = test_client.get("/api/v1/nonexistent") |
| 17 | + assert response.status_code == 404 |
| 18 | + assert "error" in response.json |
| 19 | + |
| 20 | +def test_api_auth(test_client, monkeypatch): |
| 21 | + # Set API_TOKEN for testing auth |
| 22 | + monkeypatch.setenv("API_TOKEN", "test-token") |
| 23 | + |
| 24 | + # Unauthorized request (get_content checks auth) |
| 25 | + response = test_client.get("/api/v1/blog") |
| 26 | + assert response.status_code == 401 |
| 27 | + |
| 28 | + # Authorized request |
| 29 | + headers = {"Authorization": "Bearer test-token"} |
| 30 | + response = test_client.get("/api/v1/blog", headers=headers) |
| 31 | + assert response.status_code == 404 # Content type not found, but not 401 |
| 32 | + |
| 33 | +def test_get_content_with_data(test_client): |
| 34 | + # Create content type and items |
| 35 | + ct = ContentType(name="blog", schema=[{"name": "title", "type": "text"}]) |
| 36 | + db.session.add(ct) |
| 37 | + db.session.commit() |
| 38 | + |
| 39 | + item1 = ContentItem(content_type_id=ct.id, data={"title": "First"}) |
| 40 | + item2 = ContentItem(content_type_id=ct.id, data={"title": "Second"}) |
| 41 | + db.session.add_all([item1, item2]) |
| 42 | + db.session.commit() |
| 43 | + |
| 44 | + response = test_client.get("/api/v1/blog") |
| 45 | + assert response.status_code == 200 |
| 46 | + assert response.json["meta"]["total"] == 2 |
| 47 | + assert len(response.json["data"]) == 2 |
| 48 | + # Default order is desc by created_at, item2 was added last |
| 49 | + assert response.json["data"][0]["content"]["title"] == "Second" |
| 50 | + |
| 51 | +def test_api_pagination(test_client): |
| 52 | + ct = ContentType(name="posts", schema=[{"name": "title", "type": "text"}]) |
| 53 | + db.session.add(ct) |
| 54 | + db.session.commit() |
| 55 | + |
| 56 | + for i in range(5): |
| 57 | + db.session.add(ContentItem(content_type_id=ct.id, data={"title": str(i)})) |
| 58 | + db.session.commit() |
| 59 | + |
| 60 | + response = test_client.get("/api/v1/posts?limit=2&offset=0") |
| 61 | + assert response.status_code == 200 |
| 62 | + assert response.json["meta"]["limit"] == 2 |
| 63 | + assert len(response.json["data"]) == 2 |
| 64 | + |
| 65 | + response = test_client.get("/api/v1/posts?limit=2&offset=4") |
| 66 | + assert len(response.json["data"]) == 1 |
| 67 | + |
| 68 | +def test_api_sorting(test_client): |
| 69 | + ct = ContentType(name="news", schema=[{"name": "title", "type": "text"}]) |
| 70 | + db.session.add(ct) |
| 71 | + db.session.commit() |
| 72 | + |
| 73 | + item1 = ContentItem(content_type_id=ct.id, data={"title": "A"}) |
| 74 | + item2 = ContentItem(content_type_id=ct.id, data={"title": "B"}) |
| 75 | + db.session.add_all([item1, item2]) |
| 76 | + db.session.commit() |
| 77 | + |
| 78 | + response = test_client.get("/api/v1/news?sort_by=id&order=asc") |
| 79 | + assert response.json["data"][0]["content"]["title"] == "A" |
| 80 | + |
| 81 | + response = test_client.get("/api/v1/news?sort_by=id&order=desc") |
| 82 | + assert response.json["data"][0]["content"]["title"] == "B" |
0 commit comments