-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user_model.py
148 lines (109 loc) · 3.93 KB
/
test_user_model.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""User model tests """
# python -m unittest test_user_model.py
import os
from unittest import TestCase
from sqlalchemy import exc
from models import db, User, Favorites, Groceries
# connect to db
os.environ['DATABASE_URL'] = "postgresql:///mygrub_test"
from app import app
db.create_all()
class UserModelTestCase(TestCase):
"""Test views for messages"""
def setUp(self):
"Create test client, and add sample data"
db.drop_all()
db.create_all()
# Make test users and commit to db
u1 = User.signup("test1", "[email protected]", "password")
uid1 = 1111
u1.id = uid1
u2 = User.signup("test2", "[email protected]", "password")
uid2 = 2222
u2.id = uid2
db.session.commit()
# Query users
u1 = User.query.get(uid1)
u2 = User.query.get(uid2)
# Set values
self.u1 = u1
self.uid1 = uid1
self.u2 = u2
self.uid2 = uid2
self.client = app.test_client()
def tearDown(self):
"""Tear down all settup variables"""
res = super().tearDown()
db.session.rollback()
return res
def test_user_model(self):
"""Does basic model work?"""
u = User(
email="[email protected]",
username="testuser",
password="HASHED_PASSWORD"
)
db.session.add(u)
db.session.commit()
#New user should have no favorites or groceries
self.assertEqual(len(u.favorites), 0)
self.assertEqual(len(u.groceries), 0)
#################
# New User Tests
#################
def test_valid_signup(self):
"""Does signup and password hash work?"""
# submit signup
u_test = User.signup("testtesttest", "[email protected]", "password")
uid = 9999
u_test.id = uid
db.session.commit()
# call user/check inputs
u_test = User.query.get(uid)
self.assertIsNotNone(u_test)
self.assertEqual(u_test.username, "testtesttest")
self.assertEqual(u_test.email, "[email protected]")
#Bcrypt string starts with $2b$
self.assertTrue(u_test.password.startswith('$2b$'))
# USERNAME TEST
# TO DO: Repeat username
def test_invalid_username_signup(self):
"""Sign up with no username"""
invalid = User.signup(None, "[email protected]", "password")
uid = 8888
invalid.id = uid
with self.assertRaises(exc.IntegrityError) as context:
db.session.commit()
# with self.assertRaises(ValidationError) as context:
# User.signup("test1", "[email protected]", "password")
# db.session.commit()
# EMAIL TEST
# TO DO: Repeat email
def test_invalid_email_signup(self):
"""Sign up with no email"""
invalid = User.signup("testtest", None, "password")
uid = 7777
invalid.id = uid
with self.assertRaises(exc.IntegrityError) as context:
db.session.commit()
#PASSWORDS TEST
def test_invalid_password_signup(self):
"""Sign up with no password"""
with self.assertRaises(ValueError) as context:
User.signup("testtest", "[email protected]", "")
with self.assertRaises(ValueError) as context:
User.signup("testtest", "[email protected]", None)
######################
# Authentication Tests
######################
def test_valid_authentication(self):
"""Using correct login work?"""
u = User.authenticate(self.u1.username, "password")
self.assertIsNotNone(u)
self.assertEqual(u.id, self.uid1)
def test_invalid_username(self):
"""Login with wrong username work?"""
self.assertFalse(User.authenticate("badusername", "password"))
def test_wrong_password(self):
"""Login with wrong password work?"""
self.assertFalse(User.authenticate(self.u1.username, "badpassword"))