diff options
author | kali <kali@leap.se> | 2012-08-23 23:27:13 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-08-23 23:27:13 +0900 |
commit | 48dc15ac80cbba0123c6b14ad3afc6eddabf410a (patch) | |
tree | 7654f85b65ceed8728bef16e7aff220a7c7391a7 /src/leap/base | |
parent | 7e6ab299a8017e1ff3a63d577541ea4e6c462b44 (diff) |
basic provider-definition and provider spec classes
tests green for load/dump operations on config.
defaults on config spec for LeapServiceProvider should
be outputting a minimal working config for bootstrapping
the connection.
we should be using this config mechanism for other config
operations; but probably we should specify a local_editable flag
for those configs that are only changed by server.
Diffstat (limited to 'src/leap/base')
-rw-r--r-- | src/leap/base/providers.py | 91 | ||||
-rw-r--r-- | src/leap/base/tests/test_providers.py | 123 |
2 files changed, 214 insertions, 0 deletions
diff --git a/src/leap/base/providers.py b/src/leap/base/providers.py new file mode 100644 index 00000000..6fc050a0 --- /dev/null +++ b/src/leap/base/providers.py @@ -0,0 +1,91 @@ +import configuration + +from leap.base.config import JSONLeapConfig + +########################################################## +# hacking in progress: + +# Specs are instances of configuration.Configuration class +# and have to carry an options attr. +# +# Configs have: +# - slug +# - definition + +# TODO: +# - have a good type cast repertory +# - raise validation errors? +########################################################## + + +class LeapProviderSpec(configuration.Configuration): + options = { + 'serial': { + 'type': int, + 'default': 1, + 'required': True, + }, + 'version': { + 'type': unicode, + 'default': '0.1.0' + #'required': True + }, + 'domain': { + 'type': unicode, # XXX define uri type + 'default': 'testprovider.example.org' + #'required': True, + }, + 'display_name': { + 'type': unicode, # XXX multilingual object? + 'default': 'test provider' + #'required': True + }, + 'description': { + 'default': 'test provider' + }, + 'enrollment_policy': { + 'type': unicode, # oneof ?? + 'default': 'open' + }, + 'services': { + 'type': list, # oneof ?? + 'default': ['eip'] + }, + 'api_version': { + 'type': unicode, + 'default': '0.1.0' # version regexp + }, + 'api_uri': { + 'type': unicode # uri + }, + 'public_key': { + 'type': unicode # fingerprint + }, + 'ca_cert': { + 'type': unicode + }, + 'ca_cert_uri': { + 'type': unicode + }, + } + + +class LeapProviderDefinition(JSONLeapConfig): + slug = 'definition.json' + spec = LeapProviderSpec + + +class LeapProvider(object): + # XXX ??? + # do we need this? + # can we hook here the network fetching stuff? + # maybe (bstorming a little bit): + + # config = LeapProviderDefinition + # fetcher = foo.FetcherClass + pass + + +class LeapProviderSet(object): + def __init__(self): + self.count = 0 diff --git a/src/leap/base/tests/test_providers.py b/src/leap/base/tests/test_providers.py new file mode 100644 index 00000000..2f029930 --- /dev/null +++ b/src/leap/base/tests/test_providers.py @@ -0,0 +1,123 @@ +import json +try: + import unittest2 as unittest +except ImportError: + import unittest + +import os + +from leap.testing.basetest import BaseLeapTest +from leap.base import providers + +EXPECTED_DEFAULT_CONFIG = { + "api_version": "0.1.0", + "description": "test provider", + "display_name": "test provider", + "domain": "testprovider.example.org", + "enrollment_policy": "open", + "serial": 1, + "services": [ + "eip" + ], + "version": "0.1.0" +} + + +class TestLeapProviderDefinition(BaseLeapTest): + def setUp(self): + self.definition = providers.LeapProviderDefinition() + #XXX change to self.definition.config when property is fixed + self.config = self.definition.get_config() + + def tearDown(self): + if hasattr(self, 'testfile') and os.path.isfile(self.testfile): + os.remove(self.testfile) + + # tests + + def test_provider_dump(self): + # check a good provider definition is dumped to disk + self.testfile = self.get_tempfile('test.json') + self.definition.save(to=self.testfile) + deserialized = json.load(open(self.testfile, 'rb')) + self.assertEqual(deserialized, EXPECTED_DEFAULT_CONFIG) + + def test_provider_dump_to_slug(self): + # same as above, but we test the ability to save to a + # file generated from the slug. + # XXX THIS TEST SHOULD MOVE TO test_baseconfig + self.definition.save() + filename = self.definition.filename + deserialized = json.load(open(filename, 'rb')) + self.assertEqual(deserialized, EXPECTED_DEFAULT_CONFIG) + + def test_provider_load(self): + # check loading provider from disk file + self.testfile = self.get_tempfile('test_load.json') + with open(self.testfile, 'w') as wf: + wf.write(json.dumps(EXPECTED_DEFAULT_CONFIG)) + self.definition.load(fromfile=self.testfile) + self.assertDictEqual(self.config, + EXPECTED_DEFAULT_CONFIG) + + @unittest.skip + def test_load_malformed_json_definition(self): + raise NotImplementedError + + @unittest.skip + def test_type_validation(self): + # check various type validation + # type cast + raise NotImplementedError + + +class TestLeapProvider(BaseLeapTest): + def setUp(self): + pass + + def tearDown(self): + pass + + ### + + # XXX ?? + + +class TestLeapProviderSet(BaseLeapTest): + + def setUp(self): + self.providers = providers.LeapProviderSet() + + def tearDown(self): + pass + ### + + def test_get_zero_count(self): + self.assertEqual(self.providers.count, 0) + + @unittest.skip + def test_count_defined_providers(self): + # check the method used for making + # the list of providers + raise NotImplementedError + + @unittest.skip + def test_get_default_provider(self): + raise NotImplementedError + + @unittest.skip + def test_should_be_at_least_one_provider_after_init(self): + # when we init an empty environment, + # there should be at least one provider, + # that will be a dump of the default provider definition + # somehow a high level test + raise NotImplementedError + + @unittest.skip + def test_get_eip_remote_from_default_provider(self): + # from: default provider + # expect: remote eip domain + raise NotImplementedError + +if __name__ == "__main__": + unittest.main() |