summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorTulio Casagrande <tcasagra@thoughtworks.com>2016-09-13 17:52:44 -0300
committerTulio Casagrande <tcasagra@thoughtworks.com>2016-09-13 17:52:44 -0300
commit3e9a32625a9adbf557d6b3b1d3954e455fb75356 (patch)
tree1ae6bb5688ab97f53c9beda8458c5802d3c0494d /service
parent8713e0bc40ae8b7251f58e570ecaf13511a97136 (diff)
[#778] Isolated and fixed keymanager tests
We covered a lot of missing cases on the keymanager tests. We also isolated these tests, that caused some other tests to break, so we fixed them.
Diffstat (limited to 'service')
-rw-r--r--service/test/unit/bitmask_libraries/test_keymanager.py99
-rw-r--r--service/test/unit/bitmask_libraries/test_provider.py9
-rw-r--r--service/test/unit/extensions/test_keymanager_fetch_key.py75
3 files changed, 61 insertions, 122 deletions
diff --git a/service/test/unit/bitmask_libraries/test_keymanager.py b/service/test/unit/bitmask_libraries/test_keymanager.py
index 0c46460a..89327b54 100644
--- a/service/test/unit/bitmask_libraries/test_keymanager.py
+++ b/service/test/unit/bitmask_libraries/test_keymanager.py
@@ -13,21 +13,36 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
-from mock import patch
+from mock import patch, MagicMock
from mockito import when
-
-from test_abstract_leap import AbstractLeapTest
-from leap.keymanager import openpgp, KeyNotFound
+from unittest import TestCase
from pixelated.bitmask_libraries.keymanager import Keymanager
from pixelated.config import leap_config
-class KeymanagerTest(AbstractLeapTest):
+class KeymanagerTest(TestCase):
+
+ def setUp(self):
+ self.provider = MagicMock()
+ self.soledad = MagicMock()
+ self.auth = MagicMock(token='token', auth='auth')
+ with patch('pixelated.bitmask_libraries.keymanager.KeyManager'):
+ self.keymanager = Keymanager(self.provider,
+ self.soledad,
+ 'test_user@some-server.test',
+ self.auth.token,
+ self.auth.uuid)
+
+ def tearDown(self):
+ reload(leap_config)
+
@patch('pixelated.bitmask_libraries.keymanager.KeyManager')
- def test_that_keymanager_is_created(self, keymanager_mock):
- when(self.provider)._discover_nicknym_server().thenReturn('https://nicknym.some-server.test:6425/')
- self.provider.combined_cerfificates_path = 'combined_cerfificates_path'
- self.provider.provider_api_cert = '/some/path/to/provider_ca_cert'
+ def test_keymanager_is_created(self, keymanager_mock):
+ when(self.provider)._discover_nicknym_server().thenReturn('nicknym_server')
+ self.provider.provider_api_cert = 'ca_cert_path'
+ self.provider.api_uri = 'api_uri'
+ self.provider.api_version = '1'
+ self.provider.combined_cerfificates_path = 'combined_ca_bundle'
leap_config.gpg_binary = '/path/to/gpg'
Keymanager(self.provider,
@@ -38,45 +53,41 @@ class KeymanagerTest(AbstractLeapTest):
keymanager_mock.assert_called_with(
'test_user@some-server.test',
- 'https://nicknym.some-server.test:6425/',
+ 'nicknym_server',
self.soledad,
token=self.auth.token,
- ca_cert_path='/some/path/to/provider_ca_cert',
- api_uri='https://api.some-server.test:4430',
+ ca_cert_path='ca_cert_path',
+ api_uri='api_uri',
api_version='1',
uid=self.auth.uuid,
gpgbinary='/path/to/gpg',
- combined_ca_bundle='combined_cerfificates_path')
+ combined_ca_bundle='combined_ca_bundle')
- @patch('pixelated.bitmask_libraries.keymanager.KeyManager')
- def test_gen_key(self, keymanager_mock):
- # given
- keyman = keymanager_mock.return_value
- keyman.get_key.side_effect = KeyNotFound
- keymanager = Keymanager(self.provider,
- self.soledad,
- 'test_user@some-server.test',
- self.auth.token,
- self.auth.uuid)
-
- # when/then
- keymanager.generate_openpgp_key()
-
- keyman.get_key.assert_called_with('test_user@some-server.test', private=True, fetch_remote=False)
- keyman.gen_key.assert_called_once()
- keyman.send_key.assert_called_once()
+ def test_keymanager_generate_openpgp_key_generates_key_correctly(self):
+ when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(False)
- @patch('pixelated.bitmask_libraries.keymanager.KeyManager')
- def test_existing_key_not_sent_to_leap(self, keymanager_mock):
- keyman = keymanager_mock.return_value
- keyman.get_key.side_effect = KeyNotFound
- keymanager = Keymanager(self.provider,
- self.soledad,
- 'test_user@some-server.test',
- self.auth.token,
- self.auth.uuid)
-
- when(keymanager)._key_exists('test_user@some-server.test').thenReturn(True)
- keymanager.generate_openpgp_key()
- keyman.gen_key.assert_not_called()
- keyman.send_key.assert_not_called()
+ self.keymanager._gen_key = MagicMock()
+ self.keymanager._send_key_to_leap = MagicMock()
+
+ self.keymanager.generate_openpgp_key()
+
+ self.keymanager._gen_key.assert_called_once()
+ self.keymanager._send_key_to_leap.assert_called_once()
+
+ def test_keymanager_generate_openpgp_key_dont_regenerate_preexisting_key(self):
+ when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(True)
+
+ self.keymanager._gen_key = MagicMock()
+
+ self.keymanager.generate_openpgp_key()
+
+ self.keymanager._gen_key.assert_not_called()
+
+ def test_keymanager_generate_openpgp_key_dont_upload_preexisting_key(self):
+ when(self.keymanager)._key_exists('test_user@some-server.test').thenReturn(True)
+
+ self.keymanager._send_key_to_leap = MagicMock()
+
+ self.keymanager.generate_openpgp_key()
+
+ self.keymanager._send_key_to_leap.assert_not_called()
diff --git a/service/test/unit/bitmask_libraries/test_provider.py b/service/test/unit/bitmask_libraries/test_provider.py
index 4e7c565f..708b05b6 100644
--- a/service/test/unit/bitmask_libraries/test_provider.py
+++ b/service/test/unit/bitmask_libraries/test_provider.py
@@ -139,9 +139,12 @@ PROVIDER_WEB_CERT = '/tmp/bootstrap-ca.crt'
class LeapProviderTest(AbstractLeapTest):
def setUp(self):
- leap_config.set_leap_home('/tmp/foobar')
+ leap_config.leap_home = '/tmp/foobar'
LeapCertificate.set_cert_and_fingerprint(PROVIDER_WEB_CERT, None)
+ def tearDown(self):
+ reload(leap_config)
+
def test_provider_fetches_provider_json(self):
with HTTMock(provider_json_mock, soledad_json_mock):
provider = LeapProvider('some-provider.test')
@@ -212,7 +215,7 @@ class LeapProviderTest(AbstractLeapTest):
with HTTMock(provider_json_mock, soledad_json_mock, not_found_mock):
provider = LeapProvider('some-provider.test')
provider.fetch_soledad_json()
- get_func.assert_called_with('https://api.some-provider.test:4430/1/config/soledad-service.json', verify='/some/leap/home/providers/some-provider.test/keys/client/api.pem', timeout=15)
+ get_func.assert_called_with('https://api.some-provider.test:4430/1/config/soledad-service.json', verify='/tmp/foobar/providers/some-provider.test/keys/client/api.pem', timeout=15)
def test_that_leap_fingerprint_is_validated(self):
session = MagicMock(wraps=requests.session())
@@ -232,4 +235,4 @@ class LeapProviderTest(AbstractLeapTest):
provider = LeapProvider('some-provider.test')
certs = provider.provider_api_cert
- self.assertEqual('/some/leap/home/providers/some-provider.test/keys/client/api.pem', certs)
+ self.assertEqual('/tmp/foobar/providers/some-provider.test/keys/client/api.pem', certs)
diff --git a/service/test/unit/extensions/test_keymanager_fetch_key.py b/service/test/unit/extensions/test_keymanager_fetch_key.py
deleted file mode 100644
index ef7d67d2..00000000
--- a/service/test/unit/extensions/test_keymanager_fetch_key.py
+++ /dev/null
@@ -1,75 +0,0 @@
-#
-# Copyright (c) 2014 ThoughtWorks, Inc.
-#
-# Pixelated is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# Pixelated is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Affero General Public License for more details.
-#
-# 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 unittest
-from mock import MagicMock, patch
-
-from leap.keymanager import KeyManager
-from leap.keymanager.keys import KEY_ADDRESS_KEY, KEY_TYPE_KEY, KEY_ID_KEY, KEY_FINGERPRINT_KEY, KEY_DATA_KEY, KEY_PRIVATE_KEY, KEY_LENGTH_KEY, KEY_EXPIRY_DATE_KEY, KEY_FIRST_SEEN_AT_KEY, KEY_LAST_AUDITED_AT_KEY, KEY_VALIDATION_KEY, KEY_TAGS_KEY
-from leap.keymanager.openpgp import OpenPGPKey
-from leap.keymanager.errors import KeyNotFound
-from requests.exceptions import HTTPError
-
-
-class TestDoc(object):
- def __init__(self, encryption_key):
- self.content = encryption_key
-
-sample_key = {
- KEY_ADDRESS_KEY: 'foo@bar.de',
- KEY_TYPE_KEY: 'type',
- KEY_ID_KEY: 'key_id',
- KEY_FINGERPRINT_KEY: 'fingerprint',
- KEY_DATA_KEY: 'key_data',
- KEY_PRIVATE_KEY: None,
- KEY_LENGTH_KEY: 'length',
- KEY_EXPIRY_DATE_KEY: 'expiry_date',
- KEY_FIRST_SEEN_AT_KEY: 'first_seen_at',
- KEY_LAST_AUDITED_AT_KEY: 'last_audited_at',
- KEY_VALIDATION_KEY: 'validation',
- KEY_TAGS_KEY: 'tags',
-}
-
-
-class TestExtKeyManagerFetchKey(unittest.TestCase):
-
- @patch('leap.keymanager.requests')
- def test_retrieves_key(self, requests_mock):
- nickserver_url = 'http://some/nickserver/uri'
- soledad = MagicMock()
- soledad.get_from_index.side_effect = [[], [TestDoc(sample_key)]]
-
- km = KeyManager('me@bar.de', nickserver_url, soledad, ca_cert_path='some path')
-
- result = km.get_key('foo@bar.de', OpenPGPKey)
-
- self.assertEqual(str(OpenPGPKey('foo@bar.de', key_id='key_id')), str(result))
-
- @patch('leap.keymanager.requests')
- def test_http_error_500(self, requests_mock):
- def do_request(one, data=None, verify=None):
- response = MagicMock()
- response.raise_for_status = MagicMock()
- response.raise_for_status.side_effect = HTTPError
- return response
-
- nickserver_url = 'http://some/nickserver/uri'
- soledad = MagicMock()
- soledad.get_from_index.side_effect = [[], []]
- requests_mock.get.side_effect = do_request
-
- km = KeyManager('me@bar.de', nickserver_url, soledad, ca_cert_path='some path')
-
- self.assertRaises(KeyNotFound, km.get_key, 'foo@bar.de', OpenPGPKey)