-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-kindycal.py
executable file
·483 lines (414 loc) · 16 KB
/
test-kindycal.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python
instructions=='''
- start server with empty database, eg:
/usr/local/google_appengine/dev_appserver.py --host 192.168.0.3 --clear_datastore True www
- then ./test-kindycal.py 192.168.0.3:8080 password
(password is the staff password, which defaults to just password)
'''
import sys
import os.path
d=os.path.abspath(os.path.dirname(sys.argv[0]))
sys.path.append(os.path.join(d,'www'))
import httplib
import urllib
import pq
from xn import inContext,firstLineOf
import sys
from pq import attrEquals,tagName
import random
l1=firstLineOf
import json
def toJson(x):
return json.dumps(x,sort_keys=True,indent=4,separators=(',',': '))
def fromJson(x):
return json.loads(x)
import types
def assert_equal(a,b):
assert a==b,(a,b)
pass
def assert_in(field,dictionary):
assert field in dictionary,(field,dictionary)
pass
class Scope:
def __init__(self,description):
self.description=description
self.result_=None
print '+ '+self.description
pass
def __del__(self):
print '- '+self.description+' = '+repr(self.result_)
pass
def result(self,result):
self.result_=result
return result
pass
def getSession(cookies):
x=[_.strip() for _ in cookies.split(';') if _.startswith('kc-session=')]
assert len(x)==1, (x,cookies)
return x[0].split('=')[1]
class Staff:
def __init__(self,server,staff_password):
'login in to %(server)s/staff using password %(staff_password)s'
try:
self.c=httplib.HTTPConnection(server)
self.c.set_debuglevel(1)
self.c.request('GET','/staff_login.html')
r=self.c.getresponse()
page=pq.parse(r.read())
assert len(page.find(tagName('input')).filter(attrEquals(
'name','password'))),(r,r.getheaders(),unicode(page))
params=urllib.urlencode({
'password':staff_password
})
self.c.request('POST','/staff_login.html',params)
r=self.c.getresponse()
assert r.status==302,(r.status,r.read())
content=r.read()
self.session=getSession(r.getheader('Set-Cookie'))
assert not self.session is None, self.getheaders()
self.headers={
'Cookie':'kc-session=%s'%self.session
}
assert r.getheader('location')=='http://'+server+'/events.html', repr( (r,r.getheaders(),content) )
except:
raise inContext(l1(Staff.__init__.__doc__)%vars())
pass
def logout(self,headers={}):
'logout %(self)s'
scope=Scope(l1(Staff.logout.__doc__)%vars())
try:
headers['Cookie']='kc-session=%s'%self.session
self.c.request('GET','/logout','',headers)
r=self.c.getresponse()
content=fromJson(r.read())
assert content['result']=='OK',content
except:
raise inContext(scope.description)
pass
def get(self,url,params={},headers={}):
'send HTTP GET to %(self)r page %(url)r with params %(params)r and headers %(headers)r'
scope=Scope(l1(Staff.get.__doc__)%vars())
try:
assert url.startswith('/'),url
headers['Cookie']='kc-session=%s'%self.session
self.c.request('GET',url+'?'+urllib.urlencode(params),'',headers)
return self.c.getresponse()
except:
raise inContext(scope.description)
pass
def post(self,url,params={},headers={}):
'send HTTP POST to %(self)r page %(url)r with params %(params)r and headers %(headers)r'
scope=Scope(l1(Staff.post.__doc__)%vars())
try:
assert url.startswith('/'),url
headers['Cookie']='kc-session=%s'%self.session
self.c.request('POST',url,urllib.urlencode(params),headers)
return self.c.getresponse()
except:
raise inContext(scope.description)
pass
def postMulipartFormData(self,url,parts,headers={}):
'post multipart/form-data to %(self)s url %(url)s'
scope=Scope(l1(Staff.postMulipartFormData.__doc__)%vars())
try:
boundary='------------%08x%08x' % (random.randint(0,0xffffffff),random.randint(0,0xffffffff))
while len([_ for _ in parts if boundary in _]):
boundary='------------%08x%08x' % (random.randint(0,0xffffffff),random.randint(0,0xffffffff))
pass
body='\r\n'.join(['--'+boundary+'\r\n'+part for part in parts]+['--'+boundary+'--\r\n'])
h=dict(headers.items())
h['Content-Type']='multipart/form-data; boundary=%(boundary)s'%vars()
h['Cookie']='kc-session=%s'%self.session
self.c.request('POST',url,body,h)
return self.c.getresponse()
except:
raise inContext(scope.description)
pass
def postFile(self,url,fieldName,fileName,mime_type,data,headers={}):
'post file %(fileName)s of type %(mime_type)s as field %(fieldName)s to %(self)s url %(url)s'
scope=Scope(l1(Staff.postFile.__doc__)%vars())
try:
lines=['Content-Disposition: form-data; name="%(fieldName)s"; filename="%(fileName)s"'%{
'fieldName':fieldName.replace(r'"',r'\"').encode('ascii'),
'fileName':fileName.replace(r'"',r'\"').encode('ascii')
},
'Content-Type: %(mime_type)s'%vars(),
'',
data]
part='\r\n'.join(_ for _ in lines)
return self.postMulipartFormData(url,[part],headers)
except:
raise inContext(scope.description)
pass
def uploadFile(self,fileName,mime_type,content,headers={}):
'post %(fileName)s of type %(mime_type)s and given content to /uploaded_file, returning resulting id'
scope=Scope(l1(Staff.__doc__)%vars())
try:
r=staff.postFile('/uploaded_file','filename',fileName,mime_type,content,headers)
result=fromJson(r.read())
assert 'result' in result, toJson(result).encode('utf8')
assert 'id' in result['result']
xxx=result['result']['id']
assert type(xxx) is types.IntType, xxx
assert xxx != 0, xxx
return xxx
except:
raise inContext(scope.description)
pass
def verifyFileRefcount(self,uploadedFileId,expectedCount):
'verify that %(self)s has %(expectedCount)s as uploaded_file %(uploadedFileId)s refcount'
scope=Scope(l1(Staff.verifyFileRefcount.__doc__)%vars())
try:
r=self.get('/uploaded_file_refcount',{'id':uploadedFileId})
assert r.status==200,r.status
content=fromJson(r.read())
assert 'refcount' in content,content
assert content['refcount']==expectedCount,(content,expectedCount)
except:
raise inContext(scope.description)
pass
def verifyUploadedFile(self,uploadedFileId,mime_type,content):
'verify that %(self)s uploaded_file %(uploadedFileId)s has type %(mime_type)s and content %(content)r'
scope=Scope(l1(Staff.verifyUploadedFile.__doc__)%vars())
try:
r=staff.get('/uploaded_file',{'id':'%(uploadedFileId)s'%vars()})
assert_equal(r.status,200)
assert_equal(r.getheader('Content-Type'),'text/plain')
assert_equal(r.read(),content)
except:
raise inContext(scope.description)
pass
def postJSON(self,url,params,headers={}):
'post %(params)r as json encoded "params" to %(url)s'
'return json-decode response'
scope=Scope(l1(Staff.postJSON.__doc__)%vars())
try:
r=self.post(url,{'params':toJson(params)},headers)
assert_equal(r.status,200)
return scope.result(fromJson(r.read()))
except:
raise inContext(scope.description)
pass
def __str__(self):
return 'Staff session %(session)r'%self.__dict__
pass
try:
server,staff_password=sys.argv[1:]
except:
print instructions
sys.exit(1)
pass
staff=Staff(server,staff_password)
xxx=staff.uploadFile('xxx.txt','text/plain','xxx\n')
staff.verifyFileRefcount(xxx,1)
r=staff.get('/uploaded_file',{'id':str(xxx)})
assert_equal(r.read(),'xxx\n')
yyy=staff.uploadFile('yyy.txt','text/plain','yyy\n')
staff.verifyFileRefcount(yyy,1)
r=staff.get('/uploaded_file',{'id':str(yyy)})
assert_equal(r.read(),'yyy\n')
assert xxx != yyy, (xxx,yyy)
assert_equal(staff.uploadFile('yyy2.txt','text/plain','yyy\n'),yyy)
staff.verifyFileRefcount(yyy,1)
prev_session=staff.session
staff.logout()
del staff
staff=Staff(server,staff_password)
assert prev_session!=staff.session, (prev_session,staff.session)
staff.verifyFileRefcount(xxx,0)
staff.verifyFileRefcount(yyy,0)
r=staff.get('/uploaded_file',{'id':str(yyy)})
assert_equal(r.status,404)
assert_equal(r.read(),'')
yyy3=staff.uploadFile('yyy3.txt','text/plain','yyy\n')
staff.verifyFileRefcount(yyy3,1)
assert_in('result',staff.postJSON('/event',{
'id': 101,
'groups': [0],
'dates': [ { 'year':2016,'month':3,'day':13 } ],
'name':{
'text':'event 101',
'colour':'#000000'
},
'description': {
'html': 'event 101 <img src="uploaded_file?id=%(yyy3)s">'%vars()
}
}))
staff.verifyFileRefcount(yyy3,2)
mf1=staff.uploadFile('mf1.txt','text/plain','mf1\n')
staff.verifyFileRefcount(mf1,1)
mf2=staff.uploadFile('mf2.txt','text/plain','mf2\n')
staff.verifyFileRefcount(mf2,1)
assert_in('result',staff.postJSON('/maintenance_day',{
'id':102,
'name':'md1',
'date':{'year':2016,'month':3,'day':23},
'groups':[3],
'description':{'html':'<a href="uploaded_file?id=%(mf1)s">mf1</a>'%vars()},
'maxVolunteers':2,
'volunteers':[{
'childs_name':'fred jr',
'parents_name':'fred',
'attended':True,
'note':'<a href="uploaded_file?id=%(mf2)s">mf2</a>'%vars()
}]
}))
staff.verifyFileRefcount(yyy3,2)
staff.verifyFileRefcount(mf1,2)
staff.verifyFileRefcount(mf2,2)
prev_session=staff.session
staff.logout()
staff=Staff(server,staff_password)
assert prev_session!=staff.session, (prev_session,staff.session)
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,1)
staff.verifyUploadedFile(yyy3,'text/plain','yyy\n')
staff.verifyUploadedFile(mf1,'text/plain','mf1\n')
staff.verifyUploadedFile(mf2,'text/plain','mf2\n')
assert_in('result',staff.postJSON(
'/maintenance_day',
{
'id':102,
'name':'md1',
'date':{'year':2016,'month':3,'day':23},
'groups':[3],
'description':{'html':'<a href="uploaded_file?id=%(mf1)s">mf1</a>'%vars()},
'maxVolunteers':2,
'volunteers':[{
'childs_name':'fred jr',
'parents_name':'fred',
'attended':True,
'note':'none'%vars()
}]
}))
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,0)
staff.verifyUploadedFile(mf1,'text/plain','mf1\n')
rj1=staff.postJSON('/roster_job',{
'id':0,
'name':'Chicken Feeding',
'per':'unit',
'description':'chicken feeding as per <a href="uploaded_file?id=%(mf1)s">mf1.txt</a>'%vars(),
'frequency':'as_required',
'volunteers_required':3})['result']
staff.verifyFileRefcount(mf1,2)
assert_equal(fromJson(staff.get('/roster_job',{'id':rj1}).read())['result'],{
'id':rj1,
'name':'Chicken Feeding',
'per':'unit',
'description':'chicken feeding as per <a href="uploaded_file?id=%(mf1)s">mf1.txt</a>'%vars(),
'frequency':'as_required',
'volunteers_required':3})
mf3=staff.uploadFile('mf3.jpg','image/jpeg','mf3')
staff.verifyFileRefcount(mf3,1)
assert_in('result',staff.postJSON('/roster_job',{
'id':rj1,
'name':'Chicken Feeding',
'per':'unit',
'description':'chicken feeding as like <img src="uploaded_file?id=%(mf3)s">'%vars(),
'frequency':'as_required',
'volunteers_required':3}))
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,2)
assert_equal(staff.postJSON('/add_roster_job_volunteer',{
'id':rj1,
'groups':[1,2],
'parents_name':'jock',
'childs_name':'jock jr'})['result']['added'],True)
assert_in('result',staff.postJSON('/update_roster_job_volunteer',{
'id':rj1,
'groups':[1,2],
'volunteer':{
'childs_name':'jock jr',
'parents_name':'jock',
'attended':True,
'note':''
},
'new_volunteer':{
'childs_name':'jock jr',
'parents_name':'jock',
'attended':True,
'note':'saw <img src="uploaded_file?id=%(mf3)s">'%vars()
}}))
assert_equal(staff.postJSON('/add_roster_job_volunteer',{
'id':rj1,
'groups':[1,2],
'parents_name':'fred',
'childs_name':'fred jr'})['result']['added'],True)
assert_in('result',staff.postJSON('/update_roster_job_volunteer',{
'id':rj1,
'groups':[1,2],
'volunteer':{
'childs_name':'fred jr',
'parents_name':'fred',
'attended':True,
'note':''
},
'new_volunteer':{
'childs_name':'fred jr',
'parents_name':'fred',
'attended':True,
'note':'saw <img src="uploaded_file?id=%(yyy3)s">'%vars()
}}))
staff.verifyFileRefcount(yyy3,2)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,2)
assert_in('result',staff.postJSON('/delete_roster_job_volunteer',{
'id':rj1,
'groups':[1,2],
'childs_name':'fred jr',
}))
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,2)
assert_in('result',staff.postJSON('/roster_job',{
'id':rj1,
'name':'Chicken Feeding',
'per':'unit',
'description':'chicken feeding',
'frequency':'as_required',
'volunteers_required':3}))
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,2)
rj2=staff.postJSON('/roster_job',{
'id':0,
'name':'Chicken Feeding 3',
'per':'unit',
'description':'chicken feeding as per <a href="uploaded_file?id=%(mf3)s">mf3.txt</a>'%vars(),
'frequency':'as_required',
'volunteers_required':3})['result']
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,3)
assert_in('result',staff.postJSON('/delete_roster_job',{'id':rj1}))
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,1)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,2)
assert_in('result',staff.postJSON('/delete_maintenance_day',{'id':102}))
assert_in('result',staff.postJSON('/delete_roster_job',{'id':rj2}))
staff.verifyFileRefcount(yyy3,1)
staff.verifyFileRefcount(mf1,0)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,1)
assert_in('result',staff.postJSON('/delete_event',{'id':101}))
staff.verifyFileRefcount(yyy3,0)
staff.verifyFileRefcount(mf1,0)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,1)
staff.logout()
staff=Staff(server,staff_password)
staff.verifyFileRefcount(yyy3,0)
staff.verifyFileRefcount(mf1,0)
staff.verifyFileRefcount(mf2,0)
staff.verifyFileRefcount(mf3,0)
staff.logout()