~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#restart_console: Don't double-JSON.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import urllib
 
2
 
 
3
from ivle.webapp.base.views import RESTView, JSONRESTView, named_operation
 
4
from ivle.webapp.errors import BadRequest, MethodNotAllowed
 
5
from ivle.webapp.testing import FakeUser, FakeRequest
 
6
 
 
7
class JSONRESTViewTestWithoutPUT(JSONRESTView):
 
8
    '''A small JSON REST view for testing purposes, without a PUT method.'''
 
9
    def GET(self, req):
 
10
        return {'method': 'get'}
 
11
 
 
12
    def PATCH(self, req, data):
 
13
        return {'method': 'patch',
 
14
                'result': data['result'], 'test': data['test']}
 
15
 
 
16
    @named_operation
 
17
    def do_stuff(self, req, what):
 
18
        return {'result': 'Did %s!' % what}
 
19
 
 
20
    @named_operation
 
21
    def say_something(self, req, thing='nothing'):
 
22
        return {'result': 'Said %s!' % thing}
 
23
 
 
24
    @named_operation
 
25
    def get_req_method(self, req):
 
26
        return {'method': req.method}
 
27
 
 
28
class JSONRESTViewTest(JSONRESTViewTestWithoutPUT):
 
29
    '''A small JSON REST view for testing purposes.'''
 
30
    def PUT(self, req, data):
 
31
        return {'method': 'put',
 
32
                'result': data['result'], 'test': data['test']}
 
33
 
 
34
class TestJSONRESTView:
 
35
    def testGET(self):
 
36
        req = FakeRequest()
 
37
        view = JSONRESTViewTest(req)
 
38
        view.render(req)
 
39
        assert req.content_type == 'application/json'
 
40
        assert req.response_body == '{"method": "get"}\n'
 
41
 
 
42
    def testPUT(self):
 
43
        req = FakeRequest()
 
44
        req.method = 'PUT'
 
45
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
 
46
        view = JSONRESTViewTest(req)
 
47
        view.render(req)
 
48
        assert req.content_type == 'application/json'
 
49
        assert req.response_body == \
 
50
                '{"test": "FAI\\ua746ED", "method": "put", "result": 1}\n'
 
51
 
 
52
    def testPATCH(self):
 
53
        req = FakeRequest()
 
54
        req.method = 'PATCH'
 
55
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
 
56
        view = JSONRESTViewTest(req)
 
57
        view.render(req)
 
58
        assert req.content_type == 'application/json'
 
59
        assert req.response_body == \
 
60
                '{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
 
61
 
 
62
    def testEmulatedPATCH(self):
 
63
        req = FakeRequest()
 
64
        req.method = 'PUT'
 
65
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
 
66
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
 
67
        view = JSONRESTViewTest(req)
 
68
        view.render(req)
 
69
        assert req.content_type == 'application/json'
 
70
        assert req.response_body == \
 
71
                '{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
 
72
 
 
73
    def testInvalidMethod(self):
 
74
        req = FakeRequest()
 
75
        req.method = 'FAKEANDBOGUS'
 
76
        view = JSONRESTViewTest(req)
 
77
        try:
 
78
            view.render(req)
 
79
        except MethodNotAllowed, e:
 
80
            assert e.allowed == ['GET', 'PUT', 'PATCH', 'POST']
 
81
        else:
 
82
            raise AssertionError("did not raise MethodNotAllowed")
 
83
 
 
84
    def testNoPUTMethod(self):
 
85
        req = FakeRequest()
 
86
        req.method = 'PUT'
 
87
        view = JSONRESTViewTestWithoutPUT(req)
 
88
        try:
 
89
            view.render(req)
 
90
        except MethodNotAllowed, e:
 
91
            assert e.allowed == ['GET', 'PATCH', 'POST']
 
92
        else:
 
93
            raise AssertionError("did not raise MethodNotAllowed")
 
94
 
 
95
    def testInvalidMethodWithPATCHEmulation(self):
 
96
        req = FakeRequest()
 
97
        req.method = 'FAKEANDBOGUS'
 
98
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
 
99
        view = JSONRESTViewTest(req)
 
100
        try:
 
101
            view.render(req)
 
102
        except MethodNotAllowed:
 
103
            pass
 
104
        else:
 
