Adding new unittest cases
This commit does not include any working code. I must fix the code in next commit.
This commit is contained in:
parent
52a22d418a
commit
9e92b60468
|
@ -1,6 +1,9 @@
|
|||
import dateutil.parser
|
||||
import datetime
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from flask_login import UserMixin
|
||||
|
||||
|
@ -33,6 +36,7 @@ class User(UserMixin, db.Model):
|
|||
avatar = db.Column(db.String(255), index=True)
|
||||
created_at = db.Column(db.DateTime)
|
||||
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
|
||||
confirmed = db.Column(db.Boolean, default=False)
|
||||
|
||||
@property
|
||||
def password(self):
|
||||
|
@ -44,6 +48,23 @@ class User(UserMixin, db.Model):
|
|||
|
||||
def verify_password(self, password):
|
||||
return check_password_hash(self.password_hash, password)
|
||||
|
||||
# user email confirmation
|
||||
def generate_confirmation_token(self, expiration=3600):
|
||||
s = Serializer(current_app.config['SECRET_KEY'], expiration)
|
||||
return s.dumps({'confirm': self.id})
|
||||
|
||||
def confirm(self, token):
|
||||
s = Serializer(current_app.config['SECRET_KEY'])
|
||||
try:
|
||||
data = s.loads(token)
|
||||
except:
|
||||
return False
|
||||
if data.get('confirm') != self.id:
|
||||
return False
|
||||
self.confirmed = True
|
||||
db.session.add(self)
|
||||
return True
|
||||
|
||||
def __repr__(self):
|
||||
return '<User %r>' % self.username
|
||||
|
|
|
@ -2,7 +2,6 @@ import unittest
|
|||
from flask import current_app
|
||||
from dash import create_app, db
|
||||
|
||||
|
||||
class BasicsTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dash = create_app('testing')
|
||||
|
@ -19,4 +18,7 @@ class BasicsTestCase(unittest.TestCase):
|
|||
self.assertFalse(current_app is None)
|
||||
|
||||
def test_app_is_testing(self):
|
||||
self.assertTrue(current_app.config['TESTING'])
|
||||
self.assertTrue(current_app.config['TESTING'])
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -1,7 +1,23 @@
|
|||
import datetime
|
||||
import unittest
|
||||
|
||||
from dash.models import User
|
||||
from dash import db, create_app
|
||||
|
||||
from flask import current_app
|
||||
|
||||
class UserModelTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dash = create_app('testing')
|
||||
self.app_context = self.dash.app_context()
|
||||
self.app_context.push()
|
||||
db.create_all()
|
||||
|
||||
def tearDown(self):
|
||||
db.session.remove()
|
||||
db.drop_all()
|
||||
self.app_context.pop()
|
||||
|
||||
def test_password_setter(self):
|
||||
u = User(password = 'cat')
|
||||
self.assertTrue(u.password_hash is not None)
|
||||
|
@ -19,4 +35,18 @@ class UserModelTestCase(unittest.TestCase):
|
|||
def test_password_salts_are_random(self):
|
||||
u = User(password='cat')
|
||||
u2 = User(password='cat')
|
||||
self.assertTrue(u.password_hash != u2.password_hash)
|
||||
self.assertTrue(u.password_hash != u2.password_hash)
|
||||
|
||||
def test_create_user_and_check(self):
|
||||
u = User(email="test@dash-stack.org",
|
||||
username="test",
|
||||
full_name="Dash Stack",
|
||||
password="test",
|
||||
avatar="/static/img/user2-160x160.jpg",
|
||||
created_at=datetime.datetime.now())
|
||||
db.session.add(u)
|
||||
u = User.query.filter_by(email="test@dash-stack.org").first()
|
||||
self.assertTrue(u.email == "test@dash-stack.org")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in New Issue