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

« back to all changes in this revision

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

  • Committer: David Coles
  • Date: 2010-07-28 10:45:53 UTC
  • mfrom: (1829 trunk)
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: coles.david@gmail.com-20100728104553-5z3nxt0l6kyfqfh5
MergeĀ fromĀ trunk

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.unparsed_uri = '/?' + urllib.urlencode(
 
242
            {'ivle.op': 'say_something'})
 
243
        view = JSONRESTViewTest(req, None)
 
244
        view.render(req)
 
245
        assert req.content_type == 'application/json'
 
246
        assert req.response_body == '{"result": "Said nothing!"}\n'
 
247
 
 
248
    def testGETNamedOperationDoesNotFindWriteOperation(self):
 
249
        req = FakeRequest()
 
250
        req.method = 'GET'
 
251
        req.unparsed_uri = '/?' + urllib.urlencode(
 
252
            {'ivle.op': 'do_stuff', 'what': 'something'})
 
253
        view = JSONRESTViewTest(req, None)
 
254
        try:
 
255
            view.render(req)
 
256
        except BadRequest, e:
 
257
            assert e.message == 'POST required for write operation.'
 
258
        else:
 
259
            raise AssertionError("did not raise BadRequest")
 
260
 
236
261
    def testInvalidPOSTData(self):
237
262
        req = FakeRequest()
238
263
        req.method = 'POST'