Adds cli unit tests

This commit is contained in:
Sergi Delgado Segura
2020-03-21 22:01:49 +01:00
parent 3d389a92ac
commit 2587e25637
2 changed files with 27 additions and 14 deletions

View File

@@ -1,6 +1,10 @@
import pytest import pytest
import random import random
from cli import DEFAULT_CONF
from common.config_loader import ConfigLoader
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
def prng_seed(): def prng_seed():
@@ -11,3 +15,10 @@ def get_random_value_hex(nbytes):
pseudo_random_value = random.getrandbits(8 * nbytes) pseudo_random_value = random.getrandbits(8 * nbytes)
prv_hex = "{:x}".format(pseudo_random_value) prv_hex = "{:x}".format(pseudo_random_value)
return prv_hex.zfill(2 * nbytes) return prv_hex.zfill(2 * nbytes)
def get_config():
config_loader = ConfigLoader(".", "teos_cli.conf", DEFAULT_CONF, {})
config = config_loader.build_config()
return config

View File

@@ -12,10 +12,12 @@ from common.cryptographer import Cryptographer
from common.blob import Blob from common.blob import Blob
import cli.teos_cli as teos_cli import cli.teos_cli as teos_cli
from test.cli.unit.conftest import get_random_value_hex from test.cli.unit.conftest import get_random_value_hex, get_config
common.cryptographer.logger = Logger(actor="Cryptographer", log_name_prefix=teos_cli.LOG_PREFIX) common.cryptographer.logger = Logger(actor="Cryptographer", log_name_prefix=teos_cli.LOG_PREFIX)
config = get_config()
# dummy keys for the tests # dummy keys for the tests
dummy_sk = PrivateKey() dummy_sk = PrivateKey()
dummy_pk = dummy_sk.public_key dummy_pk = dummy_sk.public_key
@@ -25,9 +27,7 @@ another_sk = PrivateKey()
# Replace the key in the module with a key we control for the tests # Replace the key in the module with a key we control for the tests
teos_cli.teos_public_key = dummy_pk teos_cli.teos_public_key = dummy_pk
# Replace endpoint with dummy one # Replace endpoint with dummy one
teos_cli.teos_api_server = "https://dummy.com" teos_endpoint = "{}:{}/".format(config.get("TEOS_SERVER"), config.get("TEOS_PORT"))
teos_cli.teos_api_port = 12345
teos_endpoint = "{}:{}/".format(teos_cli.teos_api_server, teos_cli.teos_api_port)
dummy_appointment_request = { dummy_appointment_request = {
"tx": get_random_value_hex(192), "tx": get_random_value_hex(192),
@@ -107,7 +107,7 @@ def test_add_appointment(monkeypatch):
response = {"locator": dummy_appointment.locator, "signature": get_dummy_signature()} response = {"locator": dummy_appointment.locator, "signature": get_dummy_signature()}
responses.add(responses.POST, teos_endpoint, json=response, status=200) responses.add(responses.POST, teos_endpoint, json=response, status=200)
result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)]) result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], config)
assert len(responses.calls) == 1 assert len(responses.calls) == 1
assert responses.calls[0].request.url == teos_endpoint assert responses.calls[0].request.url == teos_endpoint
@@ -128,7 +128,9 @@ def test_add_appointment_with_invalid_signature(monkeypatch):
} }
responses.add(responses.POST, teos_endpoint, json=response, status=200) responses.add(responses.POST, teos_endpoint, json=response, status=200)
result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)]) result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], config)
shutil.rmtree(config.get("APPOINTMENTS_FOLDER_NAME"))
assert result is False assert result is False
@@ -164,7 +166,7 @@ def test_post_appointment():
} }
responses.add(responses.POST, teos_endpoint, json=response, status=200) responses.add(responses.POST, teos_endpoint, json=response, status=200)
response = teos_cli.post_appointment(json.dumps(dummy_appointment_request)) response = teos_cli.post_appointment(json.dumps(dummy_appointment_request), config)
assert len(responses.calls) == 1 assert len(responses.calls) == 1
assert responses.calls[0].request.url == teos_endpoint assert responses.calls[0].request.url == teos_endpoint
@@ -181,27 +183,27 @@ def test_process_post_appointment_response():
# A 200 OK with a correct json response should return the json of the 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) responses.add(responses.POST, teos_endpoint, json=response, status=200)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request)) r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), config)
assert teos_cli.process_post_appointment_response(r) == r.json() 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 # 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) responses.replace(responses.POST, teos_endpoint, json=response, status=404)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request)) r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), config)
assert teos_cli.process_post_appointment_response(r) is None assert teos_cli.process_post_appointment_response(r) is None
# The same should happen if the response is not in json # The same should happen if the response is not in json
responses.replace(responses.POST, teos_endpoint, status=404) responses.replace(responses.POST, teos_endpoint, status=404)
r = teos_cli.post_appointment(json.dumps(dummy_appointment_request)) r = teos_cli.post_appointment(json.dumps(dummy_appointment_request), config)
assert teos_cli.process_post_appointment_response(r) is None assert teos_cli.process_post_appointment_response(r) is None
def test_save_appointment_receipt(monkeypatch): def test_save_appointment_receipt(monkeypatch):
appointments_folder = "test_appointments_receipts" appointments_folder = "test_appointments_receipts"
teos_cli.config["APPOINTMENTS_FOLDER_NAME"] = appointments_folder config["APPOINTMENTS_FOLDER_NAME"] = appointments_folder
# The functions creates a new directory if it does not exist # The functions creates a new directory if it does not exist
assert not os.path.exists(appointments_folder) assert not os.path.exists(appointments_folder)
teos_cli.save_appointment_receipt(dummy_appointment.to_dict(), get_dummy_signature()) teos_cli.save_appointment_receipt(dummy_appointment.to_dict(), get_dummy_signature(), config)
assert os.path.exists(appointments_folder) assert os.path.exists(appointments_folder)
# Check that the receipt has been saved by checking the file names # Check that the receipt has been saved by checking the file names
@@ -219,7 +221,7 @@ def test_get_appointment():
request_url = "{}get_appointment?locator={}".format(teos_endpoint, response.get("locator")) request_url = "{}get_appointment?locator={}".format(teos_endpoint, response.get("locator"))
responses.add(responses.GET, request_url, json=response, status=200) responses.add(responses.GET, request_url, json=response, status=200)
result = teos_cli.get_appointment(response.get("locator")) result = teos_cli.get_appointment(response.get("locator"), config)
assert len(responses.calls) == 1 assert len(responses.calls) == 1
assert responses.calls[0].request.url == request_url assert responses.calls[0].request.url == request_url
@@ -234,4 +236,4 @@ def test_get_appointment_err():
request_url = "{}get_appointment?locator=".format(teos_endpoint, locator) request_url = "{}get_appointment?locator=".format(teos_endpoint, locator)
responses.add(responses.GET, request_url, body=ConnectionError()) responses.add(responses.GET, request_url, body=ConnectionError())
assert not teos_cli.get_appointment(locator) assert not teos_cli.get_appointment(locator, config)