Commits

Benji York committed 8ab34d6

add test and fix for error caused by the user canceling either the
dialog to open the KDE wallet or the one to allow the application access

  • Participants
  • Parent commits 93701af

Comments (0)

Files changed (2)

File keyring/backend.py

             # The user pressed "Cancel" when prompted to unlock their keyring.
             raise PasswordSetError()
 
+def open_kwallet(kwallet_module=None, qt_module=None):
+    # Allow for the injection of module-like objects for testing purposes.
+    if kwallet_module is None:
+        kwallet_module = KWallet.Wallet
+    if qt_module is None:
+        qt_module = QtGui
+
+    # KDE wants us to instantiate an application object.
+    app = qt_module.QApplication([])
+    try:
+        kwallet = kwallet_module.openWallet(
+            kwallet_module.NetworkWallet(), kwallet_module.Synchronous)
+        if kwallet is not None:
+            if not kwallet.hasFolder('Python'):
+                kwallet.createFolder('Python')
+            kwallet.setFolder('Python')
+    finally:
+        app.exit()
+
 
 kwallet = None
 try:
 except ImportError:
     kwallet = None
 else:
-    # KDE wants us to instantiate an application object.
-    app = QtGui.QApplication([])
-    kwallet = KWallet.Wallet.openWallet(
-        KWallet.Wallet.NetworkWallet(), KWallet.Wallet.Synchronous)
-    if not kwallet.hasFolder('Python'):
-        kwallet.createFolder('Python')
-    kwallet.setFolder('Python')
-    app.exit()
+    kewllet = open_kwallet()
+
 
 class KDEKWallet(KeyringBackend):
     """KDE KWallet"""

File keyring/tests/test_backend.py

     def supported(self):
         return self.keyring.supported()
 
+
+class UnOpenableKWallet(object):
+    """A module-like object used to test KDE wallet fall-back."""
+
+    Synchronous = None
+
+    def openWallet(self, *args):
+        return None
+
+    def NetworkWallet(self):
+        return None
+
+
+class FauxQtGui(object):
+    """A fake module-like object used in testing the open_kwallet function."""
+
+    class QApplication(object):
+        def __init__(self, *args):
+            pass
+
+        def exit(self):
+            pass
+
+
+class KDEWalletCanceledTestCase(unittest.TestCase):
+
+    def test_user_canceled(self):
+        # If the user cancels either the "enter your password to unlock the
+        # keyring" dialog or clicks "deny" on the "can this application access
+        # the wallet" dialog then openWallet() will return None.  The
+        # open_wallet() function should handle that eventuality by returning
+        # None to signify that the KWallet backend is not available.
+        self.assertEqual(
+            keyring.backend.open_kwallet(UnOpenableKWallet(), FauxQtGui()),
+            None)
+
+
 class FileKeyringTestCase(BackendBasicTestCase):
     __test__ = False