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

« back to all changes in this revision

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

ivle.webapp.base.views#JSONRESTView: Check that methods are available before
    trying to execute them. Also raise a legal MethodNotAllowed with an
    'allowed' attribute indicating permitted methods
ivle.webapp.errors: Alter constructor to take mandatory 'allowed' argument.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from ivle.webapp.errors import BadRequest, MethodNotAllowed
5
5
from ivle.webapp.testing import FakeUser, FakeRequest
6
6
 
7
 
class JSONRESTViewTest(JSONRESTView):
8
 
    '''A small JSON REST view for testing purposes.'''
 
7
class JSONRESTViewTestWithoutPUT(JSONRESTView):
 
8
    '''A small JSON REST view for testing purposes, without a PUT method.'''
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
 
 
16
12
    def PATCH(self, req, data):
17
13
        return {'method': 'patch',
18
14
                'result': data['result'], 'test': data['test']}
21
17
    def do_stuff(self, what):
22
18
        return {'result': 'Did %s!' % what}
23
19
 
 
20
class JSONRESTViewTest(JSONRESTViewTestWithoutPUT):
 
21
    '''A small JSON REST view for testing purposes.'''
 
22
    def PUT(self, req, data):
 
23
        return {'method': 'put',
 
24
                'result': data['result'], 'test': data['test']}
 
25
 
24
26
class TestJSONRESTView:
25
27
    def testGET(self):
26
28
        req = FakeRequest()
66
68
        view = JSONRESTViewTest(req)
67
69
        try:
68
70
            view.render(req)
69
 
        except MethodNotAllowed:
70
 
            pass
 
71
        except MethodNotAllowed, e:
 
72
            assert e.allowed == ['GET', 'PUT', 'PATCH', 'POST']
 
73
        else:
 
74
            raise AssertionError("did not raise MethodNotAllowed")
 
75
 
 
76
    def testNoPUTMethod(self):
 
77
        req = FakeRequest()
 
78
        req.method = 'PUT'
 
79
        view = JSONRESTViewTestWithoutPUT(req)
 
80
        try:
 
81
            view.render(req)
 
82
        except MethodNotAllowed, e:
 
83
            assert e.allowed == ['GET', 'PATCH', 'POST']
71
84
        else:
72
85
            raise AssertionError("did not raise MethodNotAllowed")
73
86