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

« back to all changes in this revision

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

ivle.webapp.console.service: Port www/apps/consoleservice to new framework.
    consoleservice now lives under /console/service, and actions are taken
    in the "ivle.op" query argument, not as the last segment of the path.
    Otherwise the interface is identical.
www/apps/console/console.js: Update to new consoleservice interface.

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,
4
 
                                   require_permission)
5
 
from ivle.webapp.errors import BadRequest, MethodNotAllowed, Unauthorized
 
3
from ivle.webapp.base.views import RESTView, JSONRESTView, named_operation
 
4
from ivle.webapp.errors import BadRequest, MethodNotAllowed
6
5
from ivle.webapp.testing import FakeUser, FakeRequest
7
6
 
8
7
class JSONRESTViewTestWithoutPUT(JSONRESTView):
9
8
    '''A small JSON REST view for testing purposes, without a PUT method.'''
10
 
    def get_permissions(self, user, config):
11
 
        if user.login == u'fakeuser':
12
 
            return set(['view', 'edit'])
13
 
        if user.login == u'otheruser':
14
 
            return set(['view'])
15
 
        return set()
16
 
 
17
 
    @require_permission('view')
18
9
    def GET(self, req):
19
10
        return {'method': 'get'}
20
11
 
21
 
    @require_permission('edit')
22
12
    def PATCH(self, req, data):
23
13
        return {'method': 'patch',
24
14
                'result': data['result'], 'test': data['test']}
25
15
 
26
 
    @named_operation('view')
 
16
    @named_operation
27
17
    def do_stuff(self, req, what):
28
18
        return {'result': 'Did %s!' % what}
29
19
 
30
 
    @named_operation('edit')
 
20
    @named_operation
31
21
    def say_something(self, req, thing='nothing'):
32
22
        return {'result': 'Said %s!' % thing}
33
23
 
34
 
    @named_operation('edit')
35
 
    def do_say_something(self, req, what, thing='nothing'):
36
 
        return {'result': 'Said %s and %s!' % (what, thing)}
37
 
 
38
 
    @named_operation('view')
 
24
    @named_operation
39
25
    def get_req_method(self, req):
40
26
        return {'method': req.method}
41
27
 
42
28
class JSONRESTViewTest(JSONRESTViewTestWithoutPUT):
43
29
    '''A small JSON REST view for testing purposes.'''
44
 
    @require_permission('edit')
45
30
    def PUT(self, req, data):
46
31
        return {'method': 'put',
47
32
                'result': data['result'], 'test': data['test']}
49
34
class TestJSONRESTView:
50
35
    def testGET(self):
51
36
        req = FakeRequest()
52
 
        view = JSONRESTViewTest(req, None)
 
37
        view = JSONRESTViewTest(req)
53
38
        view.render(req)
54
39
        assert req.content_type == 'application/json'
55
40
        assert req.response_body == '{"method": "get"}\n'
58
43
        req = FakeRequest()
59
44
        req.method = 'PUT'
60
45
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
61
 
        view = JSONRESTViewTest(req, None)
 
46
        view = JSONRESTViewTest(req)
62
47
        view.render(req)
63
48
        assert req.content_type == 'application/json'
64
49
        assert req.response_body == \
68
53
        req = FakeRequest()
69
54
        req.method = 'PATCH'
70
55
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
71
 
        view = JSONRESTViewTest(req, None)
 
56
        view = JSONRESTViewTest(req)
72
57
        view.render(req)
73
58
        assert req.content_type == 'application/json'
74
59
        assert req.response_body == \
79
64
        req.method = 'PUT'
80
65
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
81
66
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
82
 
        view = JSONRESTViewTest(req, None)
 
67
        view = JSONRESTViewTest(req)
83
68
        view.render(req)
84
69
        assert req.content_type == 'application/json'
85
70
        assert req.response_body == \
88
73
    def testInvalidMethod(self):
89
74
        req = FakeRequest()
90
75
        req.method = 'FAKEANDBOGUS'
