def test_process_post_response_404(post_response): # If the response code is a rejection (lets say 404) it should raise TowerResponseError responses.add(responses.POST, add_appointment_endpoint, json=post_response, status=404) with pytest.raises(TowerResponseError): r = teos_client.post_request(json.dumps(dummy_appointment_data), add_appointment_endpoint) teos_client.process_post_response(r)
def test_process_post_response_not_json(post_response): # TowerResponseError should be raised if the response is not in json (independently of the status code) responses.add(responses.POST, add_appointment_endpoint, status=404) with pytest.raises(TowerResponseError): r = teos_client.post_request(json.dumps(dummy_appointment_data), add_appointment_endpoint) teos_client.process_post_response(r) responses.replace(responses.POST, add_appointment_endpoint, status=200) with pytest.raises(TowerResponseError): r = teos_client.post_request(json.dumps(dummy_appointment_data), add_appointment_endpoint) teos_client.process_post_response(r)
def test_add_appointment_trigger_on_cache_cannot_decrypt(teosd): _, teos_id = teosd commitment_tx, _, penalty_tx = create_txs() # Let's send the commitment to the network and mine a block generate_block_with_transactions(commitment_tx) sleep(1) # The appointment data is built using a random 32-byte value. appointment_data = build_appointment_data(get_random_value_hex(32), penalty_tx) # We cannot use teos_client.add_appointment here since it computes the locator internally, so let's do it manually. appointment_data["locator"] = compute_locator(bitcoin_cli.decoderawtransaction(commitment_tx).get("txid")) appointment_data["encrypted_blob"] = Cryptographer.encrypt(penalty_tx, get_random_value_hex(32)) appointment = Appointment.from_dict(appointment_data) signature = Cryptographer.sign(appointment.serialize(), user_sk) data = {"appointment": appointment.to_dict(), "signature": signature} # Send appointment to the server. response = teos_client.post_request(data, teos_add_appointment_endpoint) response_json = teos_client.process_post_response(response) # Check that the server has accepted the appointment tower_signature = response_json.get("signature") appointment_receipt = receipts.create_appointment_receipt(signature, response_json.get("start_block")) rpk = Cryptographer.recover_pk(appointment_receipt, tower_signature) assert teos_id == Cryptographer.get_compressed_pk(rpk) assert response_json.get("locator") == appointment.locator # The appointment should should have been immediately dropped with pytest.raises(TowerResponseError): get_appointment_info(teos_id, appointment_data["locator"])
def test_process_post_response(post_response): # A 200 OK with a correct json response should return the json of the response responses.add(responses.POST, add_appointment_endpoint, json=post_response, status=200) r = teos_client.post_request(json.dumps(dummy_appointment_data), add_appointment_endpoint) assert teos_client.process_post_response(r) == r.json()
def test_appointment_wrong_decryption_key(teosd): _, teos_id = teosd # This tests an appointment encrypted with a key that has not been derived from the same source as the locator. # Therefore the tower won't be able to decrypt the blob once the appointment is triggered. commitment_tx, _, penalty_tx = create_txs() # The appointment data is built using a random 32-byte value. appointment_data = build_appointment_data(get_random_value_hex(32), penalty_tx) # We cannot use teos_client.add_appointment here since it computes the locator internally, so let's do it manually. # We will encrypt the blob using the random value and derive the locator from the commitment tx. appointment_data["locator"] = compute_locator(bitcoin_cli.decoderawtransaction(commitment_tx).get("txid")) appointment_data["encrypted_blob"] = Cryptographer.encrypt(penalty_tx, get_random_value_hex(32)) appointment = Appointment.from_dict(appointment_data) signature = Cryptographer.sign(appointment.serialize(), user_sk) data = {"appointment": appointment.to_dict(), "signature": signature} # Send appointment to the server. response = teos_client.post_request(data, teos_add_appointment_endpoint) response_json = teos_client.process_post_response(response) # Check that the server has accepted the appointment tower_signature = response_json.get("signature") appointment_receipt = receipts.create_appointment_receipt(signature, response_json.get("start_block")) rpk = Cryptographer.recover_pk(appointment_receipt, tower_signature) assert teos_id == Cryptographer.get_compressed_pk(rpk) assert response_json.get("locator") == appointment.locator # Trigger the appointment generate_block_with_transactions(commitment_tx) # The appointment should have been removed since the decryption failed. with pytest.raises(TowerResponseError): get_appointment_info(teos_id, appointment.locator)