cubicweb/pyramid/test/test_bw_request.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Tue, 31 Mar 2020 19:15:03 +0200
changeset 12957 0c973204033a
parent 12088 477a59a45786
permissions -rw-r--r--
[server] prevent returning closed cursor to the database pool In since c8c6ad8 init_repository use repo.internal_cnx() instead of repo.system_source.get_connection() so it use the pool and we should not close cursors from the pool before returning it back. Otherwise we may have "connection already closed" error. This bug only trigger when connection-pool-size = 1. Since we are moving to use a dynamic pooler we need to get this fixed. This does not occur with sqlite since the connection wrapper instantiate new cursor everytime, but this occur with other databases.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
     1
# -*- coding: utf-8 -*-
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
     2
from io import BytesIO
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
import webtest
11514
82e86cd8e217 Move PyramidCWTest to pyramid_cubicweb.tests
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
     5
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
import pyramid.request
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11630
diff changeset
     8
from cubicweb.pyramid.core import CubicWebPyramidRequest
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11630
diff changeset
     9
from cubicweb.pyramid.test import PyramidCWTest
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
class WSGIAppTest(PyramidCWTest):
12088
477a59a45786 [pyramid] disable bwcompat by default in PyramidCWTest
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11913
diff changeset
    13
    settings = {'cubicweb.bwcompat': True}
477a59a45786 [pyramid] disable bwcompat by default in PyramidCWTest
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11913
diff changeset
    14
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
    def make_request(self, path, environ=None, **kw):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
        r = webtest.app.TestRequest.blank(path, environ, **kw)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
        request = pyramid.request.Request(r.environ)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
        request.registry = self.pyr_registry
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
        return request
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
    def test_content_type(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
            self.make_request('/', {'CONTENT_TYPE': 'text/plain'}))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
        self.assertEqual('text/plain', req.get_header('Content-Type'))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
    def test_content_body(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
            self.make_request('/', {
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
                'CONTENT_LENGTH': 12,
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
                'CONTENT_TYPE': 'text/plain',
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    34
                'wsgi.input': BytesIO(b'some content')}))
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    36
        self.assertEqual(b'some content', req.content.read())
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
    def test_big_content(self):
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11631
diff changeset
    39
        content = b'x' * 100001
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    42
            self.make_request('/', {
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    43
                'CONTENT_LENGTH': len(content),
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
                'CONTENT_TYPE': 'text/plain',
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    45
                'wsgi.input': BytesIO(content)}))
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
        self.assertEqual(content, req.content.read())
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
    def test_post(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
        self.webapp.post(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
            '/',
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
            params={'__login': self.admlogin, '__password': self.admpassword})
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
    def test_get_multiple_variables(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
            self.make_request('/?arg=1&arg=2'))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
        self.assertEqual([u'1', u'2'], req.form['arg'])
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    59
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
    def test_post_multiple_variables(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    61
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    62
            self.make_request('/', POST='arg=1&arg=2'))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    64
        self.assertEqual([u'1', u'2'], req.form['arg'])
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    65
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    66
    def test_post_files(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    67
        content_type, params = self.webapp.encode_multipart(
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    68
            (), (('filefield', 'aname', b'acontent'),))
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    69
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    70
            self.make_request('/', POST=params, content_type=content_type))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
        self.assertIn('filefield', req.form)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
        fieldvalue = req.form['filefield']
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    73
        self.assertEqual(u'aname', fieldvalue[0])
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    74
        self.assertEqual(b'acontent', fieldvalue[1].read())
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    75
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    76
    def test_post_unicode_urlencoded(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    77
        params = 'arg=%C3%A9'
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    78
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    79
            self.make_request(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    80
                '/', POST=params,
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    81
                content_type='application/x-www-form-urlencoded'))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    82
        self.assertEqual(u"é", req.form['arg'])
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
    83
11812
4e0829ade86f [pyramid] Fix 404 handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    84
    def test_404(self):
4e0829ade86f [pyramid] Fix 404 handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    85
        r = self.webapp.get('/unexisting/', status=404)
4e0829ade86f [pyramid] Fix 404 handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    86
        self.assertNotIn('error occurred', r.text)
4e0829ade86f [pyramid] Fix 404 handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    87
        self.assertIn('resource does not exist', r.text)
4e0829ade86f [pyramid] Fix 404 handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    88
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
    89
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
    90
if __name__ == '__main__':
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
    91
    from unittest import main
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
    92
    main()