|
| 1 | +# tests/test_models.py |
| 2 | + |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +from flask.ext.login import current_user |
| 7 | + |
| 8 | +from base import BaseTestCase |
| 9 | +from project import bcrypt |
| 10 | +from project.models import User |
| 11 | + |
| 12 | + |
| 13 | +class TestUser(BaseTestCase): |
| 14 | + |
| 15 | + # Ensure user can register |
| 16 | + def test_user_registeration(self): |
| 17 | + with self.client: |
| 18 | + response = self.client.post('register/', data=dict( |
| 19 | + username='Michael', email='michael@realpython.com', |
| 20 | + password='python', confirm='python' |
| 21 | + ), follow_redirects=True) |
| 22 | + self.assertIn(b'Welcome to Flask!', response.data) |
| 23 | + self.assertTrue(current_user.name == "Michael") |
| 24 | + self.assertTrue(current_user.is_active()) |
| 25 | + user = User.query.filter_by(email='michael@realpython.com').first() |
| 26 | + self.assertTrue(str(user) == '<name - Michael>') |
| 27 | + |
| 28 | + def test_get_by_id(self): |
| 29 | + # Ensure id is correct for the current/logged in user |
| 30 | + with self.client: |
| 31 | + self.client.post('/login', data=dict( |
| 32 | + username="admin", password='admin' |
| 33 | + ), follow_redirects=True) |
| 34 | + self.assertTrue(current_user.id == 1) |
| 35 | + self.assertFalse(current_user.id == 20) |
| 36 | + |
| 37 | + def test_check_password(self): |
| 38 | + # Ensure given password is correct after unhashing |
| 39 | + user = User.query.filter_by(email='ad@min.com').first() |
| 40 | + self.assertTrue(bcrypt.check_password_hash(user.password, 'admin')) |
| 41 | + self.assertFalse(bcrypt.check_password_hash(user.password, 'foobar')) |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + unittest.main() |
0 commit comments