1
+ '''
2
+ @Author: alphapenng
3
+ @Date: 2020-06-11 10:02:27
4
+ @LastEditors: alphapenng
5
+ @LastEditTime: 2020-06-11 16:50:26
6
+ @Description:
7
+ '''
8
+ import unittest
9
+
10
+ from app import app , db , Movie , User , forge , initdb
11
+
12
+ class WatchlistTestCase (unittest .TestCase ):
13
+ def setUp (self ):
14
+ app .config .update (
15
+ TESTING = True ,
16
+ SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
17
+ )
18
+ db .create_all ()
19
+ user = User (name = 'Test' , username = 'test' )
20
+ user .set_password ('123' )
21
+ movie = Movie (title = 'Test Movie Title' , year = '2019' )
22
+ db .session .add_all ([user , movie ])
23
+ db .session .commit ()
24
+
25
+ self .client = app .test_client ()
26
+ self .runner = app .test_cli_runner ()
27
+
28
+ def tearDown (self ):
29
+ db .session .remove ()
30
+ db .drop_all ()
31
+
32
+ def test_app_exist (self ):
33
+ self .assertIsNotNone (app )
34
+
35
+ def test_app_is_testing (self ):
36
+ self .assertTrue (app .config ['TESTING' ])
37
+
38
+ def test_404_page (self ):
39
+ response = self .client .get ('/nothing' )
40
+ data = response .get_data (as_text = True )
41
+ self .assertIn ('Page Not Found - 404' , data )
42
+ self .assertIn ('Go Back' , data )
43
+ self .assertEqual (response .status_code , 404 )
44
+
45
+ def test_index_page (self ):
46
+ response = self .client .get ('/' )
47
+ data = response .get_data (as_text = True )
48
+ self .assertIn ('Test\' s Watchlist' , data )
49
+ self .assertIn ('Test Movie Title' , data )
50
+ self .assertEqual (response .status_code , 200 )
51
+
52
+ def login (self ):
53
+ self .client .post ('/login' , data = dict (
54
+ username = 'test' ,
55
+ password = '123'
56
+ ), follow_redirects = True )
57
+
58
+ def test_create_item (self ):
59
+ self .login ()
60
+ response = self .client .post ('/' , data = dict (
61
+ title = 'New Movie' ,
62
+ year = '2019'
63
+ ), follow_redirects = True )
64
+ data = response .get_data (as_text = True )
65
+ self .assertIn ('Item created.' , data )
66
+ self .assertIn ('New Movie' , data )
67
+
68
+ response = self .client .post ('/' , data = dict (
69
+ title = '' ,
70
+ year = '2019'
71
+ ), follow_redirects = True )
72
+ data = response .get_data (as_text = True )
73
+ self .assertNotIn ('Item created.' , data )
74
+ self .assertIn ('Invalid input.' , data )
75
+
76
+ response = self .client .post ('/' , data = dict (
77
+ title = 'New Movie' ,
78
+ year = ''
79
+ ), follow_redirects = True )
80
+ data = response .get_data (as_text = True )
81
+ self .assertNotIn ('Item created.' , data )
82
+ self .assertIn ('Invalid input.' , data )
83
+
84
+ def test_update_item (self ):
85
+ self .login ()
86
+ response = self .client .get ('/movie/edit/1' )
87
+ data = response .get_data (as_text = True )
88
+ self .assertIn ('Edit item' , data )
89
+ self .assertIn ('Test Movie Title' , data )
90
+ self .assertIn ('2019' , data )
91
+
92
+ response = self .client .post ('/movie/edit/1' , data = dict (
93
+ title = 'New Movie Edited' ,
94
+ year = '2019'
95
+ ), follow_redirects = True )
96
+ data = response .get_data (as_text = True )
97
+ self .assertIn ('Item updated.' , data )
98
+ self .assertIn ('New Movie Edited' , data )
99
+
100
+ response = self .client .post ('/movie/edit/1' , data = dict (
101
+ title = '' ,
102
+ year = '2019'
103
+ ), follow_redirects = True )
104
+ data = response .get_data (as_text = True )
105
+ self .assertNotIn ('Item updated.' , data )
106
+ self .assertIn ('Invalid input.' , data )
107
+
108
+ response = self .client .post ('/movie/edit/1' , data = dict (
109
+ title = 'New Movie Edited Again' ,
110
+ year = ''
111
+ ), follow_redirects = True )
112
+ data = response .get_data (as_text = True )
113
+ self .assertNotIn ('Item updated.' , data )
114
+ self .assertNotIn ('New Movie Edited Again' , data )
115
+ self .assertIn ('Invalid input.' , data )
116
+
117
+ def test_delete_item (self ):
118
+ self .login ()
119
+ response = self .client .post ('/movie/delete/1' , follow_redirects = True )
120
+ data = response .get_data (as_text = True )
121
+ self .assertIn ('Item deleted.' , data )
122
+ self .assertNotIn ('Test Movie Title' , data )
123
+
124
+ def test_login_protect (self ):
125
+ response = self .client .get ('/' )
126
+ data = response .get_data (as_text = True )
127
+ self .assertNotIn ('Logout' , data )
128
+ self .assertNotIn ('Settings' , data )
129
+ self .assertNotIn ('<form method="POST">' , data )
130
+ self .assertNotIn ('Delete' , data )
131
+ # self.assertNotIn('Edit', data)
132
+
133
+ def test_login (self ):
134
+ response = self .client .post ('/login' , data = dict (
135
+ username = 'test' ,
136
+ password = '123'
137
+ ), follow_redirects = True )
138
+ data = response .get_data (as_text = True )
139
+ self .assertIn ('Login success.' , data )
140
+ self .assertIn ('Logout' , data )
141
+ self .assertIn ('Settings' , data )
142
+ self .assertIn ('Delete' , data )
143
+ self .assertIn ('Edit' , data )
144
+ self .assertIn ('<form method="POST">' , data )
145
+
146
+ response = self .client .post ('/login' , data = dict (
147
+ username = 'test' ,
148
+ password = '456'
149
+ ), follow_redirects = True )
150
+ data = response .get_data (as_text = True )
151
+ self .assertNotIn ('Login success.' , data )
152
+ self .assertIn ('Invalid username or password.' , data )
153
+
154
+ response = self .client .post ('/login' , data = dict (
155
+ username = 'wrong' ,
156
+ password = '123'
157
+ ), follow_redirects = True )
158
+ data = response .get_data (as_text = True )
159
+ self .assertNotIn ('Login success.' , data )
160
+ self .assertIn ('Invalid username or password.' , data )
161
+
162
+ response = self .client .post ('/login' , data = dict (
163
+ username = '' ,
164
+ password = '123'
165
+ ), follow_redirects = True )
166
+ data = response .get_data (as_text = True )
167
+ self .assertNotIn ('Login success.' , data )
168
+ self .assertIn ('Invalid input.' , data )
169
+
170
+ response = self .client .post ('/login' , data = dict (
171
+ username = 'test' ,
172
+ password = ''
173
+ ), follow_redirects = True )
174
+ data = response .get_data (as_text = True )
175
+ self .assertNotIn ('Login success.' , data )
176
+ self .assertIn ('Invalid input.' , data )
177
+
178
+ def test_logout (self ):
179
+ self .login ()
180
+ response = self .client .get ('/logout' , follow_redirects = True )
181
+ data = response .get_data (as_text = True )
182
+ self .assertIn ('Goodbye.' , data )
183
+ self .assertNotIn ('Logout' , data )
184
+ self .assertNotIn ('Settings' , data )
185
+ self .assertNotIn ('Delete' , data )
186
+ # self.assertNotIn('Edit', data)
187
+ self .assertNotIn ('<form method="POST">' , data )
188
+
189
+ def test_settings (self ):
190
+ self .login ()
191
+ response = self .client .get ('/settings' )
192
+ data = response .get_data (as_text = True )
193
+ self .assertIn ('Settings' , data )
194
+ self .assertIn ('Your Name' , data )
195
+
196
+ response = self .client .post ('/settings' , data = dict (
197
+ name = 'Grey Li' ,
198
+ ), follow_redirects = True )
199
+ data = response .get_data (as_text = True )
200
+ self .assertIn ('Settings updated.' , data )
201
+ self .assertIn ('Grey Li' , data )
202
+
203
+ response = self .client .post ('/settings' , data = dict (
204
+ name = '' ,
205
+ ), follow_redirects = True )
206
+ data = response .get_data (as_text = True )
207
+ self .assertNotIn ('Settings updated.' , data )
208
+ self .assertIn ('Invalid input.' , data )
209
+
210
+ def test_forge_command (self ):
211
+ result = self .runner .invoke (forge )
212
+ self .assertIn ('Done.' , result .output )
213
+ self .assertNotEqual (Movie .query .count (), 0 )
214
+
215
+ def test_initdb_command (self ):
216
+ result = self .runner .invoke (initdb )
217
+ self .assertIn ('Initialized database.' , result .output )
218
+
219
+ def test_admin_command (self ):
220
+ db .drop_all ()
221
+ db .create_all ()
222
+ result = self .runner .invoke (args = ['admin' , '--username' , 'grey' , '--password' , '123' ])
223
+ self .assertIn ('Creating user...' , result .output )
224
+ self .assertIn ('Done.' , result .output )
225
+ self .assertEqual (User .query .count (), 1 )
226
+ self .assertEqual (User .query .first ().username , 'grey' )
227
+ self .assertTrue (User .query .first ().validate_password ('123' ))
228
+
229
+ def test_admin_command_update (self ):
230
+ result = self .runner .invoke (args = ['admin' , '--username' , 'peter' , '--password' , '456' ])
231
+ self .assertIn ('Updating user...' , result .output )
232
+ self .assertIn ('Done.' , result .output )
233
+ self .assertEqual (User .query .count (), 1 )
234
+ self .assertEqual (User .query .first ().username , 'peter' )
235
+ self .assertTrue (User .query .first ().validate_password ('456' ))
236
+
237
+ if __name__ == '__main__' :
238
+ unittest .main ()
0 commit comments