Files
chunyu_project/article/tests/test_following_feed.py
T
chunyu 37cad3947d feat(C-06):关注feed+课程聚合修复
文章列表新增author_user_id/is_following与feed=following过滤;课程students_count改真实在学人数去重统计、新增total_minutes(不可解析不伪造);author_name走sync_to_async防ASGI同步阻塞。
2026-09-15 15:20:23 +08:00

70 lines
3.2 KiB
Python

"""C-06 关注 feed 回归:关注/未关注/互关/取关/匿名/分页对齐。"""
from django.test import TestCase
from rest_framework.test import APIClient
from article.models import Article
from user.models import Follow, FUser
class FollowingFeedTest(TestCase):
def setUp(self):
self.client = APIClient()
self.a = FUser.objects.create_user(username='c06fa', password='x12345678')
self.b = FUser.objects.create_user(username='c06fb', password='x12345678')
self.c = FUser.objects.create_user(username='c06fc', password='x12345678')
self.art_b = Article.objects.create(title='B文', content='x' * 500, author=self.b, status='published')
self.art_c = Article.objects.create(title='C文', content='y' * 500, author=self.c, status='published')
def test_following_only_shows_followed(self):
Follow.objects.create(follower=self.a, following=self.b)
self.client.force_authenticate(user=self.a)
r = self.client.get('/article/articles/', {'feed': 'following'})
self.assertEqual(r.status_code, 200)
titles = [x['title'] for x in r.json()['data']['results']]
self.assertEqual(titles, ['B文'])
def test_unfollowed_empty_state(self):
self.client.force_authenticate(user=self.a)
r = self.client.get('/article/articles/', {'feed': 'following'})
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json()['data']['count'], 0)
self.assertEqual(r.json()['data']['results'], [])
def test_mutual_follow_both_visible(self):
Follow.objects.create(follower=self.a, following=self.b)
Follow.objects.create(follower=self.a, following=self.c)
self.client.force_authenticate(user=self.a)
r = self.client.get('/article/articles/', {'feed': 'following'})
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json()['data']['count'], 2)
def test_unfollow_removes_from_feed(self):
f = Follow.objects.create(follower=self.a, following=self.b)
f.delete()
self.client.force_authenticate(user=self.a)
r = self.client.get('/article/articles/', {'feed': 'following'})
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json()['data']['count'], 0)
def test_anonymous_following_empty(self):
Follow.objects.create(follower=self.a, following=self.b)
r = self.client.get('/article/articles/', {'feed': 'following'})
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json()['data']['count'], 0)
def test_normal_list_unaffected(self):
Follow.objects.create(follower=self.a, following=self.b)
self.client.force_authenticate(user=self.a)
r = self.client.get('/article/articles/')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json()['data']['count'], 2)
def test_draft_not_in_feed(self):
Follow.objects.create(follower=self.a, following=self.b)
self.art_b.status = 'draft'
self.art_b.save(update_fields=['status'])
self.client.force_authenticate(user=self.a)
r = self.client.get('/article/articles/', {'feed': 'following'})
self.assertEqual(r.status_code, 200)
self.assertEqual(r.json()['data']['count'], 0)