From 1dfad9725e39b0135a25a0739dc7fb4ee6bf92ee Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Fri, 11 Nov 2016 19:59:32 +0100 Subject: [bug] get user parameter in the key commands This has been previously encapsulated in a dict, and the commands were not modified accordingly. I'm adding some very basic test for the KeymanagerService public api contract. - Resolves: #8577 --- .gitignore | 1 + src/leap/bitmask/cli/command.py | 2 + src/leap/bitmask/core/mail_services.py | 10 ++--- tests/unit/core/test_services.py | 74 ++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 tests/unit/core/test_services.py diff --git a/.gitignore b/.gitignore index a5f9a32c..47a3e6db 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ docs/cov_html/ .cache coverage.xml *,cover +_trial_temp # Sphinx documentation docs/_build/ diff --git a/src/leap/bitmask/cli/command.py b/src/leap/bitmask/cli/command.py index 1694956e..16f483a3 100644 --- a/src/leap/bitmask/cli/command.py +++ b/src/leap/bitmask/cli/command.py @@ -39,6 +39,8 @@ def _print_result(result): def default_dict_printer(result): + if not result: + return for key, value in result.items(): if value is None: value = str(value) diff --git a/src/leap/bitmask/core/mail_services.py b/src/leap/bitmask/core/mail_services.py index 183477af..b6c54047 100644 --- a/src/leap/bitmask/core/mail_services.py +++ b/src/leap/bitmask/core/mail_services.py @@ -18,7 +18,7 @@ Mail services. This is quite moving work still. -This should be moved to the different packages when it stabilizes. +This should be moved to the different submodules when it stabilizes. """ import json import os @@ -398,19 +398,19 @@ class KeymanagerService(HookableService): # commands def do_list_keys(self, userid, private=False): - km = self._container.get_instance(userid) + km = self._container.get_instance(userid['user']) d = km.get_all_keys(private=private) d.addCallback(lambda keys: [dict(key) for key in keys]) return d def do_export(self, userid, address, private=False): - km = self._container.get_instance(userid) + km = self._container.get_instance(userid['user']) d = km.get_key(address, private=private, fetch_remote=False) d.addCallback(lambda key: dict(key)) return d def do_insert(self, userid, address, rawkey, validation='Fingerprint'): - km = self._container.get_instance(userid) + km = self._container.get_instance(userid['user']) validation = ValidationLevels.get(validation) d = km.put_raw_key(rawkey, address, validation=validation) d.addCallback(lambda _: km.get_key(address, fetch_remote=False)) @@ -419,7 +419,7 @@ class KeymanagerService(HookableService): @defer.inlineCallbacks def do_delete(self, userid, address, private=False): - km = self._container.get_instance(userid) + km = self._container.get_instance(userid['user']) key = yield km.get_key(address, private=private, fetch_remote=False) km.delete_key(key) defer.returnValue(key.fingerprint) diff --git a/tests/unit/core/test_services.py b/tests/unit/core/test_services.py new file mode 100644 index 00000000..bb00dce9 --- /dev/null +++ b/tests/unit/core/test_services.py @@ -0,0 +1,74 @@ +from twisted.internet import defer +from twisted.trial import unittest + +from leap.bitmask.core.mail_services import KeymanagerService + + +class KeymanagerServiceTestCase(unittest.TestCase): + + @defer.inlineCallbacks + def test_keymanager_service_list_call(self): + kms = keymanagerServiceFactory() + yield kms.do_list_keys({'user': 'user'}) + assert kms._keymanager.loopback == ['get_all_keys'] + + @defer.inlineCallbacks + def test_keymanager_service_export_call(self): + kms = keymanagerServiceFactory() + yield kms.do_export({'user': 'user'}, 'foo@bar') + assert kms._keymanager.loopback == ['get_key'] + + @defer.inlineCallbacks + def test_keymanager_service_insert_call(self): + kms = keymanagerServiceFactory() + yield kms.do_insert({'user': 'user'}, 'foo@bar', 'aaaa') + assert kms._keymanager.loopback == ['put_raw_key', 'get_key'] + + @defer.inlineCallbacks + def test_keymanager_service_delete_call(self): + kms = keymanagerServiceFactory() + yield kms.do_delete({'user': 'user'}, 'foo@bar') + assert kms._keymanager.loopback == ['get_key', 'delete_key'] + + +class _container(object): + + class _keymanager(object): + """ + This implements the basic public contract of the Keymanager object, + just to mock the calls made from the KemanagerService. + """ + def __init__(self): + self.loopback = [] + + def get_all_keys(self, private=False): + self.loopback.append('get_all_keys') + return defer.succeed([]) + + def get_key(self, address, private=False, fetch_remote=False): + self.loopback.append('get_key') + + class _key(dict): + fingerprint = 'deadbeef' + return defer.succeed(_key()) + + def put_raw_key(self, rawkey, address, validation=""): + self.loopback.append('put_raw_key') + return defer.succeed('') + + def delete_key(self, key): + self.loopback.append('delete_key') + return defer.succeed('') + + def __init__(self): + self._instances = {'user': self._keymanager()} + + def get_instance(self, userid): + return self._instances.get(userid) + + +def keymanagerServiceFactory(): + kms = KeymanagerService() + kms._container = _container() + kms._keymanager = kms._container.get_instance('user') + return kms -- cgit v1.2.3