From 9e92b60468a5b9bddecbd76468cbab4c1e7c5180 Mon Sep 17 00:00:00 2001 From: kelepirci Date: Sun, 17 Jul 2016 23:59:23 +0300 Subject: [PATCH] Adding new unittest cases This commit does not include any working code. I must fix the code in next commit. --- dash/models.py | 21 +++++++++++++++++++++ tests/test_basics.py | 6 ++++-- tests/test_user_model.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/dash/models.py b/dash/models.py index 36512a3..150be3d 100644 --- a/dash/models.py +++ b/dash/models.py @@ -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 '' % self.username diff --git a/tests/test_basics.py b/tests/test_basics.py index d8c3bbc..17a8d95 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -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']) \ No newline at end of file + self.assertTrue(current_app.config['TESTING']) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/test_user_model.py b/tests/test_user_model.py index ad917db..2122d13 100644 --- a/tests/test_user_model.py +++ b/tests/test_user_model.py @@ -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) \ No newline at end of file + 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() \ No newline at end of file