web/test/unittest_views_staticcontrollers.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 25 Mar 2013 17:50:11 +0100 (2013-03-25)
changeset 8807 d9aaad2c52e9
parent 8694 d901c36bcfce
child 9233 7d3316bfa4ff
permissions -rw-r--r--
[session] drop useless getter and setter for security Those function have very few user and does not anything fancy. Internal user now access tx.read/write_security directly. External user have been blessed with a writable property. They are only interested in the write part of they did not used the return of the removed function. They access it through the session since I prefer the external world to not be aware of Transaction yet.
from logilab.common.testlib import tag, Tags
from cubicweb.devtools.testlib import CubicWebTC

import os
import os.path as osp
import glob

from cubicweb.utils import HTMLHead
from cubicweb.web.views.staticcontrollers import ConcatFilesHandler

class StaticControllerCacheTC(CubicWebTC):

    tags = CubicWebTC.tags | Tags('static_controller', 'cache', 'http')


    def _publish_static_files(self, url, header={}):
        req = self.request(headers=header)
        req._url = url
        return self.app_handle_request(req, url), req

    def test_static_file_are_cached(self):
        _, req = self._publish_static_files('data/cubicweb.css')
        self.assertEqual(200, req.status_out)
        self.assertIn('last-modified', req.headers_out)
        next_headers = {
            'if-modified-since': req.get_response_header('last-modified', raw=True),
        }
        _, req = self._publish_static_files('data/cubicweb.css', next_headers)
        self.assertEqual(304, req.status_out)


class ConcatFilesTC(CubicWebTC):

    tags = CubicWebTC.tags | Tags('static_controller', 'concat')

    def tearDown(self):
        super(ConcatFilesTC, self).tearDown()
        self._cleanup_concat_cache()

    def _cleanup_concat_cache(self):
        uicachedir = osp.join(self.config.apphome, 'uicache')
        for fname in glob.glob(osp.join(uicachedir, 'cache_concat_*')):
            os.unlink(osp.join(uicachedir, fname))

    def _publish_js_files(self, js_files):
        req = self.request()
        head = HTMLHead(req)
        url = head.concat_urls([req.data_url(js_file) for js_file in js_files])[len(req.base_url()):]
        req._url = url
        return self.app_handle_request(req, url), req

    def expected_content(self, js_files):
        content = u''
        for js_file in js_files:
            dirpath, rid = self.config.locate_resource(js_file)
            if dirpath is not None: # ignore resources not found
                with open(osp.join(dirpath, rid)) as f:
                    content += f.read() + '\n'
        return content

    def test_cache(self):
        js_files = ('cubicweb.ajax.js', 'jquery.js')
        result, req = self._publish_js_files(js_files)
        self.assertNotEqual(404, req.status_out)
        # check result content
        self.assertEqual(result, self.expected_content(js_files))
        # make sure we kept a cached version on filesystem
        concat_hander = ConcatFilesHandler(self.config)
        filepath = concat_hander.build_filepath(js_files)
        self.assertTrue(osp.isfile(filepath))


    def test_invalid_file_in_debug_mode(self):
        js_files = ('cubicweb.ajax.js', 'dummy.js')
        # in debug mode, an error is raised
        self.config.debugmode = True
        try:
            result, req = self._publish_js_files(js_files)
            #print result
            self.assertEqual(404, req.status_out)
        finally:
            self.config.debugmode = False

    def test_invalid_file_in_production_mode(self):
        js_files = ('cubicweb.ajax.js', 'dummy.js')
        result, req = self._publish_js_files(js_files)
        self.assertNotEqual(404, req.status_out)
        # check result content
        self.assertEqual(result, self.expected_content(js_files))


if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()