@ -9,81 +9,104 @@ from djangoblog.utils import *
from . import utils
# Create your tests here.
class AccountTest ( TestCase ) :
#xh:用户账户功能测试类,测试用户注册、登录、密码重置等核心功能
def setUp ( self ) :
self . client = Client ( )
self . factory = RequestFactory ( )
#xh:测试前置设置方法,在每个测试方法执行前运行,用于创建测试数据
self . client = Client ( ) # xh:Django测试客户端, 用于模拟HTTP请求
self . factory = RequestFactory ( ) # xh:用于创建请求对象
# xh:创建测试用户
self . blog_user = BlogUser . objects . create_user (
username = " test " ,
email = " admin@admin.com " ,
password = " 12345678 "
)
self . new_test = " xxx123--= "
self . new_test = " xxx123--= " # xh:测试用的新密码
def test_validate_account ( self ) :
#xh:测试账户验证功能,验证超级用户创建、登录、文章管理等功能
site = get_current_site ( ) . domain
# xh:创建超级用户
user = BlogUser . objects . create_superuser (
email = " liangliangyy1@gmail.com " ,
username = " liangliangyy1 " ,
password = " qwer!@#$ggg " )
testuser = BlogUser . objects . get ( username = ' liangliangyy1 ' )
# xh:测试用户登录
loginresult = self . client . login (
username = ' liangliangyy1 ' ,
password = ' qwer!@#$ggg ' )
self . assertEqual ( loginresult , True )
self . assertEqual ( loginresult , True ) # xh:断言登录成功
# xh:测试访问管理员页面
response = self . client . get ( ' /admin/ ' )
self . assertEqual ( response . status_code , 200 )
self . assertEqual ( response . status_code , 200 ) # xh:断言可以访问admin
# xh:创建测试分类
category = Category ( )
category . name = " categoryaaa "
category . creation_time = timezone . now ( )
category . last_modify_time = timezone . now ( )
category . save ( )
# xh:创建测试文章
article = Article ( )
article . title = " nicetitleaaa "
article . body = " nicecontentaaa "
article . author = user
article . category = category
article . type = ' a '
article . status = ' p '
article . type = ' a ' # xh:文章类型
article . status = ' p ' # xh:发布状态
article . save ( )
# xh:测试访问文章管理页面
response = self . client . get ( article . get_admin_url ( ) )
self . assertEqual ( response . status_code , 200 )
self . assertEqual ( response . status_code , 200 ) # xh:断言可以访问文章管理页
def test_validate_register ( self ) :
#xh:测试用户注册流程,验证用户注册、邮箱验证、登录、权限管理等完整流程
# xh:验证注册前用户不存在
self . assertEquals (
0 , len (
BlogUser . objects . filter (
email = ' user123@user.com ' ) ) )
0 , len ( BlogUser . objects . filter ( email = ' user123@user.com ' ) ) )
# xh:提交注册表单
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 ' ,
' password1 ' : ' password123!q@wE#R$T ' , # xh:密码1
' password2 ' : ' password123!q@wE#R$T ' , # xh:确认密码
} )
# xh:验证用户创建成功
self . assertEquals (
1 , len (
BlogUser . objects . filter (
email = ' user123@user.com ' ) ) )
1 , len ( BlogUser . objects . filter ( email = ' user123@user.com ' ) ) )
user = BlogUser . objects . filter ( email = ' user123@user.com ' ) [ 0 ]
# xh:生成邮箱验证签名(模拟验证流程)
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 )
# xh:访问验证结果页面
response = self . client . get ( url )
self . assertEqual ( response . status_code , 200 )
# xh:测试用户登录
self . client . login ( username = ' user1233 ' , password = ' password123!q@wE#R$T ' )
# xh:提升用户权限为超级用户
user = BlogUser . objects . filter ( email = ' user123@user.com ' ) [ 0 ]
user . is_superuser = True
user . is_staff = True
user . save ( )
delete_sidebar_cache ( )
delete_sidebar_cache ( ) # xh:清理侧边栏缓存
# xh:创建测试数据
category = Category ( )
category . name = " categoryaaa "
category . creation_time = timezone . now ( )
@ -95,90 +118,110 @@ class AccountTest(TestCase):
article . title = " nicetitle333 "
article . body = " nicecontentttt "
article . author = user
article . type = ' a '
article . status = ' p '
article . save ( )
# xh:测试文章管理页面访问
response = self . client . get ( article . get_admin_url ( ) )
self . assertEqual ( response . status_code , 200 )
# xh:测试用户登出
response = self . client . get ( reverse ( ' account:logout ' ) )
self . assertIn ( response . status_code , [ 301 , 302 , 200 ] )
self . assertIn ( response . status_code , [ 301 , 302 , 200 ] ) # xh:重定向或成功
# xh:登出后访问管理页面应该被重定向
response = self . client . get ( article . get_admin_url ( ) )
self . assertIn ( response . status_code , [ 301 , 302 , 200 ] )
# xh:测试错误密码登录
response = self . client . post ( reverse ( ' account:login ' ) , {
' username ' : ' user1233 ' ,
' password ' : ' password123 '
' password ' : ' password123 ' # xh:错误密码
} )
self . assertIn ( response . status_code , [ 301 , 302 , 200 ] )
# xh:错误密码登录后仍无法访问管理页面
response = self . client . get ( article . get_admin_url ( ) )
self . assertIn ( response . status_code , [ 301 , 302 , 200 ] )
def test_verify_email_code ( self ) :
#xh:测试邮箱验证码功能,验证验证码的生成、发送和验证流程
to_email = " admin@admin.com "
code = generate_code ( )
code = generate_code ( ) # xh:生成验证码
# xh:保存验证码到缓存/数据库
utils . set_code ( to_email , code )
# xh:发送验证邮件(测试环境可能不会实际发送)
utils . send_verify_email ( to_email , code )
# xh:测试正确验证码验证
err = utils . verify ( " admin@admin.com " , code )
self . assertEqual ( err , None )
self . assertEqual ( err , None ) # xh:断言验证成功(无错误)
# xh:测试错误邮箱验证
err = utils . verify ( " admin@123.com " , code )
self . assertEqual ( type ( err ) , str )
self . assertEqual ( type ( err ) , str ) # xh:断言返回错误信息
def test_forget_password_email_code_success ( self ) :
#xh:测试忘记密码验证码请求成功情况
resp = self . client . post (
path = reverse ( " account:forget_password_code " ) ,
data = dict ( email = " admin@admin.com " )
data = dict ( email = " admin@admin.com " ) # xh:正确邮箱格式
)
self . assertEqual ( resp . status_code , 200 )
self . assertEqual ( resp . content . decode ( " utf-8 " ) , " ok " )
self . assertEqual ( resp . status_code , 200 ) # xh:断言请求成功
self . assertEqual ( resp . content . decode ( " utf-8 " ) , " ok " ) # xh:断言返回成功消息
def test_forget_password_email_code_fail ( self ) :
#xh:测试忘记密码验证码请求失败情况,验证空数据和错误邮箱格式的处理
# xh:测试空数据提交
resp = self . client . post (
path = reverse ( " account:forget_password_code " ) ,
data = dict ( )
data = dict ( ) # xh:空数据
)
self . assertEqual ( resp . content . decode ( " utf-8 " ) , " 错误的邮箱 " )
# xh:测试错误邮箱格式
resp = self . client . post (
path = reverse ( " account:forget_password_code " ) ,
data = dict ( email = " admin@com " )
data = dict ( email = " admin@com " ) # xh:无效邮箱格式
)
self . assertEqual ( resp . content . decode ( " utf-8 " ) , " 错误的邮箱 " )
def test_forget_password_email_success ( self ) :
#xh:测试密码重置成功流程,验证完整的密码重置功能
code = generate_code ( )
utils . set_code ( self . blog_user . email , code )
utils . set_code ( self . blog_user . email , code ) # xh:设置验证码
# xh:构造密码重置数据
data = dict (
new_password1 = self . new_test ,
new_password2 = self . new_test ,
email = self . blog_user . email ,
code = code ,
new_password1 = self . new_test , # xh:新密码
new_password2 = self . new_test , # xh:确认密码
email = self . blog_user . email , # xh:用户邮箱
code = code , # xh:正确验证码
)
# xh:提交密码重置请求
resp = self . client . post (
path = reverse ( " account:forget_password " ) ,
data = data
)
self . assertEqual ( resp . status_code , 302 )
self . assertEqual ( resp . status_code , 302 ) # xh:断言重定向(成功)
# 验证用户密码是否修改成功
# xh: 验证用户密码是否修改成功
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 )
self . assertNotEqual ( blog_user , None ) # xh:断言用户存在
self . assertEqual ( blog_user . check_password ( data [ " new_password1 " ] ) , True ) # xh:断言密码修改成功
def test_forget_password_email_not_user ( self ) :
#xh:测试不存在的用户密码重置,验证系统对不存在用户的处理
data = dict (
new_password1 = self . new_test ,
new_password2 = self . new_test ,
email = " 123@123.com " ,
email = " 123@123.com " , # xh:不存在的邮箱
code = " 123456 " ,
)
resp = self . client . post (
@ -186,22 +229,22 @@ class AccountTest(TestCase):
data = data
)
self . assertEqual ( resp . status_code , 200 )
self . assertEqual ( resp . status_code , 200 ) # xh:断言返回错误页面(非重定向)
def test_forget_password_email_code_error ( self ) :
#xh:测试验证码错误情况,验证错误验证码的处理
code = generate_code ( )
utils . set_code ( self . blog_user . email , code )
utils . set_code ( self . blog_user . email , code ) # xh:设置正确验证码
data = dict (
new_password1 = self . new_test ,
new_password2 = self . new_test ,
email = self . blog_user . email ,
code = " 111111 " ,
code = " 111111 " , # xh:错误验证码
)
resp = self . client . post (
path = reverse ( " account:forget_password " ) ,
data = data
)
self . assertEqual ( resp . status_code , 200 )
self . assertEqual ( resp . status_code , 200 ) # xh:断言验证失败(非重定向)