rsnarsna
feat(pricing): unlock all platform features on Free plan and synchronize 100 DAG execution quota
a3c061f Download tests/test_forums.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 7.95 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_forums.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/tests/test_forums.py
-
curl -L -o test_forums.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/tests/test_forums.py
7.95 kB
| """ | |
| Unit & Integration Tests for ALLI Community Forums Domain | |
| Verifies: | |
| - Seeding and listing discussion topics | |
| - Creating new topics with categories and tags | |
| - View count increment upon inspection | |
| - Posting replies and atomic reply count updates | |
| - Author edit/delete authorization vs unauthorized rejection (HTTP 403) | |
| - Admin moderation (pinning, locking) | |
| - Enforcement that locked topics reject new replies | |
| - Cascade deletion of replies upon topic removal | |
| - Search and category taxonomy filters | |
| """ | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from main import app | |
| client = TestClient(app) | |
| def test_list_topics_and_default_seeds(): | |
| """Verify listing topics returns discussion topics.""" | |
| # Ensure at least one topic exists to verify list formatting | |
| client.post( | |
| "/api/forums/topics", | |
| json={ | |
| "title": "Welcome to ALLI Forums", | |
| "content": "Official discussion board for ALLI visual programming.", | |
| "category": "announcements", | |
| "tags": ["welcome", "announcements"], | |
| }, | |
| headers={"X-User-Id": "admin_alli", "X-User-Name": "ALLI Admin"} | |
| ) | |
| response = client.get("/api/forums/topics") | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "topics" in data | |
| assert data["total"] >= 1 | |
| def test_create_topic_success(): | |
| """Verify user can create a new discussion topic.""" | |
| payload = { | |
| "title": "How to optimize Python loop nodes?", | |
| "content": "I am designing a loop over 10,000 records. Is it faster to use the primitive for-loop node or synthesize a list comprehension?", | |
| "category": "help", | |
| "tags": ["python", "loops", "performance"], | |
| } | |
| headers = {"X-User-Id": "dev_alex", "X-User-Name": "AlexDeveloper"} | |
| response = client.post("/api/forums/topics", json=payload, headers=headers) | |
| assert response.status_code == 201 | |
| topic = response.json() | |
| assert topic["id"].startswith("topic_") | |
| assert topic["title"] == payload["title"] | |
| assert topic["author_id"] == "dev_alex" | |
| assert topic["author_name"] == "AlexDeveloper" | |
| assert topic["category"] == "help" | |
| assert "loops" in topic["tags"] | |
| assert topic["views_count"] == 0 | |
| assert topic["replies_count"] == 0 | |
| def test_get_topic_detail_and_view_increment(): | |
| """Verify retrieving topic detail increments view count and returns threaded replies.""" | |
| # Create topic | |
| create_res = client.post( | |
| "/api/forums/topics", | |
| json={"title": "View Count Target", "content": "Checking view count incrementation."}, | |
| headers={"X-User-Id": "dev_viewer"} | |
| ) | |
| topic_id = create_res.json()["id"] | |
| # First fetch | |
| res1 = client.get(f"/api/forums/topics/{topic_id}") | |
| assert res1.status_code == 200 | |
| detail1 = res1.json() | |
| assert detail1["topic"]["views_count"] >= 1 | |
| assert isinstance(detail1["replies"], list) | |
| assert len(detail1["replies"]) == 0 | |
| # Second fetch | |
| res2 = client.get(f"/api/forums/topics/{topic_id}") | |
| assert res2.status_code == 200 | |
| assert res2.json()["topic"]["views_count"] > detail1["topic"]["views_count"] | |
| def test_create_reply_and_atomic_count(): | |
| """Verify creating replies updates the topic's reply count and timestamp.""" | |
| # Create topic | |
| t_res = client.post( | |
| "/api/forums/topics", | |
| json={"title": "Reply Target Topic", "content": "Leave your thoughts below."}, | |
| headers={"X-User-Id": "topic_author"} | |
| ) | |
| topic_id = t_res.json()["id"] | |
| # Post reply | |
| reply_payload = { | |
| "content": "Here is a clean solution: use the Map/Transform primitive node to bypass the standard for-loop overhead.", | |
| } | |
| r_res = client.post( | |
| f"/api/forums/topics/{topic_id}/replies", | |
| json=reply_payload, | |
| headers={"X-User-Id": "responder_sam", "X-User-Name": "SamEngineer"} | |
| ) | |
| assert r_res.status_code == 201 | |
| reply = r_res.json() | |
| assert reply["id"].startswith("rep_") | |
| assert reply["author_id"] == "responder_sam" | |
| assert reply["topic_id"] == topic_id | |
| # Check topic detail | |
| detail = client.get(f"/api/forums/topics/{topic_id}").json() | |
| assert detail["topic"]["replies_count"] == 1 | |
| assert detail["topic"]["last_reply_by"] == "SamEngineer" | |
| assert len(detail["replies"]) == 1 | |
| assert detail["replies"][0]["id"] == reply["id"] | |
| def test_topic_update_authorization(): | |
| """Verify only the topic author or an admin can update topic content.""" | |
| t_res = client.post( | |
| "/api/forums/topics", | |
| json={"title": "Original Title", "content": "Original Content here."}, | |
| headers={"X-User-Id": "user_owner"} | |
| ) | |
| topic_id = t_res.json()["id"] | |
| # Unauthorized user attempt -> 403 | |
| unauth_res = client.patch( | |
| f"/api/forums/topics/{topic_id}", | |
| json={"title": "Hacked Title"}, | |
| headers={"X-User-Id": "attacker_user"} | |
| ) | |
| assert unauth_res.status_code == 403 | |
| # Authorized owner update -> 200 | |
| auth_res = client.patch( | |
| f"/api/forums/topics/{topic_id}", | |
| json={"title": "Updated Title By Owner"}, | |
| headers={"X-User-Id": "user_owner"} | |
| ) | |
| assert auth_res.status_code == 200 | |
| assert auth_res.json()["title"] == "Updated Title By Owner" | |
| def test_admin_pin_and_lock_moderation(): | |
| """Verify admin can pin and lock a topic, and locked topics block further replies.""" | |
| t_res = client.post( | |
| "/api/forums/topics", | |
| json={"title": "Lockable Announcement", "content": "Important announcement body."}, | |
| headers={"X-User-Id": "member_user"} | |
| ) | |
| topic_id = t_res.json()["id"] | |
| # Non-admin attempting to pin/lock -> 403 | |
| unauth_pin = client.patch( | |
| f"/api/forums/topics/{topic_id}", | |
| json={"is_pinned": True, "is_locked": True}, | |
| headers={"X-User-Id": "member_user"} | |
| ) | |
| assert unauth_pin.status_code == 403 | |
| # Admin pins and locks topic | |
| admin_headers = {"X-User-Id": "admin_mod", "X-Admin-Role": "true"} | |
| admin_lock = client.patch( | |
| f"/api/forums/topics/{topic_id}", | |
| json={"is_pinned": True, "is_locked": True}, | |
| headers=admin_headers | |
| ) | |
| assert admin_lock.status_code == 200 | |
| assert admin_lock.json()["is_pinned"] is True | |
| assert admin_lock.json()["is_locked"] is True | |
| # Regular user attempting to reply to locked topic -> 400 | |
| blocked_reply = client.post( | |
| f"/api/forums/topics/{topic_id}/replies", | |
| json={"content": "Can I still reply?"}, | |
| headers={"X-User-Id": "regular_user"} | |
| ) | |
| assert blocked_reply.status_code == 400 | |
| assert "locked by a moderator" in blocked_reply.json()["detail"] | |
| def test_delete_reply_and_cascade_topic(): | |
| """Verify reply deletion decrements reply count, and topic deletion cascades.""" | |
| t_res = client.post( | |
| "/api/forums/topics", | |
| json={"title": "Cascade Test Topic", "content": "Will be deleted with replies."}, | |
| headers={"X-User-Id": "author_cascade"} | |
| ) | |
| topic_id = t_res.json()["id"] | |
| r1 = client.post( | |
| f"/api/forums/topics/{topic_id}/replies", | |
| json={"content": "Reply 1"}, | |
| headers={"X-User-Id": "author_cascade"} | |
| ).json() | |
| r2 = client.post( | |
| f"/api/forums/topics/{topic_id}/replies", | |
| json={"content": "Reply 2"}, | |
| headers={"X-User-Id": "author_cascade"} | |
| ).json() | |
| # Delete reply 1 | |
| del_rep_res = client.delete(f"/api/forums/replies/{r1['id']}", headers={"X-User-Id": "author_cascade"}) | |
| assert del_rep_res.status_code == 200 | |
| # Verify count decremented | |
| detail = client.get(f"/api/forums/topics/{topic_id}").json() | |
| assert detail["topic"]["replies_count"] == 1 | |
| # Delete whole topic | |
| del_t_res = client.delete(f"/api/forums/topics/{topic_id}", headers={"X-User-Id": "author_cascade"}) | |
| assert del_t_res.status_code == 200 | |
| # Ensure topic no longer found | |
| assert client.get(f"/api/forums/topics/{topic_id}").status_code == 404 | |