Files
python-tdd/src/apps/api/tests/integrated/test_views.py

116 lines
3.9 KiB
Python
Raw Normal View History

from django.test import TestCase
from rest_framework.test import APIClient
brief sprint C1: relocate Post + Line from dashboard → billboard (no behavior change) — TDD The Post/Line models always read more like billboard tenants than dashboard ones (1st-person personal vs. 2nd-person provenance feed); the upcoming Brief model needs them in the billboard namespace as the canonical surface they FK into. C1 is a pure relocation w. zero new behavior: 789 ITs + 20 sky/Post FTs green against the moved code. - billboard.models adds Post (owner + shared_with) + Line (text + post FK), schema mirroring the legacy dashboard models 1:1; Post.get_absolute_url now reverses to `billboard:view_post`. - billboard.forms adds LineForm + ExistingPostLineForm (moved from dashboard.forms; dashboard/forms.py removed). - billboard.views absorbs new_post / view_post / share_post / my_posts (templates rendered from apps/billboard/post.html + my_posts.html). - billboard.urls adds the namespaced routes: /billboard/new-post, /billboard/post/<uuid>/, /billboard/post/<uuid>/share-post, /billboard/users/<uuid>/. dashboard.urls drops the corresponding entries. - _applet-my-posts + _applet-new-post URL refs now use the billboard: namespace; templates/apps/dashboard/{post,my_posts}.html removed. - api/serializers + api/views + api/tests/integrated/test_views imports flip dashboard.models → billboard.models (PostSerializer / PostDetailAPI / PostLinesAPI / PostsAPI all retain identifiers — the model rename to Brief lands in C2). - dashboard/tests/integrated/test_{models,views,forms} + dashboard/tests/unit/test_{models,forms} swap imports; test_views URL strings flip /dashboard/post/ → /billboard/post/, /dashboard/new_post → /billboard/new-post, /dashboard/users/ → /billboard/users/, share_post → share-post (path) / billboard:share_post (reverser). Tests stay in dashboard.tests/ for now — relocation TBD. - functional_tests/my_posts_page.py URL string flips to /billboard/users/. - Auto-generated migrations: billboard/0001_initial (CreateModel Post + Line), dashboard/0003_remove_post_* (drops legacy Post + Line), drama/0004_alter_gameevent_verb (incidental — choices field caught up). This commit drops the dashboard Post/Line tables w/o data preservation; user has confirmed staging-side wipe is acceptable. C2 introduces the Brief model + read-tracking + slide-down banner unification. C3 hooks Note-unlock + share-post-invite + magic-link / invalid-link `messages` calls into the new Brief / banner pipeline. Code architected by Disco DeDisco <discodedisco@outlook.com> Git commit message Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-08 17:20:06 -04:00
from apps.billboard.models import Line, Post
from apps.lyric.models import User
class BaseAPITest(TestCase):
# Helper fns
def setUp(self):
self.client = APIClient()
self.user = User.objects.create_user("test@example.com")
self.client.force_authenticate(user=self.user)
class PostDetailAPITest(BaseAPITest):
def test_returns_post_with_lines(self):
post = Post.objects.create(owner=self.user)
Line.objects.create(text="line 1", post=post)
Line.objects.create(text="line 2", post=post)
response = self.client.get(f"/api/posts/{post.id}/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["id"], str(post.id))
self.assertEqual(len(response.data["lines"]), 2)
class PostLinesAPITest(BaseAPITest):
def test_can_add_line_to_post(self):
post = Post.objects.create(owner=self.user)
response = self.client.post(
f"/api/posts/{post.id}/lines/",
{"text": "a new line"},
)
self.assertEqual(response.status_code, 201)
self.assertEqual(Line.objects.count(), 1)
self.assertEqual(Line.objects.first().text, "a new line")
def test_cannot_add_empty_line_to_post(self):
post = Post.objects.create(owner=self.user)
response = self.client.post(
f"/api/posts/{post.id}/lines/",
{"text": ""},
)
self.assertEqual(response.status_code, 400)
self.assertEqual(Line.objects.count(), 0)
def test_cannot_add_duplicate_line_to_post(self):
post = Post.objects.create(owner=self.user)
Line.objects.create(text="post line", post=post)
duplicate_response = self.client.post(
f"/api/posts/{post.id}/lines/",
{"text": "post line"},
)
self.assertEqual(duplicate_response.status_code, 400)
self.assertEqual(Line.objects.count(), 1)
class PostsAPITest(BaseAPITest):
def test_get_returns_only_users_posts(self):
post1 = Post.objects.create(owner=self.user)
Line.objects.create(text="line 1", post=post1)
other_user = User.objects.create_user("other@example.com")
Post.objects.create(owner=other_user)
response = self.client.get("/api/posts/")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["id"], str(post1.id))
def test_post_creates_post_with_line(self):
response = self.client.post(
"/api/posts/",
{"text": "first line"},
)
self.assertEqual(response.status_code, 201)
self.assertEqual(Post.objects.count(), 1)
self.assertEqual(Post.objects.first().owner, self.user)
self.assertEqual(Line.objects.first().text, "first line")
class UserSearchAPITest(BaseAPITest):
def test_returns_users_matching_username(self):
disco = User.objects.create_user("disco@example.com")
disco.username = "discoman"
disco.searchable = True
disco.save()
response = self.client.get("/api/users/?q=disc")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]["username"], "discoman")
def test_non_searchable_users_are_excluded(self):
alice = User.objects.create_user("alice@example.com")
alice.username = "princessAli"
alice.save() # searchable defaults to False
response = self.client.get("/api/users/?q=prin")
self.assertEqual(response.data, [])
def test_response_does_not_include_email(self):
alice = User.objects.create_user("alice@example.com")
alice.username = "princessAli"
alice.searchable = True
alice.save()
response = self.client.get("/api/users/?q=prin")
self.assertNotIn("email", response.data[0])