def test_add_topic_multiple_saved_topics(self): with test_database(test_db, (Log, Topic), create_tables=True): fixture = Topic.create(name='test/topic/test/xx') fixture = Topic.create(name='test/topic/test/xxx') topic = TopicsController() result = topic.add_topic(self.msg) parsedResponse = json.loads(result) self.assertEqual(3, len(parsedResponse['topics']))
def test_on_message_add_log_entry_to_not_valid_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') Topic.create(name='/test/topic/test1') self.msg = msg(topic='/test/topic/test2', payload='12') mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertEqual(0, Log.select().count())
def test_remove_topic_delete_existing_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): fixture = Topic.create(name='/test/topic/1') fixture = Topic.create(name='/test/topic/2') fixture = Topic.create(name='/test/topic/3') fixture = Topic.create(name='/test/topic/4') topic = TopicsController() result = topic.remove_topic(self.msg) parsedResponse = json.loads(result) self.assertEqual(3, len(parsedResponse['topics']))
def add_topic(self, msg): received_data = json.loads(msg.payload) payload = Utils().validate_data(received_data, MANAGEMENT_PASSWORD, ['password', 'client']) if payload.result == 'OK': new_topic, created = Topic.get_or_create( name=str(received_data['topic'])) saved_topics = [] for topic in Topic.select(): saved_topics.append(topic.name) payload.topics = saved_topics return payload.get_json()
def test_list_topics(self): with test_database(test_db, (Log, Topic), create_tables=True): self.payload['password'] = Settings.QUERY_PASSWORD self.msg.payload = json.dumps(self.payload) fixture = Topic.create(name='/test/topic/2') fixture = Topic.create(name='/test/topic/3') fixture = Topic.create(name='/test/topic/4') fixture = Topic.create(name='/test/topic/5') topic = TopicsController() result = topic.list_topics(self.msg) parsedResponse = json.loads(result) self.assertEqual('OK', parsedResponse['result']) self.assertEqual(4, len(parsedResponse['topics']))
def remove_topic(self, msg): received_data = json.loads(msg.payload) payload = Utils().validate_data(received_data, MANAGEMENT_PASSWORD, ['password', 'client']) if payload.result == 'OK': topic = Topic.select().where( Topic.name == str(received_data['topic'])) if topic.count() > 0: topic[0].delete_instance() else: payload.result = 'KO' payload.error = 'Topic not found' saved_topics = [] for topic in Topic.select(): saved_topics.append(topic.name) payload.topics = saved_topics return payload.get_json()
def list_topics(self, msg): received_data = json.loads(msg.payload) payload = Utils().validate_data(received_data, QUERY_PASSWORD, ['password', 'client'], topic=False) if payload.result == 'OK': saved_topics = [] for topic in Topic.select(): saved_topics.append(topic.name) payload.topics = saved_topics return payload.get_json()
def test_delete_entries_older_than_10_days_from_invalid_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now() - timedelta(days=40), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2/test2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic/test1') self.payload['options'] = 10 self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/invalid_topic' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/delete/days', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertEqual('OK', json.loads(self.client.last_publish)['result']) self.assertEqual('0', json.loads(self.client.last_publish)['values'])
def test_delete_entries_older_than_25_days(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now() - timedelta(days=40), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2/test2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic/test1') self.payload['options'] = 25 self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test1' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/delete/days', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) result_logs = Log.select().where(Log.topic == '/test/topic/test1') self.assertEqual(2, result_logs.count())
def test_on_message_delete_last_entry_from_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='10') Log.create(timestamp=datetime.now(), topic='/test/topic/test2', value='11') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='12') self.payload['options'] = None self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test1' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/delete/last', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) result_logs = Log.select().where(Log.topic == '/test/topic/test1') self.assertTrue('OK', json.loads(self.client.last_publish)['result']) self.assertEqual(1, result_logs.count()) self.assertEqual('10', result_logs[0].value)
def test_get_entries_with_invalid_time_range(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now() - timedelta(days=30), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic2/test2') Log.create(timestamp=datetime.now() - timedelta(days=20), value="12", topic='/test/topic/test1') Log.create(timestamp=datetime.now() - timedelta(days=10), value="12", topic='/test/topic/test1') self.payload['options'] = 25 self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test1' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/query/invalid_time', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertEqual('KO', json.loads(self.client.last_publish)['result']) self.assertEqual('Invalid unit time', json.loads(self.client.last_publish)['error'])
def test_on_message_get_last_entry_from_invalid_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='10') Log.create(timestamp=datetime.now(), topic='/test/topic/test2', value='11') Log.create(timestamp=datetime.now(), topic='/test/topic/test1', value='12') self.payload['options'] = None self.payload['password'] = Settings.QUERY_PASSWORD self.payload['topic'] = '/test/topic/test_invalid' self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/query/last', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertTrue('OK', json.loads(self.client.last_publish)['result']) self.assertFalse(json.loads(self.client.last_publish)['values'][0])
def test_on_connect(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') mqtt_controller = MqttController() mqtt_controller.on_connect(self.client) self.assertEqual(5, len(self.client.subscribed_topics))
def test_on_message_remove_topic(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') self.payload['options'] = None self.payload['topic'] = '/test/topic/test3' self.payload['password'] = Settings.MANAGEMENT_PASSWORD self.msg = msg(topic=Settings.ROOT_TOPIC + 'topic/remove', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertEqual(2, len(self.client.subscribed_topics)) self.assertEqual(2, Topic.select().count())
def test_on_message_list_topics_bad_password(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') self.payload['options'] = None self.payload['topic'] = '/test/topic/not_valid_topic' self.payload['password'] = '******' self.msg = msg(topic=Settings.ROOT_TOPIC + 'topic/list', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertFalse('topics' in json.loads(self.client.last_publish)) self.assertEqual('Bad Password', json.loads(self.client.last_publish)['error'])
def test_on_message_invalid_log_action(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') self.payload['options'] = None self.payload['password'] = Settings.QUERY_PASSWORD self.msg = msg(topic=Settings.ROOT_TOPIC + 'log/invalid_option', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertTrue('KO', json.loads(self.client.last_publish)['result']) self.assertEqual('Error: Invalid Log Option', json.loads(self.client.last_publish)['error'])
def test_on_message_list_topics_good_password(self): with test_database(test_db, (Log, Topic), create_tables=True): self.client.subscribed_topics = [] self.client.subscribe('/test/topic/test1') self.client.subscribe('/test/topic/test2') self.client.subscribe('/test/topic/test3') Topic.create(name='/test/topic/test1') Topic.create(name='/test/topic/test2') Topic.create(name='/test/topic/test3') self.payload['options'] = None self.payload['password'] = Settings.QUERY_PASSWORD self.msg = msg(topic=Settings.ROOT_TOPIC + 'topic/list', payload=json.dumps(self.payload)) mqtt_controller = MqttController() result = mqtt_controller.on_message(self.client, self.msg) self.assertTrue('OK', json.loads(self.client.last_publish)['result']) self.assertEqual( 3, len(json.loads(self.client.last_publish)['topics']))
def is_topic_subscribed(self, topic): if Topic.select().where(Topic.name == topic).count(): return True else: return False
def get_storaged_topics(self): return Topic.select()