test_diagnostics.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """Tests for diagnostics platform"""
  2. from unittest.mock import Mock
  3. import pytest
  4. from homeassistant.components.diagnostics import REDACTED
  5. from homeassistant.const import CONF_HOST
  6. from pytest_homeassistant_custom_component.common import MockConfigEntry
  7. from custom_components.tuya_local.const import (
  8. CONF_DEVICE_ID,
  9. CONF_LOCAL_KEY,
  10. CONF_PROTOCOL_VERSION,
  11. CONF_TYPE,
  12. DOMAIN,
  13. )
  14. from custom_components.tuya_local.diagnostics import (
  15. async_get_config_entry_diagnostics,
  16. async_get_device_diagnostics,
  17. redact_entity,
  18. )
  19. from custom_components.tuya_local.helpers.device_config import TuyaEntityConfig
  20. @pytest.mark.asyncio
  21. async def test_config_entry_diagnostics(hass):
  22. entry = MockConfigEntry(
  23. domain=DOMAIN,
  24. data={
  25. CONF_DEVICE_ID: "test_device",
  26. CONF_LOCAL_KEY: "test_key",
  27. CONF_PROTOCOL_VERSION: "auto",
  28. CONF_TYPE: "simple_switch",
  29. },
  30. )
  31. m_device = Mock()
  32. m_device._api_protocol_version_index = 0
  33. m_device._children = []
  34. m_device._cached_state = {"1": "Test"}
  35. m_device._pending_updates = {}
  36. hass.data[DOMAIN] = {"test_device": {"device": m_device}}
  37. diag = await async_get_config_entry_diagnostics(hass, entry)
  38. assert diag
  39. @pytest.mark.asyncio
  40. async def test_device_diagnostics(hass):
  41. entry = MockConfigEntry(
  42. domain=DOMAIN,
  43. data={
  44. CONF_DEVICE_ID: "test_device",
  45. CONF_LOCAL_KEY: "test_key",
  46. CONF_PROTOCOL_VERSION: "auto",
  47. CONF_TYPE: "simple_switch",
  48. },
  49. )
  50. m_device = Mock()
  51. m_device._api_protocol_version_index = 0
  52. m_device._children = []
  53. m_device._cached_state = {"1": "Test"}
  54. m_device._pending_updates = {}
  55. hass.data[DOMAIN] = {"test_device": {"device": m_device}}
  56. diag = await async_get_device_diagnostics(hass, entry, m_device)
  57. assert diag
  58. @pytest.mark.asyncio
  59. async def test_diagnostic_redaction(hass):
  60. entry = MockConfigEntry(
  61. domain=DOMAIN,
  62. data={
  63. CONF_DEVICE_ID: "test_device",
  64. CONF_LOCAL_KEY: "test_key",
  65. CONF_PROTOCOL_VERSION: "auto",
  66. CONF_HOST: "auto",
  67. CONF_TYPE: "",
  68. },
  69. )
  70. m_device = Mock()
  71. m_entity = Mock()
  72. config = TuyaEntityConfig(
  73. Mock(),
  74. {
  75. "entity": "sensor",
  76. "dps": [
  77. {
  78. "id": "1",
  79. "type": "string",
  80. "name": "sensor",
  81. },
  82. {
  83. "id": "2",
  84. "type": "string",
  85. "name": "secrets",
  86. "sensitive": True,
  87. },
  88. ],
  89. },
  90. )
  91. m_entity._config = config
  92. m_device._api_protocol_version_index = 0
  93. m_device._children = [m_entity]
  94. m_device._cached_state = {"1": "Test", "2": "secret"}
  95. m_device._pending_updates = {}
  96. hass.data[DOMAIN] = {"test_device": {"device": m_device}}
  97. diag = await async_get_device_diagnostics(hass, entry, m_device)
  98. assert diag["device_id"] is REDACTED
  99. assert diag["local_key"] is REDACTED
  100. assert diag["cached_state"]["2"] is REDACTED
  101. # Deliberately self describing. Realistic looking values get picked up by
  102. # secret scanners, and names containing "secret" or "password" trip ruff's S105.
  103. EXAMPLE_SENSITIVE_VALUE = "example-not-a-real-value"
  104. EXAMPLE_SNAPSHOT = "example-not-real-snapshot-data"
  105. def _device_with_sensitive_dp(dp_name: str, dp_value: str):
  106. """Build a device with one entity whose dp `dp_name` is sensitive."""
  107. config = TuyaEntityConfig(
  108. Mock(),
  109. {
  110. "entity": "lock",
  111. "dps": [
  112. {"id": "33", "type": "boolean", "name": "lock"},
  113. {"id": "1", "type": "string", "name": dp_name, "sensitive": True},
  114. ],
  115. },
  116. )
  117. m_entity = Mock()
  118. m_entity._config = config
  119. m_device = Mock()
  120. m_device.unique_id = "test_device"
  121. m_device._children = [m_entity]
  122. m_device.get_property = lambda dp_id: dp_value if dp_id == "1" else None
  123. return m_device, config.unique_id("test_device")
  124. def test_sensitive_attribute_is_redacted():
  125. """A sensitive dp published as an extra attribute must be redacted."""
  126. m_device, unique_id = _device_with_sensitive_dp(
  127. "unlock_password", EXAMPLE_SENSITIVE_VALUE
  128. )
  129. state = {
  130. "entity_id": "lock.front_door",
  131. "state": "locked",
  132. "attributes": {
  133. "friendly_name": "Front door",
  134. "unlock_password": EXAMPLE_SENSITIVE_VALUE,
  135. },
  136. }
  137. result = redact_entity(m_device, unique_id, state)
  138. assert result["attributes"]["unlock_password"] is REDACTED
  139. assert result["attributes"]["friendly_name"] == "Front door"
  140. assert EXAMPLE_SENSITIVE_VALUE not in str(result)
  141. # the entity's own state is not sensitive, so it stays useful
  142. assert result["state"] == "locked"
  143. def test_sensitive_primary_value_is_redacted():
  144. """A sensitive dp consumed by the platform surfaces as state, so redact it."""
  145. m_device, unique_id = _device_with_sensitive_dp("value", EXAMPLE_SENSITIVE_VALUE)
  146. state = {
  147. "entity_id": "text.door_code",
  148. "state": EXAMPLE_SENSITIVE_VALUE,
  149. "attributes": {"friendly_name": "Door code"},
  150. }
  151. result = redact_entity(m_device, unique_id, state)
  152. assert result["state"] is REDACTED
  153. assert EXAMPLE_SENSITIVE_VALUE not in str(result)
  154. def test_state_kept_when_sensitive_dp_is_not_the_state():
  155. """A camera's snapshot is sensitive, but its state is not - keep the state."""
  156. m_device, unique_id = _device_with_sensitive_dp("snapshot", EXAMPLE_SNAPSHOT)
  157. state = {
  158. "entity_id": "camera.doorbell",
  159. "state": "recording",
  160. "attributes": {"friendly_name": "Doorbell"},
  161. }
  162. result = redact_entity(m_device, unique_id, state)
  163. assert result["state"] == "recording"
  164. assert EXAMPLE_SNAPSHOT not in str(result)
  165. def test_unrelated_entity_state_is_untouched():
  166. """An entity with no sensitive dps is returned unchanged."""
  167. m_device, _ = _device_with_sensitive_dp("unlock_password", EXAMPLE_SENSITIVE_VALUE)
  168. state = {"entity_id": "lock.other", "state": "locked", "attributes": {}}
  169. assert redact_entity(m_device, "some-other-unique-id", state) == state