forked from mtnbarreto/flask-base-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_model.py
More file actions
58 lines (46 loc) · 1.97 KB
/
test_user_model.py
File metadata and controls
58 lines (46 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# project/tests/test_user_model.py
from sqlalchemy.exc import IntegrityError
from project import db
from project.models.user import User
from tests.base import BaseTestCase
from tests.utils import add_user
class TestUserModel(BaseTestCase):
def test_add_user(self):
user = add_user('justatest', 'test@test.com', 'password')
self.assertTrue(user.id)
self.assertEqual(user.username, 'justatest')
self.assertEqual(user.email, 'test@test.com')
self.assertTrue(user.password)
self.assertTrue(user.active)
self.assertTrue(user.created_at)
def test_add_user_duplicate_username(self):
add_user('justatest', 'test@test.com', 'password')
duplicate_user = User(
username='justatest',
email='test@test2.com',
password='password'
)
db.session.add(duplicate_user)
self.assertRaises(IntegrityError, db.session.commit)
def test_add_user_duplicate_email(self):
add_user('justatest', 'test@test.com', 'password')
duplicate_user = User(
username='justatest2',
email='test@test.com',
password='password'
)
db.session.add(duplicate_user)
self.assertRaises(IntegrityError, db.session.commit)
def test_passwords_are_random(self):
user_one = add_user('justatest', 'test@test.com', 'password')
user_two = add_user('justatest2', 'test@test2.com', 'password')
self.assertNotEqual(user_one.password, user_two.password)
def test_encode_auth_token(self):
user = add_user('justatest', 'test@test.com', 'test')
auth_token = user.encode_auth_token()
self.assertTrue(isinstance(auth_token, bytes))
def test_decode_auth_token(self):
user = add_user('justatest', 'test@test.com', 'test')
auth_token = user.encode_auth_token()
self.assertTrue(isinstance(auth_token, bytes))
self.assertTrue(User.decode_auth_token(auth_token), user.id)