Completed
Push — master ( 784599...e17a09 )
by
unknown
18s queued 12s
created

tests.test_mqtt.TestMQTT.test_publish_message()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
import logging
2
import threading
3
4
from unittest.case import TestCase
5
from unittest.mock import MagicMock, patch
6
7
from paho.mqtt.client import MQTTMessage
8
9
from ospd_openvas.mqtt import MQTTHandler, OpenvasMQTTHandler
10
11
12
@patch('paho.mqtt.client.Client', autospec=True)
13
class TestMQTT(TestCase):
14
    def test_on_message_not_implemented(self, mock_client: MagicMock):
15
        mqtt = MQTTHandler("foo", "bar")
16
17
        with self.assertRaises(NotImplementedError):
18
            mqtt.on_message(None, None, None)
19
20
    def test_publish_message(self, mock_client: MagicMock):
21
        mqtt = MQTTHandler("foo", "bar")
22
23
        logging.Logger.debug = MagicMock()
24
25
        mqtt.publish("foo/bar", "test123")
26
27
        logging.Logger.debug.assert_called_with(  # pylint: disable=no-member
28
            'Published message on topic %s.', "foo/bar"
29
        )
30
31
    def test_insert_results(self, mock_client: MagicMock):
32
        def start(self):
33
            self.function(*self.args, **self.kwargs)
34
35
        results_dict = {}
36
        expct_dict = {
37
            "1": [
38
                {
39
                    "type": "foo",
40
                    "host_ip": "127.0.0.1",
41
                    "hostname": "",
42
                    "port": "80",
43
                    "OID": "",
44
                    "value": "bar",
45
                }
46
            ]
47
        }
48
49
        msg = MQTTMessage()
50
        msg.topic = b"scanner/results"
51
        msg.payload = b'{"scan_id":"1","type":"foo","host_ip":"127.0.0.1","hostname":"","port":"80","OID":"","value":"bar"}'
52
53
        def add_results(results, scan_id):
54
            if not scan_id in results_dict:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable results_dict does not seem to be defined.
Loading history...
55
                results_dict[scan_id] = []
56
            results_dict[scan_id] += results
57
58
        with patch.object(threading.Timer, 'start', start):
59
            mqtt = OpenvasMQTTHandler("foo", add_results)
60
            mqtt.on_message(
61
                None,
62
                mqtt,
63
                msg,
64
            )
65
66
            self.assertDictEqual(results_dict, expct_dict)
67
68
    def test_json_format(self, mock_client: MagicMock):
69
70
        msg = MQTTMessage()
71
        msg.topic = b"scanner/results"
72
        msg.payload = b'{"scan_id":"1","type":"foo","host_ip":"127.0.0.1","hostname":"","port":"80","OID":"","value""bar"}'
73
74
        def do_nothing(results, scan_id):
75
            return
76
77
        logging.Logger.error = MagicMock()
78
79
        mqtt = OpenvasMQTTHandler("foo", do_nothing)
80
        mqtt.on_message(
81
            None,
82
            mqtt,
83
            msg,
84
        )
85
86
        logging.Logger.error.assert_called_with(  # pylint: disable=no-member
87
            "Got MQTT message in non-json format."
88
        )
89