Skip to content
Snippets Groups Projects
Unverified Commit a0abd537 authored by epenet's avatar epenet Committed by GitHub
Browse files

Adjust nacl import in tests (#119392)

parent 9e8f9abb
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,8 @@ from http import HTTPStatus
import json
from unittest.mock import patch
import pytest
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
from homeassistant.components.mobile_app.const import CONF_SECRET, DOMAIN
from homeassistant.const import CONF_WEBHOOK_ID
......@@ -66,13 +67,6 @@ async def test_registration_encryption(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None:
"""Test that registrations happen."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
return
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
api_client = await hass_client()
......@@ -111,13 +105,6 @@ async def test_registration_encryption_legacy(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None:
"""Test that registrations happen."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
return
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
api_client = await hass_client()
......
......@@ -2,8 +2,11 @@
from binascii import unhexlify
from http import HTTPStatus
import json
from unittest.mock import ANY, patch
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
import pytest
from homeassistant.components.camera import CameraEntityFeature
......@@ -35,14 +38,6 @@ async def homeassistant(hass):
def encrypt_payload(secret_key, payload, encode_json=True):
"""Return a encrypted payload given a key and dictionary of data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
prepped_key = unhexlify(secret_key)
if encode_json:
......@@ -56,14 +51,6 @@ def encrypt_payload(secret_key, payload, encode_json=True):
def encrypt_payload_legacy(secret_key, payload, encode_json=True):
"""Return a encrypted payload given a key and dictionary of data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
keylen = SecretBox.KEY_SIZE
prepped_key = secret_key.encode("utf-8")
prepped_key = prepped_key[:keylen]
......@@ -80,14 +67,6 @@ def encrypt_payload_legacy(secret_key, payload, encode_json=True):
def decrypt_payload(secret_key, encrypted_data):
"""Return a decrypted payload given a key and a string of encrypted data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
prepped_key = unhexlify(secret_key)
decrypted_data = SecretBox(prepped_key).decrypt(
......@@ -100,14 +79,6 @@ def decrypt_payload(secret_key, encrypted_data):
def decrypt_payload_legacy(secret_key, encrypted_data):
"""Return a decrypted payload given a key and a string of encrypted data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
keylen = SecretBox.KEY_SIZE
prepped_key = secret_key.encode("utf-8")
prepped_key = prepped_key[:keylen]
......
"""The tests for the Owntracks device tracker."""
import base64
import json
import pickle
from unittest.mock import patch
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
import pytest
from homeassistant.components import owntracks
......@@ -1330,23 +1334,14 @@ def generate_ciphers(secret):
# PyNaCl ciphertext generation will fail if the module
# cannot be imported. However, the test for decryption
# also relies on this library and won't be run without it.
import base64
import pickle
keylen = SecretBox.KEY_SIZE
key = secret.encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b"\0")
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
msg = json.dumps(DEFAULT_LOCATION_MESSAGE).encode("utf-8")
keylen = SecretBox.KEY_SIZE
key = secret.encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b"\0")
msg = json.dumps(DEFAULT_LOCATION_MESSAGE).encode("utf-8")
ctxt = SecretBox(key).encrypt(msg, encoder=Base64Encoder).decode("utf-8")
except (ImportError, OSError):
ctxt = ""
ctxt = SecretBox(key).encrypt(msg, encoder=Base64Encoder).decode("utf-8")
mctxt = base64.b64encode(
pickle.dumps(
......@@ -1381,9 +1376,6 @@ def mock_cipher():
def mock_decrypt(ciphertext, key):
"""Decrypt/unpickle."""
import base64
import pickle
(mkey, plaintext) = pickle.loads(base64.b64decode(ciphertext))
if key != mkey:
raise ValueError
......@@ -1504,12 +1496,6 @@ async def test_encrypted_payload_no_topic_key(hass: HomeAssistant, setup_comp) -
async def test_encrypted_payload_libsodium(hass: HomeAssistant, setup_comp) -> None:
"""Test sending encrypted message payload."""
try:
import nacl # noqa: F401
except (ImportError, OSError):
pytest.skip("PyNaCl/libsodium is not installed")
return
await setup_owntracks(hass, {CONF_SECRET: TEST_SECRET_KEY})
await send_message(hass, LOCATION_TOPIC, ENCRYPTED_LOCATION_MESSAGE)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment