This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtests.py
331 lines (286 loc) · 13.6 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import unittest, requests, json
from bizfriendly import app
from bizfriendly.models import *
from bizfriendly.routes import *
class bf_tests(unittest.TestCase):
def setUp(self):
# Set up the database settings
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://postgres@localhost/bizfriendly_test'
app.config['SECRET_KEY'] = '123456'
app.config['TESTING'] = True
db.create_all()
self.app = app.test_client()
# Fill up the DB
self.test_user = {
"name" : "Test User",
"email" : "[email protected]".lower(),
"password" : "Test Password"
}
test_user_obj = Bf_user(**self.test_user)
db.session.add(test_user_obj)
self.test_category = {
"name" : "Test Category",
"description" : "Test description",
"state" : "published",
"creator_id" : 1
}
test_category_obj = Category(**self.test_category)
db.session.add(test_category_obj)
self.test_service = {
"name" : "Test Service",
"url" : "Test Url",
"short_description" : "Test short description",
"long_description" : "Test long description",
"additional_resources" : "Test additional resources",
"tips" : "Test tips",
"state" : "published",
"category_id" : 1,
"creator_id" : 1
}
test_service_obj = Service(**self.test_service)
db.session.add(test_service_obj)
self.test_lesson = {
"name" : "Test Lesson",
"description" : "Test short description",
"ease" : "Test ease",
"state" : "published",
"service_id" : 1,
"creator_id" : 1
}
test_lesson_obj = Lesson(**self.test_lesson)
db.session.add(test_lesson_obj)
self.test_step = {
"step_type" : "Test step type",
"step_number" : 1,
"step_text" : "Test step text",
"trigger_endpoint" : "Test trigger endpoint",
"place_in_collection" : 'first',
"trigger_check" : "Test trigger check",
"trigger_value" : "Test trigger value",
"thing_to_remember" : "Test thing to remember",
"lesson_id" : 1,
"creator_id" : 1
}
test_step_obj = Step(**self.test_step)
db.session.add(test_step_obj)
db.session.commit()
self.headers = [('Content-Type', 'application/json')]
def tearDown(self):
db.drop_all()
# Test API -----------------------
def test_headers(self):
response = self.app.get('/api/v1/categories')
assert response.headers['Access-Control-Allow-Origin'] == '*'
assert response.headers['Content-Type'] == 'application/json'
def test_categories(self):
response = self.app.get('/api/v1/categories')
response = json.loads(response.data)
assert isinstance(response, dict)
assert isinstance(response['objects'], list)
assert isinstance(response['objects'][0]['id'], int)
assert isinstance(response['objects'][0]['name'], unicode)
assert isinstance(response['objects'][0]['description'], unicode)
assert isinstance(response['objects'][0]['state'], unicode)
assert isinstance(response['objects'][0]['services'], list)
assert isinstance(response['objects'][0]['creator_id'], int)
def test_categories_post(self):
self.test_category['name'] = 'New Test Category'
data = json.dumps(self.test_category)
response = self.app.post('api/v1/categories', data=data, headers=self.headers)
assert response.status_code == 201
def test_services(self):
response = self.app.get('/api/v1/services')
response = json.loads(response.data)
assert isinstance(response, dict)
assert isinstance(response['objects'], list)
assert isinstance(response['objects'][0]['id'], int)
assert isinstance(response['objects'][0]['name'], unicode)
assert isinstance(response['objects'][0]['url'], unicode)
assert isinstance(response['objects'][0]['short_description'], unicode)
assert isinstance(response['objects'][0]['long_description'], unicode)
assert isinstance(response['objects'][0]['additional_resources'], unicode)
assert isinstance(response['objects'][0]['tips'], unicode)
assert isinstance(response['objects'][0]['state'], unicode)
assert isinstance(response['objects'][0]['category_id'], int)
assert isinstance(response['objects'][0]['lessons'], list)
assert isinstance(response['objects'][0]['creator_id'], int)
def test_services_post(self):
self.test_service['name'] = 'New Test Service'
data = json.dumps(self.test_service)
response = self.app.post('api/v1/services', data=data, headers=self.headers)
assert response.status_code == 201
def test_lessons(self):
response = self.app.get('/api/v1/lessons')
response = json.loads(response.data)
assert isinstance(response, dict)
assert isinstance(response['objects'], list)
assert isinstance(response['objects'][0]['id'], int)
assert isinstance(response['objects'][0]['name'], unicode)
assert isinstance(response['objects'][0]['ease'], unicode)
assert isinstance(response['objects'][0]['description'], unicode)
assert isinstance(response['objects'][0]['state'], unicode)
assert isinstance(response['objects'][0]['service_id'], int)
assert isinstance(response['objects'][0]['steps'], list)
assert isinstance(response['objects'][0]['creator_id'], int)
def test_lessons_post(self):
self.test_lesson['name'] = 'New Test lesson'
data = json.dumps(self.test_lesson)
response = self.app.post('api/v1/lessons', data=data, headers=self.headers)
assert response.status_code == 201
def test_steps(self):
response = self.app.get('/api/v1/steps')
response = json.loads(response.data)
assert isinstance(response, dict)
assert isinstance(response['objects'], list)
assert isinstance(response['objects'][0]['id'], int)
assert isinstance(response['objects'][0]['step_type'], unicode)
assert isinstance(response['objects'][0]['step_number'], int)
assert isinstance(response['objects'][0]['step_text'], unicode)
assert isinstance(response['objects'][0]['trigger_endpoint'], unicode)
assert isinstance(response['objects'][0]['place_in_collection'], unicode)
assert isinstance(response['objects'][0]['trigger_check'], unicode)
assert isinstance(response['objects'][0]['trigger_value'], unicode)
assert isinstance(response['objects'][0]['thing_to_remember'], unicode)
assert isinstance(response['objects'][0]['lesson_id'], int)
assert isinstance(response['objects'][0]['creator_id'], int)
def test_steps_post(self):
data = json.dumps(self.test_step)
response = self.app.post('api/v1/steps', data=data, headers=self.headers)
assert response.status_code == 201
# # Test Admin --------------------------------
def test_admin(self):
response = self.app.get('/api/admin/')
assert response.headers['content-type'] == 'text/html; charset=utf-8'
def test_admin_bad_login(self):
response = self.app.get('/login')
assert response.headers['content-type'] == 'text/html; charset=utf-8'
# Test Sign Up and Sign In -------------------
def test_signup_new_email(self):
self.test_user['email'] = "[email protected]"
response = self.app.post('/signup', data=self.test_user)
assert response.status_code == 200
response = json.loads(response.data)
assert "id" in response
assert "name" in response
assert response["name"] == "Test User"
assert "email" in response
assert "access_token" in response
def test_signup_same_email(self):
response = self.app.post('/signup', data=self.test_user)
assert response.status_code == 401
response = json.loads(response.data)
assert "error" in response
assert response["error"] == 'Someone has already signed up with that email.'
def test_signin(self):
response = self.app.post('/signin', data=self.test_user)
assert response.status_code == 200
response = json.loads(response.data)
assert "id" in response
assert "name" in response
assert response["name"] == "Test User"
assert "email" in response
assert "access_token" in response
def test_signin_bad(self):
self.test_user['password'] = "1234"
response = self.app.post('/signin', data=self.test_user)
assert response.status_code == 401
response = json.loads(response.data)
assert "error" in response
assert response["error"] == "Couldn't find your email and password."
# Test Routes ---------------------------------
def test_get_data_on_attribute_path(self):
attributes = ['name',"7",0]
json_data = {
"fake" : False,
"name" : {
"fake" : False,
7 : {
"fake" : False,
0 : True
}
}
}
assert get_data_on_attribute_path(json_data, attributes)
def test_boolify(self):
assert boolify("True")
assert boolify("true")
assert not boolify("False")
assert not boolify("false")
def test_autoconvert(self):
assert type(autoconvert("100") == int)
assert type(autoconvert("false") == bool)
assert type(autoconvert("12.07") == float)
def test_check_for_new_no_original_count(self):
response = self.app.post('/signin', data=self.test_user)
response = json.loads(response.data)
auth = response["access_token"]
self.headers = [("Content-Type", "application/x-www-form-urlencoded"),("Authorization", auth)]
self.test_connection = {
"service" : "trello",
"service_access" : "3ad4b59c2f9879726aed00f9ecc3d5a6219d854e4925f44ff5ed53994d424e93"
}
self.app.post('/create_connection', data=self.test_connection, headers=self.headers)
data = {
"currentStep[id]" : 0,
"currentStep[name]" : "test Step",
"currentStep[stepType]" : "check_for_new",
"currentStep[stepNumber]" : 1,
"currentStep[stepText]" : "Test Step Text",
"currentStep[triggerEndpoint]" : "https://api.trello.com/1/member/me/boards?fields=id,name,dateLastView&key=8c1f249e2662ca4952d5df7ea8bad3bc&token=",
"currentStep[placeInCollection]" : "alphabetical",
"currentStep[triggerCheck]" : "",
"currentStep[triggerValue]" : "name",
"currentStep[thingToRemember]" : "id",
"currentStep[feedback]" : "Test Feedback",
"currentStep[nextStepNumber]" : "Test Next Step Number",
"currentStep[stepState]" : "unfinished",
"lessonName" : "Test Lesson Name",
"lessonId" : 1,
"thirdPartyService" : "trello",
"originalCount" : False
}
response = self.app.post('/check_for_new', data=data, headers=self.headers)
r = json.loads(response.data)
assert(isinstance(r["original_count"],int))
assert(r["attribute_to_display"] == False)
assert(r["attribute_to_remember"] == False)
assert(r["timeout"] == False)
assert(r["new_object_added"] == False)
def test_check_for_new_with_original_count(self):
response = self.app.post('/signin', data=self.test_user)
response = json.loads(response.data)
auth = response["access_token"]
self.headers = [("Content-Type", "application/x-www-form-urlencoded"),("Authorization", auth)]
self.test_connection = {
"service" : "trello",
"service_access" : "3ad4b59c2f9879726aed00f9ecc3d5a6219d854e4925f44ff5ed53994d424e93"
}
self.app.post('/create_connection', data=self.test_connection, headers=self.headers)
data = {
"currentStep[id]" : 0,
"currentStep[name]" : "test Step",
"currentStep[stepType]" : "check_for_new",
"currentStep[stepNumber]" : 1,
"currentStep[stepText]" : "Test Step Text",
"currentStep[triggerEndpoint]" : "https://api.trello.com/1/member/me/boards?fields=id,name,dateLastView&key=8c1f249e2662ca4952d5df7ea8bad3bc&token=",
"currentStep[placeInCollection]" : "alphabetical",
"currentStep[triggerCheck]" : "",
"currentStep[triggerValue]" : "name",
"currentStep[thingToRemember]" : "id",
"currentStep[feedback]" : "Test Feedback",
"currentStep[nextStepNumber]" : "Test Next Step Number",
"currentStep[stepState]" : "unfinished",
"lessonName" : "Test Lesson Name",
"lessonId" : 1,
"thirdPartyService" : "trello",
"originalCount" : 1
}
response = self.app.post('/check_for_new', data=data, headers=self.headers)
r = json.loads(response.data)
assert(isinstance(r["original_count"],int))
assert(r["attribute_to_display"] == "Testing")
assert(r["attribute_to_remember"] == "52ae697c3ca64bc66a01a4da")
assert(r["timeout"] == False)
assert(r["new_object_added"] == True)
if __name__ == '__main__':
unittest.main()