-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_watchlist.py
191 lines (154 loc) · 6.37 KB
/
test_watchlist.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
import unittest
from app import app, db, Movie, User, forge, initdb
class WatchlistTestCase(unittest.TestCase):
def setUp(self):
# 更新配置
app.config.update(
TESTING=True,
SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
)
db.create_all()
user = User(name='Test', username='test')
user.set_password('123')
movie = Movie(title='Test Movie', year='2019')
db.session.add_all([user, movie])
db.session.commit()
self.client = app.test_client()
self.runner = app.test_cli_runner()
def tearDown(self):
db.session.remove()
db.drop_all()
def test_app_exist(self):
self.assertIsNotNone(app)
def test_app_is_testing(self):
self.assertTrue(app.config['TESTING'])
def test_404_page(self):
response = self.client.get('/nothing')
data = response.get_data(as_text=True)
self.assertIn('Page Not Found - 404', data)
self.assertIn('Go Back', data)
self.assertEqual(response.status_code, 404)
def test_index_page(self):
response = self.client.get('/')
data =response.get_data(as_text=True)
self.assertIn('Test\'s Watchlist', data)
self.assertIn('Test Movie', data)
self.assertIn('2019', data)
self.assertEqual(response.status_code, 200)
def login(self):
self.client.post('/login', data=dict(
username='test',
password='123'
), follow_redirects=True) # 跟随重定向
def test_create_item(self):
self.login()
response = self.client.post('/', data = dict(
title='New Movie',
year='2019'
),follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Item created', data)
self.assertIn('New Movie', data)
response = self.client.post('/', data = dict(
title='',
year='2019'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item created', data)
self.assertIn('Invalid input', data)
response = self.client.post('/', data=dict(
title='New Movie',
year=''
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item Created', data)
self.assertIn('Invalid input', data)
def test_update_item(self):
self.login()
response = self.client.get('/movie/edit/1')
data = response.get_data(as_text=True)
self.assertIn('Edit item', data)
self.assertIn('Test Movie', data)
self.assertIn('2019', data)
response = self.client.post('/movie/edit/1', data=dict(
title='',
year='2019'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item Upadated', data)
self.assertIn('Invalid input', data)
response = self.client.post('/movie/edit/1', data=dict(
title='New Movie',
year=''
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Invalid input', data)
self.assertNotIn('Item updated', data)
# 测试删除条目
def test_delete_item(self):
self.login()
response = self.client.post('/movie/delete/1', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Item deleted.', data)
self.assertNotIn('Test Movie Title', data)
def test_login_protect(self):
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertNotIn('logout', data)
self.assertNotIn('Settings', data)
self.assertNotIn('<form method="post">', data)
self.assertNotIn('Delete', data)
self.assertNotIn('Edit', data)
def test_login(self):
response = self.client.post('/login', data=dict(
username='test',
password='123'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Login success.', data)
self.assertIn('Logout', data)
self.assertIn('Settings', data)
self.assertIn('Delete', data)
self.assertIn('Edit', data)
self.assertIn('<form method="post">', data)
response = self.client.post('/login', data=dict(
username='test',
password='456'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Login success.',data)
self.assertIn('Invalid username or password.',data)
response = self.client.post('/login',data=dict(
username='wrong',
password='123'
),follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Login successs.',data)
self.assertIn('Invalid username or password',data)
def test_forge_command(self):
result = self.runner.invoke(forge)
self.assertIn('Done.', result.output)
self.assertNotEqual(Movie.query.count(),0)
def test_initdb_command(self):
result = self.runner.invoke(initdb)
self.assertIn('Initialized database.', result.output)
def test_admin_command(self):
db.drop_all()
db.create_all()
result = self.runner.invoke(args=['admin', '--username', 'hn', '--password', '123'])
self.assertIn('Creating user...', result.output)
self.assertIn('Done.', result.output)
self.assertEqual(User.query.count(), 1)
self.assertEqual(User.query.first().username, 'hn')
self.assertTrue(User.query.first().validate_password('123'))
# 测试更新管理员账户
def test_admin_command_update(self):
result = self.runner.invoke(args=['admin', '--username', 'peter', '--password', '456'])
self.assertIn('Updating user...', result.output)
self.assertIn('Done.', result.output)
self.assertEqual(User.query.count(), 1)
self.assertEqual(User.query.first().username, 'peter')
self.assertTrue(User.query.first().validate_password('456'))
if __name__ == '__main__':
unittest.main()