author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Fri, 30 Apr 2010 19:07:06 +0200 | |
branch | stable |
changeset 5450 | 269dcd14b92c |
parent 5426 | 0d4853a6e5ee |
child 5556 | 9ab2b4c74baf |
child 5886 | 00a78298d30d |
permissions | -rw-r--r-- |
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
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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/>. |
0 | 18 |
"""functional tests for server'security |
19 |
""" |
|
20 |
import sys |
|
21 |
||
22 |
from logilab.common.testlib import unittest_main, TestCase |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
23 |
from cubicweb.devtools.testlib import CubicWebTC |
0 | 24 |
|
25 |
from cubicweb import Unauthorized, ValidationError |
|
26 |
from cubicweb.server.querier import check_read_access |
|
27 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
28 |
class BaseSecurityTC(CubicWebTC): |
0 | 29 |
|
30 |
def setUp(self): |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
31 |
CubicWebTC.setUp(self) |
0 | 32 |
self.create_user('iaminusersgrouponly') |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
33 |
self.readoriggroups = self.schema['Personne'].permissions['read'] |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
34 |
self.addoriggroups = self.schema['Personne'].permissions['add'] |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
35 |
|
0 | 36 |
def tearDown(self): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
37 |
CubicWebTC.tearDown(self) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
38 |
self.schema['Personne'].set_action_permissions('read', self.readoriggroups) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
39 |
self.schema['Personne'].set_action_permissions('add', self.addoriggroups) |
0 | 40 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
41 |
|
0 | 42 |
class LowLevelSecurityFunctionTC(BaseSecurityTC): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
43 |
|
0 | 44 |
def test_check_read_access(self): |
45 |
rql = u'Personne U where U nom "managers"' |
|
3252
c0e10da6f1cf
tests update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2920
diff
changeset
|
46 |
rqlst = self.repo.vreg.rqlhelper.parse(rql).children[0] |
0 | 47 |
origgroups = self.schema['Personne'].get_groups('read') |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
48 |
self.schema['Personne'].set_action_permissions('read', ('users', 'managers')) |
4711
7ef3b029e10b
[test] we should properly use vreg method to compute solutions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4691
diff
changeset
|
49 |
self.repo.vreg.solutions(self.session, rqlst, None) |
0 | 50 |
solution = rqlst.solutions[0] |
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
51 |
check_read_access(self.session, rqlst, solution, {}) |
0 | 52 |
cnx = self.login('anon') |
53 |
cu = cnx.cursor() |
|
54 |
self.assertRaises(Unauthorized, |
|
55 |
check_read_access, |
|
5419
0b7805928a27
[repo security] deal with rewriten constant nodes in check_read_access, necessary when repo is used as an external source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
56 |
self.session, rqlst, solution, {}) |
0 | 57 |
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
|
58 |
|
0 | 59 |
def test_upassword_not_selectable(self): |
60 |
self.assertRaises(Unauthorized, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
61 |
self.execute, 'Any X,P WHERE X is CWUser, X upassword P') |
0 | 62 |
self.rollback() |
63 |
cnx = self.login('iaminusersgrouponly') |
|
64 |
cu = cnx.cursor() |
|
65 |
self.assertRaises(Unauthorized, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
66 |
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
|
67 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
68 |
|
0 | 69 |
class SecurityTC(BaseSecurityTC): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
70 |
|
0 | 71 |
def setUp(self): |
72 |
BaseSecurityTC.setUp(self) |
|
73 |
# implicitly test manager can add some entities |
|
74 |
self.execute("INSERT Affaire X: X sujet 'cool'") |
|
75 |
self.execute("INSERT Societe X: X nom 'logilab'") |
|
76 |
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
|
77 |
self.execute('INSERT CWGroup X: X name "staff"') |
0 | 78 |
self.commit() |
79 |
||
80 |
def test_insert_security(self): |
|
81 |
cnx = self.login('anon') |
|
82 |
cu = cnx.cursor() |
|
83 |
cu.execute("INSERT Personne X: X nom 'bidule'") |
|
84 |
self.assertRaises(Unauthorized, cnx.commit) |
|
85 |
self.assertEquals(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
|
86 |
|
0 | 87 |
def test_insert_rql_permission(self): |
88 |
# test user can only add une affaire related to a societe he owns |
|
89 |
cnx = self.login('iaminusersgrouponly') |
|
90 |
cu = cnx.cursor() |
|
91 |
cu.execute("INSERT Affaire X: X sujet 'cool'") |
|
92 |
self.assertRaises(Unauthorized, cnx.commit) |
|
93 |
# test nothing has actually been inserted |
|
94 |
self.restore_connection() |
|
95 |
self.assertEquals(self.execute('Affaire X').rowcount, 1) |
|
96 |
cnx = self.login('iaminusersgrouponly') |
|
97 |
cu = cnx.cursor() |
|
98 |
cu.execute("INSERT Affaire X: X sujet 'cool'") |
|
99 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
100 |
cu.execute("SET A concerne S WHERE A sujet 'cool', S nom 'chouette'") |
|
101 |
cnx.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
102 |
|
0 | 103 |
def test_update_security_1(self): |
104 |
cnx = self.login('anon') |
|
105 |
cu = cnx.cursor() |
|
106 |
# local security check |
|
107 |
cu.execute( "SET X nom 'bidulechouette' WHERE X is Personne") |
|
108 |
self.assertRaises(Unauthorized, cnx.commit) |
|
109 |
self.restore_connection() |
|
110 |
self.assertEquals(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
|
111 |
|
0 | 112 |
def test_update_security_2(self): |
113 |
cnx = self.login('anon') |
|
114 |
cu = cnx.cursor() |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
115 |
self.repo.schema['Personne'].set_action_permissions('read', ('users', 'managers')) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
116 |
self.repo.schema['Personne'].set_action_permissions('add', ('guests', 'users', 'managers')) |
0 | 117 |
self.assertRaises(Unauthorized, cu.execute, "SET X nom 'bidulechouette' WHERE X is Personne") |
118 |
#self.assertRaises(Unauthorized, cnx.commit) |
|
119 |
# test nothing has actually been inserted |
|
120 |
self.restore_connection() |
|
121 |
self.assertEquals(self.execute('Personne X WHERE X nom "bidulechouette"').rowcount, 0) |
|
122 |
||
123 |
def test_update_security_3(self): |
|
124 |
cnx = self.login('iaminusersgrouponly') |
|
125 |
cu = cnx.cursor() |
|
126 |
cu.execute("INSERT Personne X: X nom 'biduuule'") |
|
127 |
cu.execute("INSERT Societe X: X nom 'looogilab'") |
|
128 |
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
|
129 |
|
0 | 130 |
def test_update_rql_permission(self): |
131 |
self.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
132 |
self.commit() |
|
133 |
# test user can only update une affaire related to a societe he owns |
|
134 |
cnx = self.login('iaminusersgrouponly') |
|
135 |
cu = cnx.cursor() |
|
136 |
cu.execute("SET X sujet 'pascool' WHERE X is Affaire") |
|
137 |
# this won't actually do anything since the selection query won't return anything |
|
138 |
cnx.commit() |
|
139 |
# to actually get Unauthorized exception, try to update an entity we can read |
|
140 |
cu.execute("SET X nom 'toto' WHERE X is Societe") |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
141 |
self.assertRaises(Unauthorized, cnx.commit) |
0 | 142 |
cu.execute("INSERT Affaire X: X sujet 'pascool'") |
143 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
144 |
cu.execute("SET A concerne S WHERE A sujet 'pascool', S nom 'chouette'") |
|
145 |
cu.execute("SET X sujet 'habahsicestcool' WHERE X sujet 'pascool'") |
|
146 |
cnx.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
147 |
|
0 | 148 |
def test_delete_security(self): |
149 |
# FIXME: sample below fails because we don't detect "owner" can't delete |
|
150 |
# user anyway, and since no user with login == 'bidule' exists, no |
|
151 |
# exception is raised |
|
152 |
#user._groups = {'guests':1} |
|
153 |
#self.assertRaises(Unauthorized, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
154 |
# self.o.execute, user, "DELETE CWUser X WHERE X login 'bidule'") |
0 | 155 |
# check local security |
156 |
cnx = self.login('iaminusersgrouponly') |
|
157 |
cu = cnx.cursor() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
158 |
self.assertRaises(Unauthorized, cu.execute, "DELETE CWGroup Y WHERE Y name 'staff'") |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
159 |
|
0 | 160 |
def test_delete_rql_permission(self): |
161 |
self.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
162 |
self.commit() |
|
163 |
# test user can only dele une affaire related to a societe he owns |
|
164 |
cnx = self.login('iaminusersgrouponly') |
|
165 |
cu = cnx.cursor() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
166 |
# this won't actually do anything since the selection query won't return anything |
0 | 167 |
cu.execute("DELETE Affaire X") |
168 |
cnx.commit() |
|
169 |
# to actually get Unauthorized exception, try to delete an entity we can read |
|
170 |
self.assertRaises(Unauthorized, cu.execute, "DELETE Societe S") |
|
171 |
cu.execute("INSERT Affaire X: X sujet 'pascool'") |
|
172 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
173 |
cu.execute("SET A concerne S WHERE A sujet 'pascool', S nom 'chouette'") |
|
174 |
cnx.commit() |
|
175 |
## # this one should fail since it will try to delete two affaires, one authorized |
|
176 |
## # and the other not |
|
177 |
## self.assertRaises(Unauthorized, cu.execute, "DELETE Affaire X") |
|
178 |
cu.execute("DELETE Affaire X WHERE X sujet 'pascool'") |
|
179 |
cnx.commit() |
|
180 |
||
181 |
||
182 |
def test_insert_relation_rql_permission(self): |
|
183 |
cnx = self.login('iaminusersgrouponly') |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
184 |
session = self.session |
0 | 185 |
cu = cnx.cursor(session) |
186 |
cu.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
187 |
# should raise Unauthorized since user don't own S |
|
188 |
# though this won't actually do anything since the selection query won't return anything |
|
189 |
cnx.commit() |
|
190 |
# to actually get Unauthorized exception, try to insert a relation were we can read both entities |
|
191 |
rset = cu.execute('Personne P') |
|
192 |
self.assertEquals(len(rset), 1) |
|
193 |
ent = rset.get_entity(0, 0) |
|
194 |
session.set_pool() # necessary |
|
195 |
self.assertRaises(Unauthorized, |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
196 |
ent.e_schema.check_perm, session, 'update', eid=ent.eid) |
0 | 197 |
self.assertRaises(Unauthorized, |
198 |
cu.execute, "SET P travaille S WHERE P is Personne, S is Societe") |
|
199 |
# test nothing has actually been inserted: |
|
200 |
self.assertEquals(cu.execute('Any P,S WHERE P travaille S,P is Personne, S is Societe').rowcount, 0) |
|
201 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
202 |
cu.execute("SET A concerne S WHERE A is Affaire, S nom 'chouette'") |
|
203 |
cnx.commit() |
|
204 |
||
205 |
def test_delete_relation_rql_permission(self): |
|
206 |
self.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
207 |
self.commit() |
|
208 |
cnx = self.login('iaminusersgrouponly') |
|
209 |
cu = cnx.cursor() |
|
210 |
# this won't actually do anything since the selection query won't return anything |
|
211 |
cu.execute("DELETE A concerne S") |
|
212 |
cnx.commit() |
|
213 |
# to actually get Unauthorized exception, try to delete a relation we can read |
|
214 |
self.restore_connection() |
|
215 |
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
|
216 |
self.execute('SET X owned_by U WHERE X eid %(x)s, U login "iaminusersgrouponly"', {'x': eid}) |
0 | 217 |
self.execute("SET A concerne S WHERE A sujet 'pascool', S is Societe") |
218 |
self.commit() |
|
219 |
cnx = self.login('iaminusersgrouponly') |
|
220 |
cu = cnx.cursor() |
|
221 |
self.assertRaises(Unauthorized, cu.execute, "DELETE A concerne S") |
|
222 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
223 |
cu.execute("SET A concerne S WHERE A is Affaire, S nom 'chouette'") |
|
224 |
cnx.commit() |
|
225 |
cu.execute("DELETE A concerne S WHERE S nom 'chouette'") |
|
226 |
||
227 |
||
228 |
def test_user_can_change_its_upassword(self): |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
229 |
ueid = self.create_user('user').eid |
0 | 230 |
cnx = self.login('user') |
231 |
cu = cnx.cursor() |
|
232 |
cu.execute('SET X upassword %(passwd)s WHERE X eid %(x)s', |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
233 |
{'x': ueid, 'passwd': 'newpwd'}) |
0 | 234 |
cnx.commit() |
235 |
cnx.close() |
|
4191
01638461d4b0
test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
236 |
cnx = self.login('user', password='newpwd') |
0 | 237 |
|
238 |
def test_user_cant_change_other_upassword(self): |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
239 |
ueid = self.create_user('otheruser').eid |
0 | 240 |
cnx = self.login('iaminusersgrouponly') |
241 |
cu = cnx.cursor() |
|
242 |
cu.execute('SET X upassword %(passwd)s WHERE X eid %(x)s', |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
243 |
{'x': ueid, 'passwd': 'newpwd'}) |
0 | 244 |
self.assertRaises(Unauthorized, cnx.commit) |
245 |
||
246 |
# read security test |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
247 |
|
0 | 248 |
def test_read_base(self): |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
249 |
self.schema['Personne'].set_action_permissions('read', ('users', 'managers')) |
0 | 250 |
cnx = self.login('anon') |
251 |
cu = cnx.cursor() |
|
252 |
self.assertRaises(Unauthorized, |
|
253 |
cu.execute, 'Personne U where U nom "managers"') |
|
254 |
||
321
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
255 |
def test_read_erqlexpr_base(self): |
0 | 256 |
eid = self.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
257 |
self.commit() |
|
258 |
cnx = self.login('iaminusersgrouponly') |
|
259 |
cu = cnx.cursor() |
|
260 |
rset = cu.execute('Affaire X') |
|
261 |
self.assertEquals(rset.rows, []) |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
262 |
self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x': eid}) |
321
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
263 |
# cache test |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
264 |
self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x': eid}) |
0 | 265 |
aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
266 |
soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0] |
|
267 |
cu.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
268 |
cnx.commit() |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
269 |
rset = cu.execute('Any X WHERE X eid %(x)s', {'x': aff2}) |
0 | 270 |
self.assertEquals(rset.rows, [[aff2]]) |
321
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
271 |
# more cache test w/ NOT eid |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
272 |
rset = cu.execute('Affaire X WHERE NOT X eid %(x)s', {'x': eid}) |
389 | 273 |
self.assertEquals(rset.rows, [[aff2]]) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
274 |
rset = cu.execute('Affaire X WHERE NOT X eid %(x)s', {'x': aff2}) |
321
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
275 |
self.assertEquals(rset.rows, []) |
4765 | 276 |
# test can't update an attribute of an entity that can't be readen |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
277 |
self.assertRaises(Unauthorized, cu.execute, 'SET X sujet "hacked" WHERE X eid %(x)s', {'x': eid}) |
4765 | 278 |
|
279 |
||
280 |
def test_entity_created_in_transaction(self): |
|
281 |
affschema = self.schema['Affaire'] |
|
282 |
origperms = affschema.permissions['read'] |
|
283 |
affschema.set_action_permissions('read', affschema.permissions['add']) |
|
284 |
try: |
|
285 |
cnx = self.login('iaminusersgrouponly') |
|
286 |
cu = cnx.cursor() |
|
287 |
aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
|
288 |
# entity created in transaction are readable *by eid* |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
289 |
self.failUnless(cu.execute('Any X WHERE X eid %(x)s', {'x':aff2})) |
4765 | 290 |
# XXX would be nice if it worked |
291 |
rset = cu.execute("Affaire X WHERE X sujet 'cool'") |
|
292 |
self.assertEquals(len(rset), 0) |
|
293 |
finally: |
|
294 |
affschema.set_action_permissions('read', origperms) |
|
295 |
cnx.close() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
296 |
|
0 | 297 |
def test_read_erqlexpr_has_text1(self): |
298 |
aff1 = self.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
|
299 |
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
|
300 |
self.execute('SET X owned_by U WHERE X eid %(x)s, U login "iaminusersgrouponly"', {'x': card1}) |
0 | 301 |
self.commit() |
302 |
cnx = self.login('iaminusersgrouponly') |
|
303 |
cu = cnx.cursor() |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
304 |
aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
0 | 305 |
soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0] |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
306 |
cu.execute("SET A concerne S WHERE A eid %(a)s, S eid %(s)s", {'a': aff2, 's': soc1}) |
0 | 307 |
cnx.commit() |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
308 |
self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x':aff1}) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
309 |
self.failUnless(cu.execute('Any X WHERE X eid %(x)s', {'x':aff2})) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
310 |
self.failUnless(cu.execute('Any X WHERE X eid %(x)s', {'x':card1})) |
0 | 311 |
rset = cu.execute("Any X WHERE X has_text 'cool'") |
312 |
self.assertEquals(sorted(eid for eid, in rset.rows), |
|
313 |
[card1, aff2]) |
|
314 |
||
315 |
def test_read_erqlexpr_has_text2(self): |
|
316 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
317 |
self.execute("INSERT Societe X: X nom 'bidule'") |
|
318 |
self.commit() |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
319 |
self.schema['Personne'].set_action_permissions('read', ('managers',)) |
0 | 320 |
cnx = self.login('iaminusersgrouponly') |
321 |
cu = cnx.cursor() |
|
322 |
rset = cu.execute('Any N WHERE N has_text "bidule"') |
|
323 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
324 |
rset = cu.execute('Any N WITH N BEING (Any N WHERE N has_text "bidule")') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
325 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
0 | 326 |
|
327 |
def test_read_erqlexpr_optional_rel(self): |
|
328 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
329 |
self.execute("INSERT Societe X: X nom 'bidule'") |
|
330 |
self.commit() |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
331 |
self.schema['Personne'].set_action_permissions('read', ('managers',)) |
0 | 332 |
cnx = self.login('anon') |
333 |
cu = cnx.cursor() |
|
334 |
rset = cu.execute('Any N,U WHERE N has_text "bidule", N owned_by U?') |
|
335 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
336 |
||
337 |
def test_read_erqlexpr_aggregat(self): |
|
338 |
self.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
|
339 |
self.commit() |
|
340 |
cnx = self.login('iaminusersgrouponly') |
|
341 |
cu = cnx.cursor() |
|
342 |
rset = cu.execute('Any COUNT(X) WHERE X is Affaire') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
343 |
self.assertEquals(rset.rows, [[0]]) |
0 | 344 |
aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
345 |
soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0] |
|
346 |
cu.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
347 |
cnx.commit() |
|
348 |
rset = cu.execute('Any COUNT(X) WHERE X is Affaire') |
|
349 |
self.assertEquals(rset.rows, [[1]]) |
|
350 |
rset = cu.execute('Any ETN, COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN') |
|
351 |
values = dict(rset) |
|
352 |
self.assertEquals(values['Affaire'], 1) |
|
353 |
self.assertEquals(values['Societe'], 2) |
|
354 |
rset = cu.execute('Any ETN, COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN WITH X BEING ((Affaire X) UNION (Societe X))') |
|
355 |
self.assertEquals(len(rset), 2) |
|
356 |
values = dict(rset) |
|
357 |
self.assertEquals(values['Affaire'], 1) |
|
358 |
self.assertEquals(values['Societe'], 2) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
359 |
|
0 | 360 |
|
361 |
def test_attribute_security(self): |
|
362 |
# only managers should be able to edit the 'test' attribute of Personne entities |
|
363 |
eid = self.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test TRUE")[0][0] |
|
364 |
self.commit() |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
365 |
self.execute('SET X test FALSE WHERE X eid %(x)s', {'x': eid}) |
0 | 366 |
self.commit() |
367 |
cnx = self.login('iaminusersgrouponly') |
|
368 |
cu = cnx.cursor() |
|
369 |
cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test TRUE") |
|
370 |
self.assertRaises(Unauthorized, cnx.commit) |
|
371 |
cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test FALSE") |
|
372 |
self.assertRaises(Unauthorized, cnx.commit) |
|
373 |
eid = cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org'")[0][0] |
|
374 |
cnx.commit() |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
375 |
cu.execute('SET X test FALSE WHERE X eid %(x)s', {'x': eid}) |
0 | 376 |
self.assertRaises(Unauthorized, cnx.commit) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
377 |
cu.execute('SET X test TRUE WHERE X eid %(x)s', {'x': eid}) |
0 | 378 |
self.assertRaises(Unauthorized, cnx.commit) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
379 |
cu.execute('SET X web "http://www.logilab.org" WHERE X eid %(x)s', {'x': eid}) |
0 | 380 |
cnx.commit() |
381 |
cnx.close() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
382 |
|
0 | 383 |
def test_attribute_security_rqlexpr(self): |
384 |
# 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
|
385 |
note = self.execute("INSERT Note X: X para 'bidule'").get_entity(0, 0) |
0 | 386 |
self.commit() |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
387 |
note.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
|
388 |
self.execute('SET X para "truc" WHERE X eid %(x)s', {'x': note.eid}) |
0 | 389 |
self.commit() |
390 |
cnx = self.login('iaminusersgrouponly') |
|
391 |
cu = cnx.cursor() |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
392 |
cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': note.eid}) |
0 | 393 |
self.assertRaises(Unauthorized, cnx.commit) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
394 |
note2 = cu.execute("INSERT Note X: X para 'bidule'").get_entity(0, 0) |
0 | 395 |
cnx.commit() |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
396 |
note2.fire_transition('markasdone') |
0 | 397 |
cnx.commit() |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
398 |
self.assertEquals(len(cu.execute('Any X WHERE X in_state S, S name "todo", X eid %(x)s', {'x': note2.eid})), |
0 | 399 |
0) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
400 |
cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': note2.eid}) |
0 | 401 |
self.assertRaises(Unauthorized, cnx.commit) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
402 |
note2.fire_transition('redoit') |
0 | 403 |
cnx.commit() |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
404 |
cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': note2.eid}) |
0 | 405 |
cnx.commit() |
406 |
||
407 |
def test_attribute_read_security(self): |
|
408 |
# anon not allowed to see users'login, but they can see users |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
409 |
self.repo.schema['CWUser'].set_action_permissions('read', ('guests', 'users', 'managers')) |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
410 |
self.repo.schema['CWUser'].rdef('login').set_action_permissions('read', ('users', 'managers')) |
0 | 411 |
cnx = self.login('anon') |
412 |
cu = cnx.cursor() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
413 |
rset = cu.execute('CWUser X') |
0 | 414 |
self.failUnless(rset) |
415 |
x = rset.get_entity(0, 0) |
|
416 |
self.assertEquals(x.login, None) |
|
417 |
self.failUnless(x.creation_date) |
|
418 |
x = rset.get_entity(1, 0) |
|
419 |
x.complete() |
|
420 |
self.assertEquals(x.login, None) |
|
421 |
self.failUnless(x.creation_date) |
|
422 |
cnx.rollback() |
|
423 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
424 |
|
0 | 425 |
class BaseSchemaSecurityTC(BaseSecurityTC): |
426 |
"""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
|
427 |
|
0 | 428 |
def test_user_can_delete_object_he_created(self): |
429 |
# even if some other user have changed object'state |
|
430 |
cnx = self.login('iaminusersgrouponly') |
|
431 |
cu = cnx.cursor() |
|
432 |
# due to security test, affaire has to concerne a societe the user owns |
|
433 |
cu.execute('INSERT Societe X: X nom "ARCTIA"') |
|
434 |
cu.execute('INSERT Affaire X: X ref "ARCT01", X concerne S WHERE S nom "ARCTIA"') |
|
435 |
cnx.commit() |
|
436 |
self.restore_connection() |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
437 |
affaire = self.execute('Any X WHERE X ref "ARCT01"').get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
438 |
affaire.fire_transition('abort') |
0 | 439 |
self.commit() |
440 |
self.assertEquals(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
|
441 |
1) |
0 | 442 |
self.assertEquals(len(self.execute('TrInfo X WHERE X wf_info_for A, A ref "ARCT01",' |
443 |
'X owned_by U, U login "admin"')), |
|
444 |
1) # TrInfo at the above state change |
|
445 |
cnx = self.login('iaminusersgrouponly') |
|
446 |
cu = cnx.cursor() |
|
447 |
cu.execute('DELETE Affaire X WHERE X ref "ARCT01"') |
|
448 |
cnx.commit() |
|
449 |
self.failIf(cu.execute('Affaire X')) |
|
450 |
||
451 |
def test_users_and_groups_non_readable_by_guests(self): |
|
452 |
cnx = self.login('anon') |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
453 |
anon = cnx.user(self.session) |
0 | 454 |
cu = cnx.cursor() |
455 |
# anonymous user can only read itself |
|
456 |
rset = cu.execute('Any L WHERE X owned_by U, U login L') |
|
457 |
self.assertEquals(rset.rows, [['anon']]) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
458 |
rset = cu.execute('CWUser X') |
0 | 459 |
self.assertEquals(rset.rows, [[anon.eid]]) |
460 |
# anonymous user can read groups (necessary to check allowed transitions for instance) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
461 |
self.assert_(cu.execute('CWGroup X')) |
0 | 462 |
# should only be able to read the anonymous user, not another one |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
463 |
origuser = self.adminsession.user |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
464 |
self.assertRaises(Unauthorized, |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
465 |
cu.execute, 'CWUser X WHERE X eid %(x)s', {'x': origuser.eid}) |
0 | 466 |
# nothing selected, nothing updated, no exception raised |
467 |
#self.assertRaises(Unauthorized, |
|
468 |
# cu.execute, 'SET X login "toto" WHERE X eid %(x)s', |
|
469 |
# {'x': self.user.eid}) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
470 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
471 |
rset = cu.execute('CWUser X WHERE X eid %(x)s', {'x': anon.eid}) |
0 | 472 |
self.assertEquals(rset.rows, [[anon.eid]]) |
473 |
# but can't modify it |
|
4915
d657b89df9f4
fix test broken by recent rql rewrite / querier changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4787
diff
changeset
|
474 |
cu.execute('SET X login "toto" WHERE X eid %(x)s', {'x': anon.eid}) |
d657b89df9f4
fix test broken by recent rql rewrite / querier changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4787
diff
changeset
|
475 |
self.assertRaises(Unauthorized, cnx.commit) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
476 |
|
0 | 477 |
def test_in_group_relation(self): |
478 |
cnx = self.login('iaminusersgrouponly') |
|
479 |
cu = cnx.cursor() |
|
480 |
rql = u"DELETE U in_group G WHERE U login 'admin'" |
|
481 |
self.assertRaises(Unauthorized, cu.execute, rql) |
|
482 |
rql = u"SET U in_group G WHERE U login 'admin', G name 'users'" |
|
483 |
self.assertRaises(Unauthorized, cu.execute, rql) |
|
484 |
||
485 |
def test_owned_by(self): |
|
486 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
487 |
self.commit() |
|
488 |
cnx = self.login('iaminusersgrouponly') |
|
489 |
cu = cnx.cursor() |
|
490 |
rql = u"SET X owned_by U WHERE U login 'iaminusersgrouponly', X is Personne" |
|
491 |
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
|
492 |
|
0 | 493 |
def test_bookmarked_by_guests_security(self): |
494 |
beid1 = self.execute('INSERT Bookmark B: B path "?vid=manage", B title "manage"')[0][0] |
|
495 |
beid2 = self.execute('INSERT Bookmark B: B path "?vid=index", B title "index", B bookmarked_by U WHERE U login "anon"')[0][0] |
|
496 |
self.commit() |
|
497 |
cnx = self.login('anon') |
|
498 |
cu = cnx.cursor() |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
499 |
anoneid = self.session.user.eid |
0 | 500 |
self.assertEquals(cu.execute('Any T,P ORDERBY lower(T) WHERE B is Bookmark,B title T,B path P,' |
501 |
'B bookmarked_by U, U eid %s' % anoneid).rows, |
|
502 |
[['index', '?vid=index']]) |
|
503 |
self.assertEquals(cu.execute('Any T,P ORDERBY lower(T) WHERE B is Bookmark,B title T,B path P,' |
|
504 |
'B bookmarked_by U, U eid %(x)s', {'x': anoneid}).rows, |
|
505 |
[['index', '?vid=index']]) |
|
506 |
# can read others bookmarks as well |
|
507 |
self.assertEquals(cu.execute('Any B where B is Bookmark, NOT B bookmarked_by U').rows, |
|
508 |
[[beid1]]) |
|
509 |
self.assertRaises(Unauthorized, cu.execute,'DELETE B bookmarked_by U') |
|
510 |
self.assertRaises(Unauthorized, |
|
511 |
cu.execute, 'SET B bookmarked_by U WHERE U eid %(x)s, B eid %(b)s', |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4915
diff
changeset
|
512 |
{'x': anoneid, 'b': beid1}) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
513 |
|
0 | 514 |
|
515 |
def test_ambigous_ordered(self): |
|
516 |
cnx = self.login('anon') |
|
517 |
cu = cnx.cursor() |
|
518 |
names = [t for t, in cu.execute('Any N ORDERBY lower(N) WHERE X name N')] |
|
519 |
self.assertEquals(names, sorted(names, key=lambda x: x.lower())) |
|
520 |
||
521 |
def test_in_state_without_update_perm(self): |
|
522 |
"""check a user change in_state without having update permission on the |
|
523 |
subject |
|
524 |
""" |
|
525 |
eid = self.execute('INSERT Affaire X: X ref "ARCT01"')[0][0] |
|
526 |
self.commit() |
|
527 |
cnx = self.login('iaminusersgrouponly') |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
528 |
session = self.session |
0 | 529 |
# needed to avoid check_perm error |
530 |
session.set_pool() |
|
531 |
# needed to remove rql expr granting update perm to the user |
|
4691
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
532 |
affaire_perms = self.schema['Affaire'].permissions.copy() |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3252
diff
changeset
|
533 |
self.schema['Affaire'].set_action_permissions('update', self.schema['Affaire'].get_groups('update')) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
534 |
try: |
4691
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
535 |
self.assertRaises(Unauthorized, |
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
536 |
self.schema['Affaire'].check_perm, session, 'update', eid=eid) |
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
537 |
cu = cnx.cursor() |
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
538 |
self.schema['Affaire'].set_action_permissions('read', ('users',)) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
539 |
aff = cu.execute('Any X WHERE X ref "ARCT01"').get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
540 |
aff.fire_transition('abort') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
541 |
cnx.commit() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
542 |
# though changing a user state (even logged user) is reserved to managers |
3447 | 543 |
user = cnx.user(self.session) |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
544 |
# XXX wether it should raise Unauthorized or ValidationError is not clear |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
545 |
# the best would probably ValidationError if the transition doesn't exist |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
546 |
# from the current state but Unauthorized if it exists but user can't pass it |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
547 |
self.assertRaises(ValidationError, user.fire_transition, 'deactivate') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
548 |
finally: |
4691
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
549 |
# restore orig perms |
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
550 |
for action, perms in affaire_perms.iteritems(): |
ae468fae9965
[test] fix test inter-dependancies pb. Pytest ok in each individual test dir, though not yet for whole cubicweb, but for different reasons
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4191
diff
changeset
|
551 |
self.schema['Affaire'].set_action_permissions(action, perms) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
552 |
|
2501
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
553 |
def test_trinfo_security(self): |
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
554 |
aff = self.execute('INSERT Affaire X: X ref "ARCT01"').get_entity(0, 0) |
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
555 |
self.commit() |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
556 |
aff.fire_transition('abort') |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
557 |
self.commit() |
2501
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
558 |
# can change tr info comment |
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
559 |
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
|
560 |
{'c': u'bouh!'}) |
2501
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
561 |
self.commit() |
2608
21856eda34f6
[F repo tests] tests have to be updated:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2501
diff
changeset
|
562 |
aff.clear_related_cache('wf_info_for', 'object') |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
563 |
trinfo = aff.latest_trinfo() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2608
diff
changeset
|
564 |
self.assertEquals(trinfo.comment, 'bouh!') |
2501
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
565 |
# but not from_state/to_state |
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
566 |
aff.clear_related_cache('wf_info_for', role='object') |
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
567 |
self.assertRaises(Unauthorized, |
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
568 |
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
|
569 |
{'ti': trinfo.eid}) |
2501
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
570 |
self.assertRaises(Unauthorized, |
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
571 |
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
|
572 |
{'ti': trinfo.eid}) |
2501
fa86d99c2c3a
test and fix wf history security
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2500
diff
changeset
|
573 |
|
0 | 574 |
if __name__ == '__main__': |
575 |
unittest_main() |