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 JSONRESTViewTest(JSONRESTView):
8
'''A small JSON REST view for testing purposes.'''
10
return {'method': 'get'}
12
def PUT(self, req, data):
13
return {'method': 'put',
14
'result': data['result'], 'test': data['test']}
16
def PATCH(self, req, data):
17
return {'method': 'patch',
18
'result': data['result'], 'test': data['test']}
21
def do_stuff(self, what):
22
return {'result': 'Did %s!' % what}
24
class TestJSONRESTView:
27
view = JSONRESTViewTest(req)
29
assert req.content_type == 'application/json'
30
assert req.response_body == '{"method": "get"}\n'
35
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
36
view = JSONRESTViewTest(req)
38
assert req.content_type == 'application/json'
39
assert req.response_body == \
40
'{"test": "FAI\\ua746ED", "method": "put", "result": 1}\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": "patch", "result": 1}\n'
52
def testEmulatedPATCH(self):
55
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
56
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
57
view = JSONRESTViewTest(req)
59
assert req.content_type == 'application/json'
60
assert req.response_body == \
61
'{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
63
def testInvalidMethod(self):
65
req.method = 'FAKEANDBOGUS'
66
view = JSONRESTViewTest(req)
69
except MethodNotAllowed:
72
raise AssertionError("did not raise MethodNotAllowed")
74
def testInvalidMethodWithPATCHEmulation(self):
76
req.method = 'FAKEANDBOGUS'
77
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
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'
143
raise AssertionError("did not raise BadRequest")