~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/base/test/test_rest.py

Merged from new-dispatch branch.
This branch is now a child of new-dispatch (until that branch is merged with
    trunk).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import urllib
 
2
 
 
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
 
6
 
 
7
class JSONRESTViewTestWithoutPUT(JSONRESTView):
 
8
    '''A small JSON REST view for testing purposes, without a PUT method.'''
 
9
    def GET(self, req):
 
10
        return {'method': 'get'}
 
11
 
 
12
    def PATCH(self, req, data):
 
13
        return {'method': 'patch',
 
14
                'result': data['result'], 'test': data['test']}
 
15
 
 
16
    @named_operation
 
17
    def do_stuff(self, req, what):
 
18
        return {'result': 'Did %s!' % what}
 
19
 
 
20
    @named_operation
 
21
    def say_something(self, req, thing='nothing'):
 
22
        return {'result': 'Said %s!' % thing}
 
23
 
 
24
    @named_operation
 
25
    def do_say_something(self, req, what, thing='nothing'):
 
26
        return {'result': 'Said %s and %s!' % (what, thing)}
 
27
 
 
28
    @named_operation
 
29
    def get_req_method(self, req):
 
30
        return {'method': req.method}
 
31
 
 
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']}
 
37
 
 
38
class TestJSONRESTView:
 
39
    def testGET(self):
 
40
        req = FakeRequest()
 
41
        view = JSONRESTViewTest(req)
 
42
        view.render(req)
 
43
        assert req.content_type == 'application/json'
 
44
        assert req.response_body == '{"method": "get"}\n'
 
45
 
 
46
    def testPUT(self):
 
47
        req = FakeRequest()
 
48
        req.method = 'PUT'
 
49
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
 
50
        view = JSONRESTViewTest(req)
 
51
        view.render(req)
 
52
        assert req.content_type == 'application/json'
 
53
        assert req.response_body == \
 
54
                '{"test": "FAI\\ua746ED", "method": "put", "result": 1}\n'
 
55
 
 
56
    def testPATCH(self):
 
57
        req = FakeRequest()
 
58
        req.method = 'PATCH'
 
59
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
 
60
        view = JSONRESTViewTest(req)
 
61
        view.render(req)
 
62
        assert req.content_type == 'application/json'
 
63
        assert req.response_body == \
 
64
                '{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
 
65
 
 
66
    def testEmulatedPATCH(self):
 
67
        req = FakeRequest()
 
68
        req.method = 'PUT'
 
69
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
 
70
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
 
71
        view = JSONRESTViewTest(req)
 
72
        view.render(req)
 
73
        assert req.content_type == 'application/json'
 
74
        assert req.response_body == \
 
75
                '{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
 
76
 
 
77
    def testInvalidMethod(self):
 
78
        req = FakeRequest()
 
79
        req.method = 'FAKEANDBOGUS'
 
80
        view = JSONRESTViewTest(req)
 
81
        try:
 
82
            view.render(req)
 
83
        except MethodNotAllowed, e:
 
84
            assert e.allowed == ['GET', 'PUT', 'PATCH', 'POST']
 
85
        else:
 
86
            raise AssertionError("did not raise MethodNotAllowed")
 
87
 
 
88
    def testNoPUTMethod(self):
 
89
        req = FakeRequest()
 
90
        req.method = 'PUT'
 
91
        view = JSONRESTViewTestWithoutPUT(req)
 
92
        try:
 
93
            view.render(req)
 
94
        except MethodNotAllowed, e:
 
95
            assert e.allowed == ['GET', 'PATCH', 'POST']
 
96
        else:
 
97
            raise AssertionError("did not raise MethodNotAllowed")
 
98
 
 
99
    def testInvalidMethodWithPATCHEmulation(self):
 
100
        req = FakeRequest()
 
101
        req.method = 'FAKEANDBOGUS'
 
102
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
 
103
        view = JSONRESTViewTest(req)
 
104
        try:
 
105
            view.render(req)
 
106
        except MethodNotAllowed:
 
107
            pass
 
108
        else:
 
109
            raise AssertionError("did not raise MethodNotAllowed")
 
110
 
 
111
    def testNamedOperation(self):
 
112
        req = FakeRequest()
 
113
        req.method = 'POST'
 
114
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
115
                                             'what': 'blah'})
 
116
        view = JSONRESTViewTest(req)
 
117
        view.render(req)
 
118
        assert req.content_type == 'application/json'
 
119
        assert req.response_body == '{"result": "Did blah!"}\n'
 
120
 
 
121
    def testPOSTWithoutName(self):
 
122
        req = FakeRequest()
 
123
        req.method = 'POST'
 
124
        req.request_body = urllib.urlencode({'what': 'blah'})
 
125
        view = JSONRESTViewTest(req)
 
126
        try:
 
127
            view.render(req)
 
128
        except BadRequest, e:
 
129
            assert e.message == 'No named operation specified.'
 
130
        else:
 
131
            raise AssertionError("did not raise BadRequest")
 
132
 
 
133
    def testNonexistentNamedOperation(self):
 
134
        req = FakeRequest()
 
135
        req.method = 'POST'
 
136
        req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
 
