Browse Source

Add more testing for the factory and config

oz123 10 years ago
parent
commit
2e323f3fed
4 changed files with 44 additions and 15 deletions
  1. 5 10
      pwman/__init__.py
  2. 9 4
      pwman/data/factory.py
  3. 20 1
      pwman/tests/db_tests.py
  4. 10 0
      pwman/tests/test_converter.py

+ 5 - 10
pwman/__init__.py

@@ -137,16 +137,11 @@ def set_win_colors(config):  # pragma: no cover
 
 
 def set_umask(config):
-    # set umask before creating/opening any files
-    try:
-        umask = config.get_value("Global", "umask")
-        if re.search(r'^\d{4}$', umask):
-            os.umask(int(umask))
-        else:
-            raise ValueError
-    except ValueError:
-        print("Could not determine umask from config!")
-        sys.exit(2)
+    umask = config.get_value("Global", "umask")
+    if re.search(r'^\d{4}$', umask):
+        os.umask(int(umask))
+    else:
+        raise config.ConfigException("Could not determine umask from config!")
 
 
 def set_db(args):

+ 9 - 4
pwman/data/factory.py

@@ -34,6 +34,13 @@ from pwman.data.database import DatabaseException
 from pwman.data.drivers import sqlite
 #from pwman.data.drivers import osqlite
 
+class FactoryException(Exception):
+
+    def __init__(self, message):
+        self.message
+
+    def __str__(self):
+        return self.message
 
 def check_db_version(type):
     if type == "SQLite":
@@ -42,7 +49,7 @@ def check_db_version(type):
             return float(ver.strip("\'"))
         except ValueError:
             return 0.3
-     # TODO: implement version checks for other supported DBs.
+    # TODO: implement version checks for other supported DBs.
 
 
 def create(dbtype, version=None, filename=None):
@@ -53,12 +60,10 @@ def create(dbtype, version=None, filename=None):
     """
     if dbtype == "SQLite":
         from pwman.data.drivers import sqlite
-        if version >= 0.4 and filename:
+        if filename:
             db = sqlite.SQLiteDatabaseNewForm(filename)
         elif version >= 0.4:
             db = sqlite.SQLiteDatabaseNewForm()
-        else:
-            db = None
     elif dbtype == "Postgresql":  # pragma: no cover
         try:
             from pwman.data.drivers import postgresql

+ 20 - 1
pwman/tests/db_tests.py

@@ -28,7 +28,7 @@ from pwman import default_config, set_xsel
 from pwman.ui import get_ui_platform
 from pwman.ui.base import get_pass_conf
 from pwman.ui.tools import CMDLoop, CliMenuItem
-from pwman import (parser_options, get_conf_options, get_conf_file)
+from pwman import (parser_options, get_conf_options, get_conf_file, set_umask)
 from pwman.data.database import __DB_FORMAT__
 from pwman.ui.mac import PwmanCliMacNew
 from pwman.ui.win import PwmanCliWinNew
@@ -379,6 +379,14 @@ class FactoryTest(unittest.TestCase):
         self.assertEquals(factory.check_db_version('SQLite'), 0.3)
         factory.sqlite = orig_sqlite
 
+    def test_factory_create(self):
+        db = factory.create('SQLite', filename='foo.db')
+        db._open()
+        self.assertTrue(os.path.exists('foo.db'))
+        os.unlink('foo.db')
+        self.assertIsInstance(db, SQLiteDatabaseNewForm)
+        self.assertRaises(DatabaseException, factory.create, 'UNKNOWN')
+
 
 class ConfigTest(unittest.TestCase):
 
@@ -483,5 +491,16 @@ class ConfigTest(unittest.TestCase):
         xsel, dbtype = get_conf_options(args, 'True')
         self.assertEqual(dbtype, 'SQLite')
 
+    def test_set_conf(self):
+        set_conf_f = getattr(config,'set_conf')
+        private_conf = getattr(config, '_conf')
+        set_conf_f({'Config':'OK'})
+        self.assertDictEqual({'Config':'OK'}, config._conf)
+        config._conf = private_conf
+
+    def test_umask(self):
+        config._defaults = {'Global': {}}
+        self.assertRaises(config.ConfigException, set_umask, config)
+
     def tearDown(self):
         config._conf = self.orig_config.copy()

+ 10 - 0
pwman/tests/test_converter.py

@@ -35,6 +35,7 @@ from pwman import default_config
 import cPickle
 from test_tools import SetupTester
 
+
 class SQLiteDatabase(Database):
     """SQLite Database implementation"""
 
@@ -337,6 +338,15 @@ class CreateTestDataBases(object):
 
     def run(self):
         self.open_dbs()
+        # before add nodes to db1 we have to create an encryption key!
+        #enc = CryptoEngine.get(dbver=dbver)
+        #key = db.loadkey()
+        #if key is not None:
+        #enc.set_cryptedkey(key)
+
+
+        #else:
+        #    self.get_user_password()
         self.add_nodes_to_db1()