Sets add_appointment_endpoint from / to /add_appointment for consistency

Also passes the base_url to add_appointment and get_appointment and builds the full endpoint inside (also for consistency)
This commit is contained in:
Sergi Delgado Segura
2020-03-24 19:03:41 +01:00
parent 307fd7dabf
commit 6ee04bd303
5 changed files with 54 additions and 61 deletions

View File

@@ -109,9 +109,6 @@ def add_appointment(args, teos_url, config):
error occurs during the process.
"""
# Currently the base_url is the same as the add_appointment_endpoint
add_appointment_endpoint = teos_url
teos_pk, cli_sk, cli_pk_der = load_keys(
config.get("TEOS_PUBLIC_KEY"), config.get("CLI_PRIVATE_KEY"), config.get("CLI_PUBLIC_KEY")
)
@@ -159,7 +156,7 @@ def add_appointment(args, teos_url, config):
data = {"appointment": appointment.to_dict(), "signature": signature, "public_key": hex_pk_der.decode("utf-8")}
# Send appointment to the server.
server_response = post_appointment(data, add_appointment_endpoint)
server_response = post_appointment(data, teos_url)
if server_response is None:
return False
@@ -233,20 +230,22 @@ def parse_add_appointment_args(args):
return appointment_data
def post_appointment(data, add_appointment_endpoint):
def post_appointment(data, teos_url):
"""
Sends appointment data to add_appointment endpoint to be processed by the tower.
Args:
data (:obj:`dict`): a dictionary containing three fields: an appointment, the client-side signature, and the
der-encoded client public key.
add_appointment_endpoint (:obj:`str`): the teos endpoint where to send appointments to.
teos_url (:obj:`str`): the teos base url.
Returns:
:obj:`dict` or ``None``: a json-encoded dictionary with the server response if the data can be posted.
None otherwise.
"""
add_appointment_endpoint = "{}/add_appointment".format(teos_url)
logger.info("Sending appointment to the Eye of Satoshi")
try:
@@ -342,29 +341,28 @@ def save_appointment_receipt(appointment, signature, config):
return False
def get_appointment(locator, get_appointment_endpoint):
def get_appointment(locator, teos_url):
"""
Gets information about an appointment from the tower.
Args:
locator (:obj:`str`): the appointment locator used to identify it.
get_appointment_endpoint (:obj:`str`): the teos endpoint where to get appointments from.
teos_url (:obj:`str`): the teos base url.
Returns:
:obj:`dict` or :obj:`None`: a dictionary containing thew appointment data if the locator is valid and the tower
responds. ``None`` otherwise.
"""
get_appointment_endpoint = "{}/get_appointment".format(teos_url)
valid_locator = check_locator_format(locator)
if not valid_locator:
logger.error("The provided locator is not valid", locator=locator)
return None
parameters = "?locator={}".format(locator)
try:
r = requests.get(url=get_appointment_endpoint + parameters, timeout=5)
r = requests.get(url="{}?locator={}".format(get_appointment_endpoint, locator), timeout=5)
return r.json()
except ConnectTimeout:
@@ -414,8 +412,7 @@ def main(args, command_line_conf):
if arg_opt in ["-h", "--help"]:
sys.exit(help_get_appointment())
get_appointment_endpoint = "{}/get_appointment".format(teos_url)
appointment_data = get_appointment(arg_opt, get_appointment_endpoint)
appointment_data = get_appointment(arg_opt, teos_url)
if appointment_data:
print(appointment_data)

View File

@@ -189,7 +189,7 @@ class API:
"""
routes = {
"/": (self.add_appointment, ["POST"]),
"/add_appointment": (self.add_appointment, ["POST"]),
"/get_appointment": (self.get_appointment, ["GET"]),
"/get_all_appointments": (self.get_all_appointments, ["GET"]),
}

View File