91
 
        view = JSONRESTViewTest(req, None)
 
76
        view = JSONRESTViewTest(req)
92
77
        try:
93
78
            view.render(req)
94
79
        except MethodNotAllowed, e:
99
84
    def testNoPUTMethod(self):
100
85
        req = FakeRequest()
101
86
        req.method = 'PUT'
102
 
        view = JSONRESTViewTestWithoutPUT(req, None)
 
87
        view = JSONRESTViewTestWithoutPUT(req)
103
88
        try:
104
89
            view.render(req)
105
90
        except MethodNotAllowed, e:
111
96
        req = FakeRequest()
112
97
        req.method = 'FAKEANDBOGUS'
113
98
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
114
 
        view = JSONRESTViewTest(req, None)
 
99
        view = JSONRESTViewTest(req)
115
100
        try:
116
101
            view.render(req)
117
102
        except MethodNotAllowed:
124
109
        req.method = 'POST'
125
110
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
126
111
                                             'what': 'blah'})
127
 
        view = JSONRESTViewTest(req, None)
 
112
        view = JSONRESTViewTest(req)
128
113
        view.render(req)
129
114
        assert req.content_type == 'application/json'
130
115
        assert req.response_body == '{"result": "Did blah!"}\n'
133
118
        req = FakeRequest()
134
119
        req.method = 'POST'
135
120
        req.request_body = urllib.urlencode({'what': 'blah'})
136
 
        view = JSONRESTViewTest(req, None)
 
121
        view = JSONRESTViewTest(req)
137
122
        try:
138
123
            view.render(req)
139
124
        except BadRequest, e:
145
130
        req = FakeRequest()
146
131
        req.method = 'POST'
147
132
        req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
148
 
        view = JSONRESTViewTest(req, None)
 
133
        view = JSONRESTViewTest(req)
149
134
        try:
150
135
            view.render(req)
151
136
        except BadRequest, e:
157
142
        req = FakeRequest()
158
143
        req.method = 'POST'
159
144
        req.request_body = urllib.urlencode({'ivle.op': 'GET'})
160
 
        view = JSONRESTViewTest(req, None)
 
145
        view = JSONRESTViewTest(req)
161
146
        try:
162
147
            view.render(req)
163
148
        except BadRequest, e:
170
155
        req.method = 'POST'
171
156
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
172
157
                                             'nothing': 'wrong'})
173
 
        view = JSONRESTViewTest(req, None)
 
158
        view = JSONRESTViewTest(req)
174
159
        try:
175
160
            view.render(req)
176
161
        except BadRequest, e:
184
169
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
185
170
                                             'what': 'blah',
186
171
                                             'toomany': 'args'})
187
 
        view = JSONRESTViewTest(req, None)
 
172
        view = JSONRESTViewTest(req)
188
173
        try:
189
174
            view.render(req)
190
175
        except BadRequest, e:
196
181
        req = FakeRequest()
197
182
        req.method = 'POST'
198
183
        req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
199
 
        view = JSONRESTViewTest(req, None)
 
184
        view = JSONRESTViewTest(req)
200
185
        view.render(req)
201
186
        assert req.content_type == 'application/json'
202
187
        assert req.response_body == '{"result": "Said nothing!"}\n'
206
191
        req.method = 'POST'
207
192
        req.request_body = urllib.urlencode({'ivle.op': 'say_something',
208
193
                                             'thing': 'something'})
209
 
        view = JSONRESTViewTest(req, None)
 
194
        view = JSONRESTViewTest(req)
210
195
        view.render(req)
211
196
        assert req.content_type == 'application/json'
212
197
        assert req.response_body == '{"result": "Said something!"}\n'
213
198
 
214
 
    def testNamedOperationWithDefaultAndMissingArgs(self):
215
 
        req = FakeRequest()
216
 
        req.method = 'POST'
217
 
        req.request_body = urllib.urlencode({'ivle.op': 'do_say_something',
218
 
                                             'thing': 'something'})
219
 
        view = JSONRESTViewTest(req, None)
