1
- from project import app
2
1
import unittest
2
+ from flask .ext .testing import TestCase
3
+ from project import app , db
4
+ from project .models import User , BlogPost
3
5
4
6
5
- class FlaskTestCase (unittest .TestCase ):
7
+ class BaseTestCase (TestCase ):
8
+ """A base test case."""
9
+
10
+ def create_app (self ):
11
+ app .config .from_object ('config.TestConfig' )
12
+ return app
13
+
14
+ def setUp (self ):
15
+ db .create_all ()
16
+ db .session .add (BlogPost ("Test post" , "This is a test. Only a test." ))
17
+ db .session .add (User ("admin" , "ad@min.com" , "admin" ))
18
+ db .session .commit ()
19
+
20
+ def tearDown (self ):
21
+ db .session .remove ()
22
+ db .drop_all ()
23
+
24
+
25
+ class FlaskTestCase (BaseTestCase ):
6
26
7
27
# Ensure that Flask was set up correctly
8
28
def test_index (self ):
9
- tester = app .test_client (self )
10
- response = tester .get ('/login' , content_type = 'html/text' )
29
+ response = self .client .get ('/login' , content_type = 'html/text' )
11
30
self .assertEqual (response .status_code , 200 )
12
31
13
32
# Ensure that the login page loads correctly
14
33
def test_login_page_loads (self ):
15
- tester = app .test_client (self )
16
- response = tester .get ('/login' )
34
+ response = self .client .get ('/login' )
17
35
self .assertIn (b'Please login' , response .data )
18
36
19
37
# Ensure login behaves correctly with correct credentials
20
38
def test_correct_login (self ):
21
- tester = app . test_client ()
22
- response = tester .post (
39
+
40
+ response = self . client .post (
23
41
'/login' ,
24
42
data = dict (username = "admin" , password = "admin" ),
25
43
follow_redirects = True
@@ -28,46 +46,41 @@ def test_correct_login(self):
28
46
29
47
# Ensure login behaves correctly with incorrect credentials
30
48
def test_incorrect_login (self ):
31
- tester = app .test_client ()
32
- response = tester .post (
49
+ response = self .client .post (
33
50
'/login' ,
34
51
data = dict (username = "wrong" , password = "wrong" ),
35
52
follow_redirects = True
36
53
)
37
- self .assertIn (b'Invalid Credentials. Please try again .' , response .data )
54
+ self .assertIn (b'Invalid username or password .' , response .data )
38
55
39
56
# Ensure logout behaves correctly
40
57
def test_logout (self ):
41
- tester = app .test_client ()
42
- tester .post (
58
+ self .client .post (
43
59
'/login' ,
44
60
data = dict (username = "admin" , password = "admin" ),
45
61
follow_redirects = True
46
62
)
47
- response = tester .get ('/logout' , follow_redirects = True )
63
+ response = self . client .get ('/logout' , follow_redirects = True )
48
64
self .assertIn (b'You were logged out' , response .data )
49
65
50
66
# Ensure that main page requires user login
51
67
def test_main_route_requires_login (self ):
52
- tester = app .test_client ()
53
- response = tester .get ('/' , follow_redirects = True )
68
+ response = self .client .get ('/' , follow_redirects = True )
54
69
self .assertIn (b'You need to login first.' , response .data )
55
70
56
71
# Ensure that logout page requires user login
57
72
def test_logout_route_requires_login (self ):
58
- tester = app .test_client ()
59
- response = tester .get ('/logout' , follow_redirects = True )
73
+ response = self .client .get ('/logout' , follow_redirects = True )
60
74
self .assertIn (b'You need to login first.' , response .data )
61
75
62
76
# Ensure that posts show up on the main page
63
77
def test_posts_show_up_on_main_page (self ):
64
- tester = app .test_client ()
65
- response = tester .post (
78
+ response = self .client .post (
66
79
'/login' ,
67
80
data = dict (username = "admin" , password = "admin" ),
68
81
follow_redirects = True
69
82
)
70
- self .assertIn (b'Hello from the shell ' , response .data )
83
+ self .assertIn (b'This is a test. Only a test. ' , response .data )
71
84
72
85
73
86
if __name__ == '__main__' :
0 commit comments