Skip to content

Commit 6fc6e6b

Browse files
committed
flake8 fixes
Test some unused variables, formatting fixes.
1 parent 9ebe9ff commit 6fc6e6b

File tree

1 file changed

+71
-60
lines changed

1 file changed

+71
-60
lines changed

test/test_security.py

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
# SOFTWARE.
2020

2121
from __future__ import print_function
22-
import os, unittest, shutil
22+
import os
23+
import shutil
24+
import unittest
2325

2426
from roundup import backends
2527
import roundup.password
@@ -41,19 +43,23 @@ def testInterfaceSecurity(self):
4143
# TODO: some asserts
4244

4345
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")
4649
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")
4953
self.db.security.addPermissionToRole('User', ai)
5054

5155
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")
5459
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")
5763
self.db.security.addPermissionToRole('Admin', ei)
5864

5965
u1 = self.db.user.create(username='one', roles='Admin')
@@ -62,13 +68,12 @@ def testAdmin(self):
6268
self.assertTrue(self.db.security.hasPermission('Edit', u1, None))
6369
self.assertTrue(not self.db.security.hasPermission('Edit', u2, None))
6470

65-
6671
def testGetPermission(self):
6772
self.db.security.getPermission('Edit')
6873
self.db.security.getPermission('View')
6974
self.assertRaises(ValueError, self.db.security.getPermission, 'x')
7075
self.assertRaises(ValueError, self.db.security.getPermission, 'Edit',
71-
'fubar')
76+
'fubar')
7277

7378
add = self.db.security.addPermission
7479
get = self.db.security.getPermission
@@ -83,9 +88,11 @@ def testGetPermission(self):
8388
epi1 = add(name="Edit", klass="issue", properties=['title'])
8489
self.assertEqual(get('Edit', 'issue', properties=['title']), epi1)
8590
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)
8996
self.db.security.set_props_only_default(True)
9097
self.assertEqual(get('Edit', 'issue', properties=['title']), epi2)
9198
api1 = add(name="View", klass="issue", properties=['title'])
@@ -94,44 +101,44 @@ def testGetPermission(self):
94101
api2 = add(name="View", klass="issue", properties=['title'])
95102
self.assertEqual(get('View', 'issue', properties=['title']), api2)
96103
self.assertNotEqual(get('View', 'issue', properties=['title']), api1)
97-
104+
98105
# check function
99106
dummy = lambda: 0
100107
eci = add(name="Edit", klass="issue", check=dummy)
101108
self.assertEqual(get('Edit', 'issue', check=dummy), eci)
102109
# props_only only makes sense if you are setting props.
103110
# make it a no-op unless properties is set.
104111
self.assertEqual(get('Edit', 'issue', check=dummy,
105-
props_only=True), eci)
112+
props_only=True), eci)
106113
aci = add(name="View", klass="issue", check=dummy)
107114
self.assertEqual(get('View', 'issue', check=dummy), aci)
108115

109116
# all
110117
epci = add(name="Edit", klass="issue", properties=['title'],
111-
check=dummy)
118+
check=dummy)
112119

113120
self.db.security.set_props_only_default(False)
114121
# implicit props_only=False
115122
self.assertEqual(get('Edit', 'issue', properties=['title'],
116-
check=dummy), epci)
123+
check=dummy), epci)
117124
# explicit props_only=False
118125
self.assertEqual(get('Edit', 'issue', properties=['title'],
119-
check=dummy, props_only=False), epci)
126+
check=dummy, props_only=False), epci)
120127

121128
# implicit props_only=True
122129
self.db.security.set_props_only_default(True)
123130
self.assertRaises(ValueError, get, 'Edit', 'issue',
124-
properties=['title'],
125-
check=dummy)
131+
properties=['title'],
132+
check=dummy)
126133
# explicit props_only=False
127134
self.assertRaises(ValueError, get, 'Edit', 'issue',
128-
properties=['title'],
129-
check=dummy, props_only=True)
135+
properties=['title'],
136+
check=dummy, props_only=True)
130137

131138
apci = add(name="View", klass="issue", properties=['title'],
132-
check=dummy)
139+
check=dummy)
133140
self.assertEqual(get('View', 'issue', properties=['title'],
134-
check=dummy), apci)
141+
check=dummy), apci)
135142

