~edwinvandeven/openstudio/2017.0

« back to all changes in this revision

Viewing changes to tests/controllers/test_20_api_controller.py

  • Committer: Edwin van de Ven
  • Date: 2017-10-10 11:43:48 UTC
  • Revision ID: edwinvandeven@home.nl-20171010114348-kjon60md9vz6n4uf
Added test for profile cancel orders

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
'''
4
 
    py.test test cases to test the API controller (api.py)
5
 
'''
6
 
 
7
 
import urllib
8
 
import gluon.contrib.simplejson as sj
9
 
from gluon.contrib.populate import populate
10
 
 
11
 
from populate_os_tables import populate_workshops_for_api_tests
12
 
 
13
 
base_url = 'http://dev.openstudioproject.com:8000'
14
 
 
15
 
def populate_schedule(web2py):
16
 
    '''
17
 
        Sets up a class and API user to test with
18
 
    '''
19
 
    web2py.db.auth_user.insert(id=2,
20
 
                               first_name='Edwin',
21
 
                               last_name='van de Ven',
22
 
                               email='edwin@openstudioproject.com',
23
 
                               teacher=True)
24
 
    web2py.db.auth_user.insert(first_name='Pietje',
25
 
                               last_name='Puk',
26
 
                               email='pietje@puk.nl',
27
 
                               teacher=True)
28
 
    web2py.db.auth_user.insert(first_name='Aimee',
29
 
                               last_name='Garcias',
30
 
                               email='aimee@ashtanga.sp',
31
 
                               teacher=True)
32
 
    web2py.db.school_classtypes.insert(Name='Mysore',
33
 
                                       Link='http://www.openstudioproject.com',
34
 
                                       Description='Description here',
35
 
                                       AllowAPI=True)
36
 
    web2py.db.school_classtypes.insert(Name='Led class',
37
 
                                       AllowAPI=True)
38
 
    web2py.db.school_classtypes.insert(Name='Private',
39
 
                                       AllowAPI=False)
40
 
    web2py.db.school_locations.insert(Name="Sittard",
41
 
                                      AllowAPI=True)
42
 
    web2py.db.school_locations.insert(Name="Maastricht",
43
 
                                      AllowAPI=False)
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,
48
 
                             school_levels_id=1,
49
 
                             Week_day=1,
50
 
                             Starttime='06:00:00',
51
 
                             Endtime='09:00:00',
52
 
                             Startdate='2014-01-01',
53
 
                             Enddate='',
54
 
                             Maxstudents=20,
55
 
                             AllowAPI=True)
56
 
    web2py.db.classes_teachers.insert(classes_id=1,
57
 
                                      auth_teacher_id=2,
58
 
                                      auth_teacher_id2=3,
59
 
                                      Startdate='2014-01-01')
60
 
    # Tuesday class + teachers
61
 
    web2py.db.classes.insert(school_locations_id=2,
62
 
                             school_classtypes_id=2,
63
 
                             school_levels_id=1,
64
 
                             Week_day=1,
65
 
                             Starttime='06:00:00',
66
 
                             Endtime='09:00:00',
67
 
                             Startdate='2014-01-01',
68
 
                             Enddate='',
69
 
                             Maxstudents=20,
70
 
                             AllowAPI=True)
71
 
    web2py.db.classes_teachers.insert(classes_id=2,
72
 
                                      auth_teacher_id=2,
73
 
                                      auth_teacher_id2=3,
74
 
                                      Startdate='2014-01-01')
75
 
    web2py.db.classes_otc.insert(
76
 
        classes_id=1,
77
 
        ClassDate='2014-01-13',
78
 
        auth_teacher_id=4,
79
 
        teacher_role=1,
80
 
        auth_teacher_id2=3,
81
 
        teacher_role2=1
82
 
    )
83
 
    # web2py.db.classes_subteachers.insert(classes_id=1,
84
 
    #                                      ClassDate='2014-01-13',
85
 
    #                                      auth_teacher_id=4,
86
 
    #                                      auth_teacher_id2=3)
87
 
    web2py.db.classes_otc.insert(
88
 
        classes_id=1,
89
 
        ClassDate='2014-01-20',
90
 
        Status='cancelled'
91
 
    )
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',
96
 
                                     Enddate='2014-01-30',
