-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
265 lines (182 loc) · 7.52 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from unittest import TestCase
from server import app
from model import connect_to_db, db, example_data, User, Task, Kao, Collect
from flask import session
from server import install_secret_key
import timehelpers
import seed
import hashes
# class FlaskTestsBasic(TestCase):
# """Flask tests."""
# def setUp(self):
# """Stuff to do before every test."""
# # Get the Flask test client
# self.client = app.test_client()
# # Show Flask errors that happen during tests
# app.config['TESTING'] = True
# def test_index(self):
# """Test homepage page."""
# result = self.client.get("/")
# self.assertIn(b"class=\"home-page-mantra\"", result.data)
def create_testdb():
connect_to_db(app, "postgresql:///testdb")
db.create_all()
# Import different types of data
seed.delete_everything()
seed.load_users()
seed.load_tasks()
seed.load_kaos()
seed.load_collects()
seed.load_used_kaos()
seed.set_val_user_id()
seed.set_val_task_id()
seed.set_val_collect_id()
seed.set_val_kao_id()
class FlaskTestsDatabase(TestCase):
"""Flask tests that use the database."""
def setUp(self):
"""Stuff to do before every test."""
# Get the Flask test client
self.client = app.test_client()
app.config['TESTING'] = True
PRESERVE_CONTEXT_ON_EXCEPTION = False
# Connect to test database
connect_to_db(app, "postgresql:///testdb")
# Create tables and add sample data
# db.drop_all()
db.create_all()
example_data()
# users = User.query.all()
# bob = User.query.get(1)
def tearDown(self):
"""Do at end of every test."""
db.session.remove()
db.drop_all()
db.engine.dispose()
def test_users_list(self):
"""Test users db."""
bob = User.query.filter_by(username="bobrules").first()
# result = self.client.get("/users")
self.assertIn("bobrules", bob.username)
def test_departments_details(self):
"""Test tasks page."""
qian = User.query.filter_by(username="qian").first()
all_tasks = Task.query.all()
bao_user_id = all_tasks[1].user_id
# print("baouser id: ",bao_user_id)
# print(User.query.get(bao_user_id))
# print(all_tasks)
self.assertIn("eat bao", qian.tasks[0].msg)
# def test_user_log_in(self):
# """test that a user can log in"""
# bob = User.query.get(1)
# with self.client as c:
# result = c.post('/login_confirm',
# data = {'username' : 'bobrules',
# 'password' : 'abc123' },
# follow_redirects=True)
# self.assertEqual(session['current_user_id'], "1")
def test_logout(self):
"""Test logout route."""
with self.client as c:
with c.session_transaction() as sess:
sess['current_user_id'] = "1"
sess['current_username'] = "bobrules"
# print("we are logged in as user id :", sess.get("current_user_id"))
result = self.client.get('/logout', follow_redirects=True)
self.assertNotIn(b'current_user_id', session)
# print("we are logged in as user id :", session.get("current_user_id"))
self.assertIn(b'now logged out', result.data)
def test_user_add_task(self):
"""tests that user can POST a task to the testdb"""
with self.client as c:
with c.session_transaction() as sess:
sess['current_user_id'] = "1"
sess['current_username'] = "bobrules"
result = c.post('/add_new_task',
data = {'msg': 'make smores cupcakes',
'repeating': "False",
'duedate':'2018-12-31'},
follow_redirects=True
)
# self.assertEqual(session['user_id'], '42')
# self.assertIn(b'make smores cupcakes', result.data)
# def test_user_complete_task(self):
# """tests that user can complete a task"""
# with self.client as c:
# with c.session_transaction() as sess:
# sess['current_user_id'] = "1"
# sess['current_username'] = "bobrules"
# result = c.post('/complete-task',
# data = {'task_id': '1'},
# follow_redirects=True
# )
# task = Task.query.get(1)
# self.assertEqual(task.is_complete, True)
# self.assertIn(b'', result.data)
# def test_user_collect_kao(self):
# """Tests that a user gets a kao when they complete all their tasks for the day"""
# with self.client as c:
# with c.session_transaction() as sess:
# sess['current_user_id'] = "2"
# sess['current_username'] = "qian"
# task = Task.query.get(3)
# print(task)
# result = c.post('/complete-task',
# data = {'task_id': '3'},
# follow_redirects=True
# )
# task = Task.query.get(3)
# print(task)
# self.assertIn(b"You have collected a Moji",result.data)
# def test_select_new_kao_route(self):
# with self.client as c:
# with c.session_transaction() as sess:
# sess['current_user_id'] = "2"
# sess['current_username'] = "qian"
# result = c.get("/select-new-kao")
# def test_select_new_kao_function(self):
# with self.client as c:
# with c.session_transaction() as sess:
# sess['current_user_id'] = "2"
# sess['current_username'] = "qian"
# used_kaos = Used_Kao.query.all()
# kaos = Kao.query.all()
# new_kao_id = timehelpers.select_new_kao(used_kaos=used_kaos,kaos=kaos)
# self.assertNotIn(new_kao_id,[kao.id for kao in used_kaos])
# self.assertNotIn(new_kao_id,[kao.id for kao in kaos])
class FlaskTestsFullDatabase(TestCase):
"""Flask tests that use the database."""
def setUp(self):
"""Stuff to do before every test."""
# Get the Flask test client
self.client = app.test_client()
app.config['TESTING'] = True
PRESERVE_CONTEXT_ON_EXCEPTION = False
# Connect to test database
create_testdb()
def tearDown(self):
"""Do at end of every test."""
db.session.remove()
db.drop_all()
db.engine.dispose()
def test_kaos(self):
kao = Kao.query.get(1)
# print(kao)
# result = self.client.get("/users")
self.assertIn(kao.kao, u'(* ^ ω ^)')
class timehelpersTest(TestCase):
""" tests that use time helpers module."""
def testTime(self):
EOD = timehelpers.get_user_EOD("US/Pacific")
class hashesTest(TestCase):
""" tests that use hashes module."""
def test_hash_val(self):
secretkey = app.config['SECRET_KEY']
my_pw = "kaosrgreat"
my_id = 18
my_hash = hashes.get_salted_hash(my_pw, my_id, secretkey)
self.assertEqual(hashes.val_salted_hash("kaosrgreat",my_id,secretkey,my_hash), True)
if __name__ == "__main__":
import unittest
unittest.main()