1
from ivle.webapp.base.views import RESTView, JSONRESTView
2
from ivle.webapp.errors import BadRequest
3
from ivle.webapp.base.views import RESTView, JSONRESTView, named_operation
4
from ivle.webapp.errors import BadRequest, MethodNotAllowed
3
5
from ivle.webapp.testing import FakeUser, FakeRequest
5
7
class JSONRESTViewTest(JSONRESTView):
15
17
return {'method': 'patch',
16
18
'result': data['result'], 'test': data['test']}
21
def do_stuff(self, what):
22
return {'result': 'Did %s!' % what}
18
24
class TestJSONRESTView:
20
26
req = FakeRequest()
72
78
view = JSONRESTViewTest(req)
81
except MethodNotAllowed:
84
raise AssertionError("did not raise MethodNotAllowed")
86
def testNamedOperation(self):
89
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
91
view = JSONRESTViewTest(req)
93
assert req.content_type == 'application/json'
94
assert req.response_body == '{"result": "Did blah!"}\n'
96
def testPOSTWithoutName(self):
99
req.request_body = urllib.urlencode({'what': 'blah'})
100
view = JSONRESTViewTest(req)
103
except BadRequest, e:
104
assert e.message == 'No named operation specified.'
106
raise AssertionError("did not raise BadRequest")
108
def testNonexistentNamedOperation(self):
111
req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
112
view = JSONRESTViewTest(req)
115
except BadRequest, e:
116
assert e.message == 'Invalid named operation.'
118
raise AssertionError("did not raise BadRequest")
120
def testDisallowedNamedOperation(self):
123
req.request_body = urllib.urlencode({'ivle.op': 'GET'})
124
view = JSONRESTViewTest(req)
127
except BadRequest, e:
128
assert e.message == 'Invalid named operation.'
130
raise AssertionError("did not raise BadRequest")
132
def testNamedOperationWithMissingArgs(self):
135
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
137
view = JSONRESTViewTest(req)
140
except BadRequest, e:
141
assert e.message == 'Missing arguments: what'
78
143
raise AssertionError("did not raise BadRequest")