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

« back to all changes in this revision

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

ivle.webapp.base.test:
 - Test named operations on JSONRESTView.
 - Check for MethodNotAllowed where appropriate, not BadRequest.

ivle.webapp.base.views#JSONRESTView:
 - Add named operation support.
 - Raise MethodNotAllowed instead of BadRequest for bad methods.

ivle.webapp.base.views#named_operation: Add. Decorator to export named ops.
ivle.webapp.errors: Add MethodNotAllowed (405).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from ivle.webapp.base.views import RESTView, JSONRESTView
2
 
from ivle.webapp.errors import BadRequest
 
1
import urllib
 
2
 
 
3
from ivle.webapp.base.views import RESTView, JSONRESTView, named_operation
 
4
from ivle.webapp.errors import BadRequest, MethodNotAllowed
3
5
from ivle.webapp.testing import FakeUser, FakeRequest
4
6
 
5
7
class JSONRESTViewTest(JSONRESTView):
15
17
        return {'method': 'patch',
16
18
                'result': data['result'], 'test': data['test']}
17
19
 
 
20
    @named_operation
 
21
    def do_stuff(self, what):
 
22
        return {'result': 'Did %s!' % what}
 
23
 
18
24
class TestJSONRESTView:
19
25
    def testGET(self):
20
26
        req = FakeRequest()
60
66
        view = JSONRESTViewTest(req)
61
67
        try:
62
68
            view.render(req)
63
 
        except BadRequest:
 
69
        except MethodNotAllowed:
64
70
            pass
65
71
        else:
66
 
            raise AssertionError("did not raise BadRequest")
 
72
            raise AssertionError("did not raise MethodNotAllowed")
67
73
 
68
74
    def testInvalidMethodWithPATCHEmulation(self):
69
75
        req = FakeRequest()
72
78
        view = JSONRESTViewTest(req)
73
79
        try:
74
80
            view.render(req)
75
 
        except BadRequest:
 
81
        except MethodNotAllowed:
76
82
            pass
77
83
        else:
 
84
            raise AssertionError("did not raise MethodNotAllowed")
 
85
 
 
86
    def testNamedOperation(self):
 
87
        req = FakeRequest()
 
88
        req.method = 'POST'
 
89
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
90
                                             'what': 'blah'})
 
91
        view = JSONRESTViewTest(req)
 
92
        view.render(req)
 
93
        assert req.content_type == 'application/json'
 
94
        assert req.response_body == '{"result": "Did blah!"}\n'
 
95
 
 
96
    def testPOSTWithoutName(self):
 
97
        req = FakeRequest()
 
98
        req.method = 'POST'
 
99
        req.request_body = urllib.urlencode({'what': 'blah'})
 
100
        view = JSONRESTViewTest(req)
 
101
        try:
 
102
            view.render(req)
 
103
        except BadRequest, e:
 
104
            assert e.message == 'No named operation specified.'
 
105
        else:
 
106
            raise AssertionError("did not raise BadRequest")
 
107
 
 
108
    def testNonexistentNamedOperation(self):
 
109
        req = FakeRequest()
 
110
        req.method = 'POST'
 
111
        req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
 
112
        view = JSONRESTViewTest(req)
 
113
        try:
 
114
            view.render(req)
 
115
        except BadRequest, e:
 
116
            assert e.message == 'Invalid named operation.'
 
117
        else:
 
118
            raise AssertionError("did not raise BadRequest")
 
119
 
 
120
    def testDisallowedNamedOperation(self):
 
121
        req = FakeRequest()
 
122
        req.method = 'POST'
 
123
        req.request_body = urllib.urlencode({'ivle.op': 'GET'})
 
124
        view = JSONRESTViewTest(req)
 
125
        try:
 
126
            view.render(req)
 
127
        except BadRequest, e:
 
128
            assert e.message == 'Invalid named operation.'
 
129
        else:
 
130
            raise AssertionError("did not raise BadRequest")
 
131
 
 
132
    def testNamedOperationWithMissingArgs(self):
 
133
        req = FakeRequest()
 
134
        req.method = 'POST'
 
135
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
136
                                             'nothing': 'wrong'})
 
137
        view = JSONRESTViewTest(req)
 
138
        try:
 
139
            view.render(req)
 
140
        except BadRequest, e:
 
141
            assert e.message == 'Missing arguments: what'
 
142
        else:
78
143
            raise AssertionError("did not raise BadRequest")