@@ -24,11 +24,9 @@ dummy_sk = PrivateKey()
dummy_pk = dummy_sk.public_key
another_sk = PrivateKey()
# Replace the key in the module with a key we control for the tests
teos_cli.teos_public_key = dummy_pk
# Replace endpoint with dummy one
teos_endpoint = "http://{}:{}/".format(config.get("TEOS_SERVER"), config.get("TEOS_PORT"))
teos_url = "http://{}:{}".format(config.get("TEOS_SERVER"), config.get("TEOS_PORT"))
add_appointment_endpoint = "{}/add_appointment".format(teos_url)
get_appointment_endpoint = "{}/get_appointment".format(teos_url)
dummy_appointment_request = {
"tx": get_random_value_hex(192),
@@ -102,16 +100,17 @@ def test_load_keys():
# TODO: 90-add-more-add-appointment-tests
@responses.activate
def test_add_appointment(monkeypatch):
# Simulate a request to add_appointment for dummy_appointment, make sure that the right endpoint is requested
# and the return value is True
monkeypatch.setattr(teos_cli, "load_keys", load_dummy_keys)
response = {"locator": dummy_appointment.locator, "signature": get_dummy_signature()}
responses.add(responses.POST, teos_endpoint, json=response, status=200)
result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], teos_endpoint, config)
responses.add(responses.POST, add_appointment_endpoint, json=response, status=200)
result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], teos_url, config)
assert len(responses.calls) == 1
assert responses.calls[0].request.url == teos_endpoint
assert responses.calls[0].request.url == add_appointment_endpoint
assert result
@@ -128,8 +127,8 @@ def test_add_appointment_with_invalid_signature(monkeypatch):
"signature": get_bad_signature(), # Sign with a bad key
}
responses.add(responses.POST, teos_endpoint, json=response, status=200)
result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], teos_endpoint, config)
responses.add(responses.POST, add_appointment_endpoint, json=response, status=200)
result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], teos_url, config)
shutil.rmtree(config.get("APPOINTMENTS_FOLDER_NAME"))
@@ -166,11 +165,11 @@ def test_post_appointment():
"signature": Cryptographer.sign(dummy_appointment.serialize(), dummy_pk),
}
responses.add(responses.POST, teos_endpoint, json=response, status=200)
response = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_endpoint)
responses.add(responses.POST, add_appointment_endpoint, json=response, status=200)
response = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_url)
assert len(responses.calls) == 1
assert responses.calls[0].request.url == teos_endpoint
assert responses.calls[0].request.url == add_appointment_endpoint
assert response
@@ -183,18 +182,18 @@ def test_process_post_appointment_response():
}
# A 200 OK with a correct json response should return the json of the response
responses.add(responses.POST, teos_endpoint, json=response, status=200)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_endpoint)
responses.add(responses.POST, add_appointment_endpoint, json=response, status=200)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_url)
assert teos_cli.process_post_appointment_response(r) == r.json()
# If we modify the response code tor a rejection (lets say 404) we should get None
responses.replace(responses.POST, teos_endpoint, json=response, status=404)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_endpoint)
responses.replace(responses.POST, add_appointment_endpoint, json=response, status=404)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_url)
assert teos_cli.process_post_appointment_response(r) is None
# The same should happen if the response is not in json
responses.replace(responses.POST, teos_endpoint, status=404)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_endpoint)
responses.replace(responses.POST, add_appointment_endpoint, status=404)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), teos_url)
assert teos_cli.process_post_appointment_response(r) is None
@@ -219,11 +218,10 @@ def test_get_appointment():
# Response of get_appointment endpoint is an appointment with status added to it.
dummy_appointment_full["status"] = "being_watched"
response = dummy_appointment_full
get_appointment_endpoint = teos_endpoint + "get_appointment"
request_url = "{}?locator={}".format(get_appointment_endpoint, response.get("locator"))
responses.add(responses.GET, request_url, json=response, status=200)
result = teos_cli.get_appointment(response.get("locator"), get_appointment_endpoint)
result = teos_cli.get_appointment(response.get("locator"), teos_url)
assert len(responses.calls) == 1
assert responses.calls[0].request.url == request_url
@@ -233,10 +231,9 @@ def test_get_appointment():
@responses.activate
def test_get_appointment_err():
locator = get_random_value_hex(16)
get_appointment_endpoint = teos_endpoint + "get_appointment"
# Test that get_appointment handles a connection error appropriately.
request_url = "{}?locator={}".format(get_appointment_endpoint, locator)
responses.add(responses.GET, request_url, body=ConnectionError())
assert not teos_cli.get_appointment(locator, get_appointment_endpoint)
assert not teos_cli.get_appointment(locator, teos_url)

