Skip to content

Commit 5cccd73

Browse files
committed
Fix round check/settings in needs_migration
Support test rounds in needs_migration Two test were missing os.environ seting to have them use config setting.
1 parent 636bad6 commit 5cccd73

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

roundup/password.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,13 @@ def needs_migration(self, config):
379379
return True
380380
if (self.scheme == "PBKDF2"):
381381
new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
382+
if ("pytest" in sys.modules and
383+
"PYTEST_CURRENT_TEST" in os.environ):
384+
if ("PYTEST_USE_CONFIG" in os.environ):
385+
new_rounds = config.PASSWORD_PBKDF2_DEFAULT_ROUNDS
386+
else:
387+
# for testing
388+
new_rounds = 1000
382389
if rounds < int(new_rounds):
383390
return True
384391
return False

test/test_cgi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,16 @@ def testPasswordMigration(self):
593593
# below will be 100000
594594
cl.db.Otk = self.db.Otk
595595
pw1 = pw
596+
# do not use the production number of PBKDF2
597+
os.environ["PYTEST_USE_CONFIG"] = "True"
596598
cl.db.config.PASSWORD_PBKDF2_DEFAULT_ROUNDS = 100000
597599
self.assertEqual(pw1.needs_migration(config=cl.db.config), True)
598600
scheme = password.Password.known_schemes[0]
599601
self.assertEqual(scheme, pw1.scheme)
600602
actions.LoginAction(cl).handle()
601603
pw = cl.db.user.get(chef, 'password')
602604
self.assertEqual(pw, 'foo')
605+
del(os.environ["PYTEST_USE_CONFIG"])
603606
# do not assert self.assertEqual(pw, pw1) as pw is a 100,000
604607
# cycle while pw1 is only 10,000. They won't compare equally.
605608

test/test_security.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,20 @@ def test_password(self):
423423
roundup.password.crypt = orig_crypt
424424

425425
def test_pbkdf2_migrate_rounds(self):
426-
self.db.config.PASSWORD_PBKDF2_DEFAULT_ROUNDS = 10000
426+
'''Check that migration happens when number of rounds in
427+
config is larger than number of rounds in current password.
428+
'''
429+
427430

428431
p = roundup.password.Password('sekrit', 'PBKDF2',
429432
config=self.db.config)
430433

431434
self.db.config.PASSWORD_PBKDF2_DEFAULT_ROUNDS = 2000000
432435

436+
os.environ["PYTEST_USE_CONFIG"] = "True"
433437
self.assertEqual(p.needs_migration(config=self.db.config), True)
438+
del(os.environ["PYTEST_USE_CONFIG"])
439+
434440

435441
def test_encodePasswordNoConfig(self):
436442
# should run cleanly as we are in a test.

0 commit comments

Comments
 (0)