cubicweb/web/test/unittest_propertysheet.py
author David Douard <david.douard@logilab.fr>
Thu, 19 Apr 2018 12:45:24 +0200
branch3.26
changeset 12287 547bb96ea2a8
parent 11057 0b59724cb3f2
child 12289 b86214011758
permissions -rw-r--r--
[test/web] add a test for css generation file permission problem (related to #17143773)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
     1
import os
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
from os.path import join, dirname
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
     3
from shutil import rmtree
10318
09273cb083e7 [web/test] don't delete uicache directory
Julien Cristau <julien.cristau@logilab.fr>
parents: 9844
diff changeset
     4
import errno
09273cb083e7 [web/test] don't delete uicache directory
Julien Cristau <julien.cristau@logilab.fr>
parents: 9844
diff changeset
     5
import tempfile
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
     6
from unittest import TestCase, main
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
     7
9844
e7d7b7793769 [webtests/propertysheet] kill an "import *"
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
     8
from cubicweb.web.propertysheet import PropertySheet, lazystr
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    10
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
DATADIR = join(dirname(__file__), 'data')
10318
09273cb083e7 [web/test] don't delete uicache directory
Julien Cristau <julien.cristau@logilab.fr>
parents: 9844
diff changeset
    12
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    13
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
class PropertySheetTC(TestCase):
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    16
    def setUp(self):
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    17
        uicache = join(DATADIR, 'uicache')
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    18
        try:
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    19
            os.makedirs(uicache)
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    20
        except OSError as err:
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    21
            if err.errno != errno.EEXIST:
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    22
                raise
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    23
        self.cachedir = tempfile.mkdtemp(dir=uicache)
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    24
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    25
    def tearDown(self):
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    26
        rmtree(self.cachedir)
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    27
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    28
    def data(self, filename):
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    29
        return join(DATADIR, filename)
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    30
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
    def test(self):
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    32
        ps = PropertySheet(self.cachedir, datadir_url='http://cwtest.com')
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    33
        ps.load(self.data('sheet1.py'))
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    34
        ps.load(self.data('sheet2.py'))
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
        # defined by sheet1
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    36
        self.assertEqual(ps['logo'], 'http://cwtest.com/logo.png')
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
        # defined by sheet1, overriden by sheet2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    38
        self.assertEqual(ps['bgcolor'], '#FFFFFF')
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        # defined by sheet2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    40
        self.assertEqual(ps['fontcolor'], 'black')
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
        # defined by sheet1, extended by sheet2
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    42
        self.assertEqual(ps['stylesheets'], ['http://cwtest.com/cubicweb.css',
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
                                              'http://cwtest.com/mycube.css'])
5938
9cb13d1b2ce4 [uiprops] introduce lazystr to get a chance to change style without having to change all properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5466
diff changeset
    44
        # lazy string defined by sheet1
9cb13d1b2ce4 [uiprops] introduce lazystr to get a chance to change style without having to change all properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5466
diff changeset
    45
        self.assertIsInstance(ps['lazy'], lazystr)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    46
        self.assertEqual(str(ps['lazy']), '#FFFFFF')
5938
9cb13d1b2ce4 [uiprops] introduce lazystr to get a chance to change style without having to change all properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5466
diff changeset
    47
        # test compilation
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    48
        self.assertEqual(ps.compile('a {bgcolor: %(bgcolor)s; size: 1%;}'),
5445
4467ed43d97d [web] use uiprops value to compile css transparently, handlig cache and reloading in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    49
                          'a {bgcolor: #FFFFFF; size: 1%;}')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5938
diff changeset
    50
        self.assertEqual(ps.process_resource(DATADIR, 'pouet.css'),
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    51
                         self.cachedir)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    52
        self.assertFalse(ps.need_reload())
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    53
        os.utime(self.data('sheet1.py'), None)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    54
        self.assertTrue(ps.need_reload())
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    55
        ps.reload()
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    56
        self.assertFalse(ps.need_reload())
5466
b5af2ac0c43c [uiprops] test and fix reloading of modified css files; update c-c newcube; deprecates config.has_resource.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5445
diff changeset
    57
        ps.process_resource(DATADIR, 'pouet.css') # put in cache
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    58
        os.utime(self.data('pouet.css'), None)
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    59
        self.assertFalse(ps.need_reload())
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
12287
547bb96ea2a8 [test/web] add a test for css generation file permission problem (related to #17143773)
David Douard <david.douard@logilab.fr>
parents: 11057
diff changeset
    61
    def test_chmod(self):
547bb96ea2a8 [test/web] add a test for css generation file permission problem (related to #17143773)
David Douard <david.douard@logilab.fr>
parents: 11057
diff changeset
    62
        ps = PropertySheet(self.cachedir, datadir_url='http://cwtest.com')
547bb96ea2a8 [test/web] add a test for css generation file permission problem (related to #17143773)
David Douard <david.douard@logilab.fr>
parents: 11057
diff changeset
    63
        ps.load(self.data('sheet1.py'))
547bb96ea2a8 [test/web] add a test for css generation file permission problem (related to #17143773)
David Douard <david.douard@logilab.fr>
parents: 11057
diff changeset
    64
        rdir = ps.process_resource(DATADIR, 'pouet.css')
547bb96ea2a8 [test/web] add a test for css generation file permission problem (related to #17143773)
David Douard <david.douard@logilab.fr>
parents: 11057
diff changeset
    65
        mode = os.stat(join(rdir, 'pouet.css')).st_mode
547bb96ea2a8 [test/web] add a test for css generation file permission problem (related to #17143773)
David Douard <david.douard@logilab.fr>
parents: 11057
diff changeset
    66
        self.assertEqual(('%o' % mode)[-4:], '0644')
547bb96ea2a8 [test/web] add a test for css generation file permission problem (related to #17143773)
David Douard <david.douard@logilab.fr>
parents: 11057
diff changeset
    67
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    68
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
if __name__ == '__main__':
10507
d54a942ee6da [web/test] Modernize unittest_propertysheet.py
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10318
diff changeset
    70
    main()