pyramid_cubicweb/tests/test_core.py
author Christophe de Vienne <christophe@unlish.com>
Fri, 08 May 2015 11:38:07 +0200
changeset 11568 a9940c6cd693
parent 11556 1eeba41a2e95
child 11611 9d2bb6bdb5c8
permissions -rw-r--r--
Rollback 'uncommitable' cnx Closes #5343870

from pyramid_cubicweb.tests import PyramidCWTest

from cubicweb.view import View
from cubicweb.web import Redirect
from cubicweb import ValidationError


class Redirector(View):
    __regid__ = 'redirector'

    def call(self, rset=None):
        self._cw.set_header('Cache-Control', 'no-cache')
        raise Redirect('http://example.org')


def put_in_uncommitable_state(request):
    try:
        request.cw_cnx.execute('SET U login NULL WHERE U login "anon"')
    except ValidationError:
        pass
    request.response.body = 'OK'
    return request.response


class CoreTest(PyramidCWTest):
    anonymous_allowed = True

    def includeme(self, config):
        config.add_route('uncommitable', '/uncommitable')
        config.add_view(put_in_uncommitable_state, route_name='uncommitable')

    def test_cw_to_pyramid_copy_headers_on_redirect(self):
        self.vreg.register(Redirector)
        try:
            res = self.webapp.get('/?vid=redirector', expect_errors=True)
            self.assertEqual(res.status_int, 303)
            self.assertEqual(res.headers['Cache-Control'], 'no-cache')
        finally:
            self.vreg.unregister(Redirector)

    def test_uncommitable_cnx(self):
        res = self.webapp.get('/uncommitable')
        self.assertEqual(res.text, 'OK')
        self.assertEqual(res.status_int, 200)