-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.py
114 lines (98 loc) · 4.39 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
import os
import unittest
from shutil import copyfile
from unittest.mock import patch
from PIL import Image
from app import app
from model import insert_people
from utils import get_email_config
from utils import get_org_config
from utils import get_smtp_url
from jobs import SendEmailJob
class UtilTest(unittest.TestCase):
def test_getSMTP_url(self):
smtp_url = get_smtp_url('[email protected]')
self.assertEqual(smtp_url, 'smtp.163.com')
class WebAPITests(unittest.TestCase):
def test_0_send_email(self):
with patch("yagmail.SMTP") as mock_smtp:
SendEmailJob().send_notice_email() # threading中raise的错误不会被catch
def test_1_getUserInfo(self):
client = app.test_client()
response = client.get('/api/getUserInfo?token=token0')
self.assertEqual(json.loads(response.data)['code'], 0)
def test_2_submitUserInfo(self):
client = app.test_client()
with patch("yagmail.SMTP") as mock_smtp:
response = client.post('/api/submitUserInfo',
json={"token": "token0", "name": "new name"})
self.assertEqual(json.loads(response.data)['code'], 0)
def test_submitPictures(self):
client = app.test_client()
client.options('/api/uploadImage')
image_file = open('config/pic.jpg', 'rb')
response = client.post('/api/uploadImage',
data={'template': image_file},
headers={'token': '1234'})
res_json = json.loads(response.data.decode('ascii'))
self.assertEqual(res_json['code'], 0)
image_file.close()
def test_addUser(self):
client = app.test_client()
json_data = {"token": "1234", "email": ["[email protected]", "abc.org"]}
response = client.post('/api/addUserData', json=json_data)
res_json = json.loads(response.data.decode('ascii'))
self.assertEqual(res_json['code'], 0)
json_data = {"token": "1234", "email": ["[email protected],wuhan2020030001"]}
response = client.post('/api/addUserData', json=json_data)
res_json = json.loads(response.data.decode('ascii'))
self.assertEqual(res_json['code'], 0)
def test_addUserRejected(self):
client = app.test_client()
json_data = {"token": "5678", "email": ["[email protected]"]}
response = client.post('/api/addUserData', json=json_data)
res_json = json.loads(response.data.decode('ascii'))
self.assertEqual(res_json['code'], 1)
def test_updateOrgConfig(self):
client = app.test_client()
json_data = {"token": "1234",
"website": "https://community.wuhan2020.org.cn/",
"username": "[email protected]",
"password": "pswd"}
response = client.post('/api/updateOrgConfig',
json=json_data,
headers={'Referer': 'http://example.org/admin.html'})
res_json = json.loads(response.data.decode('ascii'))
self.assertEqual(res_json['code'], 0)
email_config = get_email_config()
self.assertEqual(email_config["server_address"], "smtp.example.org")
org_config = get_org_config()
self.assertEqual(org_config["frontend_url"], 'http://example.org/index.html')
def test_submitSendEmailRequest(self):
client = app.test_client()
json_data = {"token": "1234", "action": "send"}
response = client.post('/api/email', json=json_data)
res_json = json.loads(response.data.decode('ascii'))
self.assertEqual(res_json['code'], 0)
json_data = {"token": "4321"}
response = client.post('/api/email', json=json_data)
res_json = json.loads(response.data.decode('ascii'))
self.assertEqual(res_json['code'], 1)
class DbOperationTests(unittest.TestCase):
def test_insert_people(self):
insert_people('[email protected]', 'fake name')
try:
insert_people('[email protected]', 'fake name')
except:
pass
def test_insert_people_new_api(self):
insert_people('[email protected],2020030001','Yang Li')
if __name__ == '__main__':
copyfile('config/data.json', 'data.json')
if not os.path.isdir("images"):
os.mkdir("images")
if not os.path.exists('pic.jpg'):
img = Image.new('RGB', (750, 1200))
img.save('pic.jpg')
unittest.main()