4
py.test test cases to test the API controller (api.py)
8
import gluon.contrib.simplejson as sj
9
from gluon.contrib.populate import populate
11
from populate_os_tables import populate_workshops_for_api_tests
13
base_url = 'http://dev.openstudioproject.com:8000'
15
def populate_schedule(web2py):
17
Sets up a class and API user to test with
19
web2py.db.auth_user.insert(id=2,
21
last_name='van de Ven',
22
email='edwin@openstudioproject.com',
24
web2py.db.auth_user.insert(first_name='Pietje',
26
email='pietje@puk.nl',
28
web2py.db.auth_user.insert(first_name='Aimee',
30
email='aimee@ashtanga.sp',
32
web2py.db.school_classtypes.insert(Name='Mysore',
33
Link='http://www.openstudioproject.com',
34
Description='Description here',
36
web2py.db.school_classtypes.insert(Name='Led class',
38
web2py.db.school_classtypes.insert(Name='Private',
40
web2py.db.school_locations.insert(Name="Sittard",
42
web2py.db.school_locations.insert(Name="Maastricht",
44
web2py.db.school_levels.insert(Name='Level 1')
45
# Monday class + teachers
46
web2py.db.classes.insert(school_locations_id=1,
47
school_classtypes_id=1,
52
Startdate='2014-01-01',
56
web2py.db.classes_teachers.insert(classes_id=1,
59
Startdate='2014-01-01')
60
# Tuesday class + teachers
61
web2py.db.classes.insert(school_locations_id=2,
62
school_classtypes_id=2,
67
Startdate='2014-01-01',
71
web2py.db.classes_teachers.insert(classes_id=2,
74
Startdate='2014-01-01')
75
web2py.db.classes_otc.insert(
77
ClassDate='2014-01-13',
83
# web2py.db.classes_subteachers.insert(classes_id=1,
84
# ClassDate='2014-01-13',
87
web2py.db.classes_otc.insert(
89
ClassDate='2014-01-20',
92
# web2py.db.classes_cancelled.insert(classes_id=1,
93
# ClassDate='2014-01-20')
94
web2py.db.school_holidays.insert(Description='Carnavals vakantie',
95
Startdate='2014-01-27',
98
web2py.db.school_holidays_locations.insert(
99
school_holidays_id = 1,
100
school_locations_id = 1 )
101
web2py.db.sys_api_users.insert(ActiveUser=True,
104
Description='test user')
108
def test_schedule_get_extension_error():
110
Check whether we get a value error when a call is made without variables
112
url = base_url + '/api/schedule_get'
113
page = urllib.urlopen(url).read()
114
assert "Extension error" in page
117
def test_schedule_get_days_extension_error():
119
Check whether we get a value error when a call is made without variables
121
url = base_url + '/api/schedule_get_days'
122
page = urllib.urlopen(url).read()
123
assert "Extension error" in page
126
def test_workshops_get_extension_error():
128
Check whether we get a value error when a class is made without variabled
130
url = base_url + '/api/workshops_get'
131
page = urllib.urlopen(url).read()
132
assert "Extension error" in page
135
def test_schedule_get_authentication_error():
137
Check whether we get an authentication error when we don't supply
138
a username and password or a wrong username and password
141
'/api/schedule_get.json?user=test&key=test&year=2014&week=1'
142
page = urllib.urlopen(url).read()
143
assert "Authentication error" in page
146
def test_schedule_get_days_authentication_error():
148
Check whether we get an authentication error when we don't supply
149
a username and password or a wrong username and password
152
'/api/schedule_get_days.json?user=test&key=test&date_start=2014-01-01&date_end=2014-01-06'
153
page = urllib.urlopen(url).read()
154
assert "Authentication error" in page
157
def test_workshops_get_authentication_error():
159
Check whether we get an authentication error when we don't supply
160
a username and password or a wrong username and password
163
'/api/workshops_get.json?user=test&key=test'
164
page = urllib.urlopen(url).read()
165
assert "Authentication error" in page
168
def test_schedule_get_value_error():
170
Check whether we specify a string for year and week
173
'/api/schedule_get.json?user=test&key=test&year=bla&week=bla'
174
page = urllib.urlopen(url).read()
175
assert "Value error" in page
178
def test_schedule_get_days_value_error():
180
Check whether we specify a string for year and week
183
'/api/schedule_get_days.json?user=test&key=test&date_start=2000&date_end=2014-01-06'
184
page = urllib.urlopen(url).read()
185
assert "Missing value" in page
188
def test_schedule_get_missing_value():
190
Check whether we specify a string for year and week
193
'/api/schedule_get.json'
194
page = urllib.urlopen(url).read()
195
assert "Missing value" in page
198
def test_schedule_get_days_missing_value():
200
Check whether we specify a string for year and week
203
'/api/schedule_get_days.json'
204
page = urllib.urlopen(url).read()
205
assert "Missing value" in page
208
def test_schedule_get_json(client, web2py):
210
Check whether we can get the information in the database through the api
211
using the JSON interface.
213
populate_schedule(web2py)
216
'/api/schedule_get.json?user=test&key=test&year=2014&week=2'
217
page = urllib.urlopen(url).read()
219
json = sj.loads(page)
221
## check locations info
222
assert json['data']['locations'][0]['id'] == 1
223
assert json['data']['locations'][0]['Name']== 'Sittard'
224
# Check that only the location with "AllowAPI = True" is shown
225
assert len(json['data']['locations']) == 1
227
## check classtypes info, sorted alphabetically
228
assert json['data']['classtypes'][0]['Name'] == 'Led class'
229
assert json['data']['classtypes'][1]['Name']== 'Mysore'
230
assert json['data']['classtypes'][1]['Link'] == 'http://www.openstudioproject.com'
231
assert json['data']['classtypes'][1]['Description'] == 'Description here'
232
# Check that only the location with "AllowAPI = True" is shown
233
assert len(json['data']['classtypes']) == 2
235
# check teachers info, sorted alphabetically
236
assert json['data']['teachers'][0]['name'] == 'Aimee Garcias'
237
assert json['data']['teachers'][1]['name'] == 'Edwin van de Ven'
240
assert json['data']['classes']['Monday']['date'] == '2014-01-06'
241
assert not json['data']['classes']['Monday']['classes'][0]['Holiday']
242
assert json['data']['classes']['Monday']['classes'][0]['LocationID'] == 1
243
assert json['data']['classes']['Monday']['classes'][0]['Location'] == 'Sittard'
244
assert json['data']['classes']['Monday']['classes'][0]['ClassTypeID'] == 1
245
assert json['data']['classes']['Monday']['classes'][0]['ClassType'] == 'Mysore'
246
assert json['data']['classes']['Monday']['classes'][0]['Starttime'] == '06:00'
247
assert json['data']['classes']['Monday']['classes'][0]['Endtime'] == '09:00'
248
assert json['data']['classes']['Monday']['classes'][0]['TeacherID'] == 2
249
assert json['data']['classes']['Monday']['classes'][0]['Teacher'] == 'Edwin van de Ven'
250
assert json['data']['classes']['Monday']['classes'][0]['TeacherID2'] == 3
251
assert json['data']['classes']['Monday']['classes'][0]['LevelID'] == 1
252
assert json['data']['classes']['Monday']['classes'][0]['Level'] == 'Level 1'
253
assert not json['data']['classes']['Monday']['classes'][0]['Subteacher']
254
assert not json['data']['classes']['Monday']['classes'][0]['Cancelled']
256
assert json['data']['classes']['Monday']['classes'][0]['BookingStatus'] == 'finished'
257
assert json['data']['classes']['Monday']['classes'][0]['LinkShop'] == \
258
'http://dev.openstudioproject.com:8000/shop/classes_book_options?clsID=1&date=2014-01-06'
260
# check classes_subteachers
262
'/api/schedule_get.json?user=test&key=test&year=2014&week=3'
263
page = urllib.urlopen(url).read()
265
json = sj.loads(page)
267
assert json['data']['classes']['Monday']['date'] == '2014-01-13'
270
assert not json['data']['classes']['Monday']['classes'][0]['Holiday']
271
assert json['data']['classes']['Monday']['classes'][0]['TeacherID'] == 4
272
assert json['data']['classes']['Monday']['classes'][0]['Teacher'] == \
274
assert json['data']['classes']['Monday']['classes'][0]['TeacherID2'] == 3
275
assert json['data']['classes']['Monday']['classes'][0]['Teacher2'] == \
277
assert json['data']['classes']['Monday']['classes'][0]['Subteacher']
278
assert not json['data']['classes']['Monday']['classes'][0]['Cancelled']
282
'/api/schedule_get.json?user=test&key=test&year=2014&week=4'
283
page = urllib.urlopen(url).read()
285
json = sj.loads(page)
287
assert json['data']['classes']['Monday']['date'] == '2014-01-20'
288
assert json['data']['classes']['Monday']['classes'][0]['Cancelled']
289
assert not json['data']['classes']['Monday']['classes'][0]['Holiday']
293
'/api/schedule_get.json?user=test&key=test&year=2014&week=5'
294
page = urllib.urlopen(url).read()
296
json = sj.loads(page)
298
assert json['data']['classes']['Monday']['date'] == '2014-01-27'
299
assert json['data']['classes']['Monday']['classes'][0]['Holiday']
301
# Check TeacherID parameter
304
'/api/schedule_get.json?user=test&key=test&year=2014&week=2&TeacherID=' + unicode(teID)
305
page = urllib.urlopen(url).read()
307
json = sj.loads(page)
309
assert json['data']['classes']['Monday']['date'] == '2014-01-06'
310
assert json['data']['classes']['Monday']['classes'][0]['TeacherID'] == teID
311
assert json['data']['classes']['Monday']['classes'][0]['Teacher'] == \
315
def test_shedule_get_days_json(client, web2py):
317
test schedule_get_days API endpoint
319
populate_schedule(web2py)
324
'/api/schedule_get_days.json?user=test&key=test&date_start=2014-01-06&date_end=2014-01-06'
325
page = urllib.urlopen(url).read()
326
json = sj.loads(page)
329
assert json['data'][0]['date'] == '2014-01-06'
330
assert not json['data'][0]['classes'][0]['Holiday']
331
assert json['data'][0]['classes'][0]['LocationID'] == 1
332
assert json['data'][0]['classes'][0]['Location'] == 'Sittard'
333
assert json['data'][0]['classes'][0]['ClassTypeID'] == 1
334
assert json['data'][0]['classes'][0]['ClassType'] == 'Mysore'
335
assert json['data'][0]['classes'][0]['Starttime'] == '06:00'
336
assert json['data'][0]['classes'][0]['Endtime'] == '09:00'
337
assert json['data'][0]['classes'][0]['TeacherID'] == 2
338
assert json['data'][0]['classes'][0]['Teacher'] == 'Edwin van de Ven'
339
assert json['data'][0]['classes'][0]['TeacherID2'] == 3
340
assert json['data'][0]['classes'][0]['LevelID'] == 1
341
assert json['data'][0]['classes'][0]['Level'] == 'Level 1'
342
assert not json['data'][0]['classes'][0]['Subteacher']
343
assert not json['data'][0]['classes'][0]['Cancelled']
345
# check subteachers class
347
'/api/schedule_get_days.json?user=test&key=test&date_start=2014-01-13&date_end=2014-01-13'
348
page = urllib.urlopen(url).read()
349
json = sj.loads(page)
351
assert json['data'][0]['date'] == '2014-01-13'
352
assert not json['data'][0]['classes'][0]['Holiday']
353
assert json['data'][0]['classes'][0]['TeacherID'] == 4
354
assert json['data'][0]['classes'][0]['Teacher'] == \
356
assert json['data'][0]['classes'][0]['TeacherID2'] == 3
357
assert json['data'][0]['classes'][0]['Teacher2'] == \
359
assert json['data'][0]['classes'][0]['Subteacher']
360
assert not json['data'][0]['classes'][0]['Cancelled']
365
'/api/schedule_get_days.json?user=test&key=test&date_start=2014-01-20&date_end=2014-01-20'
366
page = urllib.urlopen(url).read()
367
json = sj.loads(page)
369
assert json['data'][0]['date'] == '2014-01-20'
370
assert json['data'][0]['classes'][0]['Cancelled']
371
assert not json['data'][0]['classes'][0]['Holiday']
375
'/api/schedule_get_days.json?user=test&key=test&date_start=2014-01-27&date_end=2014-01-27'
376
page = urllib.urlopen(url).read()
377
json = sj.loads(page)
379
assert json['data'][0]['date'] == '2014-01-27'
380
assert json['data'][0]['classes'][0]['Holiday']
383
def test_workshops_get_json(client, web2py):
385
test workshops_get API endpoint
387
populate_workshops_for_api_tests(web2py)
389
# Check if workshop is in the list
390
url = base_url + '/api/workshops_get.json?user=test&key=test'
391
page = urllib.urlopen(url).read()
392
json = sj.loads(page)
394
workshop = web2py.db.workshops(1)
395
fws_wsp = web2py.db.workshops_products(1)
396
location = web2py.db.school_locations(1)
397
teacher = web2py.db.auth_user(workshop.auth_teacher_id)
398
teacher2 = web2py.db.auth_user(workshop.auth_teacher_id2)
400
assert json['data'][0]['Startdate'] == str(workshop.Startdate)
401
assert json['data'][0]['Enddate'] == str(workshop.Enddate)
402
assert json['data'][0]['Name'] == workshop.Name
403
assert json['data'][0]['Teacher']['id'] == workshop.auth_teacher_id
404
assert json['data'][0]['Teacher']['Name'] == teacher.display_name
405
assert json['data'][0]['Teacher2']['id'] == workshop.auth_teacher_id2
406
assert json['data'][0]['Teacher2']['Name'] == teacher2.display_name
407
assert json['data'][0]['LocationID'] == workshop.school_locations_id
408
assert json['data'][0]['Location'] == location.Name
409
assert json['data'][0]['Description'] == workshop.Description
412
def test_workshop_get_json(client, web2py):
414
test workshop_get API endpoint
416
populate_workshops_for_api_tests(web2py)
418
# Check if workshop is in the list
419
url = base_url + '/api/workshop_get.json?user=test&key=test&id=1'
420
page = urllib.urlopen(url).read()
421
json = sj.loads(page)
423
workshop = web2py.db.workshops(1)
424
fws_wsp = web2py.db.workshops_products(1)
425
location = web2py.db.school_locations(1)
426
teacher = web2py.db.auth_user(workshop.auth_teacher_id)
427
teacher2 = web2py.db.auth_user(workshop.auth_teacher_id2)
429
assert json['data']['Startdate'] == str(workshop.Startdate)
430
assert json['data']['Enddate'] == str(workshop.Enddate)
431
assert json['data']['Name'] == workshop.Name
432
assert json['data']['Teacher']['id'] == workshop.auth_teacher_id
433
assert json['data']['Teacher']['Name'] == teacher.display_name
434
assert json['data']['Teacher2']['id'] == workshop.auth_teacher_id2
435
assert json['data']['Teacher2']['Name'] == teacher2.display_name
436
assert json['data']['LocationID'] == workshop.school_locations_id
437
assert json['data']['Location'] == location.Name
438
assert json['data']['Description'] == workshop.Description
441
def workshop_get_dates(activities):
443
:param workshop: Workshop object
444
:param activities: workshop activities rows
447
if len(activities) > 0:
448
date_from = activities[0].Activitydate
450
if len(activities) > 1:
451
date_until = activities[len(activities) - 1].Activitydate
453
if len(activities) == 0: # no activities
454
date_from = T("No activities found...")
455
date_until = T("No activities found...")
457
return dict(date_from=date_from,
458
date_until=date_until)
b'\\ No newline at end of file'