-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_models.py
More file actions
111 lines (64 loc) · 2.61 KB
/
test_user_models.py
File metadata and controls
111 lines (64 loc) · 2.61 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
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
97
98
99
100
101
102
103
104
105
"""
Test of user creation, login and authentication
Running test file: comment out populate_standings_table() in app.py prior to running tests
run these tests with following command:
python -m unittest test_app.py
"""
import os
from unittest import TestCase
from sqlalchemy import exc
from models import db, User, Prediction_top
# Before app is imported, set an environmental variable
# to use test database. This needs to be done
# before app is imported, otherwise it will use the live database
os.environ['DATABASE_URL'] = "postgresql:///matchday_test"
from app import app
class UserModelTestCase(TestCase):
"""Test model for users"""
def setUp(self):
"""Drop database, removing test data and recreate for new tests"""
db.drop_all()
db.create_all()
u1 = User.signup("User_john", "John", "Smith", "john@gmail.com", "password", "Chelsea")
id1 = 1111
u1.id = id1
u2 = User.signup("User_sam", "Sam", "McClean", "sam@gmail.com", "password2", "Arsenal")
id2 = 2222
u2.id = id2
db.session.commit()
u1 = User.query.get(id1)
u2 = User.query.get(id2)
self.u1 = u1
self.id1 = id1
self.u2 = u2
self.id2 = id2
self.client = app.test_client()
def tearDown(self):
"""Remove test data"""
User.query.delete()
db.session.rollback()
def test_signup(self):
"""Test addition of new user"""
u = User.signup(username = "User_nick", first_name="Nick", last_name="Johnson", password="password", fave_team="Southampton", email="nick@gmail.com")
uid = 999
u.id = uid
db.session.commit()
u = User.query.get(uid)
self.assertIsNotNone(u)
self.assertEqual(u.username, 'User_nick')
self.assertEqual(u.first_name, 'Nick')
self.assertEqual(u.last_name, 'Johnson')
self.assertNotEqual(u.password, "password")
self.assertEqual(u.fave_team, 'Southampton')
self.assertEqual(u.email, 'nick@gmail.com')
# Bcrypt strings should start with $2b$
self.assertTrue(u.password.startswith("$2b$"))
def test_valid_authentication(self):
u = User.authenticate(self.u1.username, "password")
self.assertIsNotNone(u)
self.assertEqual(u.id, self.id1)
self.assertEqual(u.username, "User_john")
def test_invalid_username(self):
self.assertFalse(User.authenticate("badusername", "password"))
def test_wrong_password(self):
self.assertFalse(User.authenticate(self.u1.username, "badpassword"))