-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
96 lines (77 loc) · 2.84 KB
/
test.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import unittest
import bcrypt
import sqlalchemy
from flask_sqlalchemy import SQLAlchemy
from teaching_site import app, db
from teaching_site.user.models import *
from teaching_site.question.models import *
#from post.models import *
class UserTest(unittest.TestCase):
def setUp(self):
self.db_uri = 'sqlite:///%s/teaching_site/data/test_data.db' % os.path.dirname(__file__)
app.config['TESTING'] = True
# CSRF do not respond to script input, i.e. hidden_tag
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = self.db_uri
db.session.commit()
db.create_all()
password = bcrypt.hashpw('test', bcrypt.gensalt())
test_notvalid = User(
fullname = 'test not valid',
email='test@email',
username='validate',
password = password,
validation_code = '123456',
is_admin=False,
validated=False)
test_user = User(
fullname = 'test user',
email='user@email',
username='user',
password = password,
password_tmp = 'test_tmp',
is_admin=False,
validated=True)
test_admin = User(
fullname = 'test admin',
email='admin@email',
username='admin',
password = password,
is_admin=True,
validated=True)
db.session.add(test_notvalid)
db.session.add(test_user)
db.session.add(test_admin)
db.session.commit()
self.app = app.test_client()
def tearDown(self):
db.session.commit()
db.drop_all()
db.session.remove()
def login(self, user, passwd):
return self.app.post('/login', data=dict(
username = user,
password = passwd,
), follow_redirects = True)
def test_normal_login(self):
rv = self.login('user', 'test')
assert 'login as user' in str(rv.data)
assert 'Home' in str(rv.data)
def test_notvalidated_login(self):
rv = self.login('validate', 'test')
assert 'Loged in as validate' in str(rv.data)
assert 'User Data' in str(rv.data)
def test_admin_login(self):
rv = self.login('admin', 'test')
assert 'Loged in as admin' in str(rv.data)
assert 'Admin' in str(rv.data)
def test_password_tmp(self):
rv = self.login('user', 'test_tmp')
assert 'temporary password expired' in str(rv.data)
assert 'Loged in as user' in str(rv.data)
assert 'Repeat password' in str(rv.data)
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(UserTest)
unittest.TextTestRunner(verbosity=2).run(suite)