Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 150 lines (125 sloc) 5.238 kb
f2dc38c @mitsuhiko Added tests for minitwit. Testing with Flask is awesome
authored
1 # -*- coding: utf-8 -*-
2 """
3 MiniTwit Tests
4 ~~~~~~~~~~~~~~
5
6 Tests the MiniTwit application.
7
8 :copyright: (c) 2010 by Armin Ronacher.
9 :license: BSD, see LICENSE for more details.
10 """
3053fcd @mitsuhiko Make the example tests pass on Windows.
authored
11 import os
f2dc38c @mitsuhiko Added tests for minitwit. Testing with Flask is awesome
authored
12 import minitwit
13 import unittest
14 import tempfile
15
16
17 class MiniTwitTestCase(unittest.TestCase):
18
19 def setUp(self):
20 """Before each test, set up a blank database"""
dfecc86 @mitsuhiko Ported examples over to new config. documented upgrading
authored
21 self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
f2dc38c @mitsuhiko Added tests for minitwit. Testing with Flask is awesome
authored
22 self.app = minitwit.app.test_client()
23 minitwit.init_db()
24
3053fcd @mitsuhiko Make the example tests pass on Windows.
authored
25 def tearDown(self):
26 """Get rid of the database again after each test."""
27 os.close(self.db_fd)
dfecc86 @mitsuhiko Ported examples over to new config. documented upgrading
authored
28 os.unlink(minitwit.app.config['DATABASE'])
3053fcd @mitsuhiko Make the example tests pass on Windows.
authored
29
f2dc38c @mitsuhiko Added tests for minitwit. Testing with Flask is awesome
authored
30 # helper functions
31
32 def register(self, username, password, password2=None, email=None):
33 """Helper function to register a user"""
34 if password2 is None:
35 password2 = password
36 if email is None:
37 email = username + '@example.com'
38 return self.app.post('/register', data={
39 'username': username,
40 'password': password,
41 'password2': password2,
42 'email': email,
43 }, follow_redirects=True)
44
45 def login(self, username, password):
46 """Helper function to login"""
47 return self.app.post('/login', data={
48 'username': username,
49 'password': password
50 }, follow_redirects=True)
51
52 def register_and_login(self, username, password):
53 """Registers and logs in in one go"""
54 self.register(username, password)
55 return self.login(username, password)
56
57 def logout(self):
58 """Helper function to logout"""
59 return self.app.get('/logout', follow_redirects=True)
60
61 def add_message(self, text):
62 """Records a message"""
63 rv = self.app.post('/add_message', data={'text': text},
64 follow_redirects=True)
65 if text:
66 assert 'Your message was recorded' in rv.data
67 return rv
68
69 # testing functions
70
71 def test_register(self):
72 """Make sure registering works"""
73 rv = self.register('user1', 'default')
74 assert 'You were successfully registered ' \
75 'and can login now' in rv.data
76 rv = self.register('user1', 'default')
77 assert 'The username is already taken' in rv.data
78 rv = self.register('', 'default')
79 assert 'You have to enter a username' in rv.data
80 rv = self.register('meh', '')
81 assert 'You have to enter a password' in rv.data
82 rv = self.register('meh', 'x', 'y')
83 assert 'The two passwords do not match' in rv.data
84 rv = self.register('meh', 'foo', email='broken')
85 assert 'You have to enter a valid email address' in rv.data
86
87 def test_login_logout(self):
88 """Make sure logging in and logging out works"""
89 rv = self.register_and_login('user1', 'default')
90 assert 'You were logged in' in rv.data
91 rv = self.logout()
92 assert 'You were logged out' in rv.data
93 rv = self.login('user1', 'wrongpassword')
94 assert 'Invalid password' in rv.data
95 rv = self.login('user2', 'wrongpassword')
96 assert 'Invalid username' in rv.data
97
98 def test_message_recording(self):
99 """Check if adding messages works"""
100 self.register_and_login('foo', 'default')
101 self.add_message('test message 1')
102 self.add_message('<test message 2>')
103 rv = self.app.get('/')
104 assert 'test message 1' in rv.data
105 assert '&lt;test message 2&gt;' in rv.data
106
107 def test_timelines(self):
108 """Make sure that timelines work"""
109 self.register_and_login('foo', 'default')
110 self.add_message('the message by foo')
111 self.logout()
112 self.register_and_login('bar', 'default')
113 self.add_message('the message by bar')
114 rv = self.app.get('/public')
115 assert 'the message by foo' in rv.data
116 assert 'the message by bar' in rv.data
117
118 # bar's timeline should just show bar's message
119 rv = self.app.get('/')
120 assert 'the message by foo' not in rv.data
121 assert 'the message by bar' in rv.data
122
123 # now let's follow foo
124 rv = self.app.get('/foo/follow', follow_redirects=True)
125 assert 'You are now following &#34;foo&#34;' in rv.data
126
127 # we should now see foo's message
128 rv = self.app.get('/')
129 assert 'the message by foo' in rv.data
130 assert 'the message by bar' in rv.data
131
132 # but on the user's page we only want the user's message
133 rv = self.app.get('/bar')
134 assert 'the message by foo' not in rv.data
135 assert 'the message by bar' in rv.data
136 rv = self.app.get('/foo')
137 assert 'the message by foo' in rv.data
138 assert 'the message by bar' not in rv.data
139
140 # now unfollow and check if that worked
141 rv = self.app.get('/foo/unfollow', follow_redirects=True)
142 assert 'You are no longer following &#34;foo&#34;' in rv.data
143 rv = self.app.get('/')
144 assert 'the message by foo' not in rv.data
145 assert 'the message by bar' in rv.data
146
147
148 if __name__ == '__main__':
149 unittest.main()
Something went wrong with that request. Please try again.