| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  |     MiniTwit Tests | 
					
						
							|  |  |  |     ~~~~~~~~~~~~~~ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Tests the MiniTwit application. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 02:21:07 +08:00
										 |  |  |     :copyright: (c) 2014 by Armin Ronacher. | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |     :license: BSD, see LICENSE for more details. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2010-04-21 02:01:00 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | import minitwit | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | import tempfile | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MiniTwitTestCase(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         """Before each test, set up a blank database""" | 
					
						
							| 
									
										
										
										
											2010-05-28 03:17:25 +08:00
										 |  |  |         self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp() | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         self.app = minitwit.app.test_client() | 
					
						
							| 
									
										
										
										
											2014-05-02 18:46:04 +08:00
										 |  |  |         with minitwit.app.app_context(): | 
					
						
							|  |  |  |             minitwit.init_db() | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +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(minitwit.app.config['DATABASE']) | 
					
						
							| 
									
										
										
										
											2010-04-21 02:01:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |     # helper functions | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def register(self, username, password, password2=None, email=None): | 
					
						
							|  |  |  |         """Helper function to register a user""" | 
					
						
							|  |  |  |         if password2 is None: | 
					
						
							|  |  |  |             password2 = password | 
					
						
							|  |  |  |         if email is None: | 
					
						
							|  |  |  |             email = username + '@example.com' | 
					
						
							|  |  |  |         return self.app.post('/register', data={ | 
					
						
							|  |  |  |             'username':     username, | 
					
						
							|  |  |  |             'password':     password, | 
					
						
							|  |  |  |             'password2':    password2, | 
					
						
							|  |  |  |             'email':        email, | 
					
						
							|  |  |  |         }, follow_redirects=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def login(self, username, password): | 
					
						
							|  |  |  |         """Helper function to login""" | 
					
						
							|  |  |  |         return self.app.post('/login', data={ | 
					
						
							|  |  |  |             'username': username, | 
					
						
							|  |  |  |             'password': password | 
					
						
							|  |  |  |         }, follow_redirects=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def register_and_login(self, username, password): | 
					
						
							|  |  |  |         """Registers and logs in in one go""" | 
					
						
							|  |  |  |         self.register(username, password) | 
					
						
							|  |  |  |         return self.login(username, password) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def logout(self): | 
					
						
							|  |  |  |         """Helper function to logout""" | 
					
						
							|  |  |  |         return self.app.get('/logout', follow_redirects=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def add_message(self, text): | 
					
						
							|  |  |  |         """Records a message""" | 
					
						
							|  |  |  |         rv = self.app.post('/add_message', data={'text': text}, | 
					
						
							|  |  |  |                                     follow_redirects=True) | 
					
						
							|  |  |  |         if text: | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |             assert b'Your message was recorded' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         return rv | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # testing functions | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_register(self): | 
					
						
							|  |  |  |         """Make sure registering works""" | 
					
						
							|  |  |  |         rv = self.register('user1', 'default') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You were successfully registered ' \ | 
					
						
							|  |  |  |                b'and can login now' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.register('user1', 'default') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'The username is already taken' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.register('', 'default') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You have to enter a username' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.register('meh', '') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You have to enter a password' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.register('meh', 'x', 'y') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'The two passwords do not match' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.register('meh', 'foo', email='broken') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You have to enter a valid email address' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_login_logout(self): | 
					
						
							|  |  |  |         """Make sure logging in and logging out works""" | 
					
						
							|  |  |  |         rv = self.register_and_login('user1', 'default') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You were logged in' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.logout() | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You were logged out' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.login('user1', 'wrongpassword') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'Invalid password' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.login('user2', 'wrongpassword') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'Invalid username' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_message_recording(self): | 
					
						
							|  |  |  |         """Check if adding messages works""" | 
					
						
							|  |  |  |         self.register_and_login('foo', 'default') | 
					
						
							|  |  |  |         self.add_message('test message 1') | 
					
						
							|  |  |  |         self.add_message('<test message 2>') | 
					
						
							|  |  |  |         rv = self.app.get('/') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'test message 1' in rv.data | 
					
						
							|  |  |  |         assert b'<test message 2>' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_timelines(self): | 
					
						
							|  |  |  |         """Make sure that timelines work""" | 
					
						
							|  |  |  |         self.register_and_login('foo', 'default') | 
					
						
							|  |  |  |         self.add_message('the message by foo') | 
					
						
							|  |  |  |         self.logout() | 
					
						
							|  |  |  |         self.register_and_login('bar', 'default') | 
					
						
							|  |  |  |         self.add_message('the message by bar') | 
					
						
							|  |  |  |         rv = self.app.get('/public') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'the message by foo' in rv.data | 
					
						
							|  |  |  |         assert b'the message by bar' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # bar's timeline should just show bar's message | 
					
						
							|  |  |  |         rv = self.app.get('/') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'the message by foo' not in rv.data | 
					
						
							|  |  |  |         assert b'the message by bar' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # now let's follow foo | 
					
						
							|  |  |  |         rv = self.app.get('/foo/follow', follow_redirects=True) | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You are now following "foo"' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # we should now see foo's message | 
					
						
							|  |  |  |         rv = self.app.get('/') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'the message by foo' in rv.data | 
					
						
							|  |  |  |         assert b'the message by bar' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # but on the user's page we only want the user's message | 
					
						
							|  |  |  |         rv = self.app.get('/bar') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'the message by foo' not in rv.data | 
					
						
							|  |  |  |         assert b'the message by bar' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.app.get('/foo') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'the message by foo' in rv.data | 
					
						
							|  |  |  |         assert b'the message by bar' not in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # now unfollow and check if that worked | 
					
						
							|  |  |  |         rv = self.app.get('/foo/unfollow', follow_redirects=True) | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'You are no longer following "foo"' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  |         rv = self.app.get('/') | 
					
						
							| 
									
										
										
										
											2013-05-26 01:13:48 +08:00
										 |  |  |         assert b'the message by foo' not in rv.data | 
					
						
							|  |  |  |         assert b'the message by bar' in rv.data | 
					
						
							| 
									
										
										
										
											2010-04-12 00:45:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |