3
from ivle.webapp.base.views import RESTView, 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 get_req_method(self, req):
26
return {'method': req.method}
28
class JSONRESTViewTest(JSONRESTViewTestWithoutPUT):
29
'''A small JSON REST view for testing purposes.'''
30
def PUT(self, req, data):
31
return {'method': 'put',
32
'result': data['result'], 'test': data['test']}
34
class TestJSONRESTView:
37
view = JSONRESTViewTest(req)
39
assert req.content_type == 'application/json'
40
assert req.response_body == '{"method": "get"}\n'
45
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
46
view = JSONRESTViewTest(req)
48
assert req.content_type == 'application/json'
49
assert req.response_body == \
50
'{"test": "FAI\\ua746ED", "method": "put", "result": 1}\n'
55
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
56
view = JSONRESTViewTest(req)
58
assert req.content_type == 'application/json'
59
assert req.response_body == \
60
'{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
62
def testEmulatedPATCH(self):
65
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
66
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
67
view = JSONRESTViewTest(req)
69
assert req.content_type == 'application/json'
70
assert req.response_body == \
71
'{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
73
def testInvalidMethod(self):
75
req.method = 'FAKEANDBOGUS'
76
view = JSONRESTViewTest(req)
79
except MethodNotAllowed, e:
80
assert e.allowed == ['GET', 'PUT', 'PATCH', 'POST']
82
raise AssertionError("did not raise MethodNotAllowed")
84
def testNoPUTMethod(self):
87
view = JSONRESTViewTestWithoutPUT(req)
90
except MethodNotAllowed, e:
91
assert e.allowed == ['GET', 'PATCH', 'POST']
93
raise AssertionError("did not raise MethodNotAllowed")
95
def testInvalidMethodWithPATCHEmulation(self):
97
req.method = 'FAKEANDBOGUS'
98
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
99
view = JSONRESTViewTest(req)
102
except MethodNotAllowed:
105
raise AssertionError("did not raise MethodNotAllowed")
107
def testNamedOperation(self):
110
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
112
view = JSONRESTViewTest(req)
114
assert req.content_type == 'application/json'
115
assert req.response_body == '{"result": "Did blah!"}\n'
117
def testPOSTWithoutName(self):
120
req.request_body = urllib.urlencode({'what': 'blah'})
121
view = JSONRESTViewTest(req)
124
except BadRequest, e:
125
assert e.message == 'No named operation specified.'
127
raise AssertionError("did not raise BadRequest")
129
def testNonexistentNamedOperation(self):
132
req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
133
view = JSONRESTViewTest(req)
136
except BadRequest, e:
137
assert e.message == 'Invalid named operation.'
139
raise AssertionError("did not raise BadRequest")
141
def testDisallowedNamedOperation(self):
144
req.request_body = urllib.urlencode({'ivle.op': 'GET'})
145
view = JSONRESTViewTest(req)
148
except BadRequest, e:
149
assert e.message == 'Invalid named operation.'
151
raise AssertionError("did not raise BadRequest")
153
def testNamedOperationWithMissingArgs(self):
156
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
158
view = JSONRESTViewTest(req)
161
except BadRequest, e:
162
assert e.message == 'Missing arguments: what'
164
raise AssertionError("did not raise BadRequest")
166
def testNamedOperationWithExtraArgs(self):
169
req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
172
view = JSONRESTViewTest(req)
175
except BadRequest, e:
176
assert e.message == 'Extra arguments: toomany'
178
raise AssertionError("did not raise BadRequest")
180
def testNamedOperationWithDefaultArgs(self):
183
req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
184
view = JSONRESTViewTest(req)
186
assert req.content_type == 'application/json'
187
assert req.response_body == '{"result": "Said nothing!"}\n'
189
def testNamedOperationWithOverriddenDefaultArgs(self):
192
req.request_body = urllib.urlencode({'ivle.op': 'say_something',
193
'thing': 'something'})
194
view = JSONRESTViewTest(req)
196
assert req.content_type == 'application/json'
197
assert req.response_body == '{"result": "Said something!"}\n'
199
def testNamedOperationUsingRequest(self):
202
req.request_body = urllib.urlencode({'ivle.op': 'get_req_method'})
203
view = JSONRESTViewTest(req)
205
assert req.content_type == 'application/json'
206
assert req.response_body == '{"method": "POST"}\n'