Fix diagnostics export for generic camera (#75665)

Fix url redaction and add tests

Co-authored-by: Dave T <davet2001@users.noreply.github.com>
This commit is contained in:
Dave T 2022-07-24 09:21:01 +01:00 committed by GitHub
parent 82c92b5634
commit 7075032bf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 3 deletions

View file

@ -21,12 +21,12 @@ TO_REDACT = {
# A very similar redact function is in components.sql. Possible to be made common.
def redact_url(data: str) -> str:
"""Redact credentials from string url."""
url_in = yarl.URL(data)
url = url_in = yarl.URL(data)
if url_in.user:
url = url_in.with_user("****")
url = url.with_user("****")
if url_in.password:
url = url.with_password("****")
if url_in.path:
if url_in.path != "/":
url = url.with_path("****")
if url_in.query_string:
url = url.with_query("****=****")

View file

@ -1,5 +1,8 @@
"""Test generic (IP camera) diagnostics."""
import pytest
from homeassistant.components.diagnostics import REDACTED
from homeassistant.components.generic.diagnostics import redact_url
from tests.components.diagnostics import get_diagnostics_for_config_entry
@ -22,3 +25,34 @@ async def test_entry_diagnostics(hass, hass_client, setup_entry):
"content_type": "image/jpeg",
},
}
@pytest.mark.parametrize(
("url_in", "url_out_expected"),
[
(
"http://www.example.com",
"http://www.example.com",
),
(
"http://fred:letmein1@www.example.com/image.php?key=secret2",
"http://****:****@www.example.com/****?****=****",
),
(
"http://fred@www.example.com/image.php?key=secret2",
"http://****@www.example.com/****?****=****",
),
(
"http://fred@www.example.com/image.php",
"http://****@www.example.com/****",
),
(
"http://:letmein1@www.example.com",
"http://:****@www.example.com",
),
],
)
def test_redact_url(url_in, url_out_expected):
"""Test url redaction."""
url_out = redact_url(url_in)
assert url_out == url_out_expected