|
|
|
|
@ -8,61 +8,78 @@ 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()
|
|
|
|
|
|
|
|
|
|
# 配置博客评论设置(需要审核才能显示)
|
|
|
|
|
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()
|
|
|
|
|
for comment in comments:
|
|
|
|
|
comment.is_enable = True
|
|
|
|
|
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.type = 'a' # 假设'a'表示文章类型
|
|
|
|
|
article.status = 'p' # 假设'p'表示已发布
|
|
|
|
|
article.save()
|
|
|
|
|
|
|
|
|
|
# 获取评论提交的URL
|
|
|
|
|
comment_url = reverse(
|
|
|
|
|
'comments:postcomment', kwargs={
|
|
|
|
|
'article_id': article.id})
|
|
|
|
|
|
|
|
|
|
# 测试提交第一条评论
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
# 审核后评论列表应包含1条评论
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 1)
|
|
|
|
|
|
|
|
|
|
# 测试提交第二条评论
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff',
|
|
|
|
|
@ -70,40 +87,19 @@ class CommentsTest(TransactionTestCase):
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
# 获取第一条评论ID作为父评论(测试回复功能)
|
|
|
|
|
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)
|
|
|
|
|
import os
|