136143
# Reset to default. Somehow this setting looks like it
137144
# was bleeding through to other tests in test_xmlrpc.
@@ -165,18 +172,19 @@ def testAccessControls(self):
165172

166173
# property
167174
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']))
169177
user2 = self.db.user.create(username='user2', roles='Role2')
170178

171179
# check function
172180
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):
174182
# print "checking userid, itemid: %r"%((userid,itemid),)
175183
# return(itemid == '2')
176184

177185
# setup to check function new style. Make sure that
178186
# other args are passed.
179-
def check(db,userid,itemid, **other):
187+
def check(db, userid, itemid, **other):
180188
prop = other['property']
181189
prop = other['classname']
182190
prop = other['permission']
@@ -185,7 +193,7 @@ def check(db,userid,itemid, **other):
185193
# also create a check as a callable of a class
186194
# https://issues.roundup-tracker.org/issue2550952
187195
class CheckClass(object):
188-
def __call__(self, db,userid,itemid, **other):
196+
def __call__(self, db, userid, itemid, **other):
189197
prop = other['property']
190198
prop = other['classname']
191199
prop = other['permission']
@@ -240,7 +248,6 @@ def __call__(self, db,userid,itemid, **other):
240248
self.assertEqual(has('Test', user7, 'test'), 1)
241249
self.assertEqual(has('Test', none, 'test'), 0)
242250

243-
244251
# *any* access to item
245252
self.assertEqual(has('Test', user1, 'test', itemid='1'), 1)
246253
self.assertEqual(has('Test', user2, 'test', itemid='1'), 1)
@@ -313,48 +320,48 @@ def __call__(self, db,userid,itemid, **other):
313320
# now mix property and check commands
314321
# check is old style props_only = false
315322
self.assertEqual(has('Test', user7, 'test', property="c",
316-
itemid='2'), 0)
323+
itemid='2'), 0)
317324
self.assertEqual(has('Test', user7, 'test', property="c",
318-
itemid='1'), 0)
325+
itemid='1'), 0)
319326

320327
self.assertEqual(has('Test', user7, 'test', property="a",
321-
itemid='2'), 1)
328+
itemid='2'), 1)
322329
self.assertEqual(has('Test', user7, 'test', property="a",
323-
itemid='1'), 0)
330+
itemid='1'), 0)
324331

325332
# check is new style props_only = false
326333
self.assertEqual(has('Test', user6, 'test', itemid='2',
327-
property='c'), 0)
334+
property='c'), 0)
328335
self.assertEqual(has('Test', user6, 'test', itemid='1',
329-
property='c'), 0)
336+
property='c'), 0)
330337
self.assertEqual(has('Test', user6, 'test', itemid='2',
331-
property='b'), 0)
338+
property='b'), 0)
332339
self.assertEqual(has('Test', user6, 'test', itemid='1',
333-
property='b'), 1)
340+
property='b'), 1)
334341
self.assertEqual(has('Test', user6, 'test', itemid='2',
335-
property='a'), 0)
342+
property='a'), 0)
336343
self.assertEqual(has('Test', user6, 'test', itemid='1',
337-
property='a'), 1)
344+
property='a'), 1)
338345

339346
# check is old style props_only = true
340347
self.assertEqual(has('Test', user5, 'test', itemid='2',
341-
property='b'), 0)
348+
property='b'), 0)
342349
self.assertEqual(has('Test', user5, 'test', itemid='1',
343-
property='b'), 0)
350+
property='b'), 0)
344351
self.assertEqual(has('Test', user5, 'test', itemid='2',
345-
property='a'), 1)
352+
property='a'), 1)
346353
self.assertEqual(has('Test', user5, 'test', itemid='1',
347-
property='a'), 0)
354+
property='a'), 0)
348355

349356
# check is new style props_only = true
350357
self.assertEqual(has('Test', user4, 'test', itemid='2',
351-
property='b'), 0)
358+
property='b'), 0)
352359
self.assertEqual(has('Test', user4, 'test', itemid='1',
353-
property='b'), 0)
360+
property='b'), 0)
354361
self.assertEqual(has('Test', user4, 'test', itemid='2',
355-
property='a'), 0)
362+
property='a'), 0)
356363
self.assertEqual(has('Test', user4, 'test', itemid='1',
357-
property='a'), 1)
364+
property='a'), 1)
358365

