-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_watchlist.py
239 lines (201 loc) · 8.16 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import unittest
from watchlist import app,db
from watchlist.models import Movie , User
from watchlist.commands import forge, initdb
class WatchlistTestCase(unittest.TestCase):
def setUp(self):
app.config.update(
TESTING=True,
SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
)
db.create_all()
user = User(username='test', name='Test')
user.set_password('123')
movie = Movie(title='Test Movie Title', year='2021')
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.create_all()
def test_app_exit(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 Title', 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='2021'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Created Succeed!', data)
self.assertIn('New Movie', data)
response = self.client.post('/', data=dict(
title='',
year='2021'
), 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='New Movie',
year=''
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Item Created!', data)
self.assertIn('New Movie', 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 Title', data)
self.assertIn('2021', data)
response = self.client.get('/movie/edit/1', data=dict(
title='New Movie Edited',
year='2021'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Item Updated!', data)
self.assertIn('New Movie Edited', data)
response = self.client.post('/movie/edit/1', data=dict(
title='',
year='2021'
), follow_redirect=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item Updated!', data)
self.assertIn('Invalid input!', data)
response = self.client.post('/movie/edit/1', data=dict(
title='New Movie Edited',
year=''
), follow_redirect=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item updated', data)
self.assertNotIn('New Movie Edited Again', data)
self.assertIn('Invalid Input', 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_redirect=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 success', data)
self.assertIn('Invalid username or password', data)
response = self.client.post('/login', data=dict(
username='',
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='test',
password=''
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Login success', data)
self.assertIn('Invalid username or password', data)
def test_logout(self):
self.login()
response = self.client.get('/logout', follow_redirect=True)
data = response.get_data(as_text=True)
self.assertIn('Goodbye!', data)
self.assertNotIn('Logout!', data)
self.assertNotIn('Settings', data)
self.assertNotIn('Delete', data)
self.assertNotIn('Edit', data)
self.assertNotIn('<form method="post">', data)
def test_settings(self):
self.login()
response = self.client.get('/settings')
data = response.get_data(as_text=True)
self.assertIn('Settings', data)
self.assertIn('Your Name', data)
response = self.client.post('/settings', data=dict(
name='dang zhang'
), follow_redirect=True)
data = response.get_data(as_text=True)
self.assertIn('Settings Updated!', data)
self.assertIn('dang zhang', data)
response = self.client.post('/settings', data=dict(
name=''
), follow_redirect=True)
data = response.get_data(as_text=True)
self.assertNotIn('Settings Updated!', data)
self.assertIn('Invalid input', 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', 'admin', '--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, 'admin')
self.assertEqual(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()