19
19
# SOFTWARE.
20
20
21
21
from __future__ import print_function
22
- import os , unittest , shutil
22
+ import os
23
+ import shutil
24
+ import unittest
23
25
24
26
from roundup import backends
25
27
import roundup .password
@@ -41,19 +43,23 @@ def testInterfaceSecurity(self):
41
43
# TODO: some asserts
42
44
43
45
def testInitialiseSecurity (self ):
44
- ei = self .db .security .addPermission (name = "Edit" , klass = "issue" ,
45
- description = "User is allowed to edit issues" )
46
+ ei = self .db .security .addPermission (
47
+ name = "Edit" , klass = "issue" ,
48
+ description = "User is allowed to edit issues" )
46
49
self .db .security .addPermissionToRole ('User' , ei )
47
- ai = self .db .security .addPermission (name = "View" , klass = "issue" ,
48
- description = "User is allowed to access issues" )
50
+ ai = self .db .security .addPermission (
51
+ name = "View" , klass = "issue" ,
52
+ description = "User is allowed to access issues" )
49
53
self .db .security .addPermissionToRole ('User' , ai )
50
54
51
55
def testAdmin (self ):
52
- ei = self .db .security .addPermission (name = "Edit" , klass = "issue" ,
53
- description = "User is allowed to edit issues" )
56
+ ei = self .db .security .addPermission (
57
+ name = "Edit" , klass = "issue" ,
58
+ description = "User is allowed to edit issues" )
54
59
self .db .security .addPermissionToRole ('User' , ei )
55
- ei = self .db .security .addPermission (name = "Edit" , klass = None ,
56
- description = "User is allowed to edit issues" )
60
+ ei = self .db .security .addPermission (
61
+ name = "Edit" , klass = None ,
62
+ description = "User is allowed to edit issues" )
57
63
self .db .security .addPermissionToRole ('Admin' , ei )
58
64
59
65
u1 = self .db .user .create (username = 'one' , roles = 'Admin' )
@@ -62,13 +68,12 @@ def testAdmin(self):
62
68
self .assertTrue (self .db .security .hasPermission ('Edit' , u1 , None ))
63
69
self .assertTrue (not self .db .security .hasPermission ('Edit' , u2 , None ))
64
70
65
-
66
71
def testGetPermission (self ):
67
72
self .db .security .getPermission ('Edit' )
68
73
self .db .security .getPermission ('View' )
69
74
self .assertRaises (ValueError , self .db .security .getPermission , 'x' )
70
75
self .assertRaises (ValueError , self .db .security .getPermission , 'Edit' ,
71
- 'fubar' )
76
+ 'fubar' )
72
77
73
78
add = self .db .security .addPermission
74
79
get = self .db .security .getPermission
@@ -83,9 +88,11 @@ def testGetPermission(self):
83
88
epi1 = add (name = "Edit" , klass = "issue" , properties = ['title' ])
84
89
self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ]), epi1 )
85
90
epi2 = add (name = "Edit" , klass = "issue" , properties = ['title' ],
86
- props_only = True )
87
- self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ], props_only = False ), epi1 )
88
- self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ], props_only = True ), epi2 )
91
+ props_only = True )
92
+ self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ],
93
+ props_only = False ), epi1 )
94
+ self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ],
95
+ props_only = True ), epi2 )
89
96
self .db .security .set_props_only_default (True )
90
97
self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ]), epi2 )
91
98
api1 = add (name = "View" , klass = "issue" , properties = ['title' ])
@@ -94,44 +101,44 @@ def testGetPermission(self):
94
101
api2 = add (name = "View" , klass = "issue" , properties = ['title' ])
95
102
self .assertEqual (get ('View' , 'issue' , properties = ['title' ]), api2 )
96
103
self .assertNotEqual (get ('View' , 'issue' , properties = ['title' ]), api1 )
97
-
104
+
98
105
# check function
99
106
dummy = lambda : 0
100
107
eci = add (name = "Edit" , klass = "issue" , check = dummy )
101
108
self .assertEqual (get ('Edit' , 'issue' , check = dummy ), eci )
102
109
# props_only only makes sense if you are setting props.
103
110
# make it a no-op unless properties is set.
104
111
self .assertEqual (get ('Edit' , 'issue' , check = dummy ,
105
- props_only = True ), eci )
112
+ props_only = True ), eci )
106
113
aci = add (name = "View" , klass = "issue" , check = dummy )
107
114
self .assertEqual (get ('View' , 'issue' , check = dummy ), aci )
108
115
109
116
# all
110
117
epci = add (name = "Edit" , klass = "issue" , properties = ['title' ],
111
- check = dummy )
118
+ check = dummy )
112
119
113
120
self .db .security .set_props_only_default (False )
114
121
# implicit props_only=False
115
122
self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ],
116
- check = dummy ), epci )
123
+ check = dummy ), epci )
117
124
# explicit props_only=False
118
125
self .assertEqual (get ('Edit' , 'issue' , properties = ['title' ],
119
- check = dummy , props_only = False ), epci )
126
+ check = dummy , props_only = False ), epci )
120
127
121
128
# implicit props_only=True
122
129
self .db .security .set_props_only_default (True )
123
130
self .assertRaises (ValueError , get , 'Edit' , 'issue' ,
124
- properties = ['title' ],
125
- check = dummy )
131
+ properties = ['title' ],
132
+ check = dummy )
126
133
# explicit props_only=False
127
134
self .assertRaises (ValueError , get , 'Edit' , 'issue' ,
128
- properties = ['title' ],
129
- check = dummy , props_only = True )
135
+ properties = ['title' ],
136
+ check = dummy , props_only = True )
130
137
131
138
apci = add (name = "View" , klass = "issue" , properties = ['title' ],
132
- check = dummy )
139
+ check = dummy )
133
140
self .assertEqual (get ('View' , 'issue' , properties = ['title' ],
134
- check = dummy ), apci )
141
+ check = dummy ), apci )
135
142
136
143
# Reset to default. Somehow this setting looks like it
137
144
# was bleeding through to other tests in test_xmlrpc.
@@ -165,18 +172,19 @@ def testAccessControls(self):
165
172
166
173
# property
167
174
addRole (name = 'Role2' )
168
- addToRole ('Role2' , add (name = "Test" , klass = "test" , properties = ['a' ,'b' ]))
175
+ addToRole ('Role2' , add (name = "Test" , klass = "test" ,
176
+ properties = ['a' , 'b' ]))
169
177
user2 = self .db .user .create (username = 'user2' , roles = 'Role2' )
170
178
171
179
# check function
172
180
check_old_style = lambda db , userid , itemid : itemid == '2'
173
- #def check_old_style(db, userid, itemid):
181
+ # def check_old_style(db, userid, itemid):
174
182
# print "checking userid, itemid: %r"%((userid,itemid),)
175
183
# return(itemid == '2')
176
184
177
185
# setup to check function new style. Make sure that
178
186
# other args are passed.
179
- def check (db ,userid ,itemid , ** other ):
187
+ def check (db , userid , itemid , ** other ):
180
188
prop = other ['property' ]
181
189
prop = other ['classname' ]
182
190
prop = other ['permission' ]
@@ -185,7 +193,7 @@ def check(db,userid,itemid, **other):
185
193
# also create a check as a callable of a class
186
194
# https://issues.roundup-tracker.org/issue2550952
187
195
class CheckClass (object ):
188
- def __call__ (self , db ,userid ,itemid , ** other ):
196
+ def __call__ (self , db , userid , itemid , ** other ):
189
197
prop = other ['property' ]
190
198
prop = other ['classname' ]
191
199
prop = other ['permission' ]
@@ -240,7 +248,6 @@ def __call__(self, db,userid,itemid, **other):
240
248
self .assertEqual (has ('Test' , user7 , 'test' ), 1 )
241
249
self .assertEqual (has ('Test' , none , 'test' ), 0 )
242
250
243
-
244
251
# *any* access to item
245
252
self .assertEqual (has ('Test' , user1 , 'test' , itemid = '1' ), 1 )
246
253
self .assertEqual (has ('Test' , user2 , 'test' , itemid = '1' ), 1 )
@@ -313,48 +320,48 @@ def __call__(self, db,userid,itemid, **other):
313
320
# now mix property and check commands
314
321
# check is old style props_only = false
315
322
self .assertEqual (has ('Test' , user7 , 'test' , property = "c" ,
316
- itemid = '2' ), 0 )
323
+ itemid = '2' ), 0 )
317
324
self .assertEqual (has ('Test' , user7 , 'test' , property = "c" ,
318
- itemid = '1' ), 0 )
325
+ itemid = '1' ), 0 )
319
326
320
327
self .assertEqual (has ('Test' , user7 , 'test' , property = "a" ,
321
- itemid = '2' ), 1 )
328
+ itemid = '2' ), 1 )
322
329
self .assertEqual (has ('Test' , user7 , 'test' , property = "a" ,
323
- itemid = '1' ), 0 )
330
+ itemid = '1' ), 0 )
324
331
325
332
# check is new style props_only = false
326
333
self .assertEqual (has ('Test' , user6 , 'test' , itemid = '2' ,
327
- property = 'c' ), 0 )
334
+ property = 'c' ), 0 )
328
335
self .assertEqual (has ('Test' , user6 , 'test' , itemid = '1' ,
329
- property = 'c' ), 0 )
336
+ property = 'c' ), 0 )
330
337
self .assertEqual (has ('Test' , user6 , 'test' , itemid = '2' ,
331
- property = 'b' ), 0 )
338
+ property = 'b' ), 0 )
332
339
self .assertEqual (has ('Test' , user6 , 'test' , itemid = '1' ,
333
- property = 'b' ), 1 )
340
+ property = 'b' ), 1 )
334
341
self .assertEqual (has ('Test' , user6 , 'test' , itemid = '2' ,
335
- property = 'a' ), 0 )
342
+ property = 'a' ), 0 )
336
343
self .assertEqual (has ('Test' , user6 , 'test' , itemid = '1' ,
337
- property = 'a' ), 1 )
344
+ property = 'a' ), 1 )
338
345
339
346
# check is old style props_only = true
340
347
self .assertEqual (has ('Test' , user5 , 'test' , itemid = '2' ,
341
- property = 'b' ), 0 )
348
+ property = 'b' ), 0 )
342
349
self .assertEqual (has ('Test' , user5 , 'test' , itemid = '1' ,
343
- property = 'b' ), 0 )
350
+ property = 'b' ), 0 )
344
351
self .assertEqual (has ('Test' , user5 , 'test' , itemid = '2' ,
345
- property = 'a' ), 1 )
352
+ property = 'a' ), 1 )
346
353
self .assertEqual (has ('Test' , user5 , 'test' , itemid = '1' ,
347
- property = 'a' ), 0 )
354
+ property = 'a' ), 0 )
348
355
349
356
# check is new style props_only = true
350
357
self .assertEqual (has ('Test' , user4 , 'test' , itemid = '2' ,
351
- property = 'b' ), 0 )
358
+ property = 'b' ), 0 )
352
359
self .assertEqual (has ('Test' , user4 , 'test' , itemid = '1' ,
353
- property = 'b' ), 0 )
360
+ property = 'b' ), 0 )
354
361
self .assertEqual (has ('Test' , user4 , 'test' , itemid = '2' ,
355
- property = 'a' ), 0 )
362
+ property = 'a' ), 0 )
356
363
self .assertEqual (has ('Test' , user4 , 'test' , itemid = '1' ,
357
- property = 'a' ), 1 )
364
+ property = 'a' ), 1 )
358
365
359
366
def testTransitiveSearchPermissions (self ):
360
367
add = self .db .security .addPermission
@@ -420,6 +427,8 @@ def test_password(self):
420
427
roundup .password .crypt = None
421
428
with self .assertRaises (roundup .password .PasswordValueError ) as ctx :
422
429
roundup .password .test_missing_crypt ()
430
+ self .assertEqual (ctx .exception .args [0 ],
431
+ "Unsupported encryption scheme 'crypt'" )
423
432
roundup .password .crypt = orig_crypt
424
433
425
434
def test_pbkdf2_unpack_errors (self ):
@@ -428,13 +437,13 @@ def test_pbkdf2_unpack_errors(self):
428
437
with self .assertRaises (roundup .password .PasswordValueError ) as ctx :
429
438
pbkdf2_unpack ("fred$password" )
430
439
431
- self .assertEqual (ctx .exception .args [0 ],
440
+ self .assertEqual (ctx .exception .args [0 ],
432
441
'invalid PBKDF2 hash (wrong number of separators)' )
433
442
434
443
with self .assertRaises (roundup .password .PasswordValueError ) as ctx :
435
444
pbkdf2_unpack ("0200000$salt$password" )
436
445
437
- self .assertEqual (ctx .exception .args [0 ],
446
+ self .assertEqual (ctx .exception .args [0 ],
438
447
'invalid PBKDF2 hash (zero-padded rounds)' )
439
448
440
449
with self .assertRaises (roundup .password .PasswordValueError ) as ctx :
@@ -465,7 +474,6 @@ def test_pbkdf2_migrate_rounds(self):
465
474
config is larger than number of rounds in current password.
466
475
'''
467
476
468
-
469
477
p = roundup .password .Password ('sekrit' , 'PBKDF2' ,
470
478
config = self .db .config )
471
479
@@ -480,23 +488,23 @@ def test_encodePassword_errors(self):
480
488
481
489
os .environ ["PYTEST_USE_CONFIG" ] = "True"
482
490
with self .assertRaises (roundup .password .PasswordValueError ) as ctx :
483
- p = roundup .password .encodePassword ('sekrit' , 'PBKDF2' ,
484
- config = self .db .config )
491
+ roundup .password .encodePassword ('sekrit' , 'PBKDF2' ,
492
+ config = self .db .config )
485
493
486
- self .assertEqual (ctx .exception .args [0 ],
494
+ self .assertEqual (ctx .exception .args [0 ],
487
495
'invalid PBKDF2 hash (rounds too low)' )
488
496
489
497
del (os .environ ["PYTEST_USE_CONFIG" ])
490
498
491
499
with self .assertRaises (roundup .password .PasswordValueError ) as ctx :
492
- p = roundup .password .encodePassword ('sekrit' , 'fred' ,
493
- config = self .db .config )
500
+ roundup .password .encodePassword ('sekrit' , 'fred' ,
501
+ config = self .db .config )
494
502
495
- self .assertEqual (ctx .exception .args [0 ],
503
+ self .assertEqual (ctx .exception .args [0 ],
496
504
"Unknown encryption scheme 'fred'" )
497
505
498
506
def test_pbkdf2_errors (self ):
499
-
507
+
500
508
with self .assertRaises (ValueError ) as ctx :
501
509
roundup .password .pbkdf2 ('sekret' , b'saltandpepper' , 0 , 41 )
502
510
@@ -510,7 +518,7 @@ def test_pbkdf2_errors(self):
510
518
"rounds must be positive number" )
511
519
512
520
def test_pbkdf2_sha512_errors (self ):
513
-
521
+
514
522
with self .assertRaises (ValueError ) as ctx :
515
523
roundup .password .pbkdf2_sha512 ('sekret' , b'saltandpepper' , 0 , 65 )
516
524
@@ -523,16 +531,19 @@ def test_pbkdf2_sha512_errors(self):
523
531
self .assertEqual (ctx .exception .args [0 ],
524
532
"rounds must be positive number" )
525
533
526
-
527
534
def test_encodePasswordNoConfig (self ):
528
535
# should run cleanly as we are in a test.
529
536
#
530
537
p = roundup .password .encodePassword ('sekrit' , 'PBKDF2' )
538
+ # verify 1000 rounds being used becaue we are in test mode
539
+ self .assertTrue (p .startswith ("1000$" ))
531
540
532
541
del (os .environ ["PYTEST_CURRENT_TEST" ])
533
542
self .assertNotIn ("PYTEST_CURRENT_TEST" , os .environ )
534
543
535
544
with self .assertRaises (roundup .password .ConfigNotSet ) as ctx :
536
545
roundup .password .encodePassword ('sekrit' , 'PBKDF2' )
537
546
547
+ self .assertEqual (ctx .exception .args [0 ],
548
+ "encodePassword called without config." )
538
549
# vim: set filetype=python sts=4 sw=4 et si :
0 commit comments