~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
import urllib

from ivle.webapp.base.views import RESTView, JSONRESTView, named_operation
from ivle.webapp.errors import BadRequest, MethodNotAllowed
from ivle.webapp.testing import FakeUser, FakeRequest

class JSONRESTViewTest(JSONRESTView):
    '''A small JSON REST view for testing purposes.'''
    def GET(self, req):
        return {'method': 'get'}

    def PUT(self, req, data):
        return {'method': 'put',
                'result': data['result'], 'test': data['test']}

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

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

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:
            pass
        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")