|
|
|
|
@ -1,109 +1,104 @@
|
|
|
|
|
# 导入Django测试相关工具
|
|
|
|
|
from django.test import Client, RequestFactory, TransactionTestCase
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from django.urls import reverse # 用于反向解析URL
|
|
|
|
|
|
|
|
|
|
from accounts.models import BlogUser
|
|
|
|
|
from blog.models import Category, Article
|
|
|
|
|
from comments.models import Comment
|
|
|
|
|
from comments.templatetags.comments_tags import *
|
|
|
|
|
from djangoblog.utils import get_max_articleid_commentid
|
|
|
|
|
# 导入相关模型和工具
|
|
|
|
|
from accounts.models import BlogUser # 用户模型
|
|
|
|
|
from blog.models import Category, Article # 博客分类和文章模型
|
|
|
|
|
from comments.models import Comment # 评论模型
|
|
|
|
|
from comments.templatetags.comments_tags import * # 评论相关的模板标签
|
|
|
|
|
from djangoblog.utils import get_max_articleid_commentid # 工具函数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create your tests here.
|
|
|
|
|
|
|
|
|
|
# 创建测试类,继承TransactionTestCase(支持事务的测试类,适合涉及数据库事务的测试)
|
|
|
|
|
class CommentsTest(TransactionTestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.client = Client()
|
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
"""测试前的初始化设置,每个测试方法执行前都会调用"""
|
|
|
|
|
self.client = Client() # 创建测试客户端,用于模拟用户请求
|
|
|
|
|
self.factory = RequestFactory() # 创建请求工厂,用于构造请求对象
|
|
|
|
|
|
|
|
|
|
# 配置博客设置(评论需要审核)
|
|
|
|
|
from blog.models import BlogSettings
|
|
|
|
|
value = BlogSettings()
|
|
|
|
|
value.comment_need_review = True
|
|
|
|
|
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):
|
|
|
|
|
comments = article.comment_set.all()
|
|
|
|
|
"""辅助方法:更新文章所有评论为启用状态(模拟审核通过)"""
|
|
|
|
|
comments = article.comment_set.all() # 获取文章的所有评论
|
|
|
|
|
for comment in comments:
|
|
|
|
|
comment.is_enable = True
|
|
|
|
|
comment.save()
|
|
|
|
|
comment.is_enable = True # 设为启用
|
|
|
|
|
comment.save() # 保存更改
|
|
|
|
|
|
|
|
|
|
def test_validate_comment(self):
|
|
|
|
|
"""测试评论功能的核心逻辑:发布评论、回复评论、评论显示等"""
|
|
|
|
|
# 登录测试用户
|
|
|
|
|
self.client.login(username='liangliangyy1', password='liangliangyy1')
|
|
|
|
|
|
|
|
|
|
# 创建测试分类
|
|
|
|
|
category = Category()
|
|
|
|
|
category.name = "categoryccc"
|
|
|
|
|
category.save()
|
|
|
|
|
|
|
|
|
|
# 创建测试文章
|
|
|
|
|
article = Article()
|
|
|
|
|
article.title = "nicetitleccc"
|
|
|
|
|
article.body = "nicecontentccc"
|
|
|
|
|
article.author = self.user
|
|
|
|
|
article.category = category
|
|
|
|
|
article.type = 'a'
|
|
|
|
|
article.status = 'p'
|
|
|
|
|
article.author = self.user # 关联作者
|
|
|
|
|
article.category = category # 关联分类
|
|
|
|
|
article.type = 'a' # 文章类型(假设'a'表示普通文章)
|
|
|
|
|
article.status = 'p' # 发布状态(假设'p'表示已发布)
|
|
|
|
|
article.save()
|
|
|
|
|
|
|
|
|
|
# 获取评论提交的URL(反向解析评论提交视图)
|
|
|
|
|
comment_url = reverse(
|
|
|
|
|
'comments:postcomment', kwargs={
|
|
|
|
|
'article_id': article.id})
|
|
|
|
|
'article_id': article.id}) # 传入文章ID参数
|
|
|
|
|
|
|
|
|
|
# 测试发布第一条评论
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff'
|
|
|
|
|
'body': '123ffffffffff' # 评论内容
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# 验证评论提交后是否重定向(通常评论成功后会跳转到文章页)
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
|
|
|
|
# 重新获取文章对象(从数据库刷新)
|
|
|
|
|
article = Article.objects.get(pk=article.pk)
|
|
|
|
|
# 因为评论需要审核(初始is_enable=False),所以评论列表长度应为0
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 0)
|
|
|
|
|
# 调用辅助方法,将评论设为启用(模拟审核通过)
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
|
|
|
|
|
# 此时评论列表应包含1条评论
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 1)
|
|
|
|
|
|
|
|
|
|
# 测试发布第二条评论
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 2) # 验证评论数量
|
|
|
|
|
|
|
|
|
|
# 测试回复评论功能
|
|
|
|
|
# 获取第一条评论的ID作为父评论ID
|
|
|
|
|
parent_comment_id = article.comment_list()[0].id
|
|
|
|
|
|
|
|
|
|
# 发布带Markdown格式的回复(测试富文本内容)
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '''
|
|
|
|
|
# Title1
|
|
|
|
|
# Title1 (Markdown标题)
|
|
|
|
|
|
|
|
|
|
```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)
|
|
|
|
|
import os # Markdown代码块
|