Add test for schema validation

This commit is contained in:
polyfloyd 2025-05-12 20:37:08 +02:00
parent 9cc8b0fa40
commit 43650e2301
2 changed files with 81 additions and 54 deletions

118
server.py
View file

@ -6,74 +6,84 @@ import tornado.ioloop
import tornado.web
PORT = 8888
BASE_URL = 'https://bitlair.nl/state'
MQTT_HOST = 'bitlair.nl'
PORT = 8888
BASE_URL = "https://bitlair.nl/state"
MQTT_HOST = "bitlair.nl"
current_state = False
current_state = False
current_state_change = time.time()
def state():
global current_state, current_state_change
return {
"api": "0.13",
"space": "Bitlair",
"logo": BASE_URL + "/logo.png",
"url": "https://bitlair.nl/",
"contact": {
"phone": "+31337114666",
"irc": "irc://irc.smurfnet.ch/bitlair",
"twitter": "@bitlair",
"email": "info@bitlair.nl",
"ml": "general@list.bitlair.nl",
},
"spacefed": {
"spacenet": True,
"spacesaml": True,
"spacephone": False,
},
"location": {
"address": "Nijverheidsweg-Noord 77, 3812 PK Amersfoort, The Netherlands",
"lat": 52.1697399,
"lon": 5.3561364,
},
"state": {
"open": current_state,
"lastchange": int(current_state_change),
"icon": {
"open": BASE_URL + "/open.png",
"closed": BASE_URL + "/closed.png",
},
"mqtt": {
"host": "bitlair.nl",
"port": 1883,
"tls": False,
"topic": "bitlair/state",
"closed": "closed",
"open": "open",
},
},
"issue_report_channels": ["twitter"],
}
class StatejsonHandler(tornado.web.RequestHandler):
def get(self):
global current_state, current_state_change
state = {
'api': '0.13',
'space': 'Bitlair',
'logo': BASE_URL+'/logo.png',
'url': 'https://bitlair.nl/',
'contact': {
'phone': '+31337114666',
'irc': 'irc://irc.smurfnet.ch/bitlair',
'twitter': '@bitlair',
'email': 'info@bitlair.nl',
'ml': 'general@list.bitlair.nl',
},
'spacefed': {
'spacenet': True,
'spacesaml': True,
'spacephone': False,
},
'location': {
'address': 'Nijverheidsweg-Noord 77, 3812 PK Amersfoort, The Netherlands',
'lat': 52.1697399,
'lon': 5.3561364,
},
'state': {
'open': current_state,
'lastchange': int(current_state_change),
'icon': {
'open': BASE_URL+'/open.png',
'closed': BASE_URL+'/closed.png',
},
'mqtt': {
'host': 'bitlair.nl',
'port': 1883,
'tls': False,
'topic': 'bitlair/state',
'closed': 'closed',
'open': 'open'
},
},
'issue_report_channels': [ 'twitter' ],
}
self.write(state)
self.write(state())
def make_app():
return tornado.web.Application([
(r'/statejson', StatejsonHandler),
])
return tornado.web.Application(
[
(r"/statejson", StatejsonHandler),
]
)
def on_message(client, userdata, message):
global current_state, current_state_change
if message.topic == 'bitlair/state':
current_state = message.payload == b'open'
if message.topic == "bitlair/state":
current_state = message.payload == b"open"
current_state_change = time.time()
def on_connect(client, userdata, flags, rc):
client.subscribe('bitlair/state', qos=2)
if __name__ == '__main__':
def on_connect(client, userdata, flags, rc):
client.subscribe("bitlair/state", qos=2)
if __name__ == "__main__":
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect

17
server_test.py Normal file
View file

@ -0,0 +1,17 @@
import requests
from jsonschema import validate
import server
def test_schema_compliance():
resp = requests.get(
"https://raw.githubusercontent.com/SpaceApi/schema/refs/heads/master/13.json"
)
resp.raise_for_status()
schema = resp.json()
spaceapi = server.state()
validate(instance=spaceapi, schema=schema)