220
 
        try:
221
 
            view.render(req)
222
 
        except BadRequest, e:
223
 
            assert e.message == 'Missing arguments: what'
224
 
        else:
225
 
            raise AssertionError("did not raise BadRequest")
226
 
 
227
199
    def testNamedOperationUsingRequest(self):
228
200
        req = FakeRequest()
229
201
        req.method = 'POST'
230
202
        req.request_body = urllib.urlencode({'ivle.op': 'get_req_method'})
231
 
        view = JSONRESTViewTest(req, None)
 
203
        view = JSONRESTViewTest(req)
232
204
        view.render(req)
233
205
        assert req.content_type == 'application/json'
234
206
        assert req.response_body == '{"method": "POST"}\n'
235
 
 
236
 
    def testInvalidPOSTData(self):
237
 
        req = FakeRequest()
238
 
        req.method = 'POST'
239
 
        req.request_body = 'I am invalid&&&&'
240
 
        view = JSONRESTViewTest(req, None)
241
 
        try:
242
 
            view.render(req)
243
 
        except BadRequest, e:
244
 
            print e.message
245
 
            assert e.message == 'No named operation specified.'
246
 
        else:
247
 
            raise AssertionError("did not raise BadRequest")
248
 
 
249
 
    def testInvalidPATCHData(self):
250
 
        req = FakeRequest()
251
 
        req.method = 'PATCH'
252
 
        req.request_body = 'I am invalid'
253
 
        view = JSONRESTViewTest(req, None)
254
 
        try:
255
 
            view.render(req)
256
 
        except BadRequest, e:
257
 
            assert e.message == 'Invalid JSON data'
258
 
        else:
259
 
            raise AssertionError("did not raise BadRequest")
260
 
 
261
 
    def testInvalidPUTData(self):
262
 
        req = FakeRequest()
263
 
        req.method = 'PUT'
264
 
        req.request_body = 'I am invalid'
265
 
        view = JSONRESTViewTest(req, None)
266
 
        try:
267
 
            view.render(req)
268
 
        except BadRequest, e:
269
 
            assert e.message == 'Invalid JSON data'
270
 
        else:
271
 
            raise AssertionError("did not raise BadRequest")
272
 
 
273
 
class TestJSONRESTSecurity:
274
 
    def testGoodMethod(self):
275
 
        req = FakeRequest()
276
 
        req.user.login = u'otheruser'
277
 
        req.method = 'GET'
278
 
        view = JSONRESTViewTest(req, None)
279
 
        view.render(req)
280
 
        assert req.content_type == 'application/json'
281
 
        assert req.response_body == '{"method": "get"}\n'
282
 
 
283
 
    def testBadMethod(self):
284
 
        req = FakeRequest()
285
 
        req.user.login = u'otheruser'
286
 
        req.method = 'PUT'
287
 
        view = JSONRESTViewTest(req, None)
288
 
        try:
289
 
            view.render(req)
290
 
        except Unauthorized, e:
291
 
            pass
292
 
        else:
293
 
            raise AssertionError("did not raise Unauthorized")
294
 
 
295
 
    def testGoodNamedOperation(self):
296
 
        req = FakeRequest()
297
 
        req.user.login = u'otheruser'
298
 
        req.method = 'POST'
299
 
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
300
 
                                             'what': 'blah'})
301
 
        view = JSONRESTViewTest(req, None)
302
 
        view.render(req)
303
 
        assert req.content_type == 'application/json'
304
 
        assert req.response_body == '{"result": "Did blah!"}\n'
305
 
 
306
 
    def testBadNamedOperation(self):
307
 
        req = FakeRequest()
308
 
        req.user.login = u'otheruser'
309
 
        req.method = 'POST'
310
 
        req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
311
 
        view = JSONRESTViewTest(req, None)
312
 
        try:
313
 
            view.render(req)
314
 
        except Unauthorized, e:
315
 
            pass
316
 
        else:
317
 
            raise AssertionError("did not raise Unauthorized")
318