summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/bitmask_libraries/keymanager.py25
-rw-r--r--service/test/unit/bitmask_libraries/test_keymanager.py20
2 files changed, 20 insertions, 25 deletions
diff --git a/service/pixelated/bitmask_libraries/keymanager.py b/service/pixelated/bitmask_libraries/keymanager.py
index 84e3f6aa..28d5f9db 100644
--- a/service/pixelated/bitmask_libraries/keymanager.py
+++ b/service/pixelated/bitmask_libraries/keymanager.py
@@ -48,16 +48,19 @@ class Keymanager(object):
def generate_openpgp_key(self):
current_key = yield self._key_exists(self._email)
if not current_key:
- yield self._generate_key_and_send_to_leap()
+ current_key = yield self._generate_key_and_send_to_leap()
elif current_key.needs_renewal(DEFAULT_EXTENSION_THRESHOLD):
- yield self._regenerate_key_and_send_to_leap()
+ current_key = yield self._regenerate_key_and_send_to_leap()
+
+ logger.info("Current key for {}: {}".format(self._email, current_key.fingerprint))
@defer.inlineCallbacks
def _regenerate_key_and_send_to_leap(self):
logger.info("Regenerating keys - this could take a while...")
- yield self.keymanager.regenerate_key()
+ key = yield self.keymanager.regenerate_key()
try:
- yield self._send_key_to_leap()
+ yield self.keymanager.send_key()
+ defer.returnValue(key)
except Exception as e:
# what to be done when upload key error
raise UploadKeyError(e.message)
@@ -65,9 +68,10 @@ class Keymanager(object):
@defer.inlineCallbacks
def _generate_key_and_send_to_leap(self):
logger.info("Generating keys - this could take a while...")
- yield self._gen_key()
+ key = yield self.keymanager.gen_key()
try:
- yield self._send_key_to_leap()
+ yield self.keymanager.send_key()
+ defer.returnValue(key)
except Exception as e:
yield self.delete_key_pair()
raise UploadKeyError(e.message)
@@ -86,15 +90,6 @@ class Keymanager(object):
defer.returnValue(key)
@defer.inlineCallbacks
- def _gen_key(self):
- key = yield self.keymanager.gen_key()
- defer.returnValue(key)
-
- @defer.inlineCallbacks
- def _send_key_to_leap(self):
- yield self.keymanager.send_key()
-
- @defer.inlineCallbacks
def delete_key_pair(self):
private_key = yield self.get_key(self._email, private=True, fetch_remote=False)
public_key = yield self.get_key(self._email, private=False, fetch_remote=False)
diff --git a/service/test/unit/bitmask_libraries/test_keymanager.py b/service/test/unit/bitmask_libraries/test_keymanager.py
index eb6ba79b..81ba7b65 100644
--- a/service/test/unit/bitmask_libraries/test_keymanager.py
+++ b/service/test/unit/bitmask_libraries/test_keymanager.py
@@ -13,7 +13,7 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
-import datetime
+
from mock import patch, MagicMock
from mockito import when
from unittest import TestCase
@@ -70,42 +70,42 @@ class KeymanagerTest(TestCase):
def test_keymanager_generate_openpgp_key_generates_key_correctly(self):
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(None)
- self.keymanager._gen_key = MagicMock()
- self.keymanager._send_key_to_leap = MagicMock()
+ self.leap_keymanager.gen_key = MagicMock()
+ self.leap_keymanager.send_key = MagicMock()
self.keymanager.generate_openpgp_key()
- self.keymanager._gen_key.assert_called_once()
- self.keymanager._send_key_to_leap.assert_called_once()
+ self.leap_keymanager.gen_key.assert_called_once()
+ self.leap_keymanager.send_key.assert_called_once()
def test_keymanager_generate_openpgp_key_doesnt_regenerate_preexisting_key(self):
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=False)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
- self.keymanager._gen_key = MagicMock()
+ self.leap_keymanager.gen_key = MagicMock()
self.keymanager.generate_openpgp_key()
- self.keymanager._gen_key.assert_not_called()
+ self.leap_keymanager.gen_key.assert_not_called()
def test_keymanager_generate_openpgp_key_doesnt_upload_preexisting_key(self):
mock_open_pgp_key = MagicMock()
mock_open_pgp_key.needs_renewal = MagicMock(return_value=False)
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(mock_open_pgp_key)
- self.keymanager._send_key_to_leap = MagicMock()
+ self.leap_keymanager.send_key = MagicMock()
self.keymanager.generate_openpgp_key()
- self.keymanager._send_key_to_leap.assert_not_called()
+ self.leap_keymanager.send_key.assert_not_called()
@defer.inlineCallbacks
def test_keymanager_generate_openpgp_key_deletes_key_when_upload_fails(self):
when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(None)
self.keymanager.delete_key_pair = MagicMock()
- when(self.keymanager)._send_key_to_leap().thenRaise(Exception('Could not upload key'))
+ when(self.leap_keymanager).send_key().thenRaise(Exception('Could not upload key'))
with self.assertRaises(UploadKeyError):
yield self.keymanager.generate_openpgp_key()