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

« back to all changes in this revision

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

  • Committer: me at id
  • Date: 2009-01-14 23:23:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1138
ivle.auth.autherror: Remove, moving AuthError into ivle.auth itself.

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 JSONRESTViewTest(JSONRESTView):
8
 
    '''A small JSON REST view for testing purposes.'''
9
 
    def GET(self, req):
10
 
        return {'method': 'get'}
11
 
 
12
 
    def PUT(self, req, data):
13
 
        return {'method': 'put',
14
 
                'result': data['result'], 'test': data['test']}
15
 
 
16
 
    def PATCH(self, req, data):
17
 
        return {'method': 'patch',
18
 
                'result': data['result'], 'test': data['test']}
19
 
 
20
 
    @named_operation
21
 
    def do_stuff(self, what):
22
 
        return {'result': 'Did %s!' % what}
23
 
 
24
 
class TestJSONRESTView:
25
 
    def testGET(self):
26
 
        req = FakeRequest()
27
 
        view = JSONRESTViewTest(req)
28
 
        view.render(req)
29
 
        assert req.content_type == 'application/json'
30
 
        assert req.response_body == '{"method": "get"}\n'
31
 
 
32
 
    def testPUT(self):
33
 
        req = FakeRequest()
34
 
        req.method = 'PUT'
35
 
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
36
 
        view = JSONRESTViewTest(req)
37
 
        view.render(req)
38
 
        assert req.content_type == 'application/json'
39
 
        assert req.response_body == \
40
 
                '{"test": "FAI\\ua746ED", "method": "put", "result": 1}\n'
41
 
 
42
 
    def testPATCH(self):
43
 
        req = FakeRequest()
44
 
        req.method = 'PATCH'
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": "patch", "result": 1}\n'
51
 
 
52
 
    def testEmulatedPATCH(self):
53
 
        req = FakeRequest()
54
 
        req.method = 'PUT'
55
 
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
56
 
        req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
57
 
        view = JSONRESTViewTest(req)
58
 
        view.render(req)
59
 
        assert req.content_type == 'application/json'
60
 
        assert req.response_body == \
61
 
                '{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
62
 
 
63
 
    def testInvalidMethod(self):
64
 
        req = FakeRequest()
65
 
        req.method = 'FAKEANDBOGUS'
66
 
        view = JSONRESTViewTest(req)
67
 
        try:
68
 
            view.render(req)
69
 
        except MethodNotAllowed:
70
 
            pass
71
 
        else:
72
 
            raise AssertionError("did not raise MethodNotAllowed")
73
 
 
74
 
    def testInvalidMethodWithPATCHEmulation(self):
75
 
        req = FakeRequest()
76
 
        req.method = 'FAKEANDBOGUS'
77
 
        req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
78
 
        view = JSONRESTViewTest(req)
79
 
        try:
80
 
            view.render(req)
81
 
        except MethodNotAllowed:
82
 
            pass
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:
143
 
            raise AssertionError("did not raise BadRequest")