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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2010-07-03 10:50:28 UTC
  • mto: This revision was merged to the branch mainline in revision 1797.
  • Revision ID: grantw@unimelb.edu.au-20100703105028-aohivx8czzf66qqe
Replace @named_operation with @read_operation and @write_operation. Allow execution of read operations with a GET rather than a POST.

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)
 
3
from nose.tools import assert_equal
 
4
 
 
5
from ivle.webapp.base.rest import (JSONRESTView, read_operation,
 
6
                                   require_permission, write_operation)
5
7
from ivle.webapp.errors import BadRequest, MethodNotAllowed, Unauthorized
6
8
from ivle.webapp.testing import FakeUser, FakeRequest
7
9
 
23
25
        return {'method': 'patch',
24
26
                'result': data['result'], 'test': data['test']}
25
27
 
26
 
    @named_operation('view')
 
28
    @write_operation('view')
27
29
    def do_stuff(self, req, what):
28
30
        return {'result': 'Did %s!' % what}
29
31
 
30
 
    @named_operation('edit')
 
32
    @read_operation('edit')
31
33
    def say_something(self, req, thing='nothing'):
32
34
        return {'result': 'Said %s!' % thing}
33
35
 
34
 
    @named_operation('edit')
 
36
    @write_operation('edit')
35
37
    def do_say_something(self, req, what, thing='nothing'):
36
38
        return {'result': 'Said %s and %s!' % (what, thing)}
37
39
 
38
 
    @named_operation('view')
 
40
    @read_operation('view')
39
41
    def get_req_method(self, req):
40
42
        return {'method': req.method}
41
43
 
233
235
        assert req.content_type == 'application/json'
234
236
        assert req.response_body == '{"method": "POST"}\n'
235
237
 
 
238
    def testGETNamedOperation(self):
 
239
        req = FakeRequest()
 
240
        req.method = 'GET'
 
241
        req.uri = '/foo?' + urllib.urlencode({'ivle.op': 'say_something'})
 
242
        view = JSONRESTViewTest(req, None)
 
243
        view.render(req)
 
244
        assert req.content_type == 'application/json'
 
245
        assert req.response_body == '{"result": "Said nothing!"}\n'
 
246
 
 
247
    def testGETNamedOperationDoesNotFindWriteOperation(self):
 
248
        req = FakeRequest()
 
249
        req.method = 'GET'
 
250
        req.uri = '/foo?' + urllib.urlencode(
 
251
            {'ivle.op': 'do_stuff', 'what': 'something'})
 
252
        view = JSONRESTViewTest(req, None)
 
253
        try:
 
254
            view.render(req)
 
255
        except BadRequest, e:
 
256
            assert e.message == 'POST required for write operation.'
 
257
        else:
 
258
            raise AssertionError("did not raise BadRequest")
 
259
 
236
260
    def testInvalidPOSTData(self):
237
261
        req = FakeRequest()
238
262
        req.method = 'POST'