137
        view = JSONRESTViewTest(req)
 
138
        try:
 
139
            view.render(req)
 
140
        except BadRequest, e:
 
141
            assert e.message == 'Invalid named operation.'
 
142
        else:
 
143
            raise AssertionError("did not raise BadRequest")
 
144
 
 
145
    def testDisallowedNamedOperation(self):
 
146
        req = FakeRequest()
 
147
        req.method = 'POST'
 
148
        req.request_body = urllib.urlencode({'ivle.op': 'GET'})
 
149
        view = JSONRESTViewTest(req)
 
150
        try:
 
151
            view.render(req)
 
152
        except BadRequest, e:
 
153
            assert e.message == 'Invalid named operation.'
 
154
        else:
 
155
            raise AssertionError("did not raise BadRequest")
 
156
 
 
157
    def testNamedOperationWithMissingArgs(self):
 
158
        req = FakeRequest()
 
159
        req.method = 'POST'
 
160
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
161
                                             'nothing': 'wrong'})
 
162
        view = JSONRESTViewTest(req)
 
163
        try:
 
164
            view.render(req)
 
165
        except BadRequest, e:
 
166
            assert e.message == 'Missing arguments: what'
 
167
        else:
 
168
            raise AssertionError("did not raise BadRequest")
 
169
 
 
170
    def testNamedOperationWithExtraArgs(self):
 
171
        req = FakeRequest()
 
172
        req.method = 'POST'
 
173
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
174
                                             'what': 'blah',
 
175
                                             'toomany': 'args'})
 
176
        view = JSONRESTViewTest(req)
 
177
        try:
 
178
            view.render(req)
 
179
        except BadRequest, e:
 
180
            assert e.message == 'Extra arguments: toomany'
 
181
        else:
 
182
            raise AssertionError("did not raise BadRequest")
 
183
 
 
184
    def testNamedOperationWithDefaultArgs(self):
 
185
        req = FakeRequest()
 
186
        req.method = 'POST'
 
187
        req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
 
188
        view = JSONRESTViewTest(req)
 
189
        view.render(req)
 
190
        assert req.content_type == 'application/json'
 
191
        assert req.response_body == '{"result": "Said nothing!"}\n'
 
192
 
 
193
    def testNamedOperationWithOverriddenDefaultArgs(self):
 
194
        req = FakeRequest()
 
195
        req.method = 'POST'
 
196
        req.request_body = urllib.urlencode({'ivle.op': 'say_something',
 
197
                                             'thing': 'something'})
 
198
        view = JSONRESTViewTest(req)
 
199
        view.render(req)
 
200
        assert req.content_type == 'application/json'
 
201
        assert req.response_body == '{"result": "Said something!"}\n'
 
202
 
 
203
    def testNamedOperationWithDefaultAndMissingArgs(self):
 
204
        req = FakeRequest()
 
205
        req.method = 'POST'
 
206
        req.request_body = urllib.urlencode({'ivle.op': 'do_say_something',
 
207
                                             'thing': 'something'})
 
208
        view = JSONRESTViewTest(req)
 
209
        try:
 
210
            view.render(req)
 
211
        except BadRequest, e:
 
212
            assert e.message == 'Missing arguments: what'
 
213
        else:
 
214
            raise AssertionError("did not raise BadRequest")
 
215
 
 
216
    def testNamedOperationUsingRequest(self):
 
217
        req = FakeRequest()
 
218
        req.method = 'POST'
 
219
        req.request_body = urllib.urlencode({'ivle.op': 'get_req_method'})
 
220
        view = JSONRESTViewTest(req)
 
221
        view.render(req)
 
222
        assert req.content_type == 'application/json'
 
223
        assert req.response_body == '{"method": "POST"}\n'
 
224
 
 
225
    def testInvalidPOSTData(self):
 
226
        req = FakeRequest()
 
227
        req.method = 'POST'
 
228
        req.request_body = 'I am invalid&&&&'
 
229
        view = JSONRESTViewTest(req)
 
230
        try:
 
231
            view.render(req)
 
232
        except BadRequest, e:
 
233
            print e.message
 
234
            assert e.message == 'No named operation specified.'
 
235
        else:
 
236
            raise AssertionError("did not raise BadRequest")
 
237
 
 
238
    def testInvalidPATCHData(self):
 
239
        req = FakeRequest()
 
240
        req.method = 'PATCH'
 
241
        req.request_body = 'I am invalid'
 
242
        view = JSONRESTViewTest(req)
 
243
        try:
 
244
            view.render(req)
 
245
        except BadRequest, e:
 
246
            assert e.message == 'Invalid JSON data'
 
247
        else:
 
248
            raise AssertionError("did not raise BadRequest")
 
249
 
 
250
    def testInvalidPUTData(self):
 
251
        req = FakeRequest()
 
252
        req.method = 'PUT'
 
253
        req.request_body = 'I am invalid'
 
254
        view = JSONRESTViewTest(req)
 
255
        try:
 
256
            view.render(req)
 
257
        except BadRequest, e:
 
258
            assert e.message == 'Invalid JSON data'
 
259
        else:
 
260
            raise AssertionError("did not raise BadRequest")