server/test/unittest_security.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 29 Apr 2013 11:22:21 +0200
changeset 8952 8916a71e9f11
parent 8694 d901c36bcfce
child 9395 96dba2efd16d
permissions -rw-r--r--
[pkg/debian] update changelog to 3.17.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8075
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5419
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5886
00a78298d30d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    18
"""functional tests for server'security"""
00a78298d30d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    19
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
from logilab.common.testlib import unittest_main, TestCase
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    23
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    24
from rql import RQLException
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    25
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
    26
from cubicweb.devtools.testlib import CubicWebTC
8546
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    27
from cubicweb import Unauthorized, ValidationError, QueryError, Binary
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8075
diff changeset
    28
from cubicweb.schema import ERQLExpression
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
from cubicweb.server.querier import check_read_access
8546
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    30
from cubicweb.server.utils import _CRYPTO_CTX
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8075
diff changeset
    32
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
    33
class BaseSecurityTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
7072
bcf96f2a4c5d [test] properly close connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
    35
    def setup_database(self):
bcf96f2a4c5d [test] properly close connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
    36
        super(BaseSecurityTC, self).setup_database()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    37
        self.create_user(self.request(), 'iaminusersgrouponly')
8546
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    38
        hash = _CRYPTO_CTX.encrypt('oldpassword', scheme='des_crypt')
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    39
        self.create_user(self.request(), 'oldpassword', password=Binary(hash))
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    40
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
class LowLevelSecurityFunctionTC(BaseSecurityTC):
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    42
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    def test_check_read_access(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        rql = u'Personne U where U nom "managers"'
3252
c0e10da6f1cf tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
    45
        rqlst = self.repo.vreg.rqlhelper.parse(rql).children[0]
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    46
        with self.temporary_permissions(Personne={'read': ('users', 'managers')}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    47
            self.repo.vreg.solutions(self.session, rqlst, None)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    48
            solution = rqlst.solutions[0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    49
            check_read_access(self.session, rqlst, solution, {})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    50
            with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    51
                self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    52
                                  check_read_access,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    53
                                  self.session, rqlst, solution, {})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    54
                self.assertRaises(Unauthorized, cu.execute, rql)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    55
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    def test_upassword_not_selectable(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        self.assertRaises(Unauthorized,
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 389
diff changeset
    58
                          self.execute, 'Any X,P WHERE X is CWUser, X upassword P')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        self.rollback()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    60
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    61
            self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    62
                              cu.execute, 'Any X,P WHERE X is CWUser, X upassword P')
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    63
8546
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    64
    def test_update_password(self):
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    65
        """Ensure that if a user's password is stored with a deprecated hash, it will be updated on next login"""
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    66
        oldhash = str(self.session.system_sql("SELECT cw_upassword FROM cw_CWUser WHERE cw_login = 'oldpassword'").fetchone()[0])
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    67
        with self.login('oldpassword') as cu:
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    68
            pass
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    69
        newhash = str(self.session.system_sql("SELECT cw_upassword FROM cw_CWUser WHERE cw_login = 'oldpassword'").fetchone()[0])
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    70
        self.assertNotEqual(oldhash, newhash)
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    71
        self.assertTrue(newhash.startswith('$6$'))
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    72
        with self.login('oldpassword') as cu:
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    73
            pass
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    74
        self.assertEqual(newhash, str(self.session.system_sql("SELECT cw_upassword FROM cw_CWUser WHERE cw_login = 'oldpassword'").fetchone()[0]))
3d2038d6f20d [sources/native] automatically update passwords using deprecated hashes on login
Julien Cristau <julien.cristau@logilab.fr>
parents: 8488
diff changeset
    75
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    76
5888
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    77
class SecurityRewritingTC(BaseSecurityTC):
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    78
    def hijack_source_execute(self):
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    79
        def syntax_tree_search(*args, **kwargs):
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    80
            self.query = (args, kwargs)
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    81
            return []
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    82
        self.repo.system_source.syntax_tree_search = syntax_tree_search
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    83
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    84
    def tearDown(self):
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    85
        self.repo.system_source.__dict__.pop('syntax_tree_search', None)
7072
bcf96f2a4c5d [test] properly close connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
    86
        super(SecurityRewritingTC, self).tearDown()
5888
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    87
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    88
    def test_not_relation_read_security(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    89
        with self.login('iaminusersgrouponly'):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    90
            self.hijack_source_execute()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    91
            self.execute('Any U WHERE NOT A todo_by U, A is Affaire')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    92
            self.assertEqual(self.query[0][1].as_string(),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    93
                              'Any U WHERE NOT EXISTS(A todo_by U), A is Affaire')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    94
            self.execute('Any U WHERE NOT EXISTS(A todo_by U), A is Affaire')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    95
            self.assertEqual(self.query[0][1].as_string(),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
    96
                              'Any U WHERE NOT EXISTS(A todo_by U), A is Affaire')
5888
3ee80d487f11 [security] fix read rql expression insertion: we should not insert rql expr on variables only referenced in neged relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5886
diff changeset
    97
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
class SecurityTC(BaseSecurityTC):
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    99
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    def setUp(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        BaseSecurityTC.setUp(self)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        # implicitly test manager can add some entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        self.execute("INSERT Affaire X: X sujet 'cool'")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        self.execute("INSERT Societe X: X nom 'logilab'")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        self.execute("INSERT Personne X: X nom 'bidule'")
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 389
diff changeset
   106
        self.execute('INSERT CWGroup X: X name "staff"')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        self.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
    def test_insert_security(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   110
        with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   111
            cu.execute("INSERT Personne X: X nom 'bidule'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   112
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   113
            self.assertEqual(cu.execute('Personne X').rowcount, 1)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   114
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
    def test_insert_rql_permission(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
        # test user can only add une affaire related to a societe he owns
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   117
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   118
            cu.execute("INSERT Affaire X: X sujet 'cool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   119
            self.assertRaises(Unauthorized, self.commit)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        # test nothing has actually been inserted
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
   121
        self.assertEqual(self.execute('Affaire X').rowcount, 1)
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   122
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   123
            cu.execute("INSERT Affaire X: X sujet 'cool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   124
            cu.execute("INSERT Societe X: X nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   125
            cu.execute("SET A concerne S WHERE A sujet 'cool', S nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   126
            self.commit()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   127
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
    def test_update_security_1(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   129
        with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   130
            # local security check
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   131
            cu.execute( "SET X nom 'bidulechouette' WHERE X is Personne")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   132
            self.assertRaises(Unauthorized, self.commit)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
   133
        self.assertEqual(self.execute('Personne X WHERE X nom "bidulechouette"').rowcount, 0)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   134
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
    def test_update_security_2(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   136
        with self.temporary_permissions(Personne={'read': ('users', 'managers'),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   137
                                                  'add': ('guests', 'users', 'managers')}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   138
            with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   139
                self.assertRaises(Unauthorized, cu.execute, "SET X nom 'bidulechouette' WHERE X is Personne")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   140
                self.rollback()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   141
                # self.assertRaises(Unauthorized, cnx.commit)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        # test nothing has actually been inserted
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
   143
        self.assertEqual(self.execute('Personne X WHERE X nom "bidulechouette"').rowcount, 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    def test_update_security_3(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   146
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   147
            cu.execute("INSERT Personne X: X nom 'biduuule'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   148
            cu.execute("INSERT Societe X: X nom 'looogilab'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   149
            cu.execute("SET X travaille S WHERE X nom 'biduuule', S nom 'looogilab'")
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   150
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
    def test_update_rql_permission(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        self.execute("SET A concerne S WHERE A is Affaire, S is Societe")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
        self.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        # test user can only update une affaire related to a societe he owns
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   155
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   156
            cu.execute("SET X sujet 'pascool' WHERE X is Affaire")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   157
            # this won't actually do anything since the selection query won't return anything
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   158
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   159
            # to actually get Unauthorized exception, try to update an entity we can read
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   160
            cu.execute("SET X nom 'toto' WHERE X is Societe")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   161
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   162
            cu.execute("INSERT Affaire X: X sujet 'pascool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   163
            cu.execute("INSERT Societe X: X nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   164
            cu.execute("SET A concerne S WHERE A sujet 'pascool', S nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   165
            cu.execute("SET X sujet 'habahsicestcool' WHERE X sujet 'pascool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   166
            self.commit()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   167
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
    def test_delete_security(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
        # FIXME: sample below fails because we don't detect "owner" can't delete
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
        # user anyway, and since no user with login == 'bidule' exists, no
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        # exception is raised
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
        #user._groups = {'guests':1}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        #self.assertRaises(Unauthorized,
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 389
diff changeset
   174
        #                  self.o.execute, user, "DELETE CWUser X WHERE X login 'bidule'")
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
        # check local security
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   176
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   177
            self.assertRaises(Unauthorized, cu.execute, "DELETE CWGroup Y WHERE Y name 'staff'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   178
            self.rollback()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   179
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
    def test_delete_rql_permission(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
        self.execute("SET A concerne S WHERE A is Affaire, S is Societe")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
        self.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
        # test user can only dele une affaire related to a societe he owns
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   184
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   185
            # this won't actually do anything since the selection query won't return anything
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   186
            cu.execute("DELETE Affaire X")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   187
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   188
            # to actually get Unauthorized exception, try to delete an entity we can read
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   189
            self.assertRaises(Unauthorized, cu.execute, "DELETE Societe S")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   190
            self.assertRaises(QueryError, self.commit) # can't commit anymore
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   191
            self.rollback() # required after Unauthorized
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   192
            cu.execute("INSERT Affaire X: X sujet 'pascool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   193
            cu.execute("INSERT Societe X: X nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   194
            cu.execute("SET A concerne S WHERE A sujet 'pascool', S nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   195
            self.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
##         # this one should fail since it will try to delete two affaires, one authorized
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
##         # and the other not
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
##         self.assertRaises(Unauthorized, cu.execute, "DELETE Affaire X")
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   199
            cu.execute("DELETE Affaire X WHERE X sujet 'pascool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   200
            self.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
    def test_insert_relation_rql_permission(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   204
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   205
            cu.execute("SET A concerne S WHERE A is Affaire, S is Societe")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   206
            # should raise Unauthorized since user don't own S though this won't
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   207
            # actually do anything since the selection query won't return
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   208
            # anything
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   209
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   210
            # to actually get Unauthorized exception, try to insert a relation
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   211
            # were we can read both entities
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   212
            rset = cu.execute('Personne P')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   213
            self.assertEqual(len(rset), 1)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   214
            ent = rset.get_entity(0, 0)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   215
            self.assertFalse(cu.execute('Any P,S WHERE P travaille S,P is Personne, S is Societe'))
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   216
            self.assertRaises(Unauthorized, ent.cw_check_perm, 'update')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   217
            self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   218
                              cu.execute, "SET P travaille S WHERE P is Personne, S is Societe")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   219
            self.assertRaises(QueryError, self.commit) # can't commit anymore
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   220
            self.rollback()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   221
            # test nothing has actually been inserted:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   222
            self.assertFalse(cu.execute('Any P,S WHERE P travaille S,P is Personne, S is Societe'))
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   223
            cu.execute("INSERT Societe X: X nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   224
            cu.execute("SET A concerne S WHERE A is Affaire, S nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   225
            self.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    def test_delete_relation_rql_permission(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
        self.execute("SET A concerne S WHERE A is Affaire, S is Societe")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   230
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   231
            # this won't actually do anything since the selection query won't return anything
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   232
            cu.execute("DELETE A concerne S")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   233
            self.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
        # to actually get Unauthorized exception, try to delete a relation we can read
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
        eid = self.execute("INSERT Affaire X: X sujet 'pascool'")[0][0]
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4915
diff changeset
   236
        self.execute('SET X owned_by U WHERE X eid %(x)s, U login "iaminusersgrouponly"', {'x': eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        self.execute("SET A concerne S WHERE A sujet 'pascool', S is Societe")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   239
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   240
            self.assertRaises(Unauthorized, cu.execute, "DELETE A concerne S")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   241
            self.assertRaises(QueryError, self.commit) # can't commit anymore
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   242
            self.rollback() # required after Unauthorized
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   243
            cu.execute("INSERT Societe X: X nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   244
            cu.execute("SET A concerne S WHERE A is Affaire, S nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   245
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   246
            cu.execute("DELETE A concerne S WHERE S nom 'chouette'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   247
            self.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
    def test_user_can_change_its_upassword(self):
7244
a918f76441ce fix tests broken by fcb8932082a5 (continued)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7078
diff changeset
   251
        req = self.request()
a918f76441ce fix tests broken by fcb8932082a5 (continued)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7078
diff changeset
   252
        ueid = self.create_user(req, 'user').eid
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   253
        with self.login('user') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   254
            cu.execute('SET X upassword %(passwd)s WHERE X eid %(x)s',
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   255
                       {'x': ueid, 'passwd': 'newpwd'})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   256
            self.commit()
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3890
diff changeset
   257
        cnx = self.login('user', password='newpwd')
7072
bcf96f2a4c5d [test] properly close connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
   258
        cnx.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   259
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
    def test_user_cant_change_other_upassword(self):
7244
a918f76441ce fix tests broken by fcb8932082a5 (continued)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7078
diff changeset
   261
        req = self.request()
a918f76441ce fix tests broken by fcb8932082a5 (continued)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7078
diff changeset
   262
        ueid = self.create_user(req, 'otheruser').eid
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   263
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   264
            cu.execute('SET X upassword %(passwd)s WHERE X eid %(x)s',
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   265
                       {'x': ueid, 'passwd': 'newpwd'})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   266
            self.assertRaises(Unauthorized, self.commit)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
    # read security test
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   269
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
    def test_read_base(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   271
        with self.temporary_permissions(Personne={'read': ('users', 'managers')}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   272
            with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   273
                self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   274
                                  cu.execute, 'Personne U where U nom "managers"')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   275
                self.rollback()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
321
247947250382 fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 0
diff changeset
   277
    def test_read_erqlexpr_base(self):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
        eid = self.execute("INSERT Affaire X: X sujet 'cool'")[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   280
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   281
            rset = cu.execute('Affaire X')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   282
            self.assertEqual(rset.rows, [])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   283
            self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x': eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   284
            # cache test
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   285
            self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x': eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   286
            aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   287
            soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   288
            cu.execute("SET A concerne S WHERE A is Affaire, S is Societe")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   289
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   290
            rset = cu.execute('Any X WHERE X eid %(x)s', {'x': aff2})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   291
            self.assertEqual(rset.rows, [[aff2]])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   292
            # more cache test w/ NOT eid
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   293
            rset = cu.execute('Affaire X WHERE NOT X eid %(x)s', {'x': eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   294
            self.assertEqual(rset.rows, [[aff2]])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   295
            rset = cu.execute('Affaire X WHERE NOT X eid %(x)s', {'x': aff2})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   296
            self.assertEqual(rset.rows, [])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   297
            # test can't update an attribute of an entity that can't be readen
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   298
            self.assertRaises(Unauthorized, cu.execute, 'SET X sujet "hacked" WHERE X eid %(x)s', {'x': eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   299
            self.rollback()
4765
c33d12865641 more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4711
diff changeset
   300
c33d12865641 more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4711
diff changeset
   301
c33d12865641 more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4711
diff changeset
   302
    def test_entity_created_in_transaction(self):
c33d12865641 more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4711
diff changeset
   303
        affschema = self.schema['Affaire']
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   304
        with self.temporary_permissions(Affaire={'read': affschema.permissions['add']}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   305
            with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   306
                aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   307
                # entity created in transaction are readable *by eid*
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   308
                self.assertTrue(cu.execute('Any X WHERE X eid %(x)s', {'x':aff2}))
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   309
                # XXX would be nice if it worked
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   310
                rset = cu.execute("Affaire X WHERE X sujet 'cool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   311
                self.assertEqual(len(rset), 0)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   312
                self.assertRaises(Unauthorized, self.commit)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   313
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
    def test_read_erqlexpr_has_text1(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
        aff1 = self.execute("INSERT Affaire X: X sujet 'cool'")[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
        card1 = self.execute("INSERT Card X: X title 'cool'")[0][0]
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4915
diff changeset
   317
        self.execute('SET X owned_by U WHERE X eid %(x)s, U login "iaminusersgrouponly"', {'x': card1})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   319
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   320
            aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   321
            soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   322
            cu.execute("SET A concerne S WHERE A eid %(a)s, S eid %(s)s", {'a': aff2, 's': soc1})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   323
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   324
            self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x':aff1})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   325
            self.assertTrue(cu.execute('Any X WHERE X eid %(x)s', {'x':aff2}))
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   326
            self.assertTrue(cu.execute('Any X WHERE X eid %(x)s', {'x':card1}))
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   327
            rset = cu.execute("Any X WHERE X has_text 'cool'")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   328
            self.assertEqual(sorted(eid for eid, in rset.rows),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   329
                              [card1, aff2])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   330
            self.rollback()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
    def test_read_erqlexpr_has_text2(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
        self.execute("INSERT Personne X: X nom 'bidule'")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
        self.execute("INSERT Societe X: X nom 'bidule'")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   336
        with self.temporary_permissions(Personne={'read': ('managers',)}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   337
            with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   338
                rset = cu.execute('Any N WHERE N has_text "bidule"')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   339
                self.assertEqual(len(rset.rows), 1, rset.rows)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   340
                rset = cu.execute('Any N WITH N BEING (Any N WHERE N has_text "bidule")')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   341
                self.assertEqual(len(rset.rows), 1, rset.rows)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
    def test_read_erqlexpr_optional_rel(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
        self.execute("INSERT Personne X: X nom 'bidule'")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
        self.execute("INSERT Societe X: X nom 'bidule'")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   347
        with self.temporary_permissions(Personne={'read': ('managers',)}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   348
            with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   349
                rset = cu.execute('Any N,U WHERE N has_text "bidule", N owned_by U?')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   350
                self.assertEqual(len(rset.rows), 1, rset.rows)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
    def test_read_erqlexpr_aggregat(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
        self.execute("INSERT Affaire X: X sujet 'cool'")[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   355
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   356
            rset = cu.execute('Any COUNT(X) WHERE X is Affaire')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   357
            self.assertEqual(rset.rows, [[0]])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   358
            aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   359
            soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   360
            cu.execute("SET A concerne S WHERE A is Affaire, S is Societe")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   361
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   362
            rset = cu.execute('Any COUNT(X) WHERE X is Affaire')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   363
            self.assertEqual(rset.rows, [[1]])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   364
            rset = cu.execute('Any ETN, COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   365
            values = dict(rset)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   366
            self.assertEqual(values['Affaire'], 1)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   367
            self.assertEqual(values['Societe'], 2)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   368
            rset = cu.execute('Any ETN, COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN WITH X BEING ((Affaire X) UNION (Societe X))')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   369
            self.assertEqual(len(rset), 2)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   370
            values = dict(rset)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   371
            self.assertEqual(values['Affaire'], 1)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   372
            self.assertEqual(values['Societe'], 2)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   373
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
    def test_attribute_security(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
        # only managers should be able to edit the 'test' attribute of Personne entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        eid = self.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test TRUE")[0][0]
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4915
diff changeset
   378
        self.execute('SET X test FALSE WHERE X eid %(x)s', {'x': eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   380
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   381
            cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test TRUE")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   382
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   383
            cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test FALSE")
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   384
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   385
            eid = cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org'")[0][0]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   386
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   387
            cu.execute('SET X test FALSE WHERE X eid %(x)s', {'x': eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   388
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   389
            cu.execute('SET X test TRUE WHERE X eid %(x)s', {'x': eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   390
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   391
            cu.execute('SET X web "http://www.logilab.org" WHERE X eid %(x)s', {'x': eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   392
            self.commit()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   393
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
    def test_attribute_security_rqlexpr(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
        # Note.para attribute editable by managers or if the note is in "todo" state
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
   396
        note = self.execute("INSERT Note X: X para 'bidule'").get_entity(0, 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   397
        self.commit()
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   398
        note.cw_adapt_to('IWorkflowable').fire_transition('markasdone')
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4915
diff changeset
   399
        self.execute('SET X para "truc" WHERE X eid %(x)s', {'x': note.eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   401
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   402
            cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': note.eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   403
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   404
            note2 = cu.execute("INSERT Note X: X para 'bidule'").get_entity(0, 0)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   405
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   406
            note2.cw_adapt_to('IWorkflowable').fire_transition('markasdone')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   407
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   408
            self.assertEqual(len(cu.execute('Any X WHERE X in_state S, S name "todo", X eid %(x)s', {'x': note2.eid})),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   409
                              0)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   410
            cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': note2.eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   411
            self.assertRaises(Unauthorized, self.commit)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   412
            note2.cw_adapt_to('IWorkflowable').fire_transition('redoit')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   413
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   414
            cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': note2.eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   415
            self.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
    def test_attribute_read_security(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   418
        # anon not allowed to see users'login, but they can see users
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   419
        login_rdef = self.repo.schema['CWUser'].rdef('login')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   420
        with self.temporary_permissions((login_rdef, {'read': ('users', 'managers')}),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   421
                                        CWUser={'read': ('guests', 'users', 'managers')}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   422
            with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   423
                rset = cu.execute('CWUser X')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   424
                self.assertTrue(rset)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   425
                x = rset.get_entity(0, 0)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   426
                self.assertEqual(x.login, None)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   427
                self.assertTrue(x.creation_date)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   428
                x = rset.get_entity(1, 0)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   429
                x.complete()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   430
                self.assertEqual(x.login, None)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   431
                self.assertTrue(x.creation_date)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8075
diff changeset
   433
    def test_yams_inheritance_and_security_bug(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   434
        with self.temporary_permissions(Division={'read': ('managers', ERQLExpression('X owned_by U'))}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   435
            with self.login('iaminusersgrouponly'):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   436
                querier = self.repo.querier
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   437
                rqlst = querier.parse('Any X WHERE X is_instance_of Societe')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   438
                querier.solutions(self.session, rqlst, {})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   439
                querier._annotate(rqlst)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   440
                plan = querier.plan_factory(rqlst, {}, self.session)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   441
                plan.preprocess(rqlst)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   442
                self.assertEqual(
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   443
                    rqlst.as_string(),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   444
                    '(Any X WHERE X is IN(SubDivision, Societe)) UNION (Any X WHERE X is Division, EXISTS(X owned_by %(B)s))')
8452
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8075
diff changeset
   445
1ad42383a9ec [rql security] fix rql bug when using yams inheritance and read permissions (closes #2410156)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8075
diff changeset
   446
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
class BaseSchemaSecurityTC(BaseSecurityTC):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
    """tests related to the base schema permission configuration"""
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   449
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
    def test_user_can_delete_object_he_created(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
        # even if some other user have changed object'state
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   452
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   453
            # due to security test, affaire has to concerne a societe the user owns
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   454
            cu.execute('INSERT Societe X: X nom "ARCTIA"')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   455
            cu.execute('INSERT Affaire X: X ref "ARCT01", X concerne S WHERE S nom "ARCTIA"')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   456
            self.commit()
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
   457
        affaire = self.execute('Any X WHERE X ref "ARCT01"').get_entity(0, 0)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   458
        affaire.cw_adapt_to('IWorkflowable').fire_transition('abort')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   459
        self.commit()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
   460
        self.assertEqual(len(self.execute('TrInfo X WHERE X wf_info_for A, A ref "ARCT01"')),
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
   461
                          1)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
   462
        self.assertEqual(len(self.execute('TrInfo X WHERE X wf_info_for A, A ref "ARCT01",'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
                                           'X owned_by U, U login "admin"')),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
                          1) # TrInfo at the above state change
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   465
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   466
            cu.execute('DELETE Affaire X WHERE X ref "ARCT01"')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   467
            self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   468
            self.assertFalse(cu.execute('Affaire X'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
    def test_users_and_groups_non_readable_by_guests(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   471
        with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   472
            anon = cu.connection.user(self.session)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   473
            # anonymous user can only read itself
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   474
            rset = cu.execute('Any L WHERE X owned_by U, U login L')
8624
7e415f457155 [test] swap order in assert of `test_users_and_groups_non_readable_by_guests`
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   475
            self.assertEqual([['anon']], rset.rows)
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   476
            rset = cu.execute('CWUser X')
8624
7e415f457155 [test] swap order in assert of `test_users_and_groups_non_readable_by_guests`
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   477
            self.assertEqual([[anon.eid]], rset.rows)
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   478
            # anonymous user can read groups (necessary to check allowed transitions for instance)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   479
            self.assert_(cu.execute('CWGroup X'))
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   480
            # should only be able to read the anonymous user, not another one
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   481
            origuser = self.adminsession.user
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   482
            self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   483
                              cu.execute, 'CWUser X WHERE X eid %(x)s', {'x': origuser.eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   484
            # nothing selected, nothing updated, no exception raised
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   485
            #self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   486
            #                  cu.execute, 'SET X login "toto" WHERE X eid %(x)s',
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   487
            #                  {'x': self.user.eid})
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   488
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   489
            rset = cu.execute('CWUser X WHERE X eid %(x)s', {'x': anon.eid})
8624
7e415f457155 [test] swap order in assert of `test_users_and_groups_non_readable_by_guests`
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   490
            self.assertEqual([[anon.eid]], rset.rows)
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   491
            # but can't modify it
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   492
            cu.execute('SET X login "toto" WHERE X eid %(x)s', {'x': anon.eid})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   493
            self.assertRaises(Unauthorized, self.commit)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   494
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
    def test_in_group_relation(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   496
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   497
            rql = u"DELETE U in_group G WHERE U login 'admin'"
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   498
            self.assertRaises(Unauthorized, cu.execute, rql)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   499
            rql = u"SET U in_group G WHERE U login 'admin', G name 'users'"
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   500
            self.assertRaises(Unauthorized, cu.execute, rql)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   501
            self.rollback()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   502
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   503
    def test_owned_by(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
        self.execute("INSERT Personne X: X nom 'bidule'")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   505
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   506
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   507
            rql = u"SET X owned_by U WHERE U login 'iaminusersgrouponly', X is Personne"
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   508
            self.assertRaises(Unauthorized, cu.execute, rql)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   509
            self.rollback()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   510
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
    def test_bookmarked_by_guests_security(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
        beid1 = self.execute('INSERT Bookmark B: B path "?vid=manage", B title "manage"')[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
        beid2 = self.execute('INSERT Bookmark B: B path "?vid=index", B title "index", B bookmarked_by U WHERE U login "anon"')[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   515
        with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   516
            anoneid = self.session.user.eid
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   517
            self.assertEqual(cu.execute('Any T,P ORDERBY lower(T) WHERE B is Bookmark,B title T,B path P,'
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   518
                                         'B bookmarked_by U, U eid %s' % anoneid).rows,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   519
                              [['index', '?vid=index']])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   520
            self.assertEqual(cu.execute('Any T,P ORDERBY lower(T) WHERE B is Bookmark,B title T,B path P,'
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   521
                                         'B bookmarked_by U, U eid %(x)s', {'x': anoneid}).rows,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   522
                              [['index', '?vid=index']])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   523
            # can read others bookmarks as well
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   524
            self.assertEqual(cu.execute('Any B where B is Bookmark, NOT B bookmarked_by U').rows,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   525
                              [[beid1]])
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   526
            self.assertRaises(Unauthorized, cu.execute,'DELETE B bookmarked_by U')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   527
            self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   528
                              cu.execute, 'SET B bookmarked_by U WHERE U eid %(x)s, B eid %(b)s',
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   529
                              {'x': anoneid, 'b': beid1})
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   530
            self.rollback()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   531
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   532
    def test_ambigous_ordered(self):
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   533
        with self.login('anon') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   534
            names = [t for t, in cu.execute('Any N ORDERBY lower(N) WHERE X name N')]
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   535
            self.assertEqual(names, sorted(names, key=lambda x: x.lower()))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   536
8075
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   537
    def test_restrict_is_instance_ok(self):
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   538
        rset = self.execute('Any X WHERE X is_instance_of BaseTransition')
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   539
        rqlst = rset.syntax_tree()
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   540
        select = rqlst.children[0]
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   541
        x = select.get_selected_variables().next()
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   542
        self.assertRaises(RQLException, select.add_type_restriction,
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   543
                          x.variable, 'CWUser')
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   544
        select.add_type_restriction(x.variable, 'BaseTransition')
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   545
        select.add_type_restriction(x.variable, 'WorkflowTransition')
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   546
        self.assertEqual(rqlst.as_string(), 'Any X WHERE X is_instance_of WorkflowTransition')
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   547
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   548
    def test_restrict_is_instance_no_supported(self):
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   549
        rset = self.execute('Any X WHERE X is_instance_of IN(CWUser, CWGroup)')
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   550
        rqlst = rset.syntax_tree()
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   551
        select = rqlst.children[0]
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   552
        x = select.get_selected_variables().next()
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   553
        self.assertRaises(NotImplementedError, select.add_type_restriction,
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   554
                          x.variable, 'WorkflowTransition')
f57ffbfe90fe [test] add some tests for rql's add_type_restriction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7791
diff changeset
   555
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   556
    def test_in_state_without_update_perm(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   557
        """check a user change in_state without having update permission on the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   558
        subject
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   559
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   560
        eid = self.execute('INSERT Affaire X: X ref "ARCT01"')[0][0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
        self.commit()
8461
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   562
        with self.login('iaminusersgrouponly') as cu:
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   563
            session = self.session
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   564
            # needed to avoid check_perm error
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   565
            session.set_cnxset()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   566
            # needed to remove rql expr granting update perm to the user
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   567
            affschema = self.schema['Affaire']
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   568
            with self.temporary_permissions(Affaire={'update': affschema.get_groups('update'),
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   569
                                                     'read': ('users',)}):
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   570
                self.assertRaises(Unauthorized,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   571
                                  affschema.check_perm, session, 'update', eid=eid)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   572
                aff = cu.execute('Any X WHERE X ref "ARCT01"').get_entity(0, 0)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   573
                aff.cw_adapt_to('IWorkflowable').fire_transition('abort')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   574
                self.commit()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   575
                # though changing a user state (even logged user) is reserved to managers
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   576
                user = self.user(session)
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   577
                session.set_cnxset()
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   578
                # XXX wether it should raise Unauthorized or ValidationError is not clear
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   579
                # the best would probably ValidationError if the transition doesn't exist
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   580
                # from the current state but Unauthorized if it exists but user can't pass it
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   581
                self.assertRaises(ValidationError,
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   582
                                  user.cw_adapt_to('IWorkflowable').fire_transition, 'deactivate')
8af7c6d86efb [test] update server security test using login and new temporary_permissions context managers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8452
diff changeset
   583
                self.rollback() # else will fail on login cm exit
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   584
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   585
    def test_trinfo_security(self):
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   586
        aff = self.execute('INSERT Affaire X: X ref "ARCT01"').get_entity(0, 0)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   587
        iworkflowable = aff.cw_adapt_to('IWorkflowable')
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   588
        self.commit()
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   589
        iworkflowable.fire_transition('abort')
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
   590
        self.commit()
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   591
        # can change tr info comment
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   592
        self.execute('SET TI comment %(c)s WHERE TI wf_info_for X, X ref "ARCT01"',
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2608
diff changeset
   593
                     {'c': u'bouh!'})
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   594
        self.commit()
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   595
        aff.cw_clear_relation_cache('wf_info_for', 'object')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   596
        trinfo = iworkflowable.latest_trinfo()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
   597
        self.assertEqual(trinfo.comment, 'bouh!')
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   598
        # but not from_state/to_state
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   599
        aff.cw_clear_relation_cache('wf_info_for', role='object')
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   600
        self.assertRaises(Unauthorized,
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   601
                          self.execute, 'SET TI from_state S WHERE TI eid %(ti)s, S name "ben non"',
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4915
diff changeset
   602
                          {'ti': trinfo.eid})
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   603
        self.assertRaises(Unauthorized,
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   604
                          self.execute, 'SET TI to_state S WHERE TI eid %(ti)s, S name "pitetre"',
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4915
diff changeset
   605
                          {'ti': trinfo.eid})
2501
fa86d99c2c3a test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2500
diff changeset
   606
8161
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   607
    def test_emailaddress_security(self):
8649
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   608
        # check for prexisting email adresse
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   609
        if self.execute('Any X WHERE X is EmailAddress'):
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   610
            rset = self.execute('Any X, U WHERE X is EmailAddress, U use_email X')
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   611
            msg = ['Preexisting email readable by anon found!']
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   612
            tmpl = '  - "%s" used by user "%s"'
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   613
            for i in xrange(len(rset)):
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   614
                email, user = rset.get_entity(i, 0), rset.get_entity(i, 1)
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   615
                msg.append(tmpl % (email.dc_title(), user.dc_title()))
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   616
            raise RuntimeError('\n'.join(msg))
8fbb2f65721e [test] precheck initial condition
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8546
diff changeset
   617
        # actual test
8161
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   618
        self.execute('INSERT EmailAddress X: X address "hop"').get_entity(0, 0)
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   619
        self.execute('INSERT EmailAddress X: X address "anon", U use_email X WHERE U login "anon"').get_entity(0, 0)
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   620
        self.commit()
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   621
        self.assertEqual(len(self.execute('Any X WHERE X is EmailAddress')), 2)
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   622
        self.login('anon')
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   623
        self.assertEqual(len(self.execute('Any X WHERE X is EmailAddress')), 1)
6f4229eb8178 [test] fix test broken by 8158:2ee254e74382 and add a test for that change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8075
diff changeset
   624
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   625
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   626
    unittest_main()