-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
365 lines (315 loc) · 11.9 KB
/
tests.py
File metadata and controls
365 lines (315 loc) · 11.9 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import requests
import httplib
import unittest
import simplejson as json
import logging
import random
from run import *
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())
users = {
'Test': {
'name': 'Test User',
'email': 'test@mailinator.com',
'token': '123',
},
'Admin': {
'name': 'Admin User',
'email': 'admin@mailinator.com',
'token': '191191134509801459801',
},
'Delete': {
'name': 'Delete',
'email': 'delete@mailinator.com',
'token': '321',
},
'User1': {
'name': 'User1',
'email': 'user1@mailinator.com',
'token': '123456',
},
}
def auth(user):
return requests.auth.HTTPBasicAuth("{email}|{name}".format(**users[user]), users[user]['token'])
def email(user):
return users[user]['email']
#url = 'http://recommender.oscar.ncsu.edu/api/v2'
url = 'http://localhost:5000'
s = requests.Session()
s.headers.update({'Content-Type': 'application/json'})
s.auth = auth("Test")
def get_collection(col, where=None, auth=None):
params={'where': json.dumps(where)} if where else None
return s.get(url+'/'+col, params=params, auth=auth).json().get('_items', [])
def delete_collection(col, where=None, auth=None):
return s.delete(url+'/'+col,
params={'where': json.dumps(where)} if where else {},
auth=auth).json()
def get_item(col, id):
return s.get(url + '/' + col + '/' + id).json()
def create_item(collection, data, auth=None):
result = s.post(url+'/'+collection,
data=json.dumps(data),
auth=auth)
try:
return result.json()
except Exception:
return result
def create_items(collection, *data, **kwargs):
auth = kwargs.get('auth')
result = s.post(url+'/'+collection,
data=json.dumps(data),
auth=auth)
try:
return result.json()
except Exception:
return result
def upload_item(collection, files, auth=None):
result = s.post(url+'/'+collection,
files=files,
headers={'Content-Type': 'multipart/form-data'},
auth=auth)
try:
return result.json()
except Exception:
return result
def update_item(item, data, auth=None):
return s.patch(url+item['_links']['self']['href'],
headers={"If-Match": item['_etag']},
data=json.dumps(data),
auth=auth).json()
def delete_item(item, auth=None):
return s.delete(url+item['_links']['self']['href'],
headers={"If-Match": item['_etag']},
auth=auth).json()
def replace_item(item, data, auth=None):
return s.put(url+item['_links']['self']['href'],
headers={"If-Match": item['_etag']},
data=json.dumps(data),
auth=auth).json()
def ensure_users(*users):
for user in users:
get_collection('applications', auth=auth(user))
def ensure_item(col, item, auth=None):
items = get_collection(col, where=item)
if len(items) == 0:
return create_item(col, item, auth=auth)
return items[0]
def ensure_items(col, *items, **kwargs):
auth = kwargs.get('auth')
return map(lambda item: ensure_item(col, item, auth=auth),
items)
class TestBasicEndpoints(unittest.TestCase):
#pass so instance doesn't automatically run tests
def runTest(self): pass
@classmethod
def setUpClass(cls):
#ensure 3 users
ensure_users("Test", "Delete", "User1")
tools = ensure_items('tools', {'name': 'Save', 'application': 'Eclipse'})
usages = ensure_items('usages', {'tool': tools[0]['_id'], 'keyboard': 5})
clips = ensure_items('clips', {
'name': 'TestClip',
'tool': tools[0]['_id'],
'share': ['public'],
})
print ensure_items('ratings', {
'clip': clips[0]['_id'], 'value': 3, 'user': email('Test')
})
print ensure_items('ratings', {'clip': clips[0]['_id'], 'value': 3, 'user': email('User1')},
auth=auth('User1'))
def assert_success(self, result):
print result
self.assertIsNotNone(result)
self.assertIsNotNone(result['_status'])
self.assertEquals(result['_status'], 'OK')
def assert_failure(self, result, code=403):
print result
self.assertIsNotNone(result)
self.assertIsNotNone(result['_status'])
self.assertEquals(result['_status'], 'ERR')
self.assertEquals(result['_error']['code'], code)
def test_applications(self):
self.assertTrue(len(get_collection('applications')) > 0)
def test_users(self):
self.assertTrue(len(get_collection('users')) > 0)
def test_update_user(self):
u = get_item('users', 'test@mailinator.com')
result = update_item(u, {'name': 'Test User'})
self.assert_success(result)
def test_user_escalation(self):
u = get_item('users', 'test@mailinator.com')
result = update_item(u, {'roles': ['admin']})
self.assert_failure(result)
def test_admin_privileges(self):
u = get_item('users', 'test@mailinator.com')
result = update_item(u, {'roles': ['user']}, auth=auth('Admin'))
self.assert_success(result)
def test_update_other_user(self):
u = get_item('users', email('User1'))
result = update_item(u, {'name': 'Test User'})
self.assert_failure(result)
def test_create_user(self):
result = create_item('users', {'name': 'Test User', 'email': 'test2@mailinator.com'})
assert result.status_code == 405
def test_delete_user(self):
u = get_item('users', email('Delete'))
result = delete_item(u, auth=auth('Delete'))
self.assertEquals(result, {})
def test_delete_other_user(self):
u = get_item('users', email('Test'))
result = delete_item(u, auth=auth('Delete'))
self.assert_failure(result)
def test_send_notification(self):
tool = get_collection('tools', where={"application":"Eclipse"})[0]
result = create_item('notifications', {
'recipient':'user1@mailinator.com',
'message': 'Test Message',
'application': "Eclipse",
'type': 'message',
'status': 'new'
})
self.assert_success(result)
def test_update_notification(self):
notes = get_collection('notifications')
self.assertGreater(len(notes), 0)
note = notes[0]
result = update_item(note, {
'status': 'updated'
})
self.assert_success(result)
def test_update_other_notification(self):
notes = get_collection('notifications')
self.assertGreater(len(notes), 0)
note = notes[0]
result = update_item(note, {
'status': 'tampered'
}, auth('Delete'))
self.assert_failure(result)
def test_create_public_clip(self):
tool = get_collection('tools', where={"application":"Eclipse"})[0]
result = create_item('clips', {
'name': 'TestClip',
'tool': tool['_id'],
'share': ['public'],
'type': 'keyboard',
'event_frames': [25]
})
self.assert_success(result)
def test_create_clip_bad_share(self):
tool = get_collection('tools', where={"application":"Eclipse"})[0]
result = create_item('clips', {
'name': 'TestClip',
'tool': tool['_id'],
'share': ['blah'],
'type': 'keyboard',
'event_frames': [25]
})
self.assert_failure(result, code=400)
def test_create_clip_share_user(self):
tool = get_collection('tools', where={"application":"Eclipse"})[0]
result = create_item('clips', {
'name': 'TestClip',
'tool': tool['_id'],
'share': [email('User1')],
'type': 'keyboard',
'event_frames': [25]
})
self.assert_success(result)
def test_thumbnail_clip(self):
tool = get_collection('tools', where={"application":"Eclipse"})[0]
result = requests.post(url+'/clips',
files={"thumbnail": open("thumbnail.jpg", 'rb')},
data={
'name': 'Test Thumbnail Clip',
'tool': tool['_id'],
'share': json.dumps(['public']),
'type': 'keyboard',
'event_frames': json.dumps([25])
},
auth=auth('Test'))
print result.text
self.assertEqual(result.status_code, 201)
def test_new_rating(self):
clip = get_collection('clips')[0]
result = create_item('ratings', {
'clip': clip['_id'],
'value': 3
})
self.assert_success(result)
def test_update_rating(self):
rating = get_collection('ratings', where={"user": email('Test')})[0]
result = update_item(rating, {
'value': 3
})
self.assert_success(result)
def test_new_duplicate_rating(self):
rating = get_collection('ratings', where={"user": email('Test')})[0]
result = create_item('ratings', {
'clip': rating['clip'],
'value': 4
})
self.assert_failure(result, 400)
def test_new_duplicate_rating_other_user(self):
rating = get_collection('ratings', where={"user":{"$ne": email('User1')}})[0]
result = create_item('ratings', {
'clip': rating['clip'],
'value': 4
}, auth=auth('User1'))
self.assert_success(result)
def test_admin_delete_collection(self):
result = delete_collection('ratings', auth=auth('Admin'))
self.assertEquals(result, {})
def test_user_delete_collection(self):
result = delete_collection('ratings')
self.assert_failure(result)
def test_upload_image(self):
clip = get_collection('clips', where={"user": email('Test')})[0]
result = requests.post(url+'/clips/%s/images' % clip['_id'],
files={"data": open("frame000.jpg", 'rb')},
data={'name': 'TestFrame'},
auth=auth('Test'))
self.assertEqual(result.status_code, 201)
def test_create_usage(self):
tool = get_collection('tools', where={"application":"Eclipse"})[0]
result = create_item('usages', {
'tool': tool['_id'],
})
self.assert_success(result)
def test_update_usage(self):
usage = get_collection('usages', where={"user": email('Test')})[0]
result = update_item(usage, {
'keyboard': 3
})
self.assert_success(result)
# Doesn't seem to work...
# def test_admin_delete_filtered_collection(self):
# result = delete_collection('ratings', where={"user": email('Test')},
# auth=auth('Admin'))
# self.assertEquals(result, {})
def test_bulk_usage(self):
usages = [{
'app_name': 'Eclipse',
'tool_name': 'Save',
'keyboard': 5,
}, {
'app_name': 'Excel',
'tool_name': 'SUM',
'keyboard': 3,
}]
response = s.post(url+'/report-usage', data=json.dumps(usages))
self.assert_success(response.json())
def test_upload_events(self):
result = create_item('events', {
'application': 'Eclipse',
'tool': 'Save',
'time': int((datetime.now() - datetime(1970,1,1)).total_seconds()*1000)
})
self.assert_success(result)
test = TestBasicEndpoints() #useful for manual testing
if __name__ == '__main__':
logger.setLevel(logging.ERROR)
unittest.main()
else:
httplib.HTTPConnection.debuglevel = 1
logger.setLevel(logging.DEBUG)