Fixes bug when dealing with empty JSON requests or empty appointment field

When posting a request via requests.post the json field was dumped to json, but it shouldn't have been since requests deals with this internally. That meant that the requests made by the code didn't match proper JSON.
In line with this, the API was only parsing this type POST requests correctly, making add_appointment to fail if a proper formatted JSON was passed.

On top of that, empty appointments were not checked in the Inspector before trying to get data from them, making it crash if a JSON was posted to add_appointment not containing the `appointment` field. Unit tests for this should be added.
This commit is contained in:
Sergi Delgado Segura
2020-03-24 19:55:41 +01:00
parent 6ee04bd303
commit dd53ad68fb
4 changed files with 6 additions and 4 deletions

View File

@@ -249,7 +249,7 @@ def post_appointment(data, teos_url):
logger.info("Sending appointment to the Eye of Satoshi")
try:
return requests.post(url=add_appointment_endpoint, json=json.dumps(data), timeout=5)
return requests.post(url=add_appointment_endpoint, json=data, timeout=5)
except ConnectTimeout:
logger.error("Can't connect to the Eye of Satoshi's API. Connection timeout")

View File

@@ -1,5 +1,4 @@
import os
import json
import logging
from flask import Flask, request, abort, jsonify
@@ -55,7 +54,7 @@ class API:
if request.is_json:
# Check content type once if properly defined
request_data = json.loads(request.get_json())
request_data = request.get_json()
appointment = self.inspector.inspect(
request_data.get("appointment"), request_data.get("signature"), request_data.get("public_key")
)

View File

@@ -54,6 +54,9 @@ class Inspector:
Errors are defined in :mod:`Errors <teos.errors>`.
"""
if appointment_data is None:
return errors.APPOINTMENT_EMPTY_FIELD, "empty appointment received"
block_height = self.block_processor.get_block_count()
if block_height is not None:

View File

@@ -72,7 +72,7 @@ def new_appt_data():
def add_appointment(new_appt_data):
r = requests.post(url=add_appointment_endpoint, json=json.dumps(new_appt_data), timeout=5)
r = requests.post(url=add_appointment_endpoint, json=new_appt_data, timeout=5)
if r.status_code == 200:
appointments.append(new_appt_data["appointment"])