359366
def testTransitiveSearchPermissions(self):
360367
add = self.db.security.addPermission
@@ -420,6 +427,8 @@ def test_password(self):
420427
roundup.password.crypt = None
421428
with self.assertRaises(roundup.password.PasswordValueError) as ctx:
422429
roundup.password.test_missing_crypt()
430+
self.assertEqual(ctx.exception.args[0],
431+
"Unsupported encryption scheme 'crypt'")
423432
roundup.password.crypt = orig_crypt
424433

425434
def test_pbkdf2_unpack_errors(self):
@@ -428,13 +437,13 @@ def test_pbkdf2_unpack_errors(self):
428437
with self.assertRaises(roundup.password.PasswordValueError) as ctx:
429438
pbkdf2_unpack("fred$password")
430439

431-
self.assertEqual(ctx.exception.args[0],
440+
self.assertEqual(ctx.exception.args[0],
432441
'invalid PBKDF2 hash (wrong number of separators)')
433442

434443
with self.assertRaises(roundup.password.PasswordValueError) as ctx:
435444
pbkdf2_unpack("0200000$salt$password")
436445

437-
self.assertEqual(ctx.exception.args[0],
446+
self.assertEqual(ctx.exception.args[0],
438447
'invalid PBKDF2 hash (zero-padded rounds)')
439448

440449
with self.assertRaises(roundup.password.PasswordValueError) as ctx:
@@ -465,7 +474,6 @@ def test_pbkdf2_migrate_rounds(self):
465474
config is larger than number of rounds in current password.
466475
'''
467476

468-
469477
p = roundup.password.Password('sekrit', 'PBKDF2',
470478
config=self.db.config)
471479

@@ -480,23 +488,23 @@ def test_encodePassword_errors(self):
480488

481489
os.environ["PYTEST_USE_CONFIG"] = "True"
482490
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)
485493

486-
self.assertEqual(ctx.exception.args[0],
494+
self.assertEqual(ctx.exception.args[0],
487495
'invalid PBKDF2 hash (rounds too low)')
488496

489497
del(os.environ["PYTEST_USE_CONFIG"])
490498

491499
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)
494502

495-
self.assertEqual(ctx.exception.args[0],
503+
self.assertEqual(ctx.exception.args[0],
496504
"Unknown encryption scheme 'fred'")
497505

498506
def test_pbkdf2_errors(self):
499-
507+
500508
with self.assertRaises(ValueError) as ctx:
501509
roundup.password.pbkdf2('sekret', b'saltandpepper', 0, 41)
502510

@@ -510,7 +518,7 @@ def test_pbkdf2_errors(self):
510518
"rounds must be positive number")
511519

512520
def test_pbkdf2_sha512_errors(self):
513-
521+
514522
with self.assertRaises(ValueError) as ctx:
515523
roundup.password.pbkdf2_sha512('sekret', b'saltandpepper', 0, 65)
516524

@@ -523,16 +531,19 @@ def test_pbkdf2_sha512_errors(self):
523531
self.assertEqual(ctx.exception.args[0],
524532
"rounds must be positive number")
525533

526-
527534
def test_encodePasswordNoConfig(self):
528535
# should run cleanly as we are in a test.
529536
#
530537
p = roundup.password.encodePassword('sekrit', 'PBKDF2')
538+
# verify 1000 rounds being used becaue we are in test mode
539+
self.assertTrue(p.startswith("1000$"))
531540

532541
del(os.environ["PYTEST_CURRENT_TEST"])
533542
self.assertNotIn("PYTEST_CURRENT_TEST", os.environ)
534543

535544
with self.assertRaises(roundup.password.ConfigNotSet) as ctx:
536545
roundup.password.encodePassword('sekrit', 'PBKDF2')
537546

547+
self.assertEqual(ctx.exception.args[0],
548+
"encodePassword called without config.")
538549
# vim: set filetype=python sts=4 sw=4 et si :

0 commit comments

Comments
 (0)