Prevent discovery of IPP printers lacking identifier (#35630)
This commit is contained in:
1 parent
5496a8ca05
commit
e8ee3c7d4d
7 files changed
+117
-81
No files matched your search
@@ -1,6 +1,9 @@
|
||||
"""Tests for the IPP integration."""
|
||||
import os
|
||||
|
||||
import aiohttp
|
||||
from pyipp import IPPConnectionUpgradeRequired, IPPError
|
||||
|
||||
from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_UUID, DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
@@ -18,21 +21,25 @@ from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
ATTR_HOSTNAME = "hostname"
|
||||
ATTR_PROPERTIES = "properties"
|
||||
|
||||
HOST = "192.168.1.31"
|
||||
PORT = 631
|
||||
BASE_PATH = "/ipp/print"
|
||||
|
||||
IPP_ZEROCONF_SERVICE_TYPE = "_ipp._tcp.local."
|
||||
IPPS_ZEROCONF_SERVICE_TYPE = "_ipps._tcp.local."
|
||||
|
||||
ZEROCONF_NAME = "EPSON XP-6000 Series"
|
||||
ZEROCONF_HOST = "192.168.1.31"
|
||||
ZEROCONF_HOST = HOST
|
||||
ZEROCONF_HOSTNAME = "EPSON123456.local."
|
||||
ZEROCONF_PORT = 631
|
||||
|
||||
ZEROCONF_PORT = PORT
|
||||
ZEROCONF_RP = "ipp/print"
|
||||
|
||||
MOCK_USER_INPUT = {
|
||||
CONF_HOST: "192.168.1.31",
|
||||
CONF_PORT: 361,
|
||||
CONF_HOST: HOST,
|
||||
CONF_PORT: PORT,
|
||||
CONF_SSL: False,
|
||||
CONF_VERIFY_SSL: False,
|
||||
CONF_BASE_PATH: "/ipp/print",
|
||||
CONF_BASE_PATH: BASE_PATH,
|
||||
}
|
||||
|
||||
MOCK_ZEROCONF_IPP_SERVICE_INFO = {
|
||||
@@ -41,7 +48,7 @@ MOCK_ZEROCONF_IPP_SERVICE_INFO = {
|
||||
CONF_HOST: ZEROCONF_HOST,
|
||||
ATTR_HOSTNAME: ZEROCONF_HOSTNAME,
|
||||
CONF_PORT: ZEROCONF_PORT,
|
||||
ATTR_PROPERTIES: {"rp": "ipp/print"},
|
||||
ATTR_PROPERTIES: {"rp": ZEROCONF_RP},
|
||||
}
|
||||
|
||||
MOCK_ZEROCONF_IPPS_SERVICE_INFO = {
|
||||
@@ -50,7 +57,7 @@ MOCK_ZEROCONF_IPPS_SERVICE_INFO = {
|
||||
CONF_HOST: ZEROCONF_HOST,
|
||||
ATTR_HOSTNAME: ZEROCONF_HOSTNAME,
|
||||
CONF_PORT: ZEROCONF_PORT,
|
||||
ATTR_PROPERTIES: {"rp": "ipp/print"},
|
||||
ATTR_PROPERTIES: {"rp": ZEROCONF_RP},
|
||||
}
|
||||
|
||||
|
||||
@@ -61,30 +68,75 @@ def load_fixture_binary(filename):
|
||||
return fptr.read()
|
||||
|
||||
|
||||
def mock_connection(
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
host: str = HOST,
|
||||
port: int = PORT,
|
||||
ssl: bool = False,
|
||||
base_path: str = BASE_PATH,
|
||||
conn_error: bool = False,
|
||||
conn_upgrade_error: bool = False,
|
||||
ipp_error: bool = False,
|
||||
no_unique_id: bool = False,
|
||||
parse_error: bool = False,
|
||||
version_not_supported: bool = False,
|
||||
):
|
||||
"""Mock the IPP connection."""
|
||||
scheme = "https" if ssl else "http"
|
||||
ipp_url = f"{scheme}://{host}:{port}"
|
||||
|
||||
if ipp_error:
|
||||
aioclient_mock.post(f"{ipp_url}{base_path}", exc=IPPError)
|
||||
return
|
||||
|
||||
if conn_error:
|
||||
aioclient_mock.post(f"{ipp_url}{base_path}", exc=aiohttp.ClientError)
|
||||
return
|
||||
|
||||
if conn_upgrade_error:
|
||||
aioclient_mock.post(f"{ipp_url}{base_path}", exc=IPPConnectionUpgradeRequired)
|
||||
return
|
||||
|
||||
fixture = "ipp/get-printer-attributes.bin"
|
||||
if no_unique_id:
|
||||
fixture = "ipp/get-printer-attributes-success-nodata.bin"
|
||||
elif version_not_supported:
|
||||
fixture = "ipp/get-printer-attributes-error-0x0503.bin"
|
||||
|
||||
if parse_error:
|
||||
content = "BAD"
|
||||
else:
|
||||
content = load_fixture_binary(fixture)
|
||||
|
||||
aioclient_mock.post(
|
||||
f"{ipp_url}{base_path}",
|
||||
content=content,
|
||||
headers={"Content-Type": "application/ipp"},
|
||||
)
|
||||
|
||||
|
||||
async def init_integration(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
skip_setup: bool = False,
|
||||
host: str = HOST,
|
||||
port: int = PORT,
|
||||
ssl: bool = False,
|
||||
base_path: str = BASE_PATH,
|
||||
uuid: str = "cfe92100-67c4-11d4-a45f-f8d027761251",
|
||||
unique_id: str = "cfe92100-67c4-11d4-a45f-f8d027761251",
|
||||
conn_error: bool = False,
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the IPP integration in Home Assistant."""
|
||||
fixture = "ipp/get-printer-attributes.bin"
|
||||
aioclient_mock.post(
|
||||
"http://192.168.1.31:631/ipp/print",
|
||||
content=load_fixture_binary(fixture),
|
||||
headers={"Content-Type": "application/ipp"},
|
||||
)
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=unique_id,
|
||||
data={
|
||||
CONF_HOST: "192.168.1.31",
|
||||
CONF_PORT: 631,
|
||||
CONF_SSL: False,
|
||||
CONF_HOST: host,
|
||||
CONF_PORT: port,
|
||||
CONF_SSL: ssl,
|
||||
CONF_VERIFY_SSL: True,
|
||||
CONF_BASE_PATH: "/ipp/print",
|
||||
CONF_BASE_PATH: base_path,
|
||||
CONF_UUID: uuid,
|
||||
},
|
||||
)
|
||||
@@ -92,6 +144,14 @@ async def init_integration(
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
if not skip_setup:
|
||||
mock_connection(
|
||||
aioclient_mock,
|
||||
host=host,
|
||||
port=port,
|
||||
ssl=ssl,
|
||||
base_path=base_path,
|
||||
conn_error=conn_error,
|
||||
)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
Reference in new issue
Block a user