| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  |     Flaskr Tests | 
					
						
							|  |  |  |     ~~~~~~~~~~~~ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Tests the Flaskr application. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 02:21:07 +08:00
										 |  |  |     :copyright: (c) 2014 by Armin Ronacher. | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |     :license: BSD, see LICENSE for more details. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2010-04-21 02:01:00 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | import flaskr | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | import tempfile | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FlaskrTestCase(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         """Before each test, set up a blank database""" | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() | 
					
						
							| 
									
										
										
										
											2011-06-18 03:53:11 +08:00
										 |  |  |         flaskr.app.config['TESTING'] = True | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |         self.app = flaskr.app.test_client() | 
					
						
							| 
									
										
										
										
											2014-05-02 18:46:04 +08:00
										 |  |  |         with flaskr.app.app_context(): | 
					
						
							|  |  |  |             flaskr.init_db() | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-21 02:01:00 +08:00
										 |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         """Get rid of the database again after each test.""" | 
					
						
							|  |  |  |         os.close(self.db_fd) | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         os.unlink(flaskr.app.config['DATABASE']) | 
					
						
							| 
									
										
										
										
											2010-04-21 02:01:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |     def login(self, username, password): | 
					
						
							|  |  |  |         return self.app.post('/login', data=dict( | 
					
						
							|  |  |  |             username=username, | 
					
						
							|  |  |  |             password=password | 
					
						
							|  |  |  |         ), follow_redirects=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def logout(self): | 
					
						
							|  |  |  |         return self.app.get('/logout', follow_redirects=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # testing functions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-16 08:03:45 +08:00
										 |  |  |     def test_empty_db(self): | 
					
						
							|  |  |  |         """Start with a blank database.""" | 
					
						
							|  |  |  |         rv = self.app.get('/') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'No entries here so far' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-16 08:03:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |     def test_login_logout(self): | 
					
						
							|  |  |  |         """Make sure login and logout works""" | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         rv = self.login(flaskr.app.config['USERNAME'], | 
					
						
							|  |  |  |                         flaskr.app.config['PASSWORD']) | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You were logged in' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |         rv = self.logout() | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You were logged out' in rv.data | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         rv = self.login(flaskr.app.config['USERNAME'] + 'x', | 
					
						
							|  |  |  |                         flaskr.app.config['PASSWORD']) | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'Invalid username' in rv.data | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         rv = self.login(flaskr.app.config['USERNAME'], | 
					
						
							|  |  |  |                         flaskr.app.config['PASSWORD'] + 'x') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'Invalid password' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_messages(self): | 
					
						
							|  |  |  |         """Test that messages work""" | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         self.login(flaskr.app.config['USERNAME'], | 
					
						
							|  |  |  |                    flaskr.app.config['PASSWORD']) | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  |         rv = self.app.post('/add', data=dict( | 
					
						
							|  |  |  |             title='<Hello>', | 
					
						
							|  |  |  |             text='<strong>HTML</strong> allowed here' | 
					
						
							|  |  |  |         ), follow_redirects=True) | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'No entries here so far' not in rv.data | 
					
						
							|  |  |  |         assert b'<Hello>' in rv.data | 
					
						
							|  |  |  |         assert b'<strong>HTML</strong> allowed here' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-14 22:44:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |