|
|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
|
from djangoblog.utils import *
|
|
|
|
|
@ -90,20 +91,118 @@ class AccountTest(TestCase):
|
|
|
|
|
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')
|
|
|
|
|
@ -215,11 +314,174 @@ class AccountTest(TestCase):
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -232,11 +494,35 @@ class AccountTest(TestCase):
|
|
|
|
|
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
|
|
|
|
|
|