Selaa lähdekoodia

add dummy call back which is past to main cli loop

this makes the testing completely hands free.
Finally ...
oz123 11 vuotta sitten
vanhempi
commit
c76a9fb193
6 muutettua tiedostoa jossa 37 lisäystä ja 27 poistoa
  1. 10 1
      pwman/data/database.py
  2. 3 16
      pwman/tests/db_tests.py
  3. 2 4
      pwman/ui/cli.py
  4. 9 0
      pwman/ui/tools.py
  5. 11 4
      pwman/util/crypto.py
  6. 2 2
      scripts/pwman3

+ 10 - 1
pwman/data/database.py

@@ -45,11 +45,20 @@ class Database(object):
         if key is not None:
             enc.set_cryptedkey(key)
         else:
-            self.changepassword()
+            #self.changepassword()
+            self.get_user_password()
 
     def close(self):
         pass
 
+    def get_user_password(self):
+        """
+        get the databases password from the user
+        """
+        enc = CryptoEngine.get()
+        newkey = enc.changepassword()
+        return self.savekey(newkey)
+
     def changepassword(self):
         """
         Change the databases password.

+ 3 - 16
pwman/tests/db_tests.py

@@ -1,7 +1,7 @@
 import os
 import os.path
 import sys
-from Crypto.Random import OSRNG
+from pwman.ui.tools import CLICallback, DummyCallback
 
 if 'darwin' in sys.platform:
     from pwman.ui.mac import PwmanCliMac as PwmanCliOld
@@ -51,20 +51,7 @@ class SetupTester(object):
         dbver = 0.4
         dbtype = config.get_value("Database", "type")
         db = pwman.data.factory.create(dbtype, dbver)
-        # TODO:
-        # use db to insert an encrypted password here to the table
-        # KEY
-        # This way the key will be passed by "SELECT THEKEY FROM KEY"
-        # and there won't be a any prompting for password
-        ce = CryptoEngine()
-        ce.set_callback(raw_input)
-        random = OSRNG.new()
-        key = str(random.read(32)).encode('base64')
-        password = '12345'
-        cipher = ce._getcipher_real(password, ce._algo)
-        cipher.encrypt(ce._preparedata(key,
-                       cipher.block_size)).encode('base64')
-        self.cli = PwmanCliNew(db, self.xselpath)
+        self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)
 
 
 class DBTests(unittest.TestCase):
@@ -175,6 +162,6 @@ class CLITests(unittest.TestCase):
 
     #TODO: implement a test for get_ids
 
-    def test_get_ids():
+    def test_get_ids(self):
         #used by do_cp or do_open
         pass

+ 2 - 4
pwman/ui/cli.py

@@ -44,7 +44,6 @@ import ast
 from pwman.ui import tools
 from pwman.ui.tools import CliMenu, CMDLoop
 from pwman.ui.tools import CliMenuItem
-from pwman.ui.tools import CLICallback
 from colorama import Fore
 from pwman.ui.base import HelpUI, BaseUI
 import getpass
@@ -932,7 +931,7 @@ class PwmanCliNew(Aliases, BaseCommands):
     """
     Inherit from the BaseCommands and Aliases
     """
-    def __init__(self, db, hasxsel):
+    def __init__(self, db, hasxsel, callback):
         """
         initialize CLI interface, set up the DB
         connecion, see if we have xsel ...
@@ -944,8 +943,7 @@ class PwmanCliNew(Aliases, BaseCommands):
         self.hasxsel = hasxsel
         try:
             enc = CryptoEngine.get()
-            #enc.set_callback(CLICallback())
-            enc._callback = CLICallback()
+            enc._callback = callback()
             self._db = db
             self._db.open()
         except Exception, e:

+ 9 - 0
pwman/ui/tools.py

@@ -383,8 +383,17 @@ class CliMenuItem(object):
 
 
 class CLICallback(Callback):
+
     def getinput(self, question):
         return raw_input(question)
 
     def getsecret(self, question):
         return getpass.getpass(question + ":")
+
+class DummyCallback(Callback):
+
+    def getinput(self, question):
+        return '12345'
+
+    def getsecret(self, question):
+        return '12345'

+ 11 - 4
pwman/util/crypto.py

@@ -221,6 +221,13 @@ class CryptoEngine(object):
         """
         return self._callback
 
+    def get_user_password():
+        "get the password from the user"
+        if self._callback is None:
+            raise CryptoNoCallbackException("No call back class has been "
+                                            "specified")
+
+
     def changepassword(self):
         """
         Creates a new key. The key itself is actually stored in
@@ -229,8 +236,8 @@ class CryptoEngine(object):
         password for the database.
         If oldKeyCrypted is none, then a new password is generated."""
         if self._callback is None:
-            raise CryptoNoCallbackException("No call back class has been \
-specified")
+            raise CryptoNoCallbackException("No call back class has been "
+                                            "specified")
         if self._keycrypted is None:
             # Generate a new key, 32 bits in length, if that's
             # too long for the Cipher, _getCipherReal will sort it out
@@ -310,8 +317,8 @@ password again")
 
         while tries < max_tries:
             try:
-                password = self._callback.getsecret("Please enter your \
-password")
+                password = self._callback.getsecret("Please enter your "
+                                                    "password")
                 tmpcipher = self._getcipher_real(password, self._algo)
                 plainkey = tmpcipher.decrypt(str(self._keycrypted).decode(
                     'base64'))

+ 2 - 2
scripts/pwman3

@@ -69,7 +69,7 @@ else:
     from pwman.ui.cli import PwmanCliNew
     OSX = False
 
-
+from pwman.ui.tools import CLICallback, DummyCallback
 import pwman.util.config as config
 import pwman.data.factory
 from pwman.data.convertdb import PwmanConvertDB
@@ -139,7 +139,7 @@ try:
 
     db = pwman.data.factory.create(dbtype, dbver)
     if dbver >= 0.4:
-        cli = PwmanCliNew(db, xselpath)
+        cli = PwmanCliNew(db, xselpath, CLICallback)
     elif dbver < 0.4:
         cli = PwmanCli(db, xselpath)
 except SystemExit, e: