|
|
<<<<<<< HEAD
|
|
|
<<<<<<< HEAD
|
|
|
<<<<<<< HEAD
|
|
|
from django.test import TestCase
|
|
|
|
|
|
from djangoblog.utils import *
|
|
|
|
|
|
|
|
|
class DjangoBlogTest(TestCase):
|
|
|
def setUp(self):
|
|
|
pass
|
|
|
|
|
|
def test_utils(self):
|
|
|
md5 = get_sha256('test')
|
|
|
self.assertIsNotNone(md5)
|
|
|
c = CommonMarkdown.get_markdown('''
|
|
|
# Title1
|
|
|
|
|
|
```python
|
|
|
import os
|
|
|
```
|
|
|
|
|
|
[url](https://www.lylinux.net/)
|
|
|
|
|
|
[ddd](http://www.baidu.com)
|
|
|
|
|
|
|
|
|
''')
|
|
|
self.assertIsNotNone(c)
|
|
|
d = {
|
|
|
'd': 'key1',
|
|
|
'd2': 'key2'
|
|
|
}
|
|
|
data = parse_dict_to_url(d)
|
|
|
self.assertIsNotNone(data)
|
|
|
|
|
|
from django.test import Client, RequestFactory, TestCase
|
|
|
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
|
|
|
=======
|
|
|
import os
|
|
|
|
|
|
from django.conf import settings
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
|
|
from django.core.management import call_command
|
|
|
from django.core.paginator import Paginator
|
|
|
from django.templatetags.static import static
|
|
|
from django.test import Client, RequestFactory, TestCase
|
|
|
from django.urls import reverse
|
|
|
from django.utils import timezone
|
|
|
|
|
|
from accounts.models import BlogUser
|
|
|
from blog.forms import BlogSearchForm
|
|
|
from blog.models import Article, Category, Tag, SideBar, Links
|
|
|
from blog.templatetags.blog_tags import load_pagination_info, load_articletags
|
|
|
from djangoblog.utils import get_current_site, get_sha256
|
|
|
from oauth.models import OAuthUser, OAuthConfig
|
|
|
>>>>>>> hyt_branch
|
|
|
|
|
|
|
|
|
# Create your tests here.
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
class AccountTest(TestCase):
|
|
|
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(
|
|
|
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)
|
|
|
|
|
|
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()
|
|
|
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
def test_validate_register(self):
|
|
|
=======
|
|
|
|
|
|
from django.test import Client, RequestFactory, TestCase
|
|
|
|
|
|
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 *
|
|
|
# 导入当前应用(accounts)的工具函数,用于测试账号相关工具功能
|
|
|
from . import utils
|
|
|
|
|
|
|
|
|
# 定义账号功能测试类,继承TestCase(基础测试用例类)
|
|
|
class AccountTest(TestCase):
|
|
|
# 测试前初始化方法,每个测试方法执行前自动运行
|
|
|
def setUp(self):
|
|
|
# 初始化测试客户端,用于模拟用户发起HTTP请求
|
|
|
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(
|
|
|
email="liangliangyy1@gmail.com", # 超级用户邮箱
|
|
|
username="liangliangyy1", # 超级用户名
|
|
|
password="qwer!@#$ggg") # 超级用户密码
|
|
|
# 从数据库中查询刚创建的超级用户,用于后续验证
|
|
|
testuser = BlogUser.objects.get(username='liangliangyy1')
|
|
|
|
|
|
# 模拟超级用户登录,返回登录结果(布尔值)
|
|
|
loginresult = self.client.login(
|
|
|
username='liangliangyy1', # 登录用户名
|
|
|
password='qwer!@#$ggg') # 登录密码
|
|
|
# 断言:登录结果应为True(登录成功)
|
|
|
self.assertEqual(loginresult, True)
|
|
|
# 模拟超级用户访问管理员后台首页
|
|
|
response = self.client.get('/admin/')
|
|
|
# 断言:响应状态码应为200(访问成功)
|
|
|
self.assertEqual(response.status_code, 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' # 文章类型(假设'a'代表普通文章)
|
|
|
article.status = 'p' # 文章状态(假设'p'代表已发布)
|
|
|
article.save() # 保存文章到测试数据库
|
|
|
|
|
|
# 模拟访问该文章的管理员编辑页(通过文章模型的自定义方法获取URL)
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
# 断言:响应状态码应为200(管理员有权限访问,访问成功)
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试账号注册功能(注册、邮箱验证、登录、权限提升、文章管理、登出)
|
|
|
def test_validate_register(self):
|
|
|
# 断言:数据库中初始不存在邮箱为'user123@user.com'的用户(计数为0)
|
|
|
>>>>>>> zh_branch
|
|
|
self.assertEquals(
|
|
|
0, len(
|
|
|
BlogUser.objects.filter(
|
|
|
email='user123@user.com')))
|
|
|
<<<<<<< HEAD
|
|
|
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',
|
|
|
})
|
|
|
=======
|
|
|
# 模拟POST请求提交注册表单,访问注册接口
|
|
|
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', # 密码确认(与密码一致)
|
|
|
})
|
|
|
# 断言:注册后数据库中应存在该邮箱用户(计数为1)
|
|
|
>>>>>>> zh_branch
|
|
|
self.assertEquals(
|
|
|
1, len(
|
|
|
BlogUser.objects.filter(
|
|
|
email='user123@user.com')))
|
|
|
<<<<<<< HEAD
|
|
|
user = BlogUser.objects.filter(email='user123@user.com')[0]
|
|
|
sign = get_sha256(get_sha256(settings.SECRET_KEY + str(user.id)))
|
|
|
path = reverse('accounts:result')
|
|
|
url = '{path}?type=validation&id={id}&sign={sign}'.format(
|
|
|
path=path, id=user.id, sign=sign)
|
|
|
response = self.client.get(url)
|
|
|
self.assertEqual(response.status_code, 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()
|
|
|
category = Category()
|
|
|
category.name = "categoryaaa"
|
|
|
category.creation_time = timezone.now()
|
|
|
category.last_modify_time = timezone.now()
|
|
|
category.save()
|
|
|
|
|
|
article = Article()
|
|
|
article.category = category
|
|
|
article.title = "nicetitle333"
|
|
|
article.body = "nicecontentttt"
|
|
|
article.author = user
|
|
|
|
|
|
article.type = 'a'
|
|
|
article.status = 'p'
|
|
|
article.save()
|
|
|
|
|
|
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])
|
|
|
|
|
|
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])
|
|
|
|
|
|
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",
|
|
|
)
|
|
|
=======
|
|
|
# 从数据库中查询刚注册的用户
|
|
|
user = BlogUser.objects.filter(email='user123@user.com')[0]
|
|
|
# 生成用户邮箱验证的签名(双重SHA256加密,结合密钥和用户ID)
|
|
|
sign = get_sha256(get_sha256(settings.SECRET_KEY + str(user.id)))
|
|
|
# 反向解析验证结果页的URL
|
|
|
path = reverse('accounts:result')
|
|
|
# 拼接完整的邮箱验证URL(包含用户ID和签名)
|
|
|
url = '{path}?type=validation&id={id}&sign={sign}'.format(
|
|
|
path=path, id=user.id, sign=sign)
|
|
|
# 模拟访问邮箱验证URL,完成验证
|
|
|
response = self.client.get(url)
|
|
|
# 断言:验证页面访问成功(状态码200)
|
|
|
self.assertEqual(response.status_code, 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 # 设置为管理员(有权访问admin后台)
|
|
|
user.save() # 保存权限修改
|
|
|
# 调用工具函数删除侧边栏缓存(避免缓存影响测试结果)
|
|
|
delete_sidebar_cache()
|
|
|
# 创建测试分类(用于后续文章关联)
|
|
|
category = Category()
|
|
|
category.name = "categoryaaa" # 分类名称
|
|
|
category.creation_time = timezone.now() # 创建时间
|
|
|
category.last_modify_time = timezone.now() # 最后修改时间
|
|
|
category.save() # 保存分类
|
|
|
|
|
|
# 创建测试文章(关联上述分类和提升权限后的用户)
|
|
|
article = Article()
|
|
|
article.category = category # 所属分类
|
|
|
article.title = "nicetitle333" # 文章标题
|
|
|
article.body = "nicecontentttt" # 文章内容
|
|
|
article.author = user # 文章作者(提升权限后的用户)
|
|
|
article.type = 'a' # 文章类型
|
|
|
article.status = 'p' # 文章状态(已发布)
|
|
|
article.save() # 保存文章
|
|
|
|
|
|
# 模拟访问该文章的管理员编辑页
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
# 断言:访问成功(状态码200,因用户已提升为管理员)
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 模拟用户登出(访问登出接口)
|
|
|
response = self.client.get(reverse('account:logout'))
|
|
|
# 断言:登出响应状态码在[301,302,200]内(重定向或成功)
|
|
|
self.assertIn(response.status_code, [301, 302, 200])
|
|
|
|
|
|
# 登出后再次访问文章管理员编辑页(应无权限)
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
# 断言:响应状态码在[301,302,200]内(可能重定向到登录页)
|
|
|
self.assertIn(response.status_code, [301, 302, 200])
|
|
|
|
|
|
# 模拟使用错误密码登录(密码不匹配)
|
|
|
response = self.client.post(reverse('account:login'), {
|
|
|
'username': 'user1233', # 正确用户名
|
|
|
'password': 'password123' # 错误密码
|
|
|
})
|
|
|
# 断言:登录响应状态码在[301,302,200]内(登录失败可能重定向或返回表单)
|
|
|
self.assertIn(response.status_code, [301, 302, 200])
|
|
|
|
|
|
# 错误登录后访问文章管理员编辑页(仍无权限)
|
|
|
response = self.client.get(article.get_admin_url())
|
|
|
# 断言:响应状态码在[301,302,200]内(可能重定向到登录页)
|
|
|
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)
|
|
|
# 断言:验证无错误(返回None)
|
|
|
self.assertEqual(err, None)
|
|
|
|
|
|
# 验证:使用错误邮箱和正确验证码
|
|
|
err = utils.verify("admin@123.com", code)
|
|
|
# 断言:验证错误,错误类型为字符串(返回错误信息)
|
|
|
self.assertEqual(type(err), str)
|
|
|
|
|
|
# 测试“忘记密码-发送验证码”功能的成功场景
|
|
|
def test_forget_password_email_code_success(self):
|
|
|
# 模拟POST请求提交邮箱,访问“发送忘记密码验证码”接口
|
|
|
resp = self.client.post(
|
|
|
path=reverse("account:forget_password_code"), # 反向解析接口URL
|
|
|
data=dict(email="admin@admin.com") # 提交已存在的测试邮箱
|
|
|
)
|
|
|
|
|
|
# 断言:响应状态码为200(请求处理成功)
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
# 断言:响应内容为"ok"(表示验证码发送成功)
|
|
|
self.assertEqual(resp.content.decode("utf-8"), "ok")
|
|
|
|
|
|
# 测试“忘记密码-发送验证码”功能的失败场景
|
|
|
def test_forget_password_email_code_fail(self):
|
|
|
# 模拟POST请求:不提交邮箱(空数据)
|
|
|
resp = self.client.post(
|
|
|
path=reverse("account:forget_password_code"),
|
|
|
data=dict() # 空数据
|
|
|
)
|
|
|
# 断言:响应内容为“错误的邮箱”(无邮箱参数,请求失败)
|
|
|
self.assertEqual(resp.content.decode("utf-8"), "错误的邮箱")
|
|
|
|
|
|
# 模拟POST请求:提交格式错误的邮箱(无效邮箱)
|
|
|
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, # 正确的验证码
|
|
|
)
|
|
|
# 模拟POST请求提交重置密码数据,访问重置密码接口
|
|
|
resp = self.client.post(
|
|
|
path=reverse("account:forget_password"), # 反向解析接口URL
|
|
|
data=data
|
|
|
)
|
|
|
# 断言:响应状态码为302(重置成功,重定向到登录页或结果页)
|
|
|
self.assertEqual(resp.status_code, 302)
|
|
|
|
|
|
# 验证:数据库中用户密码是否已更新
|
|
|
blog_user = BlogUser.objects.filter(
|
|
|
email=self.blog_user.email, # 按邮箱查询测试用户
|
|
|
).first() # 获取查询结果的第一个(唯一用户)
|
|
|
# 断言:查询到用户(用户存在)
|
|
|
self.assertNotEqual(blog_user, None)
|
|
|
# 断言:用户密码与新密码匹配(check_password方法验证哈希密码)
|
|
|
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", # 任意验证码
|
|
|
)
|
|
|
# 模拟POST请求提交数据,访问重置密码接口
|
|
|
>>>>>>> zh_branch
|
|
|
resp = self.client.post(
|
|
|
path=reverse("account:forget_password"),
|
|
|
data=data
|
|
|
)
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
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",
|
|
|
)
|
|
|
=======
|
|
|
# 断言:响应状态码为200(请求处理完成,但重置失败,返回表单页)
|
|
|
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", # 错误的验证码
|
|
|
)
|
|
|
# 模拟POST请求提交数据,访问重置密码接口
|
|
|
>>>>>>> zh_branch
|
|
|
resp = self.client.post(
|
|
|
path=reverse("account:forget_password"),
|
|
|
data=data
|
|
|
)
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
|
|
|
|
|
=======
|
|
|
# 断言:响应状态码为200(请求处理完成,但验证码错误,返回表单页)
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
>>>>>>> zh_branch
|
|
|
=======
|
|
|
class ArticleTest(TestCase):
|
|
|
"""
|
|
|
文章模型测试类
|
|
|
|
|
|
这个测试类用于测试博客系统的核心功能,包括:
|
|
|
- 文章创建和验证
|
|
|
- 搜索功能
|
|
|
- 分页功能
|
|
|
- 文件上传
|
|
|
- 管理命令
|
|
|
- 错误页面处理
|
|
|
"""
|
|
|
|
|
|
def setUp(self):
|
|
|
"""
|
|
|
测试初始化方法
|
|
|
在每个测试方法执行前运行,用于设置测试环境
|
|
|
"""
|
|
|
self.client = Client() # Django测试客户端,用于模拟HTTP请求
|
|
|
self.factory = RequestFactory() # 请求工厂,用于创建请求对象
|
|
|
|
|
|
def test_validate_article(self):
|
|
|
"""
|
|
|
测试文章验证和核心功能
|
|
|
|
|
|
这个测试方法验证博客系统的核心功能:
|
|
|
- 用户创建和认证
|
|
|
- 文章创建和关联
|
|
|
- 搜索功能
|
|
|
- 分页功能
|
|
|
- RSS和站点地图
|
|
|
- 管理后台访问
|
|
|
"""
|
|
|
# 获取当前站点域名
|
|
|
site = get_current_site().domain
|
|
|
|
|
|
# 创建测试用户(超级用户)
|
|
|
user = BlogUser.objects.get_or_create(
|
|
|
email="liangliangyy@gmail.com",
|
|
|
username="liangliangyy")[0]
|
|
|
user.set_password("liangliangyy")
|
|
|
user.is_staff = True # 设置为管理员
|
|
|
user.is_superuser = True # 设置为超级用户
|
|
|
user.save()
|
|
|
|
|
|
# 测试用户详情页访问
|
|
|
response = self.client.get(user.get_absolute_url())
|
|
|
self.assertEqual(response.status_code, 200) # 断言返回200状态码
|
|
|
|
|
|
# 测试管理后台页面访问
|
|
|
response = self.client.get('/admin/servermanager/emailsendlog/')
|
|
|
response = self.client.get('admin/admin/logentry/')
|
|
|
|
|
|
# 创建侧边栏测试数据
|
|
|
s = SideBar()
|
|
|
s.sequence = 1
|
|
|
s.name = 'test'
|
|
|
s.content = 'test content'
|
|
|
s.is_enable = True
|
|
|
s.save()
|
|
|
|
|
|
# 创建分类测试数据
|
|
|
category = Category()
|
|
|
category.name = "category"
|
|
|
category.creation_time = timezone.now()
|
|
|
category.last_mod_time = timezone.now()
|
|
|
category.save()
|
|
|
|
|
|
# 创建标签测试数据
|
|
|
tag = Tag()
|
|
|
tag.name = "nicetag"
|
|
|
tag.save()
|
|
|
|
|
|
# 创建文章测试数据
|
|
|
article = Article()
|
|
|
article.title = "nicetitle"
|
|
|
article.body = "nicecontent"
|
|
|
article.author = user
|
|
|
article.category = category
|
|
|
article.type = 'a' # 文章类型
|
|
|
article.status = 'p' # 发布状态
|
|
|
|
|
|
article.save()
|
|
|
# 验证初始标签数量为0
|
|
|
self.assertEqual(0, article.tags.count())
|
|
|
# 添加标签到文章
|
|
|
article.tags.add(tag)
|
|
|
article.save()
|
|
|
# 验证标签数量为1
|
|
|
self.assertEqual(1, article.tags.count())
|
|
|
|
|
|
# 批量创建20篇文章用于测试分页和搜索
|
|
|
for i in range(20):
|
|
|
article = Article()
|
|
|
article.title = "nicetitle" + str(i)
|
|
|
article.body = "nicetitle" + str(i)
|
|
|
article.author = user
|
|
|
article.category = category
|
|
|
article.type = 'a'
|
|
|
article.status = 'p'
|
|
|
article.save()
|
|
|
article.tags.add(tag)
|
|
|
article.save()
|
|
|
|
|
|
# 测试搜索功能(如果启用了Elasticsearch)
|
|
|
from blog.documents import ELASTICSEARCH_ENABLED
|
|
|
if ELASTICSEARCH_ENABLED:
|
|
|
call_command("build_index") # 构建搜索索引
|
|
|
response = self.client.get('/search', {'q': 'nicetitle'})
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试文章详情页访问
|
|
|
response = self.client.get(article.get_absolute_url())
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试百度推送通知
|
|
|
from djangoblog.spider_notify import SpiderNotify
|
|
|
SpiderNotify.notify(article.get_absolute_url())
|
|
|
|
|
|
# 测试标签页访问
|
|
|
response = self.client.get(tag.get_absolute_url())
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试分类页访问
|
|
|
response = self.client.get(category.get_absolute_url())
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试搜索页面
|
|
|
response = self.client.get('/search', {'q': 'django'})
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试模板标签函数
|
|
|
s = load_articletags(article)
|
|
|
self.assertIsNotNone(s)
|
|
|
|
|
|
# 用户登录测试
|
|
|
self.client.login(username='liangliangyy', password='liangliangyy')
|
|
|
|
|
|
# 测试文章归档页面
|
|
|
response = self.client.get(reverse('blog:archives'))
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试各种分页场景
|
|
|
p = Paginator(Article.objects.all(), settings.PAGINATE_BY)
|
|
|
self.check_pagination(p, '', '') # 基础分页
|
|
|
|
|
|
p = Paginator(Article.objects.filter(tags=tag), settings.PAGINATE_BY)
|
|
|
self.check_pagination(p, '分类标签归档', tag.slug) # 标签分页
|
|
|
|
|
|
p = Paginator(
|
|
|
Article.objects.filter(
|
|
|
author__username='liangliangyy'), settings.PAGINATE_BY)
|
|
|
self.check_pagination(p, '作者文章归档', 'liangliangyy') # 作者分页
|
|
|
|
|
|
p = Paginator(Article.objects.filter(category=category), settings.PAGINATE_BY)
|
|
|
self.check_pagination(p, '分类目录归档', category.slug) # 分类分页
|
|
|
|
|
|
# 测试搜索表单
|
|
|
f = BlogSearchForm()
|
|
|
f.search()
|
|
|
|
|
|
# 测试百度批量推送
|
|
|
from djangoblog.spider_notify import SpiderNotify
|
|
|
SpiderNotify.baidu_notify([article.get_full_url()])
|
|
|
|
|
|
# 测试Gravatar相关功能
|
|
|
from blog.templatetags.blog_tags import gravatar_url, gravatar
|
|
|
u = gravatar_url('liangliangyy@gmail.com')
|
|
|
u = gravatar('liangliangyy@gmail.com')
|
|
|
|
|
|
# 测试友情链接功能
|
|
|
link = Links(
|
|
|
sequence=1,
|
|
|
name="lylinux",
|
|
|
link='https://wwww.lylinux.net')
|
|
|
link.save()
|
|
|
response = self.client.get('/links.html')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试RSS订阅
|
|
|
response = self.client.get('/feed/')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试站点地图
|
|
|
response = self.client.get('/sitemap.xml')
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
# 测试管理后台操作
|
|
|
self.client.get("/admin/blog/article/1/delete/")
|
|
|
self.client.get('/admin/servermanager/emailsendlog/')
|
|
|
self.client.get('/admin/admin/logentry/')
|
|
|
self.client.get('/admin/admin/logentry/1/change/')
|
|
|
|
|
|
def check_pagination(self, p, type, value):
|
|
|
"""
|
|
|
分页功能测试辅助方法
|
|
|
|
|
|
Args:
|
|
|
p: Paginator分页对象
|
|
|
type: 分页类型(用于生成URL)
|
|
|
value: 分页参数值
|
|
|
"""
|
|
|
for page in range(1, p.num_pages + 1):
|
|
|
# 加载分页信息
|
|
|
s = load_pagination_info(p.page(page), type, value)
|
|
|
self.assertIsNotNone(s)
|
|
|
# 测试上一页链接
|
|
|
if s['previous_url']:
|
|
|
response = self.client.get(s['previous_url'])
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
# 测试下一页链接
|
|
|
if s['next_url']:
|
|
|
response = self.client.get(s['next_url'])
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
def test_image(self):
|
|
|
"""
|
|
|
图片上传和头像处理测试
|
|
|
"""
|
|
|
import requests
|
|
|
# 下载测试图片
|
|
|
rsp = requests.get(
|
|
|
'https://www.python.org/static/img/python-logo.png')
|
|
|
imagepath = os.path.join(settings.BASE_DIR, 'python.png')
|
|
|
with open(imagepath, 'wb') as file:
|
|
|
file.write(rsp.content)
|
|
|
|
|
|
# 测试未授权上传(应该返回403)
|
|
|
rsp = self.client.post('/upload')
|
|
|
self.assertEqual(rsp.status_code, 403)
|
|
|
|
|
|
# 生成签名用于授权上传
|
|
|
sign = get_sha256(get_sha256(settings.SECRET_KEY))
|
|
|
with open(imagepath, 'rb') as file:
|
|
|
imgfile = SimpleUploadedFile(
|
|
|
'python.png', file.read(), content_type='image/jpg')
|
|
|
form_data = {'python.png': imgfile}
|
|
|
# 测试授权上传
|
|
|
rsp = self.client.post(
|
|
|
'/upload?sign=' + sign, form_data, follow=True)
|
|
|
self.assertEqual(rsp.status_code, 200)
|
|
|
# 清理测试文件
|
|
|
os.remove(imagepath)
|
|
|
|
|
|
# 测试工具函数
|
|
|
from djangoblog.utils import save_user_avatar, send_email
|
|
|
send_email(['qq@qq.com'], 'testTitle', 'testContent')
|
|
|
save_user_avatar(
|
|
|
'https://www.python.org/static/img/python-logo.png')
|
|
|
|
|
|
def test_errorpage(self):
|
|
|
"""测试404错误页面"""
|
|
|
rsp = self.client.get('/eee')
|
|
|
self.assertEqual(rsp.status_code, 404)
|
|
|
|
|
|
def test_commands(self):
|
|
|
"""
|
|
|
测试Django管理命令
|
|
|
|
|
|
验证系统提供的各种管理命令是否能正常执行
|
|
|
"""
|
|
|
# 创建测试用户
|
|
|
user = BlogUser.objects.get_or_create(
|
|
|
email="liangliangyy@gmail.com",
|
|
|
username="liangliangyy")[0]
|
|
|
user.set_password("liangliangyy")
|
|
|
user.is_staff = True
|
|
|
user.is_superuser = True
|
|
|
user.save()
|
|
|
|
|
|
# 创建OAuth配置
|
|
|
c = OAuthConfig()
|
|
|
c.type = 'qq'
|
|
|
c.appkey = 'appkey'
|
|
|
c.appsecret = 'appsecret'
|
|
|
c.save()
|
|
|
|
|
|
# 创建OAuth用户
|
|
|
u = OAuthUser()
|
|
|
u.type = 'qq'
|
|
|
u.openid = 'openid'
|
|
|
u.user = user
|
|
|
u.picture = static("/blog/img/avatar.png")
|
|
|
u.metadata = '''
|
|
|
{
|
|
|
"figureurl": "https://qzapp.qlogo.cn/qzapp/101513904/C740E30B4113EAA80E0D9918ABC78E82/30"
|
|
|
}'''
|
|
|
u.save()
|
|
|
|
|
|
u = OAuthUser()
|
|
|
u.type = 'qq'
|
|
|
u.openid = 'openid1'
|
|
|
u.picture = 'https://qzapp.qlogo.cn/qzapp/101513904/C740E30B4113EAA80E0D9918ABC78E82/30'
|
|
|
u.metadata = '''
|
|
|
{
|
|
|
"figureurl": "https://qzapp.qlogo.cn/qzapp/101513904/C740E30B4113EAA80E0D9918ABC78E82/30"
|
|
|
}'''
|
|
|
u.save()
|
|
|
|
|
|
# 测试各种管理命令
|
|
|
from blog.documents import ELASTICSEARCH_ENABLED
|
|
|
if ELASTICSEARCH_ENABLED:
|
|
|
call_command("build_index") # 构建搜索索引
|
|
|
call_command("ping_baidu", "all") # 百度推送
|
|
|
call_command("create_testdata") # 创建测试数据
|
|
|
call_command("clear_cache") # 清理缓存
|
|
|
call_command("sync_user_avatar") # 同步用户头像
|
|
|
call_command("build_search_words") # 构建搜索词
|
|
|
>>>>>>> hyt_branch
|