You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.5 KiB
137 lines
4.5 KiB
from django.test import Client, RequestFactory, TransactionTestCase
|
|
from django.urls import reverse
|
|
|
|
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
|
|
|
|
|
|
class CommentsTest(TransactionTestCase):
|
|
"""
|
|
评论相关功能测试类
|
|
使用 TransactionTestCase 允许测试包含事务的数据库操作
|
|
"""
|
|
|
|
def setUp(self):
|
|
"""
|
|
测试初始化工作:
|
|
- 创建请求客户端
|
|
- 配置博客系统为“评论需要审核”
|
|
- 创建一个超级管理员用户(用于登录发表评论)
|
|
"""
|
|
self.client = Client()
|
|
self.factory = RequestFactory()
|
|
|
|
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):
|
|
"""
|
|
将文章下所有评论改为 is_enable=True
|
|
模拟管理员审核通过评论(使评论显示)
|
|
"""
|
|
comments = article.comment_set.all()
|
|
for comment in comments:
|
|
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.save()
|
|
|
|
# 文章评论提交 URL
|
|
comment_url = reverse('comments:postcomment', kwargs={'article_id': article.id})
|
|
|
|
# 用户提交第一条评论
|
|
response = self.client.post(comment_url, {'body': '123ffffffffff'})
|
|
self.assertEqual(response.status_code, 302) # 正常应重定向(提交成功)
|
|
|
|
# 因为评论需要审核,未批准前评论数为 0
|
|
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 功能)
|
|
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) # 第一条评论应当有 1 个子评论
|
|
|
|
# 渲染评论组件标签是否正常返回
|
|
data = show_comment_item(comment, True)
|
|
self.assertIsNotNone(data)
|
|
|
|
# 测试工具函数获取最大文章/评论 id
|
|
s = get_max_articleid_commentid()
|
|
self.assertIsNotNone(s)
|
|
|
|
# 测试发送评论邮件通知(若配置邮件服务则会成功)
|
|
from comments.utils import send_comment_email
|
|
send_comment_email(comment)
|
|
send_comment_email(comment)
|