2016-03-08 00:45:37 +00:00
|
|
|
"""The tests for the MQTT component embedded server."""
|
2016-10-18 03:16:36 +00:00
|
|
|
from unittest.mock import Mock, MagicMock, patch
|
2016-03-08 00:45:37 +00:00
|
|
|
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-03-08 00:45:37 +00:00
|
|
|
import homeassistant.components.mqtt as mqtt
|
|
|
|
|
2018-02-15 21:06:14 +00:00
|
|
|
from tests.common import get_test_home_assistant, mock_coro
|
2016-03-08 00:45:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMQTT:
|
|
|
|
"""Test the MQTT component."""
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
|
|
|
"""Setup things to be run when tests are started."""
|
|
|
|
self.hass = get_test_home_assistant()
|
2018-02-15 21:06:14 +00:00
|
|
|
setup_component(self.hass, 'http', {
|
|
|
|
'api_password': 'super_secret'
|
|
|
|
})
|
2016-03-08 00:45:37 +00:00
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
@patch('passlib.apps.custom_app_context', Mock(return_value=''))
|
|
|
|
@patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock()))
|
2016-11-11 06:45:38 +00:00
|
|
|
@patch('hbmqtt.broker.Broker', Mock(return_value=MagicMock()))
|
2017-02-18 22:17:18 +00:00
|
|
|
@patch('hbmqtt.broker.Broker.start', Mock(return_value=mock_coro()))
|
2016-03-08 00:45:37 +00:00
|
|
|
@patch('homeassistant.components.mqtt.MQTT')
|
2016-11-11 06:45:38 +00:00
|
|
|
def test_creating_config_with_http_pass(self, mock_mqtt):
|
2016-03-08 00:45:37 +00:00
|
|
|
"""Test if the MQTT server gets started and subscribe/publish msg."""
|
2017-02-20 06:43:10 +00:00
|
|
|
mock_mqtt().async_connect.return_value = mock_coro(True)
|
2016-11-11 06:45:38 +00:00
|
|
|
self.hass.bus.listen_once = MagicMock()
|
2016-03-08 00:45:37 +00:00
|
|
|
password = 'super_secret'
|
|
|
|
|
|
|
|
self.hass.config.api = MagicMock(api_password=password)
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, mqtt.DOMAIN, {})
|
2016-03-08 00:45:37 +00:00
|
|
|
assert mock_mqtt.called
|
2017-03-01 04:33:19 +00:00
|
|
|
from pprint import pprint
|
|
|
|
pprint(mock_mqtt.mock_calls)
|
2017-02-20 06:43:10 +00:00
|
|
|
assert mock_mqtt.mock_calls[1][1][5] == 'homeassistant'
|
|
|
|
assert mock_mqtt.mock_calls[1][1][6] == password
|
2016-03-08 00:45:37 +00:00
|
|
|
|
2017-03-01 04:33:19 +00:00
|
|
|
@patch('passlib.apps.custom_app_context', Mock(return_value=''))
|
|
|
|
@patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock()))
|
|
|
|
@patch('hbmqtt.broker.Broker', Mock(return_value=MagicMock()))
|
|
|
|
@patch('hbmqtt.broker.Broker.start', Mock(return_value=mock_coro()))
|
|
|
|
@patch('homeassistant.components.mqtt.MQTT')
|
|
|
|
def test_creating_config_with_http_no_pass(self, mock_mqtt):
|
|
|
|
"""Test if the MQTT server gets started and subscribe/publish msg."""
|
2017-02-20 06:43:10 +00:00
|
|
|
mock_mqtt().async_connect.return_value = mock_coro(True)
|
2017-03-01 04:33:19 +00:00
|
|
|
self.hass.bus.listen_once = MagicMock()
|
2016-04-05 02:56:25 +00:00
|
|
|
|
2016-03-08 00:45:37 +00:00
|
|
|
self.hass.config.api = MagicMock(api_password=None)
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, mqtt.DOMAIN, {})
|
2016-03-08 00:45:37 +00:00
|
|
|
assert mock_mqtt.called
|
2017-02-20 06:43:10 +00:00
|
|
|
assert mock_mqtt.mock_calls[1][1][5] is None
|
|
|
|
assert mock_mqtt.mock_calls[1][1][6] is None
|
2016-03-08 00:45:37 +00:00
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
@patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock()))
|
2017-02-18 22:17:18 +00:00
|
|
|
@patch('hbmqtt.broker.Broker.start', return_value=mock_coro())
|
2016-11-11 06:45:38 +00:00
|
|
|
def test_broker_config_fails(self, mock_run):
|
2016-03-08 00:45:37 +00:00
|
|
|
"""Test if the MQTT component fails if server fails."""
|
|
|
|
from hbmqtt.broker import BrokerException
|
|
|
|
|
2016-11-11 06:45:38 +00:00
|
|
|
mock_run.side_effect = BrokerException
|
2016-03-08 00:45:37 +00:00
|
|
|
|
|
|
|
self.hass.config.api = MagicMock(api_password=None)
|
2016-10-27 07:16:23 +00:00
|
|
|
|
|
|
|
assert not setup_component(self.hass, mqtt.DOMAIN, {
|
2016-04-04 04:22:04 +00:00
|
|
|
mqtt.DOMAIN: {mqtt.CONF_EMBEDDED: {}}
|
2016-03-08 00:45:37 +00:00
|
|
|
})
|