-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
97 lines (70 loc) · 2.89 KB
/
tests.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
import os
import unittest
from server import app, session
from model import db, User, Question
import seed
from sqlalchemy import exc
class TestCase (unittest.TestCase):
def setUp(self):
"""Set up a testing db"""
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['TESTING'] = True
self.app = app.test_client()
db.app = app
db.init_app(app)
db.create_all()
def tearDown(self):
"""Delete testing db"""
db.session.remove()
db.drop_all()
# ==============================================================================
# Testing User Model
# ==============================================================================
def test_user(self):
u = User(first_name='John', email='[email protected]', password='test') # Make a user
db.session.add(u)
db.session.commit()
self.assertEqual(u.first_name, 'John')
# Duplicate critical information
u = User(first_name='Jane', email='[email protected]', password='test')
db.session.add(u)
with self.assertRaises(exc.IntegrityError):
db.session.commit()
db.session.rollback()
# See what happens if we are missing information
u = User(first_name='Jane', email='[email protected]')
db.session.add(u)
with self.assertRaises(exc.IntegrityError):
db.session.commit()
db.session.rollback()
# ==============================================================================
# Testing Login/Logout Views
# ==============================================================================
def login(self, email, password):
return self.app.post('/login', data=dict(
email=email,
password=password
), follow_redirects=True)
def logout(self):
return self.app.get('/logout', follow_redirects=True)
def test_login_logout(self):
q = Question(question_text="Why?")
db.session.add(q)
db.session.commit()
u = User(first_name='John', email='[email protected]', password='test')
db.session.add(u)
db.session.commit()
# rv = response from server of the profile page (hopefully!)
rv = self.login('[email protected]', 'test')
# self.assertTrue(session['user_id'] == u.user_id)
self.assertTrue('Logout' in rv.data)
self.assertFalse('Log In' in rv.data)
rv = self.logout()
# self.assertTrue('user_id' not in session)
self.assertTrue('login' in rv.data)
rv = self.login('[email protected]', 'testb')
self.assertTrue('Incorrect password' in rv.data)
rv = self.login('[email protected]', 'test')
self.assertTrue('No such user' in rv.data)
if __name__ == '__main__':
unittest.main()