Skip to content

Commit e8994f7

Browse files
committed
Validate the SpaceAPI JSON using the validator service
1 parent f33b21f commit e8994f7

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

tests/test_spaceapi.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1+
import requests
12
from spacedirectory.models.space import Space
23

34
from tests.utils import FastAPIVCRTestCase, client
45

56

7+
def validate_spacejson_schema(data):
8+
"""
9+
Call the SpaceAPI validator service
10+
"""
11+
resp = requests.post(
12+
"https://validator.spaceapi.io/v2/validateJSON",
13+
headers={"Content-Type": "application/json"},
14+
json=data,
15+
)
16+
17+
assert resp.ok
18+
rdata = resp.json()
19+
20+
# If we have schema errors, print them so they'll be visible when the test fails
21+
if "schemaErrors" in rdata and len(rdata["schemaErrors"]):
22+
for err in rdata["schemaErrors"]:
23+
print(err)
24+
assert rdata["valid"] is True
25+
26+
627
class SpaceAPINoNetworkTestCase(FastAPIVCRTestCase):
728
"""
829
Test the SpaceAPI when we've got no on-going network to the backend systems
930
"""
1031

1132
vcr_enabled = False
1233

13-
def test_space_json_no_network(self):
34+
def setUp(self):
1435
# Disable the service functions (covered by separate tests)
1536
# Use the 'imported path' rather than the module path, see: https://nedbatchelder.com/blog/201908/why_your_mock_doesnt_work.html#h_mock_it_where_its_used
1637
self.mocker.patch("hackspaceapi.spaceapi.get_entity_state", return_value=None)
17-
self.mocker.patch(
18-
"hackspaceapi.spaceapi.get_prometheus_metric", return_value=None
19-
)
38+
self.mocker.patch("hackspaceapi.spaceapi.get_prometheus_metric", return_value=None)
2039
self.mocker.patch("hackspaceapi.spaceapi.get_membership_data", return_value=[])
2140

41+
def test_space_json_no_network(self):
2242
response = client.get("/space.json")
2343
assert response.status_code == 200
2444
assert response.headers["Content-Type"] == "application/json"
@@ -39,6 +59,10 @@ def test_space_json_no_network(self):
3959
# Last change shouldn't be in the state
4060
assert "lastchange" not in data["state"]
4161

62+
def test_validate_space_json_no_network(self):
63+
data = client.get("/space.json").json()
64+
validate_spacejson_schema(data)
65+
4266

4367
class SpaceAPITestCase(FastAPIVCRTestCase):
4468
"""
@@ -56,3 +80,7 @@ def test_space_json(self):
5680

5781
# Check we provide a valid SpaceAPI JSON
5882
assert type(Space(data)) == Space
83+
84+
def test_validate_space_json(self):
85+
data = client.get("/space.json").json()
86+
validate_spacejson_schema(data)

tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __inject_fixtures(self, mocker):
2525
def _get_vcr(self, **kwargs):
2626
myvcr = super(FastAPIVCRTestCase, self)._get_vcr(**kwargs)
2727
myvcr.ignore_localhost = True
28-
myvcr.ignore_hosts = ["testserver"]
28+
myvcr.ignore_hosts = ["testserver", "validator.spaceapi.io"]
2929
myvcr.filter_headers = ["Authorization"]
3030
myvcr.filter_query_parameters = ["start", "end"]
3131
myvcr.match_on = ["method", "scheme", "port", "path", "query"]

0 commit comments

Comments
 (0)