diff test/test_admin.py @ 7204:ccb0e566e0be

Add missing space in message; add tests; update .po Testing for updateconfig updating password_pbkdf2_default_rounds. Added missing space in one message and updated locale files as a result.
author John Rouillard <rouilj@ieee.org>
date Wed, 01 Mar 2023 12:56:36 -0500
parents 0c6617db0b97
children 1a3d4703c7d6
line wrap: on
line diff
--- a/test/test_admin.py	Wed Mar 01 10:51:37 2023 -0500
+++ b/test/test_admin.py	Wed Mar 01 12:56:36 2023 -0500
@@ -5,6 +5,7 @@
 #
 
 from __future__ import print_function
+import fileinput
 import unittest, os, shutil, errno, sys, difflib, cgi, re
 
 from roundup.admin import AdminTool
@@ -45,6 +46,28 @@
                     f.write(i)
         f.truncate()
 
+def replace_in_file(filename, original, replacement):
+    """replace text in a file. All occurances of original
+       will be replaced by replacement"""
+
+    for line in fileinput.input(filename, inplace = 1): 
+        print(line.replace(original, replacement))
+
+    fileinput.close()
+
+def find_in_file(filename, regexp):
+    """search for regexp in file.
+       If not found return false. If found return match.
+    """
+    with open(filename) as f:
+        contents = f.read()
+
+    m = re.search(regexp, contents, re.MULTILINE)
+
+    if not m: return False
+
+    return m[0]
+
 class AdminTest(object):
 
     backend = None
@@ -396,6 +419,100 @@
         self.assertTrue(filecmp.cmp(self.dirname + "/config.ini",
                                     self.dirname + "/foo2.ini"))
 
+    def testUpdateconfigPbkdf2(self):
+        ''' Note the tests will fail if you run this under pdb.
+            the context managers capture the pdb prompts and this screws
+            up the stdout strings with (pdb) prefixed to the line.
+        '''
+        import filecmp
+
+        self.admin=AdminTool()
+        self.install_init()
+
+        with captured_output() as (out, err):
+            sys.argv=['main', '-i', self.dirname, 'updateconfig',
+                      self.dirname + "/config2.ini"]
+            ret = self.admin.main()
+
+        out = out.getvalue().strip()
+        print(out)
+        self.assertEqual(out, "")
+        self.assertTrue(os.path.isfile(self.dirname + "/config2.ini"))
+        # Autogenerated date header is different. Remove it
+        # so filecmp passes.
+        normalize_file(self.dirname + "/config2.ini",
+                       [ '# Autogenerated at' ])
+        normalize_file(self.dirname + "/config.ini",
+                       [ '# Autogenerated at' ])
+
+        self.assertTrue(filecmp.cmp(self.dirname + "/config.ini",
+                                    self.dirname + "/config2.ini"))
+
+        # Reopen the db closed by previous call
+        self.admin=AdminTool()
+
+        ### test replacement of old default value
+        replace_in_file(self.dirname + "/config.ini",
+                        "= 2000000", "= 10000")
+        with captured_output() as (out, err):
+            sys.argv=['main', '-i', self.dirname, 'update',
+                      self.dirname + "/config2.ini"]
+            ret = self.admin.main()
+
+        out = out.getvalue().strip()
+        print(out)
+        expected = "from old default of 10000 to new default of 2000000."
+
+        self.assertIn(expected, out)
+        self.assertTrue(os.path.isfile(self.dirname + "/config2.ini"))
+        self.assertEqual(find_in_file(self.dirname + "/config2.ini",
+                                     "^password_.*= 2000000$"),
+                         "password_pbkdf2_default_rounds = 2000000")
+
+        # Reopen the db closed by previous call
+        self.admin=AdminTool()
+
+        ### test replacement of too small value
+        replace_in_file(self.dirname + "/config.ini",
+                        "= 10000", "= 10001")
+        with captured_output() as (out, err):
+            sys.argv=['main', '-i', self.dirname, 'update',
+                      self.dirname + "/config2.ini"]
+            ret = self.admin.main()
+
+        out = out.getvalue().strip()
+        print(out)
+        expected = ("Update 'password_pbkdf2_default_rounds' to a number "
+                    "equal to or larger\nthan 2000000.")
+
+        self.assertIn(expected, out)
+        self.assertTrue(os.path.isfile(self.dirname + "/config2.ini"))
+        self.assertEqual(find_in_file(self.dirname + "/config2.ini",
+                                     "^password_.*= 10001$"),
+                         "password_pbkdf2_default_rounds = 10001")
+
+
+        # Reopen the db closed by previous call
+        self.admin=AdminTool()
+
+        ### test no action if value is large enough
+        replace_in_file(self.dirname + "/config.ini",
+                        "= 10001", "= 2000001")
+        with captured_output() as (out, err):
+            sys.argv=['main', '-i', self.dirname, 'update',
+                      self.dirname + "/config2.ini"]
+            ret = self.admin.main()
+
+        out = out.getvalue().strip()
+        print(out)
+        expected = ""
+
+        self.assertEqual(expected, out)
+        self.assertTrue(os.path.isfile(self.dirname + "/config2.ini"))
+        self.assertEqual(find_in_file(self.dirname + "/config2.ini",
+                                     "^password_.*= 2000001$"),
+                         "password_pbkdf2_default_rounds = 2000001")
+
 
     def testCliParse(self):
         ''' Note the tests will fail if you run this under pdb.

Roundup Issue Tracker: http://roundup-tracker.org/