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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import urllib

from ivle.webapp.base.rest import (JSONRESTView, named_operation,
                                   require_permission)
from ivle.webapp.errors import BadRequest, MethodNotAllowed, Unauthorized
from ivle.webapp.testing import FakeUser, FakeRequest

class JSONRESTViewTestWithoutPUT(JSONRESTView):
    '''A small JSON REST view for testing purposes, without a PUT method.'''
    def get_permissions(self, user):
        if user.login == u'fakeuser':
            return set(['view', 'edit'])
        if user.login == u'otheruser':
            return set(['view'])
        return set()

    @require_permission('view')
    def GET(self, req):
        return {'method': 'get'}

    @require_permission('edit')
    def PATCH(self, req, data):
        return {'method': 'patch',
                'result': data['result'], 'test': data['test']}

    @named_operation('view')
    def do_stuff(self, req, what):
        return {'result': 'Did %s!' % what}

    @named_operation('edit')
    def say_something(self, req, thing='nothing'):
        return {'result': 'Said %s!' % thing}

    @named_operation('edit')
    def do_say_something(self, req, what, thing='nothing'):
        return {'result': 'Said %s and %s!' % (what, thing)}

    @named_operation('view')
    def get_req_method(self, req):
        return {'method': req.method}

class JSONRESTViewTest(JSONRESTViewTestWithoutPUT):
    '''A small JSON REST view for testing purposes.'''
    @require_permission('edit')
    def PUT(self, req, data):
        return {'method': 'put',
                'result': data['result'], 'test': data['test']}

class TestJSONRESTView:
    def testGET(self):
        req = FakeRequest()
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == '{"method": "get"}\n'

    def testPUT(self):
        req = FakeRequest()
        req.method = 'PUT'
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == \
                '{"test": "FAI\\ua746ED", "method": "put", "result": 1}\n'

    def testPATCH(self):
        req = FakeRequest()
        req.method = 'PATCH'
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == \
                '{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'

    def testEmulatedPATCH(self):
        req = FakeRequest()
        req.method = 'PUT'
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == \
                '{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'

    def testInvalidMethod(self):
        req = FakeRequest()
        req.method = 'FAKEANDBOGUS'
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except MethodNotAllowed, e:
            assert e.allowed == ['GET', 'PUT', 'PATCH', 'POST']
        else:
            raise AssertionError("did not raise MethodNotAllowed")

    def testNoPUTMethod(self):
        req = FakeRequest()
        req.method = 'PUT'
        view = JSONRESTViewTestWithoutPUT(req)
        try:
            view.render(req)
        except MethodNotAllowed, e:
            assert e.allowed == ['GET', 'PATCH', 'POST']
        else:
            raise AssertionError("did not raise MethodNotAllowed")

    def testInvalidMethodWithPATCHEmulation(self):
        req = FakeRequest()
        req.method = 'FAKEANDBOGUS'
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except MethodNotAllowed:
            pass
        else:
            raise AssertionError("did not raise MethodNotAllowed")

    def testNamedOperation(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
                                             'what': 'blah'})
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == '{"result": "Did blah!"}\n'

    def testPOSTWithoutName(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'what': 'blah'})
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'No named operation specified.'
        else:
            raise AssertionError("did not raise BadRequest")

    def testNonexistentNamedOperation(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'enoent'})
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'Invalid named operation.'
        else:
            raise AssertionError("did not raise BadRequest")

    def testDisallowedNamedOperation(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'GET'})
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'Invalid named operation.'
        else:
            raise AssertionError("did not raise BadRequest")

    def testNamedOperationWithMissingArgs(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
                                             'nothing': 'wrong'})
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'Missing arguments: what'
        else:
            raise AssertionError("did not raise BadRequest")

    def testNamedOperationWithExtraArgs(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
                                             'what': 'blah',
                                             'toomany': 'args'})
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'Extra arguments: toomany'
        else:
            raise AssertionError("did not raise BadRequest")

    def testNamedOperationWithDefaultArgs(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == '{"result": "Said nothing!"}\n'

    def testNamedOperationWithOverriddenDefaultArgs(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'say_something',
                                             'thing': 'something'})
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == '{"result": "Said something!"}\n'

    def testNamedOperationWithDefaultAndMissingArgs(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'do_say_something',
                                             'thing': 'something'})
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'Missing arguments: what'
        else:
            raise AssertionError("did not raise BadRequest")

    def testNamedOperationUsingRequest(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'get_req_method'})
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == '{"method": "POST"}\n'

    def testInvalidPOSTData(self):
        req = FakeRequest()
        req.method = 'POST'
        req.request_body = 'I am invalid&&&&'
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            print e.message
            assert e.message == 'No named operation specified.'
        else:
            raise AssertionError("did not raise BadRequest")

    def testInvalidPATCHData(self):
        req = FakeRequest()
        req.method = 'PATCH'
        req.request_body = 'I am invalid'
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'Invalid JSON data'
        else:
            raise AssertionError("did not raise BadRequest")

    def testInvalidPUTData(self):
        req = FakeRequest()
        req.method = 'PUT'
        req.request_body = 'I am invalid'
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except BadRequest, e:
            assert e.message == 'Invalid JSON data'
        else:
            raise AssertionError("did not raise BadRequest")

class TestJSONRESTSecurity:
    def testGoodMethod(self):
        req = FakeRequest()
        req.user.login = u'otheruser'
        req.method = 'GET'
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == '{"method": "get"}\n'

    def testBadMethod(self):
        req = FakeRequest()
        req.user.login = u'otheruser'
        req.method = 'PUT'
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except Unauthorized, e:
            pass
        else:
            raise AssertionError("did not raise Unauthorized")

    def testGoodNamedOperation(self):
        req = FakeRequest()
        req.user.login = u'otheruser'
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'do_stuff',
                                             'what': 'blah'})
        view = JSONRESTViewTest(req)
        view.render(req)
        assert req.content_type == 'application/json'
        assert req.response_body == '{"result": "Did blah!"}\n'

    def testBadNamedOperation(self):
        req = FakeRequest()
        req.user.login = u'otheruser'
        req.method = 'POST'
        req.request_body = urllib.urlencode({'ivle.op': 'say_something'})
        view = JSONRESTViewTest(req)
        try:
            view.render(req)
        except Unauthorized, e:
            pass
        else:
            raise AssertionError("did not raise Unauthorized")