From fbf144622d96ae63e6d17807be92f446f3addaa8 Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Thu, 15 Oct 2020 16:44:34 +0200 Subject: [PATCH] Setup integration test to test data file --- .gitignore | 3 ++- torch_sub/test/integration_pub_sub.py | 34 ++++++++++++++++----------- torch_sub/torch_sub.py | 17 ++++++++++++-- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 82195aa..b39cc1b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ venv -.idea \ No newline at end of file +.idea +/torch_sub/test/clients.json \ No newline at end of file diff --git a/torch_sub/test/integration_pub_sub.py b/torch_sub/test/integration_pub_sub.py index a78b48e..5c87a48 100644 --- a/torch_sub/test/integration_pub_sub.py +++ b/torch_sub/test/integration_pub_sub.py @@ -9,18 +9,18 @@ from torch_sub import torch_sub host = "mqtt.example.com" port = 8883 -configPath = "agent-config/" -mqttCaFile = configPath + "ca.crt" -mqttCertFile = configPath + "vagrant.crt" -mqttKeyFile = configPath + "vagrant.key" +config_path = "agent-config/" +mqtt_ca_file = config_path + "ca.crt" +mqtt_cert_file = config_path + "vagrant.crt" +mqtt_key_file = config_path + "vagrant.key" def agent_connect(): client = mqtt.Client() client.tls_set( - ca_certs=mqttCaFile, - certfile=mqttCertFile, - keyfile=mqttKeyFile, + ca_certs=mqtt_ca_file, + certfile=mqtt_cert_file, + keyfile=mqtt_key_file, cert_reqs=ssl.CERT_REQUIRED) client.connect(host, port, 60) return client @@ -39,22 +39,28 @@ def agent_publish(client_id, onion_hostname): topic = "torch/" + client_id + "/wake" client.publish(topic, json.dumps(payload)) - print("Log: Connected to MQTT Broker at %s://%s:%s/%s" % ("mqtts", host, port, topic)) - print("Log: Published payload: " + json.dumps(payload)) + print("Debug: Connected to MQTT Broker at %s://%s:%s/%s" % ("mqtts", host, port, topic)) + print("Debug: Published payload: " + json.dumps(payload)) client.disconnect() - print("Log: Disconnected from MQTT Broker") + print("Debug: Disconnected from MQTT Broker") pass -class MyTestCase(unittest.TestCase): - def test_something(self): - torch_sub.attach(host, port) +class GivenBrokerAndTorchAgent(unittest.TestCase): + def test_when_agent_publishes_should_get_hostname_from_subscriber(self): + + clients_json = "clients.json" + + torch_sub.attach(host, port, clients_json) agent_publish("client1", "crazyonion.onion") - self.assertEqual(True, False) + file = open(clients_json, "r") + response = json.load(file) + + self.assertEqual(response['results']['client1']['onionAddress'], "crazyonion.onion") if __name__ == '__main__': diff --git a/torch_sub/torch_sub.py b/torch_sub/torch_sub.py index d37b906..fd9b873 100644 --- a/torch_sub/torch_sub.py +++ b/torch_sub/torch_sub.py @@ -1,2 +1,15 @@ -def attach(host, port): - return None \ No newline at end of file +import json + + +def attach(host, port, datafile): + + response = { + 'results': { + 'client1': { + 'onionAddress': 'crazytime' + } + } + } + + with open(datafile, 'w') as outfile: + json.dump(response, outfile) \ No newline at end of file