author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 25 May 2009 12:57:19 +0200 | |
branch | stable |
changeset 1913 | 7d7bfd30c7f5 |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""functional tests for server'security |
2 |
""" |
|
3 |
import sys |
|
4 |
||
5 |
from logilab.common.testlib import unittest_main, TestCase |
|
6 |
from cubicweb.devtools.apptest import RepositoryBasedTC |
|
7 |
||
8 |
from cubicweb import Unauthorized, ValidationError |
|
9 |
from cubicweb.server.querier import check_read_access |
|
10 |
||
11 |
class BaseSecurityTC(RepositoryBasedTC): |
|
12 |
||
13 |
def setUp(self): |
|
14 |
RepositoryBasedTC.setUp(self) |
|
15 |
self.create_user('iaminusersgrouponly') |
|
16 |
self.readoriggroups = self.schema['Personne'].get_groups('read') |
|
17 |
self.addoriggroups = self.schema['Personne'].get_groups('add') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
18 |
|
0 | 19 |
def tearDown(self): |
20 |
RepositoryBasedTC.tearDown(self) |
|
21 |
self.schema['Personne'].set_groups('read', self.readoriggroups) |
|
22 |
self.schema['Personne'].set_groups('add', self.addoriggroups) |
|
23 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
24 |
|
0 | 25 |
class LowLevelSecurityFunctionTC(BaseSecurityTC): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
26 |
|
0 | 27 |
def test_check_read_access(self): |
28 |
rql = u'Personne U where U nom "managers"' |
|
29 |
rqlst = self.repo.querier._rqlhelper.parse(rql).children[0] |
|
30 |
origgroups = self.schema['Personne'].get_groups('read') |
|
31 |
self.schema['Personne'].set_groups('read', ('users', 'managers')) |
|
32 |
self.repo.querier._rqlhelper.compute_solutions(rqlst) |
|
33 |
solution = rqlst.solutions[0] |
|
34 |
check_read_access(self.schema, self.session.user, rqlst, solution) |
|
35 |
cnx = self.login('anon') |
|
36 |
cu = cnx.cursor() |
|
37 |
self.assertRaises(Unauthorized, |
|
38 |
check_read_access, |
|
39 |
self.schema, cnx.user(self.current_session()), rqlst, solution) |
|
40 |
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
|
41 |
|
0 | 42 |
def test_upassword_not_selectable(self): |
43 |
self.assertRaises(Unauthorized, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
44 |
self.execute, 'Any X,P WHERE X is CWUser, X upassword P') |
0 | 45 |
self.rollback() |
46 |
cnx = self.login('iaminusersgrouponly') |
|
47 |
cu = cnx.cursor() |
|
48 |
self.assertRaises(Unauthorized, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
49 |
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
|
50 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
51 |
|
0 | 52 |
class SecurityTC(BaseSecurityTC): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
53 |
|
0 | 54 |
def setUp(self): |
55 |
BaseSecurityTC.setUp(self) |
|
56 |
# implicitly test manager can add some entities |
|
57 |
self.execute("INSERT Affaire X: X sujet 'cool'") |
|
58 |
self.execute("INSERT Societe X: X nom 'logilab'") |
|
59 |
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
|
60 |
self.execute('INSERT CWGroup X: X name "staff"') |
0 | 61 |
self.commit() |
62 |
||
63 |
def test_insert_security(self): |
|
64 |
cnx = self.login('anon') |
|
65 |
cu = cnx.cursor() |
|
66 |
cu.execute("INSERT Personne X: X nom 'bidule'") |
|
67 |
self.assertRaises(Unauthorized, cnx.commit) |
|
68 |
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
|
69 |
|
0 | 70 |
def test_insert_rql_permission(self): |
71 |
# test user can only add une affaire related to a societe he owns |
|
72 |
cnx = self.login('iaminusersgrouponly') |
|
73 |
cu = cnx.cursor() |
|
74 |
cu.execute("INSERT Affaire X: X sujet 'cool'") |
|
75 |
self.assertRaises(Unauthorized, cnx.commit) |
|
76 |
# test nothing has actually been inserted |
|
77 |
self.restore_connection() |
|
78 |
self.assertEquals(self.execute('Affaire X').rowcount, 1) |
|
79 |
cnx = self.login('iaminusersgrouponly') |
|
80 |
cu = cnx.cursor() |
|
81 |
cu.execute("INSERT Affaire X: X sujet 'cool'") |
|
82 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
83 |
cu.execute("SET A concerne S WHERE A sujet 'cool', S nom 'chouette'") |
|
84 |
cnx.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
85 |
|
0 | 86 |
def test_update_security_1(self): |
87 |
cnx = self.login('anon') |
|
88 |
cu = cnx.cursor() |
|
89 |
# local security check |
|
90 |
cu.execute( "SET X nom 'bidulechouette' WHERE X is Personne") |
|
91 |
self.assertRaises(Unauthorized, cnx.commit) |
|
92 |
self.restore_connection() |
|
93 |
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
|
94 |
|
0 | 95 |
def test_update_security_2(self): |
96 |
cnx = self.login('anon') |
|
97 |
cu = cnx.cursor() |
|
98 |
self.repo.schema['Personne'].set_groups('read', ('users', 'managers')) |
|
99 |
self.repo.schema['Personne'].set_groups('add', ('guests', 'users', 'managers')) |
|
100 |
self.assertRaises(Unauthorized, cu.execute, "SET X nom 'bidulechouette' WHERE X is Personne") |
|
101 |
#self.assertRaises(Unauthorized, cnx.commit) |
|
102 |
# test nothing has actually been inserted |
|
103 |
self.restore_connection() |
|
104 |
self.assertEquals(self.execute('Personne X WHERE X nom "bidulechouette"').rowcount, 0) |
|
105 |
||
106 |
def test_update_security_3(self): |
|
107 |
cnx = self.login('iaminusersgrouponly') |
|
108 |
cu = cnx.cursor() |
|
109 |
cu.execute("INSERT Personne X: X nom 'biduuule'") |
|
110 |
cu.execute("INSERT Societe X: X nom 'looogilab'") |
|
111 |
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
|
112 |
|
0 | 113 |
def test_update_rql_permission(self): |
114 |
self.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
115 |
self.commit() |
|
116 |
# test user can only update une affaire related to a societe he owns |
|
117 |
cnx = self.login('iaminusersgrouponly') |
|
118 |
cu = cnx.cursor() |
|
119 |
cu.execute("SET X sujet 'pascool' WHERE X is Affaire") |
|
120 |
# this won't actually do anything since the selection query won't return anything |
|
121 |
cnx.commit() |
|
122 |
# to actually get Unauthorized exception, try to update an entity we can read |
|
123 |
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
|
124 |
self.assertRaises(Unauthorized, cnx.commit) |
0 | 125 |
cu.execute("INSERT Affaire X: X sujet 'pascool'") |
126 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
127 |
cu.execute("SET A concerne S WHERE A sujet 'pascool', S nom 'chouette'") |
|
128 |
cu.execute("SET X sujet 'habahsicestcool' WHERE X sujet 'pascool'") |
|
129 |
cnx.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
130 |
|
0 | 131 |
def test_delete_security(self): |
132 |
# FIXME: sample below fails because we don't detect "owner" can't delete |
|
133 |
# user anyway, and since no user with login == 'bidule' exists, no |
|
134 |
# exception is raised |
|
135 |
#user._groups = {'guests':1} |
|
136 |
#self.assertRaises(Unauthorized, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
137 |
# self.o.execute, user, "DELETE CWUser X WHERE X login 'bidule'") |
0 | 138 |
# check local security |
139 |
cnx = self.login('iaminusersgrouponly') |
|
140 |
cu = cnx.cursor() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
141 |
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
|
142 |
|
0 | 143 |
def test_delete_rql_permission(self): |
144 |
self.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
145 |
self.commit() |
|
146 |
# test user can only dele une affaire related to a societe he owns |
|
147 |
cnx = self.login('iaminusersgrouponly') |
|
148 |
cu = cnx.cursor() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
149 |
# this won't actually do anything since the selection query won't return anything |
0 | 150 |
cu.execute("DELETE Affaire X") |
151 |
cnx.commit() |
|
152 |
# to actually get Unauthorized exception, try to delete an entity we can read |
|
153 |
self.assertRaises(Unauthorized, cu.execute, "DELETE Societe S") |
|
154 |
cu.execute("INSERT Affaire X: X sujet 'pascool'") |
|
155 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
156 |
cu.execute("SET A concerne S WHERE A sujet 'pascool', S nom 'chouette'") |
|
157 |
cnx.commit() |
|
158 |
## # this one should fail since it will try to delete two affaires, one authorized |
|
159 |
## # and the other not |
|
160 |
## self.assertRaises(Unauthorized, cu.execute, "DELETE Affaire X") |
|
161 |
cu.execute("DELETE Affaire X WHERE X sujet 'pascool'") |
|
162 |
cnx.commit() |
|
163 |
||
164 |
||
165 |
def test_insert_relation_rql_permission(self): |
|
166 |
cnx = self.login('iaminusersgrouponly') |
|
167 |
session = self.current_session() |
|
168 |
cu = cnx.cursor(session) |
|
169 |
cu.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
170 |
# should raise Unauthorized since user don't own S |
|
171 |
# though this won't actually do anything since the selection query won't return anything |
|
172 |
cnx.commit() |
|
173 |
# to actually get Unauthorized exception, try to insert a relation were we can read both entities |
|
174 |
rset = cu.execute('Personne P') |
|
175 |
self.assertEquals(len(rset), 1) |
|
176 |
ent = rset.get_entity(0, 0) |
|
177 |
session.set_pool() # necessary |
|
178 |
self.assertRaises(Unauthorized, |
|
179 |
ent.e_schema.check_perm, session, 'update', ent.eid) |
|
180 |
self.assertRaises(Unauthorized, |
|
181 |
cu.execute, "SET P travaille S WHERE P is Personne, S is Societe") |
|
182 |
# test nothing has actually been inserted: |
|
183 |
self.assertEquals(cu.execute('Any P,S WHERE P travaille S,P is Personne, S is Societe').rowcount, 0) |
|
184 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
185 |
cu.execute("SET A concerne S WHERE A is Affaire, S nom 'chouette'") |
|
186 |
cnx.commit() |
|
187 |
||
188 |
def test_delete_relation_rql_permission(self): |
|
189 |
self.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
190 |
self.commit() |
|
191 |
cnx = self.login('iaminusersgrouponly') |
|
192 |
cu = cnx.cursor() |
|
193 |
# this won't actually do anything since the selection query won't return anything |
|
194 |
cu.execute("DELETE A concerne S") |
|
195 |
cnx.commit() |
|
196 |
# to actually get Unauthorized exception, try to delete a relation we can read |
|
197 |
self.restore_connection() |
|
198 |
eid = self.execute("INSERT Affaire X: X sujet 'pascool'")[0][0] |
|
199 |
self.execute('SET X owned_by U WHERE X eid %(x)s, U login "iaminusersgrouponly"', {'x': eid}, 'x') |
|
200 |
self.execute("SET A concerne S WHERE A sujet 'pascool', S is Societe") |
|
201 |
self.commit() |
|
202 |
cnx = self.login('iaminusersgrouponly') |
|
203 |
cu = cnx.cursor() |
|
204 |
self.assertRaises(Unauthorized, cu.execute, "DELETE A concerne S") |
|
205 |
cu.execute("INSERT Societe X: X nom 'chouette'") |
|
206 |
cu.execute("SET A concerne S WHERE A is Affaire, S nom 'chouette'") |
|
207 |
cnx.commit() |
|
208 |
cu.execute("DELETE A concerne S WHERE S nom 'chouette'") |
|
209 |
||
210 |
||
211 |
def test_user_can_change_its_upassword(self): |
|
212 |
ueid = self.create_user('user') |
|
213 |
cnx = self.login('user') |
|
214 |
cu = cnx.cursor() |
|
215 |
cu.execute('SET X upassword %(passwd)s WHERE X eid %(x)s', |
|
216 |
{'x': ueid, 'passwd': 'newpwd'}, 'x') |
|
217 |
cnx.commit() |
|
218 |
cnx.close() |
|
219 |
cnx = self.login('user', 'newpwd') |
|
220 |
||
221 |
def test_user_cant_change_other_upassword(self): |
|
222 |
ueid = self.create_user('otheruser') |
|
223 |
cnx = self.login('iaminusersgrouponly') |
|
224 |
cu = cnx.cursor() |
|
225 |
cu.execute('SET X upassword %(passwd)s WHERE X eid %(x)s', |
|
226 |
{'x': ueid, 'passwd': 'newpwd'}, 'x') |
|
227 |
self.assertRaises(Unauthorized, cnx.commit) |
|
228 |
||
229 |
# read security test |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
230 |
|
0 | 231 |
def test_read_base(self): |
232 |
self.schema['Personne'].set_groups('read', ('users', 'managers')) |
|
233 |
cnx = self.login('anon') |
|
234 |
cu = cnx.cursor() |
|
235 |
self.assertRaises(Unauthorized, |
|
236 |
cu.execute, 'Personne U where U nom "managers"') |
|
237 |
||
321
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
238 |
def test_read_erqlexpr_base(self): |
0 | 239 |
eid = self.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
240 |
self.commit() |
|
241 |
cnx = self.login('iaminusersgrouponly') |
|
242 |
cu = cnx.cursor() |
|
243 |
rset = cu.execute('Affaire X') |
|
244 |
self.assertEquals(rset.rows, []) |
|
245 |
self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x': eid}, 'x') |
|
321
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
246 |
# cache test |
0 | 247 |
self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x': eid}, 'x') |
248 |
aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
|
249 |
soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0] |
|
250 |
cu.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
251 |
cnx.commit() |
|
252 |
rset = cu.execute('Any X WHERE X eid %(x)s', {'x': aff2}, 'x') |
|
253 |
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
|
254 |
# more cache test w/ NOT eid |
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
255 |
rset = cu.execute('Affaire X WHERE NOT X eid %(x)s', {'x': eid}, 'x') |
389 | 256 |
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
|
257 |
rset = cu.execute('Affaire X WHERE NOT X eid %(x)s', {'x': aff2}, 'x') |
247947250382
fix security bug w/ query using 'NOT X eid 123'
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
258 |
self.assertEquals(rset.rows, []) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
259 |
|
0 | 260 |
def test_read_erqlexpr_has_text1(self): |
261 |
aff1 = self.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
|
262 |
card1 = self.execute("INSERT Card X: X title 'cool'")[0][0] |
|
263 |
self.execute('SET X owned_by U WHERE X eid %(x)s, U login "iaminusersgrouponly"', {'x': card1}, 'x') |
|
264 |
self.commit() |
|
265 |
cnx = self.login('iaminusersgrouponly') |
|
266 |
cu = cnx.cursor() |
|
267 |
aff2 = cu.execute("INSERT Affaire X: X sujet 'cool', X in_state S WHERE S name 'pitetre'")[0][0] |
|
268 |
soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0] |
|
269 |
cu.execute("SET A concerne S WHERE A eid %(a)s, S eid %(s)s", {'a': aff2, 's': soc1}, |
|
270 |
('a', 's')) |
|
271 |
cnx.commit() |
|
272 |
self.assertRaises(Unauthorized, cu.execute, 'Any X WHERE X eid %(x)s', {'x':aff1}, 'x') |
|
273 |
self.failUnless(cu.execute('Any X WHERE X eid %(x)s', {'x':aff2}, 'x')) |
|
274 |
self.failUnless(cu.execute('Any X WHERE X eid %(x)s', {'x':card1}, 'x')) |
|
275 |
rset = cu.execute("Any X WHERE X has_text 'cool'") |
|
276 |
self.assertEquals(sorted(eid for eid, in rset.rows), |
|
277 |
[card1, aff2]) |
|
278 |
||
279 |
def test_read_erqlexpr_has_text2(self): |
|
280 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
281 |
self.execute("INSERT Societe X: X nom 'bidule'") |
|
282 |
self.commit() |
|
283 |
self.schema['Personne'].set_groups('read', ('managers',)) |
|
284 |
cnx = self.login('iaminusersgrouponly') |
|
285 |
cu = cnx.cursor() |
|
286 |
rset = cu.execute('Any N WHERE N has_text "bidule"') |
|
287 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
288 |
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
|
289 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
0 | 290 |
|
291 |
def test_read_erqlexpr_optional_rel(self): |
|
292 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
293 |
self.execute("INSERT Societe X: X nom 'bidule'") |
|
294 |
self.commit() |
|
295 |
self.schema['Personne'].set_groups('read', ('managers',)) |
|
296 |
cnx = self.login('anon') |
|
297 |
cu = cnx.cursor() |
|
298 |
rset = cu.execute('Any N,U WHERE N has_text "bidule", N owned_by U?') |
|
299 |
self.assertEquals(len(rset.rows), 1, rset.rows) |
|
300 |
||
301 |
def test_read_erqlexpr_aggregat(self): |
|
302 |
self.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
|
303 |
self.commit() |
|
304 |
cnx = self.login('iaminusersgrouponly') |
|
305 |
cu = cnx.cursor() |
|
306 |
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
|
307 |
self.assertEquals(rset.rows, [[0]]) |
0 | 308 |
aff2 = cu.execute("INSERT Affaire X: X sujet 'cool'")[0][0] |
309 |
soc1 = cu.execute("INSERT Societe X: X nom 'chouette'")[0][0] |
|
310 |
cu.execute("SET A concerne S WHERE A is Affaire, S is Societe") |
|
311 |
cnx.commit() |
|
312 |
rset = cu.execute('Any COUNT(X) WHERE X is Affaire') |
|
313 |
self.assertEquals(rset.rows, [[1]]) |
|
314 |
rset = cu.execute('Any ETN, COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN') |
|
315 |
values = dict(rset) |
|
316 |
self.assertEquals(values['Affaire'], 1) |
|
317 |
self.assertEquals(values['Societe'], 2) |
|
318 |
rset = cu.execute('Any ETN, COUNT(X) GROUPBY ETN WHERE X is ET, ET name ETN WITH X BEING ((Affaire X) UNION (Societe X))') |
|
319 |
self.assertEquals(len(rset), 2) |
|
320 |
values = dict(rset) |
|
321 |
self.assertEquals(values['Affaire'], 1) |
|
322 |
self.assertEquals(values['Societe'], 2) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
323 |
|
0 | 324 |
|
325 |
def test_attribute_security(self): |
|
326 |
# only managers should be able to edit the 'test' attribute of Personne entities |
|
327 |
eid = self.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test TRUE")[0][0] |
|
328 |
self.commit() |
|
329 |
self.execute('SET X test FALSE WHERE X eid %(x)s', {'x': eid}, 'x') |
|
330 |
self.commit() |
|
331 |
cnx = self.login('iaminusersgrouponly') |
|
332 |
cu = cnx.cursor() |
|
333 |
cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test TRUE") |
|
334 |
self.assertRaises(Unauthorized, cnx.commit) |
|
335 |
cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org', X test FALSE") |
|
336 |
self.assertRaises(Unauthorized, cnx.commit) |
|
337 |
eid = cu.execute("INSERT Personne X: X nom 'bidule', X web 'http://www.debian.org'")[0][0] |
|
338 |
cnx.commit() |
|
339 |
cu.execute('SET X test FALSE WHERE X eid %(x)s', {'x': eid}, 'x') |
|
340 |
self.assertRaises(Unauthorized, cnx.commit) |
|
341 |
cu.execute('SET X test TRUE WHERE X eid %(x)s', {'x': eid}, 'x') |
|
342 |
self.assertRaises(Unauthorized, cnx.commit) |
|
343 |
cu.execute('SET X web "http://www.logilab.org" WHERE X eid %(x)s', {'x': eid}, 'x') |
|
344 |
cnx.commit() |
|
345 |
cnx.close() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
346 |
|
0 | 347 |
def test_attribute_security_rqlexpr(self): |
348 |
# Note.para attribute editable by managers or if the note is in "todo" state |
|
349 |
eid = self.execute("INSERT Note X: X para 'bidule', X in_state S WHERE S name 'done'")[0][0] |
|
350 |
self.commit() |
|
351 |
self.execute('SET X para "truc" WHERE X eid %(x)s', {'x': eid}, 'x') |
|
352 |
self.commit() |
|
353 |
cnx = self.login('iaminusersgrouponly') |
|
354 |
cu = cnx.cursor() |
|
355 |
cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': eid}, 'x') |
|
356 |
self.assertRaises(Unauthorized, cnx.commit) |
|
357 |
eid2 = cu.execute("INSERT Note X: X para 'bidule'")[0][0] |
|
358 |
cnx.commit() |
|
359 |
cu.execute("SET X in_state S WHERE X eid %(x)s, S name 'done'", {'x': eid2}, 'x') |
|
360 |
cnx.commit() |
|
361 |
self.assertEquals(len(cu.execute('Any X WHERE X in_state S, S name "todo", X eid %(x)s', {'x': eid2}, 'x')), |
|
362 |
0) |
|
363 |
cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': eid2}, 'x') |
|
364 |
self.assertRaises(Unauthorized, cnx.commit) |
|
365 |
cu.execute("SET X in_state S WHERE X eid %(x)s, S name 'todo'", {'x': eid2}, 'x') |
|
366 |
cnx.commit() |
|
367 |
cu.execute("SET X para 'chouette' WHERE X eid %(x)s", {'x': eid2}, 'x') |
|
368 |
cnx.commit() |
|
369 |
||
370 |
def test_attribute_read_security(self): |
|
371 |
# anon not allowed to see users'login, but they can see users |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
372 |
self.repo.schema['CWUser'].set_groups('read', ('guests', 'users', 'managers')) |
0 | 373 |
self.repo.schema['login'].set_groups('read', ('users', 'managers')) |
374 |
cnx = self.login('anon') |
|
375 |
cu = cnx.cursor() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
376 |
rset = cu.execute('CWUser X') |
0 | 377 |
self.failUnless(rset) |
378 |
x = rset.get_entity(0, 0) |
|
379 |
self.assertEquals(x.login, None) |
|
380 |
self.failUnless(x.creation_date) |
|
381 |
x = rset.get_entity(1, 0) |
|
382 |
x.complete() |
|
383 |
self.assertEquals(x.login, None) |
|
384 |
self.failUnless(x.creation_date) |
|
385 |
cnx.rollback() |
|
386 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
387 |
|
0 | 388 |
class BaseSchemaSecurityTC(BaseSecurityTC): |
389 |
"""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
|
390 |
|
0 | 391 |
def test_user_can_delete_object_he_created(self): |
392 |
# even if some other user have changed object'state |
|
393 |
cnx = self.login('iaminusersgrouponly') |
|
394 |
cu = cnx.cursor() |
|
395 |
# due to security test, affaire has to concerne a societe the user owns |
|
396 |
cu.execute('INSERT Societe X: X nom "ARCTIA"') |
|
397 |
cu.execute('INSERT Affaire X: X ref "ARCT01", X concerne S WHERE S nom "ARCTIA"') |
|
398 |
cnx.commit() |
|
399 |
self.restore_connection() |
|
400 |
self.execute('SET X in_state S WHERE X ref "ARCT01", S name "ben non"') |
|
401 |
self.commit() |
|
402 |
self.assertEquals(len(self.execute('TrInfo X WHERE X wf_info_for A, A ref "ARCT01"')), |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
403 |
2) |
0 | 404 |
self.assertEquals(len(self.execute('TrInfo X WHERE X wf_info_for A, A ref "ARCT01",' |
405 |
'X owned_by U, U login "admin"')), |
|
406 |
1) # TrInfo at the above state change |
|
407 |
self.assertEquals(len(self.execute('TrInfo X WHERE X wf_info_for A, A ref "ARCT01",' |
|
408 |
'X owned_by U, U login "iaminusersgrouponly"')), |
|
409 |
1) # TrInfo created at creation time |
|
410 |
cnx = self.login('iaminusersgrouponly') |
|
411 |
cu = cnx.cursor() |
|
412 |
cu.execute('DELETE Affaire X WHERE X ref "ARCT01"') |
|
413 |
cnx.commit() |
|
414 |
self.failIf(cu.execute('Affaire X')) |
|
415 |
||
416 |
def test_users_and_groups_non_readable_by_guests(self): |
|
417 |
cnx = self.login('anon') |
|
418 |
anon = cnx.user(self.current_session()) |
|
419 |
cu = cnx.cursor() |
|
420 |
# anonymous user can only read itself |
|
421 |
rset = cu.execute('Any L WHERE X owned_by U, U login L') |
|
422 |
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
|
423 |
rset = cu.execute('CWUser X') |
0 | 424 |
self.assertEquals(rset.rows, [[anon.eid]]) |
425 |
# 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
|
426 |
self.assert_(cu.execute('CWGroup X')) |
0 | 427 |
# should only be able to read the anonymous user, not another one |
428 |
origuser = self.session.user |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
429 |
self.assertRaises(Unauthorized, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
430 |
cu.execute, 'CWUser X WHERE X eid %(x)s', {'x': origuser.eid}, 'x') |
0 | 431 |
# nothing selected, nothing updated, no exception raised |
432 |
#self.assertRaises(Unauthorized, |
|
433 |
# cu.execute, 'SET X login "toto" WHERE X eid %(x)s', |
|
434 |
# {'x': self.user.eid}) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
435 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
389
diff
changeset
|
436 |
rset = cu.execute('CWUser X WHERE X eid %(x)s', {'x': anon.eid}, 'x') |
0 | 437 |
self.assertEquals(rset.rows, [[anon.eid]]) |
438 |
# but can't modify it |
|
439 |
cu.execute('SET X login "toto" WHERE X eid %(x)s', {'x': anon.eid}) |
|
440 |
self.assertRaises(Unauthorized, cnx.commit) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
441 |
|
0 | 442 |
def test_in_group_relation(self): |
443 |
cnx = self.login('iaminusersgrouponly') |
|
444 |
cu = cnx.cursor() |
|
445 |
rql = u"DELETE U in_group G WHERE U login 'admin'" |
|
446 |
self.assertRaises(Unauthorized, cu.execute, rql) |
|
447 |
rql = u"SET U in_group G WHERE U login 'admin', G name 'users'" |
|
448 |
self.assertRaises(Unauthorized, cu.execute, rql) |
|
449 |
||
450 |
def test_owned_by(self): |
|
451 |
self.execute("INSERT Personne X: X nom 'bidule'") |
|
452 |
self.commit() |
|
453 |
cnx = self.login('iaminusersgrouponly') |
|
454 |
cu = cnx.cursor() |
|
455 |
rql = u"SET X owned_by U WHERE U login 'iaminusersgrouponly', X is Personne" |
|
456 |
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
|
457 |
|
0 | 458 |
def test_bookmarked_by_guests_security(self): |
459 |
beid1 = self.execute('INSERT Bookmark B: B path "?vid=manage", B title "manage"')[0][0] |
|
460 |
beid2 = self.execute('INSERT Bookmark B: B path "?vid=index", B title "index", B bookmarked_by U WHERE U login "anon"')[0][0] |
|
461 |
self.commit() |
|
462 |
cnx = self.login('anon') |
|
463 |
cu = cnx.cursor() |
|
464 |
anoneid = self.current_session().user.eid |
|
465 |
self.assertEquals(cu.execute('Any T,P ORDERBY lower(T) WHERE B is Bookmark,B title T,B path P,' |
|
466 |
'B bookmarked_by U, U eid %s' % anoneid).rows, |
|
467 |
[['index', '?vid=index']]) |
|
468 |
self.assertEquals(cu.execute('Any T,P ORDERBY lower(T) WHERE B is Bookmark,B title T,B path P,' |
|
469 |
'B bookmarked_by U, U eid %(x)s', {'x': anoneid}).rows, |
|
470 |
[['index', '?vid=index']]) |
|
471 |
# can read others bookmarks as well |
|
472 |
self.assertEquals(cu.execute('Any B where B is Bookmark, NOT B bookmarked_by U').rows, |
|
473 |
[[beid1]]) |
|
474 |
self.assertRaises(Unauthorized, cu.execute,'DELETE B bookmarked_by U') |
|
475 |
self.assertRaises(Unauthorized, |
|
476 |
cu.execute, 'SET B bookmarked_by U WHERE U eid %(x)s, B eid %(b)s', |
|
477 |
{'x': anoneid, 'b': beid1}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
478 |
|
0 | 479 |
|
480 |
def test_ambigous_ordered(self): |
|
481 |
cnx = self.login('anon') |
|
482 |
cu = cnx.cursor() |
|
483 |
names = [t for t, in cu.execute('Any N ORDERBY lower(N) WHERE X name N')] |
|
484 |
self.assertEquals(names, sorted(names, key=lambda x: x.lower())) |
|
485 |
||
486 |
def test_in_state_without_update_perm(self): |
|
487 |
"""check a user change in_state without having update permission on the |
|
488 |
subject |
|
489 |
""" |
|
490 |
eid = self.execute('INSERT Affaire X: X ref "ARCT01"')[0][0] |
|
491 |
self.commit() |
|
492 |
cnx = self.login('iaminusersgrouponly') |
|
493 |
session = self.current_session() |
|
494 |
# needed to avoid check_perm error |
|
495 |
session.set_pool() |
|
496 |
# needed to remove rql expr granting update perm to the user |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
497 |
self.schema['Affaire'].set_rqlexprs('update', ()) |
0 | 498 |
self.assertRaises(Unauthorized, |
499 |
self.schema['Affaire'].check_perm, session, 'update', eid) |
|
500 |
cu = cnx.cursor() |
|
501 |
cu.execute('SET X in_state S WHERE X ref "ARCT01", S name "abort"') |
|
502 |
cnx.commit() |
|
503 |
# though changing a user state (even logged user) is reserved to managers |
|
504 |
rql = u"SET X in_state S WHERE X eid %(x)s, S name 'deactivated'" |
|
505 |
# XXX wether it should raise Unauthorized or ValidationError is not clear |
|
506 |
# the best would probably ValidationError if the transition doesn't exist |
|
507 |
# from the current state but Unauthorized if it exists but user can't pass it |
|
508 |
self.assertRaises(ValidationError, cu.execute, rql, {'x': cnx.user(self.current_session()).eid}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
509 |
|
0 | 510 |
if __name__ == '__main__': |
511 |
unittest_main() |