MCPcopy Index your code
hub / github.com/realpython/discover-flask / TestUser

Class TestUser

tests/test_users.py:14–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13
14class TestUser(BaseTestCase):
15
16 # Ensure user can register
17 def test_user_registeration(self):
18 with self.client:
19 response = self.client.post('register/', data=dict(
20 username='Michael', email='michael@realpython.com',
21 password='python', confirm='python'
22 ), follow_redirects=True)
23 self.assertIn(b'Welcome to Flask!', response.data)
24 self.assertTrue(current_user.name == "Michael")
25 self.assertTrue(current_user.is_active())
26 user = User.query.filter_by(email='michael@realpython.com').first()
27 self.assertTrue(str(user) == '<name - Michael>')
28
29 # Ensure errors are thrown during an incorrect user registration
30 def test_incorrect_user_registeration(self):
31 with self.client:
32 response = self.client.post('register/', data=dict(
33 username='Michael', email='michael',
34 password='python', confirm='python'
35 ), follow_redirects=True)
36 self.assertIn(b'Invalid email address.', response.data)
37 self.assertIn(b'/register/', request.url)
38
39 # Ensure id is correct for the current/logged in user
40 def test_get_by_id(self):
41 with self.client:
42 self.client.post('/login', data=dict(
43 username="admin", password='admin'
44 ), follow_redirects=True)
45 self.assertTrue(current_user.id == 1)
46 self.assertFalse(current_user.id == 20)
47
48 # Ensure given password is correct after unhashing
49 def test_check_password(self):
50 user = User.query.filter_by(email='ad@min.com').first()
51 self.assertTrue(bcrypt.check_password_hash(user.password, 'admin'))
52 self.assertFalse(bcrypt.check_password_hash(user.password, 'foobar'))
53
54
55class UserViewsTests(BaseTestCase):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected