forked from miguelgrinberg/flasky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 15: Unit tests with the Flask test client (15b)
- Loading branch information
1 parent
ca278c2
commit 359a494
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import re | ||
import unittest | ||
from app import create_app, db | ||
from app.models import User, Role | ||
|
||
class FlaskClientTestCase(unittest.TestCase): | ||
def setUp(self): | ||
self.app = create_app('testing') | ||
self.app_context = self.app.app_context() | ||
self.app_context.push() | ||
db.create_all() | ||
Role.insert_roles() | ||
self.client = self.app.test_client(use_cookies=True) | ||
|
||
def tearDown(self): | ||
db.session.remove() | ||
db.drop_all() | ||
self.app_context.pop() | ||
|
||
def test_home_page(self): | ||
response = self.client.get('/') | ||
self.assertEqual(response.status_code, 200) | ||
self.assertTrue(b'Stranger' in response.data) | ||
|
||
def test_register_and_login(self): | ||
# register a new account | ||
response = self.client.post('/auth/register', data={ | ||
'email': '[email protected]', | ||
'username': 'john', | ||
'password': 'cat', | ||
'password2': 'cat' | ||
}) | ||
self.assertEqual(response.status_code, 302) | ||
|
||
# login with the new account | ||
response = self.client.post('/auth/login', data={ | ||
'email': '[email protected]', | ||
'password': 'cat' | ||
}, follow_redirects=True) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertTrue(re.search(b'Hello,\s+john!', response.data)) | ||
self.assertTrue( | ||
b'You have not confirmed your account yet' in response.data) | ||
|
||
# send a confirmation token | ||
user = User.query.filter_by(email='[email protected]').first() | ||
token = user.generate_confirmation_token() | ||
response = self.client.get('/auth/confirm/{}'.format(token), | ||
follow_redirects=True) | ||
user.confirm(token) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertTrue( | ||
b'You have confirmed your account' in response.data) | ||
|
||
# log out | ||
response = self.client.get('/auth/logout', follow_redirects=True) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertTrue(b'You have been logged out' in response.data) |