hooks/test/unittest_syncsession.py
author Christophe de Vienne <christophe@unlish.com>
Sat, 30 Jan 2016 23:06:18 +0100
changeset 11097 900c27ea30e9
parent 9863 3fbad401eb61
child 11127 6464edfa95bb
permissions -rw-r--r--
[cwvreg] Fix propertyvalues hot updates When CWProperty instances are touched, the corresponding value gets updated in vreg['propertyvalues'], but their type was ignored.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7192
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
9863
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
     2
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
7192
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
#
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
# This file is part of CubicWeb.
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
#
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
# any later version.
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
#
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
# details.
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
#
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
"""functional tests for core hooks
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
Note:
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
  syncschema.py hooks are mostly tested in server/test/unittest_migrations.py
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
"""
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
from cubicweb import ValidationError
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
from cubicweb.devtools.testlib import CubicWebTC
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
class CWPropertyHooksTC(CubicWebTC):
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
    def test_unexistant_cwproperty(self):
9863
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    31
        with self.admin_access.web_request() as req:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    32
            with self.assertRaises(ValidationError) as cm:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    33
                req.execute('INSERT CWProperty X: X pkey "bla.bla", '
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    34
                            'X value "hop", X for_user U')
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    35
            cm.exception.translate(unicode)
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    36
            self.assertEqual(cm.exception.errors,
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    37
                             {'pkey-subject': 'unknown property key bla.bla'})
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    38
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    39
            with self.assertRaises(ValidationError) as cm:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    40
                req.execute('INSERT CWProperty X: X pkey "bla.bla", X value "hop"')
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    41
            cm.exception.translate(unicode)
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    42
            self.assertEqual(cm.exception.errors,
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    43
                             {'pkey-subject': 'unknown property key bla.bla'})
7192
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
    def test_site_wide_cwproperty(self):
9863
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    46
        with self.admin_access.web_request() as req:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    47
            with self.assertRaises(ValidationError) as cm:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    48
                req.execute('INSERT CWProperty X: X pkey "ui.site-title", '
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    49
                            'X value "hop", X for_user U')
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    50
            self.assertEqual(cm.exception.errors,
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    51
                             {'for_user-subject': "site-wide property can't be set for user"})
7192
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
    def test_system_cwproperty(self):
9863
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    54
        with self.admin_access.web_request() as req:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    55
            with self.assertRaises(ValidationError) as cm:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    56
                req.execute('INSERT CWProperty X: X pkey "system.version.cubicweb", '
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    57
                            'X value "hop", X for_user U')
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    58
            self.assertEqual(cm.exception.errors,
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    59
                             {'for_user-subject': "site-wide property can't be set for user"})
7192
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
    def test_bad_type_cwproperty(self):
9863
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    62
        with self.admin_access.web_request() as req:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    63
            with self.assertRaises(ValidationError) as cm:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    64
                req.execute('INSERT CWProperty X: X pkey "ui.language", '
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    65
                            'X value "hop", X for_user U')
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    66
            self.assertEqual(cm.exception.errors,
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    67
                             {'value-subject': u'unauthorized value'})
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    68
            with self.assertRaises(ValidationError) as cm:
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    69
                req.execute('INSERT CWProperty X: X pkey "ui.language", X value "hop"')
3fbad401eb61 [tests/syncsession] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8694
diff changeset
    70
            self.assertEqual(cm.exception.errors, {'value-subject': u'unauthorized value'})
7192
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
11097
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    72
    def test_vreg_propertyvalues_update(self):
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    73
        self.vreg.register_property(
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    74
            'test.int', type='Int', help='', sitewide=True)
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    75
        with self.admin_access.repo_cnx() as cnx:
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    76
            cnx.execute('INSERT CWProperty X: X pkey "test.int", X value "42"')
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    77
            cnx.commit()
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    78
        self.assertEqual(self.vreg.property_value('test.int'), 42)
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    79
900c27ea30e9 [cwvreg] Fix propertyvalues hot updates
Christophe de Vienne <christophe@unlish.com>
parents: 9863
diff changeset
    80
7192
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
if __name__ == '__main__':
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
    from logilab.common.testlib import unittest_main
9e92c8558fea [test, cw props] ensure we can't add user specific / system-wide properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
    unittest_main()