|
|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
#zyl:
|
|
|
|
|
from django.test import Client, RequestFactory, TransactionTestCase
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
|
|
@ -11,99 +12,102 @@ from djangoblog.utils import get_max_articleid_commentid
|
|
|
|
|
# Create your tests here.
|
|
|
|
|
|
|
|
|
|
class CommentsTest(TransactionTestCase):
|
|
|
|
|
"""
|
|
|
|
|
评论功能测试类
|
|
|
|
|
|
|
|
|
|
使用 TransactionTestCase 而非 TestCase,因为:
|
|
|
|
|
- 测试涉及复杂的数据库操作(嵌套评论、信号触发等)
|
|
|
|
|
- TransactionTestCase 在每个测试方法后回滚事务,确保测试隔离性
|
|
|
|
|
- 需要测试实际的数据库事务行为(如外键级联删除)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
"""
|
|
|
|
|
测试前的初始化设置,每个测试方法执行前都会运行
|
|
|
|
|
创建测试所需的客户端、用户、和博客配置
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# 创建测试客户端,用于模拟 HTTP 请求
|
|
|
|
|
self.client = Client()
|
|
|
|
|
|
|
|
|
|
# 创建请求工厂,用于生成请求对象(测试中未实际使用)
|
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
|
|
|
|
|
# 配置博客设置:开启评论审核功能
|
|
|
|
|
# 这意味着新评论默认 is_enable=False,需要手动审核
|
|
|
|
|
from blog.models import BlogSettings
|
|
|
|
|
value = BlogSettings()
|
|
|
|
|
value.comment_need_review = True
|
|
|
|
|
value.save()
|
|
|
|
|
|
|
|
|
|
# 创建超级用户用于登录测试
|
|
|
|
|
# 使用明确的邮箱、用户名和密码,确保可预测性
|
|
|
|
|
self.user = BlogUser.objects.create_superuser(
|
|
|
|
|
email="liangliangyy1@gmail.com",
|
|
|
|
|
username="liangliangyy1",
|
|
|
|
|
password="liangliangyy1")
|
|
|
|
|
|
|
|
|
|
def update_article_comment_status(self, article):
|
|
|
|
|
"""
|
|
|
|
|
辅助方法:批量启用文章下的所有评论
|
|
|
|
|
模拟管理员审核通过评论的操作
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
article: Article 对象,要更新评论的文章
|
|
|
|
|
"""
|
|
|
|
|
# 获取文章的所有评论
|
|
|
|
|
comments = article.comment_set.all()
|
|
|
|
|
|
|
|
|
|
# 遍历并启用每个评论
|
|
|
|
|
for comment in comments:
|
|
|
|
|
comment.is_enable = True
|
|
|
|
|
comment.save()
|
|
|
|
|
|
|
|
|
|
def test_validate_comment(self):
|
|
|
|
|
"""
|
|
|
|
|
核心测试方法:验证评论功能的完整流程
|
|
|
|
|
|
|
|
|
|
测试场景包括:
|
|
|
|
|
1. 发布主评论(待审核状态)
|
|
|
|
|
2. 审核后评论可见
|
|
|
|
|
3. 发布多个评论
|
|
|
|
|
4. 发布嵌套回复评论(带 Markdown 格式)
|
|
|
|
|
5. 测试模板标签功能
|
|
|
|
|
6. 测试工具函数
|
|
|
|
|
7. 测试邮件通知功能
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# 1. 登录测试用户
|
|
|
|
|
self.client.login(username='liangliangyy1', password='liangliangyy1')
|
|
|
|
|
|
|
|
|
|
# 2. 创建测试分类
|
|
|
|
|
category = Category()
|
|
|
|
|
category.name = "categoryccc"
|
|
|
|
|
category.save()
|
|
|
|
|
|
|
|
|
|
# 3. 创建测试文章
|
|
|
|
|
article = Article()
|
|
|
|
|
article.title = "nicetitleccc"
|
|
|
|
|
article.body = "nicecontentccc"
|
|
|
|
|
article.author = self.user
|
|
|
|
|
article.category = category
|
|
|
|
|
article.type = 'a'
|
|
|
|
|
article.status = 'p'
|
|
|
|
|
article.type = 'a' # 文章类型:a 表示普通文章
|
|
|
|
|
article.status = 'p' # 发布状态:p 表示已发布
|
|
|
|
|
article.save()
|
|
|
|
|
|
|
|
|
|
# 4. 获取发布评论的 URL
|
|
|
|
|
# 使用命名空间 'comments:postcomment' 和文章 ID 作为参数
|
|
|
|
|
comment_url = reverse(
|
|
|
|
|
'comments:postcomment', kwargs={
|
|
|
|
|
'article_id': article.id})
|
|
|
|
|
|
|
|
|
|
# 5. 测试发布第一条评论(待审核状态)
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff'
|
|
|
|
|
'body': '123ffffffffff' # 满足最小长度要求
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# 验证:评论提交后应重定向(302)回文章详情页
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
|
|
|
|
article = Article.objects.get(pk=article.pk)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 0)
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 1)
|
|
|
|
|
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
|
|
|
|
article = Article.objects.get(pk=article.pk)
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 2)
|
|
|
|
|
parent_comment_id = article.comment_list()[0].id
|
|
|
|
|
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '''
|
|
|
|
|
# Title1
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
import os
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[url](https://www.lylinux.net/)
|
|
|
|
|
|
|
|
|
|
[ddd](http://www.baidu.com)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
''',
|
|
|
|
|
'parent_comment_id': parent_comment_id
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
article = Article.objects.get(pk=article.pk)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 3)
|
|
|
|
|
comment = Comment.objects.get(id=parent_comment_id)
|
|
|
|
|
tree = parse_commenttree(article.comment_list(), comment)
|
|
|
|
|
self.assertEqual(len(tree), 1)
|
|
|
|
|
data = show_comment_item(comment, True)
|
|
|
|
|
self.assertIsNotNone(data)
|
|
|
|
|
s = get_max_articleid_commentid()
|
|
|
|
|
self.assertIsNotNone(s)
|
|
|
|
|
|
|
|
|
|
from comments.utils import send_comment_email
|
|
|
|
|
send_comment_email(comment)
|
|
|
|
|
# 验证:由于需要审核,评论列表应为空(is
|