From 30570bd89c04a56b35b91a0bc1d5fc00bb6ad266 Mon Sep 17 00:00:00 2001 From: kali Date: Mon, 24 Sep 2012 22:21:50 +0900 Subject: add schema to JSONLeapConfig classes and a jsonvalidate function too, that calls to jsonchemea.validate(self, data) with self.schema We're using the specs to both purposes now: * providing a type casting system for our config options (work in progress for the type casting) * json schema validation --- src/leap/base/tests/test_config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/leap/base/tests') diff --git a/src/leap/base/tests/test_config.py b/src/leap/base/tests/test_config.py index bede5ea1..d03149b2 100644 --- a/src/leap/base/tests/test_config.py +++ b/src/leap/base/tests/test_config.py @@ -38,14 +38,14 @@ class JSONLeapConfigTest(BaseLeapTest): class DummyTestConfig(config.JSONLeapConfig): __metaclass__ = config.MetaConfigWithSpec - spec = {} + spec = {'properties': {}} with self.assertRaises(exceptions.ImproperlyConfigured) as exc: DummyTestConfig() exc.startswith("missing slug") class DummyTestConfig(config.JSONLeapConfig): __metaclass__ = config.MetaConfigWithSpec - spec = {} + spec = {'properties': {}} slug = "foo" DummyTestConfig() -- cgit v1.2.3 From 5d8e518d03e9fd045a75a63fec79b52392266c26 Mon Sep 17 00:00:00 2001 From: kali Date: Tue, 25 Sep 2012 07:19:07 +0900 Subject: make test for provider pass --- src/leap/base/tests/test_providers.py | 41 ++++++++++++++++------------------- 1 file changed, 19 insertions(+), 22 deletions(-) (limited to 'src/leap/base/tests') diff --git a/src/leap/base/tests/test_providers.py b/src/leap/base/tests/test_providers.py index 9e0ff90c..d667a7e0 100644 --- a/src/leap/base/tests/test_providers.py +++ b/src/leap/base/tests/test_providers.py @@ -4,6 +4,10 @@ try: except ImportError: import unittest +# XXX FIXME +import logging +logging.basicConfig() + import os from leap import __branding as BRANDING @@ -12,24 +16,25 @@ 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" + u"api_version": u"0.1.0", + u"description": {u'en': u"Test provider"}, + u"display_name": {u'en': u"Test Provider"}, + u"domain": u"testprovider.example.org", + u"enrollment_policy": u"open", + u"serial": 1, + u"services": [ + u"eip" ], - "version": "0.1.0" + u"version": u"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() + self.definition.save() + self.definition.load() + self.config = self.definition.config def tearDown(self): if hasattr(self, 'testfile') and os.path.isfile(self.testfile): @@ -57,6 +62,7 @@ class TestLeapProviderDefinition(BaseLeapTest): self.testfile = self.get_tempfile('test.json') self.definition.save(to=self.testfile) deserialized = json.load(open(self.testfile, 'rb')) + self.maxDiff = None self.assertEqual(deserialized, EXPECTED_DEFAULT_CONFIG) def test_provider_dump_to_slug(self): @@ -88,17 +94,8 @@ class TestLeapProviderDefinition(BaseLeapTest): # type cast raise NotImplementedError - -class TestLeapProvider(BaseLeapTest): - def setUp(self): - pass - - def tearDown(self): - pass - - ### - - # XXX ?? + def test_provider_validation(self): + self.definition.jsonvalidate(self.config) class TestLeapProviderSet(BaseLeapTest): -- cgit v1.2.3 From abf481cab381a86d8a9c5607a131b56636081382 Mon Sep 17 00:00:00 2001 From: kali Date: Tue, 25 Sep 2012 05:48:06 +0900 Subject: refactored jsonconfig, included jsonschema validation and type casting. --- src/leap/base/tests/test_providers.py | 19 ++++--- src/leap/base/tests/test_validation.py | 92 ++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 src/leap/base/tests/test_validation.py (limited to 'src/leap/base/tests') diff --git a/src/leap/base/tests/test_providers.py b/src/leap/base/tests/test_providers.py index d667a7e0..8d3b8847 100644 --- a/src/leap/base/tests/test_providers.py +++ b/src/leap/base/tests/test_providers.py @@ -1,15 +1,13 @@ +import copy import json try: import unittest2 as unittest except ImportError: import unittest - -# XXX FIXME -import logging -logging.basicConfig() - import os +import jsonschema + from leap import __branding as BRANDING from leap.testing.basetest import BaseLeapTest from leap.base import providers @@ -25,6 +23,7 @@ EXPECTED_DEFAULT_CONFIG = { u"services": [ u"eip" ], + u"languages": [u"en"], u"version": u"0.1.0" } @@ -84,6 +83,13 @@ class TestLeapProviderDefinition(BaseLeapTest): self.assertDictEqual(self.config, EXPECTED_DEFAULT_CONFIG) + def test_provider_validation(self): + self.definition.validate(self.config) + _config = copy.deepcopy(self.config) + _config['serial'] = 'aaa' + with self.assertRaises(jsonschema.ValidationError): + self.definition.validate(_config) + @unittest.skip def test_load_malformed_json_definition(self): raise NotImplementedError @@ -94,9 +100,6 @@ class TestLeapProviderDefinition(BaseLeapTest): # type cast raise NotImplementedError - def test_provider_validation(self): - self.definition.jsonvalidate(self.config) - class TestLeapProviderSet(BaseLeapTest): diff --git a/src/leap/base/tests/test_validation.py b/src/leap/base/tests/test_validation.py new file mode 100644 index 00000000..87e99648 --- /dev/null +++ b/src/leap/base/tests/test_validation.py @@ -0,0 +1,92 @@ +import copy +import datetime +#import json +try: + import unittest2 as unittest +except ImportError: + import unittest +import os + +import jsonschema + +from leap.base.config import JSONLeapConfig +from leap.base import pluggableconfig +from leap.testing.basetest import BaseLeapTest + +SAMPLE_CONFIG_DICT = { + 'prop_one': 1, + 'prop_uri': "http://example.org", + 'prop_date': '2012-12-12', +} + +EXPECTED_CONFIG = { + 'prop_one': 1, + 'prop_uri': "http://example.org", + 'prop_date': datetime.datetime(2012, 12, 12) +} + +sample_spec = { + 'description': 'sample schema definition', + 'type': 'object', + 'properties': { + 'prop_one': { + 'type': int, + 'default': 1, + 'required': True + }, + 'prop_uri': { + 'type': str, + 'default': 'http://example.org', + 'required': True, + 'format': 'uri' + }, + 'prop_date': { + 'type': str, + 'default': '2012-12-12', + 'format': 'date' + } + } +} + + +class SampleConfig(JSONLeapConfig): + spec = sample_spec + + @property + def slug(self): + return os.path.expanduser('~/sampleconfig.json') + + +class TestJSONLeapConfigValidation(BaseLeapTest): + def setUp(self): + self.sampleconfig = SampleConfig() + self.sampleconfig.save() + self.sampleconfig.load() + self.config = self.sampleconfig.config + + def tearDown(self): + if hasattr(self, 'testfile') and os.path.isfile(self.testfile): + os.remove(self.testfile) + + # tests + + def test_good_validation(self): + self.sampleconfig.validate(SAMPLE_CONFIG_DICT) + + def test_broken_int(self): + _config = copy.deepcopy(SAMPLE_CONFIG_DICT) + _config['prop_one'] = '1' + with self.assertRaises(jsonschema.ValidationError): + self.sampleconfig.validate(_config) + + def test_format_property(self): + # JsonSchema Validator does not check the format property. + # We should have to extend the Configuration class + blah = copy.deepcopy(SAMPLE_CONFIG_DICT) + blah['prop_uri'] = 'xxx' + with self.assertRaises(pluggableconfig.TypeCastException): + self.sampleconfig.validate(blah) + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3