diff --git a/federation/hostmeta/generators.py b/federation/hostmeta/generators.py index 70c842c..94c0dab 100644 --- a/federation/hostmeta/generators.py +++ b/federation/hostmeta/generators.py @@ -2,6 +2,7 @@ from base64 import b64encode import json import os from string import Template +from jsonschema import validate from xrd import XRD, Link, Element @@ -180,7 +181,7 @@ class SocialRelayWellKnown(object): See WIP spec: https://wiki.diasporafoundation.org/Relay_servers_for_public_posts - Schema see `federation/tests/schemas/social-relay-well-known` + Schema see `schemas/social-relay-well-known.json` Args: subscribe (bool) @@ -189,6 +190,9 @@ class SocialRelayWellKnown(object): Returns: JSON document (str) + + Raises: + ValidationError on `render` if values don't conform to schema """ def __init__(self, subscribe, tags=(), scope="all", *args, **kwargs): self.doc = { @@ -198,4 +202,11 @@ class SocialRelayWellKnown(object): } def render(self): + self.validate_doc() return json.dumps(self.doc) + + def validate_doc(self): + schema_path = os.path.join(os.path.dirname(__file__), "schemas", "social-relay-well-known.json") + with open(schema_path) as f: + schema = json.load(f) + validate(self.doc, schema) diff --git a/federation/tests/schemas/social-relay-well-known b/federation/hostmeta/schemas/social-relay-well-known.json similarity index 100% rename from federation/tests/schemas/social-relay-well-known rename to federation/hostmeta/schemas/social-relay-well-known.json diff --git a/federation/tests/hostmeta/test_generators.py b/federation/tests/hostmeta/test_generators.py index 931ec3e..e00b1a6 100644 --- a/federation/tests/hostmeta/test_generators.py +++ b/federation/tests/hostmeta/test_generators.py @@ -94,7 +94,7 @@ class TestDiasporaHCardGenerator(object): class TestSocialRelayWellKnownGenerator(object): def test_valid_social_relay_well_known(self): - with open("federation/tests/schemas/social-relay-well-known") as f: + with open("federation/hostmeta/schemas/social-relay-well-known.json") as f: schema = json.load(f) well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="tags") assert well_known.doc["subscribe"] == True @@ -103,7 +103,7 @@ class TestSocialRelayWellKnownGenerator(object): validate(well_known.doc, schema) def test_valid_social_relay_well_known_empty_tags(self): - with open("federation/tests/schemas/social-relay-well-known") as f: + with open("federation/hostmeta/schemas/social-relay-well-known.json") as f: schema = json.load(f) well_known = SocialRelayWellKnown(subscribe=False) assert well_known.doc["subscribe"] == False @@ -112,7 +112,7 @@ class TestSocialRelayWellKnownGenerator(object): validate(well_known.doc, schema) def test_invalid_social_relay_well_known(self): - with open("federation/tests/schemas/social-relay-well-known") as f: + with open("federation/hostmeta/schemas/social-relay-well-known.json") as f: schema = json.load(f) well_known_doc = { "subscribe": "true", @@ -123,8 +123,17 @@ class TestSocialRelayWellKnownGenerator(object): validate(well_known_doc, schema) def test_invalid_social_relay_well_known_scope(self): - with open("federation/tests/schemas/social-relay-well-known") as f: + with open("federation/hostmeta/schemas/social-relay-well-known.json") as f: schema = json.load(f) well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="cities") with pytest.raises(ValidationError): validate(well_known.doc, schema) + + def test_render_validates_valid_document(self): + well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="tags") + well_known.render() + + def test_render_validates_invalid_document(self): + well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="cities") + with pytest.raises(ValidationError): + well_known.render() diff --git a/setup.py b/setup.py index 479ba2f..347e2b7 100644 --- a/setup.py +++ b/setup.py @@ -14,12 +14,12 @@ setup( install_requires=[ "dirty-validators==0.3.2", "lxml==3.4.4", + "jsonschema==2.5.1", "pycrypto==2.6.1", "python-dateutil==2.4.2", "python-xrd==0.1", ], test_require=[ - "jsonschema==2.5.1", "pytest==2.7.2", ], include_package_data=True,