pyramid_cubicweb/tests/test_core.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 07 Jul 2016 14:30:32 +0200
changeset 11630 1400aee10df4
parent 11611 9d2bb6bdb5c8
permissions -rw-r--r--
Port to Python3 (closes #14159555) Add py34 environments to tox configuration (only for CubicWeb >= 3.23). And depend on hg version of cubicweb-pyramid since it is not currently Python3-compatible.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
from pyramid_cubicweb.tests import PyramidCWTest
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     2
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
from cubicweb.view import View
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
from cubicweb.web import Redirect
11568
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
     5
from cubicweb import ValidationError
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
class Redirector(View):
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
    __regid__ = 'redirector'
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
    def call(self, rset=None):
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
        self._cw.set_header('Cache-Control', 'no-cache')
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
        raise Redirect('http://example.org')
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
11568
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    16
def put_in_uncommitable_state(request):
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    17
    try:
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    18
        request.cw_cnx.execute('SET U login NULL WHERE U login "anon"')
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    19
    except ValidationError:
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    20
        pass
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    21
    request.response.body = b'OK'
11568
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    22
    return request.response
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    23
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    24
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
class CoreTest(PyramidCWTest):
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
    anonymous_allowed = True
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
11568
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    28
    def includeme(self, config):
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    29
        config.add_route('uncommitable', '/uncommitable')
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    30
        config.add_view(put_in_uncommitable_state, route_name='uncommitable')
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    31
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
    def test_cw_to_pyramid_copy_headers_on_redirect(self):
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
        self.vreg.register(Redirector)
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
        try:
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
            res = self.webapp.get('/?vid=redirector', expect_errors=True)
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
            self.assertEqual(res.status_int, 303)
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
            self.assertEqual(res.headers['Cache-Control'], 'no-cache')
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
        finally:
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
            self.vreg.unregister(Redirector)
11568
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    40
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    41
    def test_uncommitable_cnx(self):
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    42
        res = self.webapp.get('/uncommitable')
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    43
        self.assertEqual(res.text, 'OK')
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11556
diff changeset
    44
        self.assertEqual(res.status_int, 200)
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11568
diff changeset
    45
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11568
diff changeset
    46
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11568
diff changeset
    47
if __name__ == '__main__':
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11568
diff changeset
    48
    from unittest import main
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11568
diff changeset
    49
    main()