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

« back to all changes in this revision

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

ivle.webapp.admin, ivle.webapp.groups, ivle.webapp.tutorial: Remove broken
    'py:strip's from <html> elements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import urllib
2
2
 
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
6
6
 
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.'''
9
9
    def GET(self, req):
10
10
        return {'method': 'get'}
11
11
 
 
12
    def PUT(self, req, data):
 
13
        return {'method': 'put',
 
14
                'result': data['result'], 'test': data['test']}
 
15
 
12
16
    def PATCH(self, req, data):
13
17
        return {'method': 'patch',
14
18
                'result': data['result'], 'test': data['test']}
15
19
 
16
20
    @named_operation
17
 
    def do_stuff(self, req, what):
 
21
    def do_stuff(self, what):
18
22
        return {'result': 'Did %s!' % what}
19
23
 
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
24
class TestJSONRESTView:
39
25
    def testGET(self):
40
26
        req = FakeRequest()
80
66
        view = JSONRESTViewTest(req)
81
67
        try:
82
68
            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']
 
69
        except MethodNotAllowed:
 
70
            pass
96
71
        else:
97
72
            raise AssertionError("did not raise MethodNotAllowed")
98
73
 
166
141
            assert e.message == 'Missing arguments: what'
167
142
        else:
168
143
            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")