3
from ivle.webapp.base.rest import JSONRESTView, named_operation
3
from ivle.webapp.base.views import RESTView, JSONRESTView, named_operation
4
4
from ivle.webapp.errors import BadRequest, MethodNotAllowed
5
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.'''
7
class JSONRESTViewTest(JSONRESTView):
8
'''A small JSON REST view for testing purposes.'''
10
10
return {'method': 'get'}
12
def PUT(self, req, data):
13
return {'method': 'put',
14
'result': data['result'], 'test': data['test']}
12
16
def PATCH(self, req, data):
13
17
return {'method': 'patch',
14
18
'result': data['result'], 'test': data['test']}
17
def do_stuff(self, req, what):
21
def do_stuff(self, what):
18
22
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
24
class TestJSONRESTView:
40
26
req = FakeRequest()
80
66
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']
69
except MethodNotAllowed:
97
72
raise AssertionError("did not raise MethodNotAllowed")
166
141
assert e.message == 'Missing arguments: what'
168
143
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")