File size: 7,954 Bytes
6818eaf a3c061f 6818eaf a3c061f 6818eaf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | """
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
|