3
from ivle.webapp.base.rest import JSONRESTView, named_operation
4
from ivle.webapp.errors import BadRequest, MethodNotAllowed
5
from ivle.webapp.testing import FakeUser, FakeRequest
7
class JSONRESTViewTestWithoutPUT(JSONRESTView):
8
'''A small JSON REST view for testing purposes, without a PUT method.'''
10
return {'method': 'get'}
12
def PATCH(self, req, data):
13
return {'method': 'patch',
14
'result': data['result'], 'test': data['test']}
17
def do_stuff(self, req, what):
18
return {'result': 'Did %s!' % what}
21
def say_something(self, req, thing='nothing'):
22
return {'result': 'Said %s!' % thing}
25
def do_say_something(self, req, what, thing='nothing'):
26
return {'result': 'Said %s and %s!' % (what, thing)}
29
def get_req_method(self, req):
30
return {'method': req.method}
32
class JSONRESTViewTest(JSONRESTViewTestWithoutPUT):
33
'''A small JSON REST view for testing purposes.'''
34
def PUT(self, req, data):
35
return {'method': 'put',
36
'result': data['result'], 'test': data['test']}
38
class TestJSONRESTView:
41
view = JSONRESTViewTest(req)
43
assert req.content_type == 'application/json'
44
assert req.response_body == '{"method": "get"}\n'
49
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
50
view = JSONRESTViewTest(req)
52
assert req.content_type == 'application/json'
53
assert req.response_body == \
54
'{"test": "FAI\\ua746ED", "method": "put", "result": 1}\n'
59
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
60
view = JSONRESTViewTest(req)
62
assert req.content_type == 'application/json'
63
assert req.response_body == \
64
'{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
66
def testEmulatedPATCH(self):
69
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
70
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
71
view = JSONRESTViewTest(req)
73
assert req.content_type == 'application/json'
74
assert req.response_body == \
75
'{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
77
def testInvalidMethod(self):
79
req.method = 'FAKEANDBOGUS'
80
view = JSONRESTViewTest(req)
83
except MethodNotAllowed, e:
84
assert e.allowed == ['GET', 'PUT', 'PATCH', 'POST']
86
raise AssertionError("did not raise MethodNotAllowed")
88
def testNoPUTMethod(self):
91
view = JSONRESTViewTestWithoutPUT(req)
94
except MethodNotAllowed, e:
95
assert e.allowed == ['GET', 'PATCH', 'POST']
97
raise AssertionError("did not raise MethodNotAllowed")
99
def testInvalidMethodWithPATCHEmulation(self):
101
req.method = 'FAKEANDBOGUS'
102
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
103
view = JSONRESTViewTest(req)
106
except MethodNotAllowed:
109
raise AssertionError("did not raise MethodNotAllowed")
111
def testNamedOperation(self):
114
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
116
view = JSONRESTViewTest(req)
118
assert req.content_type == 'application/json'
119
assert req.response_body == '{"result": "Did blah!"}\n'
121
def testPOSTWithoutName(self):
124
req.request_body = urllib.urlencode({'what': 'blah'})
125
view = JSONRESTViewTest(req)
128
except BadRequest, e:
129
assert e.message == 'No named operation specified.'
131
raise AssertionError("did not raise BadRequest")
133
def testNonexistentNamedOperation(self):
136
req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
137
view = JSONRESTViewTest(req)
140
except BadRequest, e:
141
assert e.message == 'Invalid named operation.'
143
raise AssertionError("did not raise BadRequest")
145
def testDisallowedNamedOperation(self):
148
req.request_body = urllib.urlencode({'ivle.op': 'GET'})
149
view = JSONRESTViewTest(req)
152
except BadRequest, e:
153
assert e.message == 'Invalid named operation.'
155
raise AssertionError("did not raise BadRequest")
157
def testNamedOperationWithMissingArgs(self):
160
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
162
view = JSONRESTViewTest(req)
165
except BadRequest, e:
166
assert e.message == 'Missing arguments: what'
168
raise AssertionError("did not raise BadRequest")
170
def testNamedOperationWithExtraArgs(self):
173
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
176
view = JSONRESTViewTest(req)
179
except BadRequest, e:
180
assert e.message == 'Extra arguments: toomany'
182
raise AssertionError("did not raise BadRequest")
184
def testNamedOperationWithDefaultArgs(self):
187
req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
188
view = JSONRESTViewTest(req)
190
assert req.content_type == 'application/json'
191
assert req.response_body == '{"result": "Said nothing!"}\n'
193
def testNamedOperationWithOverriddenDefaultArgs(self):
196
req.request_body = urllib.urlencode({'ivle.op': 'say_something',
197
'thing': 'something'})
198
view = JSONRESTViewTest(req)
200
assert req.content_type == 'application/json'
201
assert req.response_body == '{"result": "Said something!"}\n'
203
def testNamedOperationWithDefaultAndMissingArgs(self):
206
req.request_body = urllib.urlencode({'ivle.op': 'do_say_something',
207
'thing': 'something'})
208
view = JSONRESTViewTest(req)
211
except BadRequest, e:
212
assert e.message == 'Missing arguments: what'
214
raise AssertionError("did not raise BadRequest")
216
def testNamedOperationUsingRequest(self):
219
req.request_body = urllib.urlencode({'ivle.op': 'get_req_method'})
220
view = JSONRESTViewTest(req)
222
assert req.content_type == 'application/json'
223
assert req.response_body == '{"method": "POST"}\n'
225
def testInvalidPOSTData(self):
228
req.request_body = 'I am invalid&&&&'
229
view = JSONRESTViewTest(req)
232
except BadRequest, e:
234
assert e.message == 'No named operation specified.'
236
raise AssertionError("did not raise BadRequest")
238
def testInvalidPATCHData(self):
241
req.request_body = 'I am invalid'
242
view = JSONRESTViewTest(req)
245
except BadRequest, e:
246
assert e.message == 'Invalid JSON data'
248
raise AssertionError("did not raise BadRequest")
250
def testInvalidPUTData(self):
253
req.request_body = 'I am invalid'
254
view = JSONRESTViewTest(req)
257
except BadRequest, e:
258
assert e.message == 'Invalid JSON data'
260
raise AssertionError("did not raise BadRequest")