View File

@@ -24,14 +24,9 @@ from test.teos.e2e.conftest import (
cli_config = get_config(DATA_DIR, CONF_FILE_NAME, DEFAULT_CONF)
common.cryptographer.logger = Logger(actor="Cryptographer", log_name_prefix="")
# # We'll use teos_cli to add appointments. The expected input format is a list of arguments with a json-encoded
# # appointment
# teos_cli.teos_api_server = "http://{}".format(HOST)
# teos_cli.teos_api_port = PORT
teos_base_endpoint = "http://{}:{}".format(cli_config.get("TEOS_SERVER"), cli_config.get("TEOS_PORT"))
teos_add_appointment_endpoint = teos_base_endpoint
teos_get_appointment_endpoint = teos_base_endpoint + "/get_appointment"
teos_add_appointment_endpoint = "{}/add_appointment".format(teos_base_endpoint)
teos_get_appointment_endpoint = "{}/get_appointment".format(teos_base_endpoint)
# Run teosd
teosd_process = run_teosd()
@@ -46,7 +41,7 @@ def broadcast_transaction_and_mine_block(bitcoin_cli, commitment_tx, addr):
def get_appointment_info(locator):
# Check that the justice has been triggered (the appointment has moved from Watcher to Responder)
sleep(1) # Let's add a bit of delay so the state can be updated
return teos_cli.get_appointment(locator, teos_get_appointment_endpoint)
return teos_cli.get_appointment(locator, teos_base_endpoint)
def test_appointment_life_cycle(bitcoin_cli, create_txs):
@@ -55,7 +50,7 @@ def test_appointment_life_cycle(bitcoin_cli, create_txs):
appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx)
locator = compute_locator(commitment_tx_id)
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_base_endpoint, cli_config) is True
appointment_info = get_appointment_info(locator)
assert appointment_info is not None
@@ -105,7 +100,7 @@ def test_appointment_malformed_penalty(bitcoin_cli, create_txs):
appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, mod_penalty_tx.hex())
locator = compute_locator(commitment_tx_id)
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_base_endpoint, cli_config) is True
# Broadcast the commitment transaction and mine a block
new_addr = bitcoin_cli.getnewaddress()
@@ -143,7 +138,7 @@ def test_appointment_wrong_key(bitcoin_cli, create_txs):
data = {"appointment": appointment.to_dict(), "signature": signature, "public_key": hex_pk_der.decode("utf-8")}
# Send appointment to the server.
response = teos_cli.post_appointment(data, teos_add_appointment_endpoint)
response = teos_cli.post_appointment(data, teos_base_endpoint)
response_json = teos_cli.process_post_appointment_response(response)
# Check that the server has accepted the appointment
@@ -179,8 +174,8 @@ def test_two_identical_appointments(bitcoin_cli, create_txs):
locator = compute_locator(commitment_tx_id)
# Send the appointment twice
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_base_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_base_endpoint, cli_config) is True
# Broadcast the commitment transaction and mine a block
new_addr = bitcoin_cli.getnewaddress()
@@ -213,8 +208,8 @@ def test_two_appointment_same_locator_different_penalty(bitcoin_cli, create_txs)
appointment2_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx2)
locator = compute_locator(commitment_tx_id)
assert teos_cli.add_appointment([json.dumps(appointment1_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment2_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment1_data)], teos_base_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment2_data)], teos_base_endpoint, cli_config) is True
# Broadcast the commitment transaction and mine a block
new_addr = bitcoin_cli.getnewaddress()
@@ -241,7 +236,7 @@ def test_appointment_shutdown_teos_trigger_back_online(create_txs, bitcoin_cli):
appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx)
locator = compute_locator(commitment_tx_id)
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_base_endpoint, cli_config) is True
# Restart teos
teosd_process.terminate()
@@ -279,7 +274,7 @@ def test_appointment_shutdown_teos_trigger_while_offline(create_txs, bitcoin_cli
appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx)
locator = compute_locator(commitment_tx_id)
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True
assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_base_endpoint, cli_config) is True
# Check that the appointment is still in the Watcher
appointment_info = get_appointment_info(locator)

