Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion custom_components/sonoff/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from .core.const import DOMAIN, PRIVATE_KEYS
from .core.ewelink import XRegistry

from copy import deepcopy

async def async_get_config_entry_diagnostics(hass: HomeAssistant, entry: ConfigEntry):
try:
if XRegistry.config:
config = XRegistry.config.copy()
config = deepcopy(XRegistry.config)
for k in (CONF_USERNAME, CONF_PASSWORD):
if config.get(k):
config[k] = "***"
Expand Down
59 changes: 59 additions & 0 deletions tests/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import asyncio
from types import SimpleNamespace

from custom_components.sonoff.core.const import DOMAIN
from custom_components.sonoff.core.ewelink import XRegistry
from custom_components.sonoff.diagnostics import async_get_config_entry_diagnostics


class FakeHass:
def __init__(self):
self.data = {}

async def async_add_executor_job(self, func, *args):
return func(*args)


def test_diagnostics_does_not_mutate_config():
entry = SimpleNamespace(
entry_id="entry1",
options={},
)

# noinspection PyTypeChecker
registry = XRegistry(None)
registry.devices = {}
registry.cloud.auth = None

hass = FakeHass()
hass.data[DOMAIN] = {
entry.entry_id: registry,
}

original_config = XRegistry.config

try:
XRegistry.config = {
"username": "user@example.com",
"password": "secret-password",
"devices": {
"1000123456": {
"devicekey": "real-device-key",
}
},
}

result = asyncio.run(async_get_config_entry_diagnostics(hass, entry))

assert result["config"]["username"] == "***"
assert result["config"]["password"] == "***"
assert result["config"]["devices"]["1000123456"]["devicekey"] == "***"

assert XRegistry.config["username"] == "user@example.com"
assert XRegistry.config["password"] == "secret-password"
assert (
XRegistry.config["devices"]["1000123456"]["devicekey"]
== "real-device-key"
)
finally:
XRegistry.config = original_config
Loading