test_homeassistant.py 953 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import unittest.mock
  2. import pytest
  3. import systemctl_mqtt._homeassistant
  4. # pylint: disable=protected-access
  5. @pytest.mark.parametrize(
  6. ("hostname", "expected_node_id"),
  7. [
  8. ("raspberrypi", "raspberrypi"),
  9. ("da-sh", "da-sh"),
  10. ("under_score", "under_score"),
  11. ("someone evil mocked the hostname", "someoneevilmockedthehostname"),
  12. ],
  13. )
  14. def test_get_default_node_id(hostname, expected_node_id):
  15. with unittest.mock.patch(
  16. "systemctl_mqtt._utils.get_hostname", return_value=hostname
  17. ):
  18. assert systemctl_mqtt._homeassistant.get_default_node_id() == expected_node_id
  19. @pytest.mark.parametrize(
  20. ("node_id", "valid"),
  21. [
  22. ("raspberrypi", True),
  23. ("da-sh", True),
  24. ("under_score", True),
  25. ('" or ""="', False),
  26. ("", False),
  27. ],
  28. )
  29. def test_validate_node_id(node_id, valid):
  30. assert systemctl_mqtt._homeassistant.validate_node_id(node_id) == valid