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

« back to all changes in this revision

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

ivle.webapp.testing: Add, with fake request and user.
ivle.webapp.base.test: Add! Test the JSONRESTView, using the new mocks.

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