105
            raise AssertionError("did not raise MethodNotAllowed")
 
106
 
 
107
    def testNamedOperation(self):
 
108
        req = FakeRequest()
 
109
        req.method = 'POST'
 
110
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
111
                                             'what': 'blah'})
 
112
        view = JSONRESTViewTest(req)
 
113
        view.render(req)
 
114
        assert req.content_type == 'application/json'
 
115
        assert req.response_body == '{"result": "Did blah!"}\n'
 
116
 
 
117
    def testPOSTWithoutName(self):
 
118
        req = FakeRequest()
 
119
        req.method = 'POST'
 
120
        req.request_body = urllib.urlencode({'what': 'blah'})
 
121
        view = JSONRESTViewTest(req)
 
122
        try:
 
123
            view.render(req)
 
124
        except BadRequest, e:
 
125
            assert e.message == 'No named operation specified.'
 
126
        else:
 
127
            raise AssertionError("did not raise BadRequest")
 
128
 
 
129
    def testNonexistentNamedOperation(self):
 
130
        req = FakeRequest()
 
131
        req.method = 'POST'
 
132
        req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
 
133
        view = JSONRESTViewTest(req)
 
134
        try:
 
135
            view.render(req)
 
136
        except BadRequest, e:
 
137
            assert e.message == 'Invalid named operation.'
 
138
        else:
 
139
            raise AssertionError("did not raise BadRequest")
 
140
 
 
141
    def testDisallowedNamedOperation(self):
 
142
        req = FakeRequest()
 
143
        req.method = 'POST'
 
144
        req.request_body = urllib.urlencode({'ivle.op': 'GET'})
 
145
        view = JSONRESTViewTest(req)
 
146
        try:
 
147
            view.render(req)
 
148
        except BadRequest, e:
 
149
            assert e.message == 'Invalid named operation.'
 
150
        else:
 
151
            raise AssertionError("did not raise BadRequest")
 
152
 
 
153
    def testNamedOperationWithMissingArgs(self):
 
154
        req = FakeRequest()
 
155
        req.method = 'POST'
 
156
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
157
                                             'nothing': 'wrong'})
 
158
        view = JSONRESTViewTest(req)
 
159
        try:
 
160
            view.render(req)
 
161
        except BadRequest, e:
 
162
            assert e.message == 'Missing arguments: what'
 
163
        else:
 
164
            raise AssertionError("did not raise BadRequest")
 
165
 
 
166
    def testNamedOperationWithExtraArgs(self):
 
167
        req = FakeRequest()
 
168
        req.method = 'POST'
 
169
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
 
170
                                             'what': 'blah',
 
171
                                             'toomany': 'args'})
 
172
        view = JSONRESTViewTest(req)
 
173
        try:
 
174
            view.render(req)
 
175
        except BadRequest, e:
 
176
            assert e.message == 'Extra arguments: toomany'
 
177
        else:
 
178
            raise AssertionError("did not raise BadRequest")
 
179
 
 
180
    def testNamedOperationWithDefaultArgs(self):
 
181
        req = FakeRequest()
 
182
        req.method = 'POST'
 
183
        req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
 
184
        view = JSONRESTViewTest(req)
 
185
        view.render(req)
 
186
        assert req.content_type == 'application/json'
 
187
        assert req.response_body == '{"result": "Said nothing!"}\n'
 
188
 
 
189
    def testNamedOperationWithOverriddenDefaultArgs(self):
 
190
        req = FakeRequest()
 
191
        req.method = 'POST'
 
192
        req.request_body = urllib.urlencode({'ivle.op': 'say_something',
 
193
                                             'thing': 'something'})
 
194
        view = JSONRESTViewTest(req)
 
195
        view.render(req)
 
196
        assert req.content_type == 'application/json'
 
197
        assert req.response_body == '{"result": "Said something!"}\n'
 
198
 
 
199
    def testNamedOperationUsingRequest(self):
 
200
        req = FakeRequest()
 
201
        req.method = 'POST'
 
202
        req.request_body = urllib.urlencode({'ivle.op': 'get_req_method'})
 
203
        view = JSONRESTViewTest(req)
 
204
        view.render(req)
 
205
        assert req.content_type == 'application/json'
 
206
        assert req.response_body == '{"method": "POST"}\n'