automatically use bundled certificate it set to auto (#6707)

* automatically use bundled certificate if certificate-parameter is set to 'auto' and seperate this from which port is specified

* Fix travis error and long lines

* Update __init__.py
This commit is contained in:
printzlau 2017-03-19 22:51:53 +01:00 committed by Paulus Schoutsen
parent acf75b5253
commit 1cb2a6add0
2 changed files with 21 additions and 22 deletions

View File

@ -119,7 +119,7 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_CERTIFICATE): cv.isfile,
vol.Optional(CONF_CERTIFICATE): vol.Any('auto', cv.isfile),
vol.Inclusive(CONF_CLIENT_KEY, 'client_key_auth',
msg=CLIENT_KEY_AUTH_MSG): cv.isfile,
vol.Inclusive(CONF_CLIENT_CERT, 'client_key_auth',
@ -317,8 +317,8 @@ def async_setup(hass, config):
certificate = os.path.join(os.path.dirname(__file__),
'addtrustexternalcaroot.crt')
# When the port indicates mqtts, use bundled certificates from requests
if certificate is None and port == 8883:
# When the certificate is set to auto, use bundled certs from requests
if certificate == 'auto':
certificate = requests.certs.where()
will_message = conf.get(CONF_WILL_MESSAGE)

View File

@ -377,8 +377,24 @@ def test_setup_fails_if_no_connect_broker(hass):
@asyncio.coroutine
def test_setup_uses_certificate_on_mqtts_port(hass):
"""Test setup uses bundled certificates when mqtts port is requested."""
def test_setup_uses_certificate_on_certificate_set_to_auto(hass):
"""Test setup uses bundled certs when certificate is set to auto."""
test_broker_cfg = {mqtt.DOMAIN: {mqtt.CONF_BROKER: 'test-broker',
'certificate': 'auto'}}
with mock.patch('homeassistant.components.mqtt.MQTT') as mock_MQTT:
yield from async_setup_component(hass, mqtt.DOMAIN, test_broker_cfg)
assert mock_MQTT.called
import requests.certs
expectedCertificate = requests.certs.where()
assert mock_MQTT.mock_calls[0][1][7] == expectedCertificate
@asyncio.coroutine
def test_setup_does_not_use_certificate_on_mqtts_port(hass):
"""Test setup doesn't use bundled certs when certificate is not set."""
test_broker_cfg = {mqtt.DOMAIN: {mqtt.CONF_BROKER: 'test-broker',
'port': 8883}}
@ -388,23 +404,6 @@ def test_setup_uses_certificate_on_mqtts_port(hass):
assert mock_MQTT.called
assert mock_MQTT.mock_calls[0][1][2] == 8883
import requests.certs
expectedCertificate = requests.certs.where()
assert mock_MQTT.mock_calls[0][1][7] == expectedCertificate
@asyncio.coroutine
def test_setup_uses_certificate_not_on_mqtts_port(hass):
"""Test setup doesn't use bundled certificates when not mqtts port."""
test_broker_cfg = {mqtt.DOMAIN: {mqtt.CONF_BROKER: 'test-broker',
'port': 1883}}
with mock.patch('homeassistant.components.mqtt.MQTT') as mock_MQTT:
yield from async_setup_component(hass, mqtt.DOMAIN, test_broker_cfg)
assert mock_MQTT.called
assert mock_MQTT.mock_calls[0][1][2] == 1883
import requests.certs
mqttsCertificateBundle = requests.certs.where()
assert mock_MQTT.mock_calls[0][1][7] != mqttsCertificateBundle