2019-07-09 13:41:28 +00:00
|
|
|
from app import create_app, db
|
2021-11-15 13:10:28 +00:00
|
|
|
from app.models import User, Role, Permission
|
|
|
|
from tests import TestConfig
|
2021-09-15 10:31:53 +00:00
|
|
|
import time
|
|
|
|
import unittest
|
2019-07-08 08:52:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserModelTestCase(unittest.TestCase):
|
2019-07-09 13:41:28 +00:00
|
|
|
def setUp(self):
|
2021-09-15 10:31:53 +00:00
|
|
|
self.app = create_app(TestConfig)
|
2019-07-09 13:41:28 +00:00
|
|
|
self.app_context = self.app.app_context()
|
|
|
|
self.app_context.push()
|
|
|
|
db.create_all()
|
|
|
|
Role.insert_roles()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
db.session.remove()
|
|
|
|
db.drop_all()
|
|
|
|
self.app_context.pop()
|
|
|
|
|
2019-07-08 08:52:36 +00:00
|
|
|
def test_password_setter(self):
|
|
|
|
u = User(password='cat')
|
|
|
|
self.assertTrue(u.password_hash is not None)
|
|
|
|
|
|
|
|
def test_no_password_getter(self):
|
|
|
|
u = User(password='cat')
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
u.password
|
|
|
|
|
|
|
|
def test_password_verification(self):
|
|
|
|
u = User(password='cat')
|
|
|
|
self.assertTrue(u.verify_password('cat'))
|
|
|
|
self.assertFalse(u.verify_password('dog'))
|
|
|
|
|
|
|
|
def test_password_salts_are_random(self):
|
|
|
|
u = User(password='cat')
|
|
|
|
u2 = User(password='cat')
|
|
|
|
self.assertTrue(u.password_hash != u2.password_hash)
|
2019-07-08 14:09:00 +00:00
|
|
|
|
|
|
|
def test_valid_confirmation_token(self):
|
|
|
|
u = User(password='cat')
|
|
|
|
db.session.add(u)
|
|
|
|
db.session.commit()
|
|
|
|
token = u.generate_confirmation_token()
|
|
|
|
self.assertTrue(u.confirm(token))
|
|
|
|
|
|
|
|
def test_invalid_confirmation_token(self):
|
|
|
|
u1 = User(password='cat')
|
|
|
|
u2 = User(password='dog')
|
|
|
|
db.session.add(u1)
|
|
|
|
db.session.add(u2)
|
|
|
|
db.session.commit()
|
|
|
|
token = u1.generate_confirmation_token()
|
|
|
|
self.assertFalse(u2.confirm(token))
|
|
|
|
|
|
|
|
def test_expired_confirmation_token(self):
|
|
|
|
u = User(password='cat')
|
|
|
|
db.session.add(u)
|
|
|
|
db.session.commit()
|
|
|
|
token = u.generate_confirmation_token(1)
|
|
|
|
time.sleep(2)
|
|
|
|
self.assertFalse(u.confirm(token))
|
2019-07-09 13:41:28 +00:00
|
|
|
|
|
|
|
def test_user_role(self):
|
|
|
|
u = User(email='john@example.com', password='cat')
|
|
|
|
self.assertTrue(u.can(Permission.CREATE_JOB))
|
|
|
|
self.assertFalse(u.can(Permission.ADMIN))
|