97
 
                                     Classes=True)
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,
102
 
                                   Username='test',
103
 
                                   APIKey='test',
104
 
                                   Description='test user')
105
 
    web2py.db.commit()
106
 
 
107
 
 
108
 
def test_schedule_get_extension_error():
109
 
    '''
110
 
        Check whether we get a value error when a call is made without variables
111
 
    '''
112
 
    url = base_url + '/api/schedule_get'
113
 
    page = urllib.urlopen(url).read()
114
 
    assert "Extension error" in page
115
 
 
116
 
 
117
 
def test_schedule_get_days_extension_error():
118
 
    '''
119
 
        Check whether we get a value error when a call is made without variables
120
 
    '''
121
 
    url = base_url + '/api/schedule_get_days'
122
 
    page = urllib.urlopen(url).read()
123
 
    assert "Extension error" in page
124
 
 
125
 
 
126
 
def test_workshops_get_extension_error():
127
 
    '''
128
 
        Check whether we get a value error when a class is made without variabled
129
 
    '''
130
 
    url = base_url + '/api/workshops_get'
131
 
    page = urllib.urlopen(url).read()
132
 
    assert "Extension error" in page
133
 
 
134
 
 
135
 
def test_schedule_get_authentication_error():
136
 
    '''
137
 
        Check whether we get an authentication error when we don't supply
138
 
        a username and password or a wrong username and password
139
 
    '''
140
 
    url = base_url + \
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
144
 
 
145
 
 
146
 
def test_schedule_get_days_authentication_error():
147
 
    '''
148
 
        Check whether we get an authentication error when we don't supply
149
 
        a username and password or a wrong username and password
150
 
    '''
151
 
    url = base_url + \
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
155
 
 
156
 
 
157
 
def test_workshops_get_authentication_error():
158
 
    '''
159
 
        Check whether we get an authentication error when we don't supply
160
 
        a username and password or a wrong username and password
161
 
    '''
162
 
    url = base_url + \
163
 
        '/api/workshops_get.json?user=test&key=test'
164
 
    page = urllib.urlopen(url).read()
165
 
    assert "Authentication error" in page
166
 
 
167
 
 
168
 
def test_schedule_get_value_error():
169
 
    '''
170
 
        Check whether we specify a string for year and week
171
 
    '''
172
 
    url = base_url + \
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
176
 
 
177
 
 
178
 
def test_schedule_get_days_value_error():
179
 
    '''
180
 
        Check whether we specify a string for year and week
181
 
    '''
182
 
    url = base_url + \
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
186
 
 
187
 
 
188
 
def test_schedule_get_missing_value():
189
 
    '''
190
 
        Check whether we specify a string for year and week
191
 
    '''
192
 
    url = base_url + \
193
 
        '/api/schedule_get.json'
194
 
    page = urllib.urlopen(url).read()
195
 
    assert "Missing value" in page
196
 
 
197
 
 
198
 
def test_schedule_get_days_missing_value():
199
 
    '''
200
 
        Check whether we specify a string for year and week
201
 
    '''
202
 
    url = base_url + \
203
 
        '/api/schedule_get_days.json'
204
 
    page = urllib.urlopen(url).read()
205
 
    assert "Missing value" in page
206
 
 
207
 
 
208
 
def test_schedule_get_json(client, web2py):
209
 
    '''
210
 
        Check whether we can get the information in the database through the api
211
 
        using the JSON interface.
212
 
    '''
213
 
    populate_schedule(web2py)
214
 
 
215
 
    url = base_url + \
216
 
        '/api/schedule_get.json?user=test&key=test&year=2014&week=2'
217
 
    page = urllib.urlopen(url).read()
218
 
 
219
 
    json = sj.loads(page)
220
 
 
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
226
 
 
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
234
 
 
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'
238
 
 
239
 
    # check classes info
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']
255
 
 
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'
259
 
 
260
 
    # check classes_subteachers
261
 
    url = base_url + \
262
 
        '/api/schedule_get.json?user=test&key=test&year=2014&week=3'
263
 
    page = urllib.urlopen(url).read()
264
 
 
265
 
    json = sj.loads(page)
266
 
 
267
 
    assert json['data']['classes']['Monday']['date'] == '2014-01-13'
