Move imports in owntracks component (#27780)
* Move imports in owntracks component * Fix nacl import * Fix nacl import 2 * Fix nacl import 3 * Add helper.supports_encryption tests * Fix tests helper 1 * Fix tests 2 * Add not_supports_encryption + get_cipher_error tests * Code cov * Fix nacl_not_imported test
This commit is contained in:
1 parent
f6b48dec94
commit
eb23846cfe
5 files changed
+84
-13
No files matched your search
@@ -1406,6 +1406,25 @@ def config_context(hass, setup_comp):
|
||||
patch_save.stop()
|
||||
|
||||
|
||||
@pytest.fixture(name="not_supports_encryption")
|
||||
def mock_not_supports_encryption():
|
||||
"""Mock non successful nacl import."""
|
||||
with patch(
|
||||
"homeassistant.components.owntracks.messages.supports_encryption",
|
||||
return_value=False,
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="get_cipher_error")
|
||||
def mock_get_cipher_error():
|
||||
"""Mock non successful cipher."""
|
||||
with patch(
|
||||
"homeassistant.components.owntracks.messages.get_cipher", side_effect=OSError()
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@patch("homeassistant.components.owntracks.messages.get_cipher", mock_cipher)
|
||||
async def test_encrypted_payload(hass, setup_comp):
|
||||
"""Test encrypted payload."""
|
||||
@@ -1422,6 +1441,22 @@ async def test_encrypted_payload_topic_key(hass, setup_comp):
|
||||
assert_location_latitude(hass, LOCATION_MESSAGE["lat"])
|
||||
|
||||
|
||||
async def test_encrypted_payload_not_supports_encryption(
|
||||
hass, setup_comp, not_supports_encryption
|
||||
):
|
||||
"""Test encrypted payload with no supported encryption."""
|
||||
await setup_owntracks(hass, {CONF_SECRET: TEST_SECRET_KEY})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert hass.states.get(DEVICE_TRACKER_STATE) is None
|
||||
|
||||
|
||||
async def test_encrypted_payload_get_cipher_error(hass, setup_comp, get_cipher_error):
|
||||
"""Test encrypted payload with no supported encryption."""
|
||||
await setup_owntracks(hass, {CONF_SECRET: TEST_SECRET_KEY})
|
||||
await send_message(hass, LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
|
||||
assert hass.states.get(DEVICE_TRACKER_STATE) is None
|
||||
|
||||
|
||||
@patch("homeassistant.components.owntracks.messages.get_cipher", mock_cipher)
|
||||
async def test_encrypted_payload_no_key(hass, setup_comp):
|
||||
"""Test encrypted payload with no key, ."""
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Test the owntracks_http platform."""
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.owntracks import helper
|
||||
|
||||
|
||||
@pytest.fixture(name="nacl_imported")
|
||||
def mock_nacl_imported():
|
||||
"""Mock a successful import."""
|
||||
with patch("homeassistant.components.owntracks.helper.nacl"):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="nacl_not_imported")
|
||||
def mock_nacl_not_imported():
|
||||
"""Mock non successful import."""
|
||||
with patch("homeassistant.components.owntracks.helper.nacl", new=None):
|
||||
yield
|
||||
|
||||
|
||||
def test_supports_encryption(nacl_imported):
|
||||
"""Test if env supports encryption."""
|
||||
assert helper.supports_encryption()
|
||||
|
||||
|
||||
def test_supports_encryption_failed(nacl_not_imported):
|
||||
"""Test if env does not support encryption."""
|
||||
assert not helper.supports_encryption()
|
||||
Reference in new issue
Block a user