diff options
author | kali <kali@leap.se> | 2012-08-28 05:30:38 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-08-28 05:30:38 +0900 |
commit | 568d52ccf33e6d7683f36f5fe2e3c32b47892216 (patch) | |
tree | 0b40f9b9a79a0cf707dbf134fea14e45a4135186 /src | |
parent | 4a46723219e5284bec21b9dccd6589a670babc63 (diff) |
eipchecker.fetch definition and tests
deprecated base:test_config.test_complete_file (dup functionality)
Diffstat (limited to 'src')
-rw-r--r-- | src/leap/base/constants.py | 16 | ||||
-rw-r--r-- | src/leap/base/tests/test_config.py | 37 | ||||
-rw-r--r-- | src/leap/eip/checks.py | 93 | ||||
-rw-r--r-- | src/leap/eip/exceptions.py | 4 | ||||
-rw-r--r-- | src/leap/eip/tests/test_checks.py | 43 |
5 files changed, 164 insertions, 29 deletions
diff --git a/src/leap/base/constants.py b/src/leap/base/constants.py index 55308a31..991a1dfe 100644 --- a/src/leap/base/constants.py +++ b/src/leap/base/constants.py @@ -6,3 +6,19 @@ # (we expect testers to add it to their /etc/hosts DEFAULT_TEST_PROVIDER = "testprovider.example.org" + +DEFINITION_EXPECTED_PATH = "provider-definition.json" + +DEFAULT_PROVIDER_DEFINITION = { + u'api_uri': u'https://api.testprovider.example.org/', + u'api_version': u'0.1.0', + u'ca_cert': u'8aab80ae4326fd30721689db813733783fe0bd7e', + u'ca_cert_uri': u'https://testprovider.example.org/cacert.pem', + u'description': {u'en': u'This is a test provider'}, + u'display_name': {u'en': u'Test Provider'}, + u'domain': u'testprovider.example.org', + u'enrollment_policy': u'open', + u'public_key': u'cb7dbd679f911e85bc2e51bd44afd7308ee19c21', + u'serial': 1, + u'services': [u'eip'], + u'version': u'0.1.0'} diff --git a/src/leap/base/tests/test_config.py b/src/leap/base/tests/test_config.py index 93de0782..54e4484c 100644 --- a/src/leap/base/tests/test_config.py +++ b/src/leap/base/tests/test_config.py @@ -59,26 +59,27 @@ class ProviderDefinitionTestCase(ProviderTest): with open(os.path.join(path, 'eip.json'), 'w') as fp: json.dump(eipconstants.EIP_SAMPLE_JSON, fp) - def test_complete_file(self): - with mock.patch.object(requests, "get") as mock_method: - mock_method.return_value.status_code = 200 - mock_method.return_value.json = { + # moved to eip.test_checks.test_fetch_definition + #def test_complete_file(self): + #with mock.patch.object(requests, "get") as mock_method: + #mock_method.return_value.status_code = 200 + #mock_method.return_value.json = { #XXX get from providers template - u'api_uri': u'https://api.testprovider.org/', - u'api_version': u'0.1.0', - u'ca_cert': u'8aab80ae4326fd30721689db813733783fe0bd7e', - u'ca_cert_uri': u'https://testprovider.org/cacert.pem', - u'description': {u'en': u'This is a test provider'}, - u'display_name': {u'en': u'Test Provider'}, - u'domain': u'testprovider.org', - u'enrollment_policy': u'open', - u'public_key': u'cb7dbd679f911e85bc2e51bd44afd7308ee19c21', - u'serial': 1, - u'services': [u'eip'], - u'version': u'0.1.0'} + #u'api_uri': u'https://api.testprovider.org/', + #u'api_version': u'0.1.0', + #u'ca_cert': u'8aab80ae4326fd30721689db813733783fe0bd7e', + #u'ca_cert_uri': u'https://testprovider.org/cacert.pem', + #u'description': {u'en': u'This is a test provider'}, + #u'display_name': {u'en': u'Test Provider'}, + #u'domain': u'testprovider.org', + #u'enrollment_policy': u'open', + #u'public_key': u'cb7dbd679f911e85bc2e51bd44afd7308ee19c21', + #u'serial': 1, + #u'services': [u'eip'], + #u'version': u'0.1.0'} # XXX why init to localhost? - cf = config.Configuration("http://localhost/") - self.assertIn('default', cf.providers) + #cf = config.Configuration("http://localhost/") + #self.assertIn('default', cf.providers) # # provider fetch tests block diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index 1726e73a..dbb7d524 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -1,10 +1,17 @@ +import json import logging -logger = logging.getLogger(name=__name__) import os +logger = logging.getLogger(name=__name__) + +import requests + from leap.base import config as baseconfig +from leap.base import constants as baseconstants from leap.eip import config as eipconfig from leap.eip import constants as eipconstants +from leap.eip import exceptions as eipexceptions +from leap.util.fileutil import mkdir_p class EIPChecker(object): @@ -12,10 +19,17 @@ class EIPChecker(object): Several tests needed to ensure a EIPConnection can be sucessful + use run_all to run all checks. """ - #def __init__(self): - ## no init needed atm.. - #pass + + def __init__(self, fetcher=requests): + """ + we do not want to accept too many + argument on init. we want tests + to be explicitely run. + """ + self.config = None + self.fetcher = fetcher def run_all(self, checker=None): """ @@ -30,7 +44,11 @@ class EIPChecker(object): # let's call all tests # needed for a sane eip session. + # TODO: get rid of check_default. + # check_complete should + # be enough. checker.check_default_eipconfig() + checker.check_is_there_default_provider() checker.fetch_definition() checker.fetch_eip_config() @@ -44,6 +62,10 @@ class EIPChecker(object): checks if default eipconfig exists, and dumps a default file if not """ + # XXX ONLY a transient check + # because some old function still checks + # for eip config at the beginning. + # it *really* does not make sense to # dump it right now, we can get an in-memory # config object and dump it to disk in a @@ -51,12 +73,55 @@ class EIPChecker(object): if not self._is_there_default_eipconfig(): self._dump_default_eipconfig() - def check_is_there_default_provider(self): - raise NotImplementedError - - def fetch_definition(self): + def check_is_there_default_provider(self, config=None): + """ + raises EIPMissingDefaultProvider if no + default provider found on eip config. + This is catched by ui and runs FirstRunWizard (MVS+) + """ + # if config is not None: + # config = config + # else: self.get_eipconfig + # XXX parse EIPConfig. + # XXX get default_provider. + eipcfg = self._get_default_eipconfig_path() + with open(eipcfg, 'r') as fp: + config = json.load(fp) + provider = config.get('provider', None) + if provider is None: + raise eipexceptions.EIPMissingDefaultProvider + if config: + self.config = config + return True + + def fetch_definition(self, skip_download=False, + config=None, uri=None): # check_and_get_definition_file - raise NotImplementedError + if skip_download: + return True + if config is None: + config = self.config + if uri is None: + if config: + domain = config.get('provider', None) + else: + domain = None + uri = self._get_provider_definition_uri( + domain=domain) + + # XXX move to JSONConfig Fetcher + request = self.fetcher.get(uri) + request.raise_for_status() + + definition_file = os.path.join( + baseconfig.get_default_provider_path(), + baseconstants.DEFINITION_EXPECTED_PATH) + + folder, filename = os.path.split(definition_file) + if not os.path.isdir(folder): + mkdir_p(folder) + with open(definition_file, 'wb') as f: + f.write(json.dumps(request.json, indent=4)) def fetch_eip_config(self): raise NotImplementedError @@ -67,7 +132,9 @@ class EIPChecker(object): def ping_gateway(self): raise NotImplementedError + # # private helpers + # def _get_default_eipconfig_path(self): return baseconfig.get_config_file(eipconstants.EIP_CONFIG) @@ -79,3 +146,11 @@ class EIPChecker(object): def _dump_default_eipconfig(self): eipconfig.dump_default_eipconfig( self._get_default_eipconfig_path()) + + def _get_provider_definition_uri(self, domain=None, path=None): + if domain is None: + domain = baseconstants.DEFAULT_TEST_PROVIDER + if path is None: + path = baseconstants.DEFINITION_EXPECTED_PATH + return "https://%s/%s" % (domain, path) + diff --git a/src/leap/eip/exceptions.py b/src/leap/eip/exceptions.py index 3719c605..800c7f0e 100644 --- a/src/leap/eip/exceptions.py +++ b/src/leap/eip/exceptions.py @@ -61,3 +61,7 @@ class EIPInitNoKeyFileError(Exception): class EIPInitBadKeyFilePermError(Exception): pass + + +class EIPMissingDefaultProvider(Exception): + pass diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index ea2b3d15..8c022907 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -1,3 +1,4 @@ +import copy import json try: import unittest2 as unittest @@ -5,11 +6,15 @@ except ImportError: import unittest import os -from mock import Mock +from mock import patch, Mock + +import requests from leap.base import config as baseconfig +from leap.base.constants import DEFAULT_PROVIDER_DEFINITION from leap.eip import checks as eipchecks from leap.eip import constants as eipconstants +from leap.eip import exceptions as eipexceptions from leap.testing.basetest import BaseLeapTest @@ -57,7 +62,7 @@ class EIPCheckTest(BaseLeapTest): # test individual check methods - def test_dump_default_eipconfig(self): + def test_check_default_eipconfig(self): checker = eipchecks.EIPChecker() # no eip config (empty home) eipconfig = baseconfig.get_config_file(eipconstants.EIP_CONFIG) @@ -72,6 +77,40 @@ class EIPCheckTest(BaseLeapTest): # TODO: when new JSONConfig class is in place, we shold # run validation methods. + def test_check_is_there_default_provider(self): + checker = eipchecks.EIPChecker() + # we do dump a sample eip config, but lacking a + # default provider entry. + # This error will be possible catched in a different + # place, when JSONConfig does validation of required fields. + + sampleconfig = copy.copy(eipconstants.EIP_SAMPLE_JSON) + # blank out default_provider + sampleconfig['provider'] = None + eipcfg_path = checker._get_default_eipconfig_path() + with open(eipcfg_path, 'w') as fp: + json.dump(sampleconfig, fp) + with self.assertRaises(eipexceptions.EIPMissingDefaultProvider): + checker.check_is_there_default_provider() + + sampleconfig = eipconstants.EIP_SAMPLE_JSON + eipcfg_path = checker._get_default_eipconfig_path() + with open(eipcfg_path, 'w') as fp: + json.dump(sampleconfig, fp) + self.assertTrue(checker.check_is_there_default_provider()) + + def test_fetch_definition(self): + with patch.object(requests, "get") as mocked_get: + mocked_get.return_value.status_code = 200 + mocked_get.return_value.json = DEFAULT_PROVIDER_DEFINITION + checker = eipchecks.EIPChecker(fetcher=requests) + sampleconfig = eipconstants.EIP_SAMPLE_JSON + checker.fetch_definition(config=sampleconfig) + + # XXX TODO check for ConnectionError, HTTPError, InvalidUrl + # (and proper EIPExceptions are raised). + + # Look at base.test_config. if __name__ == "__main__": unittest.main() |