Always validate .well-known/social-relay when rendering it

Not much point in schema otherwise
merge-requests/130/head
Jason Robinson 2015-07-16 23:58:41 +03:00
rodzic 478f55069d
commit 921a9b527d
4 zmienionych plików z 26 dodań i 6 usunięć

Wyświetl plik

@ -2,6 +2,7 @@ from base64 import b64encode
import json import json
import os import os
from string import Template from string import Template
from jsonschema import validate
from xrd import XRD, Link, Element 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 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: Args:
subscribe (bool) subscribe (bool)
@ -189,6 +190,9 @@ class SocialRelayWellKnown(object):
Returns: Returns:
JSON document (str) JSON document (str)
Raises:
ValidationError on `render` if values don't conform to schema
""" """
def __init__(self, subscribe, tags=(), scope="all", *args, **kwargs): def __init__(self, subscribe, tags=(), scope="all", *args, **kwargs):
self.doc = { self.doc = {
@ -198,4 +202,11 @@ class SocialRelayWellKnown(object):
} }
def render(self): def render(self):
self.validate_doc()
return json.dumps(self.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)

Wyświetl plik

@ -94,7 +94,7 @@ class TestDiasporaHCardGenerator(object):
class TestSocialRelayWellKnownGenerator(object): class TestSocialRelayWellKnownGenerator(object):
def test_valid_social_relay_well_known(self): 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) schema = json.load(f)
well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="tags") well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="tags")
assert well_known.doc["subscribe"] == True assert well_known.doc["subscribe"] == True
@ -103,7 +103,7 @@ class TestSocialRelayWellKnownGenerator(object):
validate(well_known.doc, schema) validate(well_known.doc, schema)
def test_valid_social_relay_well_known_empty_tags(self): 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) schema = json.load(f)
well_known = SocialRelayWellKnown(subscribe=False) well_known = SocialRelayWellKnown(subscribe=False)
assert well_known.doc["subscribe"] == False assert well_known.doc["subscribe"] == False
@ -112,7 +112,7 @@ class TestSocialRelayWellKnownGenerator(object):
validate(well_known.doc, schema) validate(well_known.doc, schema)
def test_invalid_social_relay_well_known(self): 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) schema = json.load(f)
well_known_doc = { well_known_doc = {
"subscribe": "true", "subscribe": "true",
@ -123,8 +123,17 @@ class TestSocialRelayWellKnownGenerator(object):
validate(well_known_doc, schema) validate(well_known_doc, schema)
def test_invalid_social_relay_well_known_scope(self): 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) schema = json.load(f)
well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="cities") well_known = SocialRelayWellKnown(subscribe=True, tags=("foo", "bar"), scope="cities")
with pytest.raises(ValidationError): with pytest.raises(ValidationError):
validate(well_known.doc, schema) 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()

Wyświetl plik

@ -14,12 +14,12 @@ setup(
install_requires=[ install_requires=[
"dirty-validators==0.3.2", "dirty-validators==0.3.2",
"lxml==3.4.4", "lxml==3.4.4",
"jsonschema==2.5.1",
"pycrypto==2.6.1", "pycrypto==2.6.1",
"python-dateutil==2.4.2", "python-dateutil==2.4.2",
"python-xrd==0.1", "python-xrd==0.1",
], ],
test_require=[ test_require=[
"jsonschema==2.5.1",
"pytest==2.7.2", "pytest==2.7.2",
], ],
include_package_data=True, include_package_data=True,