46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
import unittest
|
|
|
|
from app.views import *
|
|
from db.database_manager import DatabaseManager # 确保你可以导入DatabaseManager
|
|
|
|
class LoginTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
# 设置 Flask 测试模式
|
|
app.testing = True
|
|
self.client = app.test_client()
|
|
|
|
def test_login_get(self):
|
|
# 测试 GET 请求返回登录页面
|
|
response = self.client.get('/login')
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn('text/html', response.content_type)
|
|
|
|
def test_successful_login_post(self):
|
|
# 测试有效的登录 POST 请求
|
|
with self.client:
|
|
response = self.client.post('/login', data={
|
|
'number': 'G0001',
|
|
'password': '1'
|
|
})
|
|
# 根据你的应用逻辑调整断言
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_invalid_login_post(self):
|
|
# 测试无效的登录 POST 请求
|
|
response = self.client.post('/login', data={
|
|
'number': 'admin',
|
|
'password': 'admin'
|
|
})
|
|
# 将返回的数据解析为JSON
|
|
json_data = response.get_json()
|
|
|
|
# 确认JSON响应中的值
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertFalse(json_data['success'])
|
|
self.assertEqual(json_data['message'], "无效的用户名或密码")
|
|
|
|
# 运行测试
|
|
if __name__ == '__main__':
|
|
unittest.main()
|