70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""schema 校验回归:mobile/email 格式 + click/sort 上限(防 INT 溢出 500)。"""
|
|
from datetime import date
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from modules.system.user.schemas import UserForm, UserInfoForm
|
|
from modules.article.schemas import ArticleForm
|
|
from modules.notice.schemas import NoticeForm
|
|
from modules.param.schemas import ParamForm
|
|
from modules.file_template.schemas import FileTemplateForm
|
|
|
|
|
|
def _base_user():
|
|
return dict(
|
|
realname='张三', nickname='张三', gender=1, avatar='a.png',
|
|
mobile='13800000000', email='a@b.com', birthday=date(2000, 1, 1),
|
|
deptId=1, levelId=1, positionId=1,
|
|
city=['110000', '110100', '110101'], address='北京市',
|
|
username='zhangsan', password='123456', status=1, sort=1, roles=[1, 2],
|
|
)
|
|
|
|
|
|
def test_user_mobile_accepts_valid_and_rejects_bad():
|
|
UserForm(**{**_base_user(), 'mobile': '13800000000'})
|
|
for bad in ('abc', '12345', '23800000000', '1380000000', '138000000001'):
|
|
with pytest.raises(ValidationError):
|
|
UserForm(**{**_base_user(), 'mobile': bad})
|
|
|
|
|
|
def test_user_email_accepts_valid_and_rejects_bad():
|
|
UserForm(**{**_base_user(), 'email': 'a@b.com'})
|
|
for bad in ('not-an-email', 'a@b', '@b.com', 'a b@c.com'):
|
|
with pytest.raises(ValidationError):
|
|
UserForm(**{**_base_user(), 'email': bad})
|
|
|
|
|
|
def test_user_info_form_mobile_email_format():
|
|
# 修改个人信息表单同样校验格式
|
|
base = dict(realname='张三', nickname='zs', gender=1, address='x')
|
|
UserInfoForm(**{**base, 'mobile': '13800000000', 'email': 'a@b.com'})
|
|
with pytest.raises(ValidationError):
|
|
UserInfoForm(**{**base, 'mobile': 'abc', 'email': 'a@b.com'})
|
|
with pytest.raises(ValidationError):
|
|
UserInfoForm(**{**base, 'mobile': '13800000000', 'email': 'bad'})
|
|
|
|
|
|
def test_article_click_overflow_rejected():
|
|
ArticleForm(title='t', click=2147483647, sort=1)
|
|
with pytest.raises(ValidationError):
|
|
ArticleForm(title='t', click=2147483648, sort=1)
|
|
|
|
|
|
def test_notice_click_overflow_rejected():
|
|
NoticeForm(title='t', source=1, url='u', status=1, isTop=1, click=2147483647)
|
|
with pytest.raises(ValidationError):
|
|
NoticeForm(title='t', source=1, url='u', status=1, isTop=1, click=2147483648)
|
|
|
|
|
|
def test_param_sort_overflow_rejected():
|
|
ParamForm(name='n', code='c', value='v', type=0, status=1, sort=99999)
|
|
with pytest.raises(ValidationError):
|
|
ParamForm(name='n', code='c', value='v', type=0, status=1, sort=100000)
|
|
|
|
|
|
def test_file_template_sort_overflow_rejected():
|
|
FileTemplateForm(sort=99999)
|
|
with pytest.raises(ValidationError):
|
|
FileTemplateForm(sort=100000)
|