Can receive message and record client record to file

This commit is contained in:
bj
2020-10-15 19:07:17 +02:00
parent fbf144622d
commit e580fd343b
2 changed files with 74 additions and 17 deletions
+36 -8
View File
@@ -1,5 +1,7 @@
import json
import ssl
import threading
import time
import unittest
from datetime import datetime
@@ -9,10 +11,15 @@ from torch_sub import torch_sub
host = "mqtt.example.com"
port = 8883
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"
agent_config_path = "agent-config/"
mqtt_ca_file = agent_config_path + "ca.crt"
mqtt_cert_file = agent_config_path + "vagrant.crt"
mqtt_key_file = agent_config_path + "vagrant.key"
subscriber_config_path = "subscriber-config/"
subscriber_ca_file = subscriber_config_path + "ca.crt"
subscriber_cert_file = subscriber_config_path + "subscriber.crt"
subscriber_key_file = subscriber_config_path + "subscriber.key"
def agent_connect():
@@ -48,19 +55,40 @@ def agent_publish(client_id, onion_hostname):
pass
def subscriber_thread(host, port, filename, topic, cafile, certfile, keyfile):
torch_sub.attach(host,
port,
filename,
topic=topic,
mqtt_ca_file=cafile,
mqtt_cert_file=certfile,
mqtt_key_file=keyfile)
class GivenBrokerAndTorchAgent(unittest.TestCase):
def test_when_agent_publishes_should_get_hostname_from_subscriber(self):
outfile = "clients.json"
clients_json = "clients.json"
threading.Thread(target=subscriber_thread,
args=(host,
port,
outfile,
"torch/+/wake",
subscriber_ca_file,
subscriber_cert_file,
subscriber_key_file),
daemon=True).start()
torch_sub.attach(host, port, clients_json)
time.sleep(0.5)
agent_publish("client1", "crazyonion.onion")
file = open(clients_json, "r")
time.sleep(0.5)
file = open(outfile, "r")
response = json.load(file)
self.assertEqual(response['results']['client1']['onionAddress'], "crazyonion.onion")
self.assertEqual(response['client1']['onionAddress'], "crazyonion.onion")
if __name__ == '__main__':