268
 
 
269
 
 
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'] == \
273
 
        'Aimee Garcias'
274
 
    assert json['data']['classes']['Monday']['classes'][0]['TeacherID2'] == 3
275
 
    assert json['data']['classes']['Monday']['classes'][0]['Teacher2'] == \
276
 
        'Pietje Puk'
277
 
    assert json['data']['classes']['Monday']['classes'][0]['Subteacher']
278
 
    assert not json['data']['classes']['Monday']['classes'][0]['Cancelled']
279
 
 
280
 
    # check cancelled
281
 
    url = base_url + \
282
 
        '/api/schedule_get.json?user=test&key=test&year=2014&week=4'
283
 
    page = urllib.urlopen(url).read()
284
 
 
285
 
    json = sj.loads(page)
286
 
 
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']
290
 
 
291
 
    # check holiday
292
 
    url = base_url + \
293
 
        '/api/schedule_get.json?user=test&key=test&year=2014&week=5'
294
 
    page = urllib.urlopen(url).read()
295
 
 
296
 
    json = sj.loads(page)
297
 
 
298
 
    assert json['data']['classes']['Monday']['date'] == '2014-01-27'
299
 
    assert json['data']['classes']['Monday']['classes'][0]['Holiday']
300
 
 
301
 
    # Check TeacherID parameter
302
 
    teID = 2
303
 
    url = base_url + \
304
 
        '/api/schedule_get.json?user=test&key=test&year=2014&week=2&TeacherID=' + unicode(teID)
305
 
    page = urllib.urlopen(url).read()
306
 
 
307
 
    json = sj.loads(page)
308
 
 
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'] == \
312
 
        'Edwin van de Ven'
313
 
 
314
 
 
315
 
def test_shedule_get_days_json(client, web2py):
316
 
    '''
317
 
        test schedule_get_days API endpoint
318
 
    '''
319
 
    populate_schedule(web2py)
320
 
 
321
 
 
322
 
    # check normal class
323
 
    url = base_url + \
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)
327
 
 
328
 
 
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']
344
 
 
345
 
    # check subteachers class
346
 
    url = base_url + \
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)
350
 
 
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'] == \
355
 
        'Aimee Garcias'
356
 
    assert json['data'][0]['classes'][0]['TeacherID2'] == 3
357
 
    assert json['data'][0]['classes'][0]['Teacher2'] == \
358
 
        'Pietje Puk'
359
 
    assert json['data'][0]['classes'][0]['Subteacher']
360
 
    assert not json['data'][0]['classes'][0]['Cancelled']
361
 
 
362
 
 
363
 
    # check cancelled
364
 
    url = base_url + \
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)
368
 
 
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']
372
 
 
373
 
    # check holiday
374
 
    url = base_url + \
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)
378
 
 
379
 
    assert json['data'][0]['date'] == '2014-01-27'
380
 
    assert json['data'][0]['classes'][0]['Holiday']
381
 
 
382
 
 
383
 
def test_workshops_get_json(client, web2py):
384
 
    '''
385
 
        test workshops_get API endpoint
386
 
    '''
387
 
    populate_workshops_for_api_tests(web2py)
388
 
 
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)
393
 
 
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)
399
 
 
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
410
 
 
411
 
 
412
 
def test_workshop_get_json(client, web2py):
413
 
    '''
414
 
        test workshop_get API endpoint
415
 
    '''
416
 
    populate_workshops_for_api_tests(web2py)
417
 
 
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)
422
 
 
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)
428
 
 
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
439
 
 
440
 
 
441
 
def workshop_get_dates(activities):
442
 
    '''
443
 
        :param workshop: Workshop object
444
 
        :param activities: workshop activities rows
445
 
    '''
446
 
    date_until = ''
447
 
    if len(activities) > 0:
448
 
        date_from = activities[0].Activitydate
449
 
 
450
 
    if len(activities) > 1:
451
 
        date_until = activities[len(activities) - 1].Activitydate
452
 
 
453
 
    if len(activities) == 0: # no activities
454
 
        date_from = T("No activities found...")
455
 
        date_until = T("No activities found...")
456
 
 
457
 
    return dict(date_from=date_from,
458
 
                date_until=date_until)
 
 
b'\\ No newline at end of file'