web/test/unittest_views_staticcontrollers.py
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 06 Jan 2016 11:47:08 +0100
changeset 11080 245f440b814a
parent 10907 9ae707db5265
permissions -rw-r--r--
[devtools] fix get_bounds Say an attribute has two IntervalBoundConstraints, one with minvalue=0 and the other with maxvalue=10 (not literally, or that could be a single constraint, but e.g. with references to other attributes). We don't want to override the minvalue of the former with None just because the latter happens last in the constraints list. This doesn't fix the case where two constraints both have a min or a max value. Meh.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     1
# -*- coding: utf-8 -*-
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     2
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     4
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     5
# This file is part of CubicWeb.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     6
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    10
# any later version.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    11
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    15
# details.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    16
#
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    19
from contextlib import contextmanager
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    20
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    21
from logilab.common import tempattr
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    22
from logilab.common.testlib import Tags
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from cubicweb.devtools.testlib import CubicWebTC
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
import os
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
import os.path as osp
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
import glob
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
from cubicweb.utils import HTMLHead
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
from cubicweb.web.views.staticcontrollers import ConcatFilesHandler
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    32
class staticfilespublishermixin(object):
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    33
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    34
    @contextmanager
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    35
    def _publish_static_files(self, url, header={}):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    36
        with self.admin_access.web_request(headers=header) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    37
            req._url = url
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    38
            self.app_handle_request(req, url)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    39
            yield req
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    40
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    41
class StaticControllerCacheTC(staticfilespublishermixin, CubicWebTC):
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    42
    tags = CubicWebTC.tags | Tags('static_controller', 'cache', 'http')
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    43
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    44
    def test_static_file_are_cached(self):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    45
        with self._publish_static_files('data/cubicweb.css') as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    46
            self.assertEqual(200, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    47
            self.assertIn('last-modified', req.headers_out)
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    48
        next_headers = {
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    49
            'if-modified-since': req.get_response_header('last-modified', raw=True),
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    50
        }
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    51
        with self._publish_static_files('data/cubicweb.css', next_headers) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    52
            self.assertEqual(304, req.status_out)
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    53
10398
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    54
class StaticDirectoryControllerTC(staticfilespublishermixin, CubicWebTC):
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    55
10398
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    56
    def test_check_static_dir_access(self):
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    57
        """write a file in the static directory and test the access"""
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    58
        staticdir = osp.join(self.session.vreg.config.static_directory)
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    59
        if not os.path.exists(staticdir):
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    60
            os.makedirs(staticdir)
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    61
        filename = osp.join(staticdir, 'test')
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    62
        with open(filename, 'a') as f:
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    63
            with self._publish_static_files('static/test') as req:
bd00b58b00d9 [web] Fix the path to the `static directory` (closes #5355965)
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 9812
diff changeset
    64
                self.assertEqual(200, req.status_out)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    65
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    66
class DataControllerTC(staticfilespublishermixin, CubicWebTC):
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    67
    tags = CubicWebTC.tags | Tags('static_controller', 'data', 'http')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    68
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    69
    def _check_datafile_ok(self, fname):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    70
        with self._publish_static_files(fname) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    71
            self.assertEqual(200, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    72
            self.assertIn('last-modified', req.headers_out)
10852
e35d23686d1f [web] set proper Cache-Control header for static files
Julien Cristau <julien.cristau@logilab.fr>
parents: 10851
diff changeset
    73
            self.assertIn('expires', req.headers_out)
e35d23686d1f [web] set proper Cache-Control header for static files
Julien Cristau <julien.cristau@logilab.fr>
parents: 10851
diff changeset
    74
            self.assertEqual(req.get_response_header('cache-control'),
e35d23686d1f [web] set proper Cache-Control header for static files
Julien Cristau <julien.cristau@logilab.fr>
parents: 10851
diff changeset
    75
                             {'max-age': 604800})
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    76
        next_headers = {
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    77
            'if-modified-since': req.get_response_header('last-modified', raw=True),
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    78
        }
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    79
        with self._publish_static_files(fname, next_headers) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    80
            self.assertEqual(304, req.status_out)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    81
10851
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
    82
    def _check_datafile_redirect(self, fname, expected):
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
    83
        with self._publish_static_files(fname) as req:
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
    84
            self.assertEqual(302, req.status_out)
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
    85
            self.assertEqual(req.get_response_header('location'),
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
    86
                             req.base_url() + expected)
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
    87
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    88
    def _check_no_datafile(self, fname):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    89
        with self._publish_static_files(fname) as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    90
            self.assertEqual(404, req.status_out)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    91
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    92
    def test_static_data_mode(self):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    93
        hash = self.vreg.config.instance_md5_version()
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    94
        self.assertEqual(32, len(hash))
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    95
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    96
        with tempattr(self.vreg.config, 'mode', 'test'):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    97
            self._check_datafile_ok('data/cubicweb.css')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    98
            self._check_no_datafile('data/does/not/exist')
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    99
            self._check_no_datafile('data/%s/cubicweb.css' % ('0'*len(hash)))
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
   100
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
   101
        with tempattr(self.vreg.config, 'mode', 'notest'):
10851
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
   102
            self.config._init_base_url()  # reset config.datadir_url
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
   103
            self._check_datafile_redirect('data/cubicweb.css', 'data/%s/cubicweb.css' % hash)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
   104
            self._check_datafile_ok('data/%s/cubicweb.css' % hash)
10851
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
   105
            self._check_no_datafile('data/%s/does/not/exist' % hash)
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
   106
            self._check_datafile_redirect('data/%s/does/not/exist' % ('0'*len(hash)),
70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
Julien Cristau <julien.cristau@logilab.fr>
parents: 10694
diff changeset
   107
                                          'data/%s/%s/does/not/exist' % (hash, '0'*len(hash)))
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
   108
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
   109
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
class ConcatFilesTC(CubicWebTC):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
8323
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   112
    tags = CubicWebTC.tags | Tags('static_controller', 'concat')
fe60a77ae4a7 static-file: properly set/use cache header for static file (closes #2255013)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   113
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    def tearDown(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        super(ConcatFilesTC, self).tearDown()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
        self._cleanup_concat_cache()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
    def _cleanup_concat_cache(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        uicachedir = osp.join(self.config.apphome, 'uicache')
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        for fname in glob.glob(osp.join(uicachedir, 'cache_concat_*')):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
            os.unlink(osp.join(uicachedir, fname))
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   123
    @contextmanager
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    def _publish_js_files(self, js_files):
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   125
        with self.admin_access.web_request() as req:
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   126
            head = HTMLHead(req)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   127
            url = head.concat_urls([req.data_url(js_file)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   128
                                    for js_file in js_files])[len(req.base_url()):]
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   129
            req._url = url
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   130
            res = self.app_handle_request(req, url)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   131
            yield res, req
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
    def expected_content(self, js_files):
10694
7ece2df9cc5c [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10398
diff changeset
   134
        content = b''
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        for js_file in js_files:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
            dirpath, rid = self.config.locate_resource(js_file)
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
            if dirpath is not None: # ignore resources not found
10694
7ece2df9cc5c [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10398
diff changeset
   138
                with open(osp.join(dirpath, rid), 'rb') as f:
7ece2df9cc5c [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10398
diff changeset
   139
                    content += f.read() + b'\n'
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        return content
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
    def test_cache(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        js_files = ('cubicweb.ajax.js', 'jquery.js')
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   144
        with self._publish_js_files(js_files) as (result, req):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   145
            self.assertNotEqual(404, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   146
            # check result content
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   147
            self.assertEqual(result, self.expected_content(js_files))
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   148
            # make sure we kept a cached version on filesystem
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   149
            concat_hander = ConcatFilesHandler(self.config)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   150
            filepath = concat_hander.build_filepath(js_files)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   151
            self.assertTrue(osp.isfile(filepath))
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
    def test_invalid_file_in_debug_mode(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
        js_files = ('cubicweb.ajax.js', 'dummy.js')
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
        # in debug mode, an error is raised
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
        self.config.debugmode = True
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
        try:
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   159
            with self._publish_js_files(js_files) as (result, req):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   160
                #print result
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   161
                self.assertEqual(404, req.status_out)
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
        finally:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
            self.config.debugmode = False
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
    def test_invalid_file_in_production_mode(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        js_files = ('cubicweb.ajax.js', 'dummy.js')
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   167
        with self._publish_js_files(js_files) as (result, req):
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   168
            self.assertNotEqual(404, req.status_out)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   169
            # check result content
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   170
            self.assertEqual(result, self.expected_content(js_files))
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
if __name__ == '__main__':
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
    from logilab.common.testlib import unittest_main
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
    unittest_main()