web/test/unittest_views_staticcontrollers.py
author Rémi Cardona <remi.cardona@logilab.fr>
Tue, 22 Sep 2015 14:18:14 +0200
changeset 10694 7ece2df9cc5c
parent 10398 bd00b58b00d9
child 10851 70b21cf8b45c
permissions -rw-r--r--
[py3k] unicode vs str vs bytes vs the world
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)
9233
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    73
        next_headers = {
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    74
            '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
    75
        }
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
    76
        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
    77
            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
    78
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    79
    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
    80
        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
    81
            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
    82
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    83
    def test_static_data_mode(self):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    84
        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
    85
        self.assertEqual(32, len(hash))
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    86
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    87
        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
    88
            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
    89
            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
    90
            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
    91
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    92
        with tempattr(self.vreg.config, 'mode', 'notest'):
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    93
            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
    94
            self._check_datafile_ok('data/%s/cubicweb.css' % hash)
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    95
            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
    96
            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
    97
7d3316bfa4ff [web] allow /data/ url again (closes #2464798)
David Douard <david.douard@logilab.fr>
parents: 8694
diff changeset
    98
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
class ConcatFilesTC(CubicWebTC):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
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
   101
    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
   102
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    def tearDown(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        super(ConcatFilesTC, self).tearDown()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        self._cleanup_concat_cache()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    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
   108
        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
   109
        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
   110
            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
   111
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   112
    @contextmanager
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    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
   114
        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
   115
            head = HTMLHead(req)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   116
            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
   117
                                    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
   118
            req._url = url
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   119
            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
   120
            yield res, req
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
    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
   123
        content = b''
8298
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        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
   125
            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
   126
            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
   127
                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
   128
                    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
   129
        return content
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
    def test_cache(self):
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        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
   133
        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
   134
            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
   135
            # check result content
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   136
            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
   137
            # 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
   138
            concat_hander = ConcatFilesHandler(self.config)
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   139
            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
   140
            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
   141
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
    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
   144
        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
   145
        # 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
   146
        self.config.debugmode = True
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        try:
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   148
            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
   149
                #print result
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   150
                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
   151
        finally:
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
            self.config.debugmode = False
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_production_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')
9812
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   156
        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
   157
            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
   158
            # check result content
122c0b6a82a3 [webtests/views_staticcontrollers] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9233
diff changeset
   159
            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
   160
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
if __name__ == '__main__':
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
    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
   164
    unittest_main()
2a4bc6f75e9c [twisted] move out url logic from twisted (closes #2200593)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165