|
|
|
|
@ -1,231 +1,109 @@
|
|
|
|
|
from django.test import Client, RequestFactory, TestCase
|
|
|
|
|
from django.test import Client, RequestFactory, TransactionTestCase
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
|
from accounts.models import BlogUser
|
|
|
|
|
from blog.models import Article, Category
|
|
|
|
|
from djangoblog.utils import *
|
|
|
|
|
from . import utils
|
|
|
|
|
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.
|
|
|
|
|
# 创建测试类(继承Django的TestCase)
|
|
|
|
|
class AccountTest(TestCase):
|
|
|
|
|
# 测试初始化方法(每个测试方法运行前都会执行)
|
|
|
|
|
|
|
|
|
|
class CommentsTest(TransactionTestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
# 初始化测试客户端(模拟浏览器请求)
|
|
|
|
|
self.client = Client()
|
|
|
|
|
# 初始化请求工厂(用于生成请求对象)
|
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
# 创建一个普通测试用户
|
|
|
|
|
self.blog_user = BlogUser.objects.create_user(
|
|
|
|
|
username="test",
|
|
|
|
|
email="admin@admin.com",
|
|
|
|
|
password="12345678"
|
|
|
|
|
)
|
|
|
|
|
# 测试用的随机字符串
|
|
|
|
|
self.new_test = "xxx123--="
|
|
|
|
|
# 测试用户账号验证功能
|
|
|
|
|
def test_validate_account(self):
|
|
|
|
|
# 获取当前站点域名
|
|
|
|
|
site = get_current_site().domain
|
|
|
|
|
# 创建一个超级用户(用于测试管理员权限)
|
|
|
|
|
user = BlogUser.objects.create_superuser(
|
|
|
|
|
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="qwer!@#$ggg")
|
|
|
|
|
# 从数据库获取刚创建的超级用户(验证是否创建成功)
|
|
|
|
|
testuser = BlogUser.objects.get(username='liangliangyy1')
|
|
|
|
|
# 测试登录功能
|
|
|
|
|
loginresult = self.client.login(
|
|
|
|
|
username='liangliangyy1',
|
|
|
|
|
password='qwer!@#$ggg')
|
|
|
|
|
self.assertEqual(loginresult, True)# 验证登录成功
|
|
|
|
|
# 测试访问管理员后台
|
|
|
|
|
response = self.client.get('/admin/')
|
|
|
|
|
self.assertEqual(response.status_code, 200)# 验证返回200状态码
|
|
|
|
|
# 创建测试分类
|
|
|
|
|
category = Category()
|
|
|
|
|
category.name = "categoryaaa"
|
|
|
|
|
category.creation_time = timezone.now()
|
|
|
|
|
category.last_modify_time = timezone.now()
|
|
|
|
|
category.save()
|
|
|
|
|
# 创建测试文章
|
|
|
|
|
article = Article()
|
|
|
|
|
article.title = "nicetitleaaa"
|
|
|
|
|
article.body = "nicecontentaaa"
|
|
|
|
|
article.author = user# 关联超级用户
|
|
|
|
|
article.category = category# 关联上面创建的分类
|
|
|
|
|
article.type = 'a' # 文章类型
|
|
|
|
|
article.status = 'p' # 发布状态
|
|
|
|
|
article.save()
|
|
|
|
|
# 测试访问文章的管理URL
|
|
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
|
|
self.assertEqual(response.status_code, 200) # 验证返回200状态码
|
|
|
|
|
|
|
|
|
|
# 测试用户注册功能
|
|
|
|
|
def test_validate_register(self):
|
|
|
|
|
# 验证测试邮箱初始不存在
|
|
|
|
|
self.assertEquals(
|
|
|
|
|
0, len(
|
|
|
|
|
BlogUser.objects.filter(
|
|
|
|
|
email='user123@user.com')))
|
|
|
|
|
# 模拟注册请求
|
|
|
|
|
response = self.client.post(reverse('account:register'), {
|
|
|
|
|
'username': 'user1233',
|
|
|
|
|
'email': 'user123@user.com',
|
|
|
|
|
'password1': 'password123!q@wE#R$T',
|
|
|
|
|
'password2': 'password123!q@wE#R$T',
|
|
|
|
|
})
|
|
|
|
|
# 验证用户已创建(通过邮箱查询)
|
|
|
|
|
self.assertEquals(
|
|
|
|
|
1, len(
|
|
|
|
|
BlogUser.objects.filter(
|
|
|
|
|
email='user123@user.com')))
|
|
|
|
|
# 获取刚注册的用户
|
|
|
|
|
user = BlogUser.objects.filter(email='user123@user.com')[0]
|
|
|
|
|
# 生成验证签名(用于邮箱验证等场景)
|
|
|
|
|
sign = get_sha256(get_sha256(settings.SECRET_KEY + str(user.id)))
|
|
|
|
|
# 构造验证URL
|
|
|
|
|
path = reverse('accounts:result')
|
|
|
|
|
url = '{path}?type=validation&id={id}&sign={sign}'.format(
|
|
|
|
|
path=path, id=user.id, sign=sign)
|
|
|
|
|
# 测试访问验证URL
|
|
|
|
|
response = self.client.get(url)
|
|
|
|
|
self.assertEqual(response.status_code, 200) # 验证返回200状态码
|
|
|
|
|
# 使用测试客户端登录
|
|
|
|
|
self.client.login(username='user1233', password='password123!q@wE#R$T')
|
|
|
|
|
# 获取指定邮箱的用户并设置为超级用户和工作人员
|
|
|
|
|
user = BlogUser.objects.filter(email='user123@user.com')[0]
|
|
|
|
|
user.is_superuser = True
|
|
|
|
|
user.is_staff = True
|
|
|
|
|
user.save()
|
|
|
|
|
# 删除侧边栏缓存
|
|
|
|
|
delete_sidebar_cache()
|
|
|
|
|
# 创建分类
|
|
|
|
|
password="liangliangyy1")
|
|
|
|
|
|
|
|
|
|
def update_article_comment_status(self, article):
|
|
|
|
|
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 = "categoryaaa"
|
|
|
|
|
category.creation_time = timezone.now()
|
|
|
|
|
category.last_modify_time = timezone.now()
|
|
|
|
|
category.name = "categoryccc"
|
|
|
|
|
category.save()
|
|
|
|
|
# 创建文章
|
|
|
|
|
|
|
|
|
|
article = Article()
|
|
|
|
|
article.title = "nicetitleccc"
|
|
|
|
|
article.body = "nicecontentccc"
|
|
|
|
|
article.author = self.user
|
|
|
|
|
article.category = category
|
|
|
|
|
article.title = "nicetitle333"
|
|
|
|
|
article.body = "nicecontentttt"
|
|
|
|
|
article.author = user
|
|
|
|
|
|
|
|
|
|
article.type = 'a'
|
|
|
|
|
article.status = 'p'
|
|
|
|
|
article.save()
|
|
|
|
|
# 测试已登录用户访问文章管理URL
|
|
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
# 测试注销功能
|
|
|
|
|
response = self.client.get(reverse('account:logout'))
|
|
|
|
|
self.assertIn(response.status_code, [301, 302, 200])
|
|
|
|
|
# 测试注销后访问文章管理URL(应重定向)
|
|
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
|
|
self.assertIn(response.status_code, [301, 302, 200])
|
|
|
|
|
# 测试使用错误密码登录
|
|
|
|
|
response = self.client.post(reverse('account:login'), {
|
|
|
|
|
'username': 'user1233',
|
|
|
|
|
'password': 'password123'# 注意这里密码与登录时使用的不同
|
|
|
|
|
})
|
|
|
|
|
self.assertIn(response.status_code, [301, 302, 200])
|
|
|
|
|
# 测试使用错误密码登录后访问文章管理URL(应重定向)
|
|
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
|
|
self.assertIn(response.status_code, [301, 302, 200])
|
|
|
|
|
# 测试邮箱验证码验证
|
|
|
|
|
def test_verify_email_code(self):
|
|
|
|
|
to_email = "admin@admin.com"
|
|
|
|
|
code = generate_code() # 生成验证码
|
|
|
|
|
utils.set_code(to_email, code)# 存储验证码
|
|
|
|
|
utils.send_verify_email(to_email, code) # 发送验证邮件(实际测试中可能不会真的发送)
|
|
|
|
|
# 测试正确验证码
|
|
|
|
|
err = utils.verify("admin@admin.com", code)
|
|
|
|
|
self.assertEqual(err, None)
|
|
|
|
|
# 测试错误邮箱
|
|
|
|
|
err = utils.verify("admin@123.com", code)
|
|
|
|
|
self.assertEqual(type(err), str)# 应返回错误信息字符串
|
|
|
|
|
# 测试忘记密码发送验证码功能 - 成功情况
|
|
|
|
|
def test_forget_password_email_code_success(self):
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
path=reverse("account:forget_password_code"),
|
|
|
|
|
data=dict(email="admin@admin.com") # 使用正确邮箱格式
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
|
self.assertEqual(resp.content.decode("utf-8"), "ok")# 验证返回成功消息
|
|
|
|
|
# 测试忘记密码发送验证码功能 - 失败情况
|
|
|
|
|
def test_forget_password_email_code_fail(self):
|
|
|
|
|
# 测试不提供邮箱
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
path=reverse("account:forget_password_code"),
|
|
|
|
|
data=dict()
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(resp.content.decode("utf-8"), "错误的邮箱")
|
|
|
|
|
# 测试提供错误格式邮箱
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
path=reverse("account:forget_password_code"),
|
|
|
|
|
data=dict(email="admin@com")
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(resp.content.decode("utf-8"), "错误的邮箱")
|
|
|
|
|
# 测试忘记密码重置功能 - 成功情况
|
|
|
|
|
def test_forget_password_email_success(self):
|
|
|
|
|
code = generate_code()
|
|
|
|
|
utils.set_code(self.blog_user.email, code)# 为测试用户设置验证码
|
|
|
|
|
# 准备重置密码数据
|
|
|
|
|
data = dict(
|
|
|
|
|
new_password1=self.new_test, # 新密码
|
|
|
|
|
new_password2=self.new_test,# 确认密码
|
|
|
|
|
email=self.blog_user.email,# 用户邮箱
|
|
|
|
|
code=code, # 验证码
|
|
|
|
|
)
|
|
|
|
|
# 提交重置密码请求
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
path=reverse("account:forget_password"),
|
|
|
|
|
data=data
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(resp.status_code, 302) # 应重定向
|
|
|
|
|
|
|
|
|
|
# 验证用户密码是否修改成功
|
|
|
|
|
blog_user = BlogUser.objects.filter(
|
|
|
|
|
email=self.blog_user.email,
|
|
|
|
|
).first() # type: BlogUser
|
|
|
|
|
self.assertNotEqual(blog_user, None)
|
|
|
|
|
self.assertEqual(blog_user.check_password(data["new_password1"]), True)
|
|
|
|
|
# 测试忘记密码重置功能 - 用户不存在情况
|
|
|
|
|
def test_forget_password_email_not_user(self):
|
|
|
|
|
data = dict(
|
|
|
|
|
new_password1=self.new_test,
|
|
|
|
|
new_password2=self.new_test,
|
|
|
|
|
email="123@123.com",# 不存在的邮箱
|
|
|
|
|
code="123456",
|
|
|
|
|
)
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
path=reverse("account:forget_password"),
|
|
|
|
|
data=data
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(resp.status_code, 200) # 应返回错误页面而非重定向
|
|
|
|
|
|
|
|
|
|
# 测试忘记密码重置功能 - 验证码错误情况
|
|
|
|
|
def test_forget_password_email_code_error(self):
|
|
|
|
|
code = generate_code()
|
|
|
|
|
utils.set_code(self.blog_user.email, code)# 设置正确验证码
|
|
|
|
|
# 使用错误验证码提交
|
|
|
|
|
data = dict(
|
|
|
|
|
new_password1=self.new_test,
|
|
|
|
|
new_password2=self.new_test,
|
|
|
|
|
email=self.blog_user.email,
|
|
|
|
|
code="111111",# 错误验证码
|
|
|
|
|
)
|
|
|
|
|
resp = self.client.post(
|
|
|
|
|
path=reverse("account:forget_password"),
|
|
|
|
|
data=data
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.assertEqual(resp.status_code, 200)# 应返回错误页面而非重定向
|
|
|
|
|
|
|
|
|
|
comment_url = reverse(
|
|
|
|
|
'comments:postcomment', kwargs={
|
|
|
|
|
'article_id': article.id})
|
|
|
|
|
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|