View File

@@ -28,6 +28,10 @@ from common.constants import LOCATOR_LEN_BYTES
TEOS_API = "http://{}:{}".format(HOST, PORT)
add_appointment_endpoint = "{}/add_appointment".format(TEOS_API)
get_appointment_endpoint = "{}/get_appointment".format(TEOS_API)
get_all_appointment_endpoint = "{}/get_all_appointments".format(TEOS_API)
MULTIPLE_APPOINTMENTS = 10
appointments = []
@@ -68,7 +72,7 @@ def new_appt_data():
def add_appointment(new_appt_data):
r = requests.post(url=TEOS_API, json=json.dumps(new_appt_data), timeout=5)
r = requests.post(url=add_appointment_endpoint, json=json.dumps(new_appt_data), timeout=5)
if r.status_code == 200:
appointments.append(new_appt_data["appointment"])
@@ -88,7 +92,7 @@ def test_add_appointment(run_api, run_bitcoind, new_appt_data):
def test_request_random_appointment():
r = requests.get(url=TEOS_API + "/get_appointment?locator=" + get_random_value_hex(LOCATOR_LEN_BYTES))
r = requests.get(url="{}?locator={}".format(get_appointment_endpoint, get_random_value_hex(LOCATOR_LEN_BYTES)))
assert r.status_code == 200
received_appointments = json.loads(r.content)
@@ -123,7 +127,7 @@ def test_add_too_many_appointment(new_appt_data):
def test_get_all_appointments_watcher():
r = requests.get(url=TEOS_API + "/get_all_appointments")
r = requests.get(url=get_all_appointment_endpoint)
assert r.status_code == 200 and r.reason == "OK"
received_appointments = json.loads(r.content)
@@ -147,7 +151,7 @@ def test_get_all_appointments_responder():
generate_blocks(6)
# Get all appointments
r = requests.get(url=TEOS_API + "/get_all_appointments")
r = requests.get(url=get_all_appointment_endpoint)
received_appointments = json.loads(r.content)
# Make sure there is not pending locator in the watcher
@@ -164,7 +168,7 @@ def test_request_appointment_watcher(new_appt_data):
assert r.status_code == 200
# Next we can request it
r = requests.get(url=TEOS_API + "/get_appointment?locator=" + new_appt_data["appointment"]["locator"])
r = requests.get(url="{}?locator={}".format(get_appointment_endpoint, new_appt_data["appointment"]["locator"]))
assert r.status_code == 200
# Each locator may point to multiple appointments, check them all
@@ -173,7 +177,7 @@ def test_request_appointment_watcher(new_appt_data):
# Take the status out and leave the received appointments ready to compare
appointment_status = [appointment.pop("status") for appointment in received_appointments]
# Check that the appointment is within the received appoints
# Check that the appointment is within the received appointments
assert new_appt_data["appointment"] in received_appointments
# Check that all the appointments are being watched
@@ -191,7 +195,7 @@ def test_request_appointment_responder(new_appt_data):
# Generate a block to trigger the watcher
generate_block()
r = requests.get(url=TEOS_API + "/get_appointment?locator=" + new_appt_data["appointment"]["locator"])
r = requests.get(url="{}?locator={}".format(get_appointment_endpoint, new_appt_data["appointment"]["locator"]))
assert r.status_code == 200
received_appointments = json.loads(r.content)