test_homeassistant.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # systemctl-mqtt - MQTT client triggering & reporting shutdown on systemd-based systems
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import unittest.mock
  18. import pytest
  19. import systemctl_mqtt._homeassistant
  20. # pylint: disable=protected-access
  21. @pytest.mark.parametrize(
  22. ("hostname", "expected_node_id"),
  23. [
  24. ("raspberrypi", "raspberrypi"),
  25. ("da-sh", "da-sh"),
  26. ("under_score", "under_score"),
  27. ("someone evil mocked the hostname", "someoneevilmockedthehostname"),
  28. ],
  29. )
  30. def test_get_default_node_id(hostname, expected_node_id):
  31. with unittest.mock.patch(
  32. "systemctl_mqtt._utils.get_hostname", return_value=hostname
  33. ):
  34. assert systemctl_mqtt._homeassistant.get_default_node_id() == expected_node_id
  35. @pytest.mark.parametrize(
  36. ("node_id", "valid"),
  37. [
  38. ("raspberrypi", True),
  39. ("da-sh", True),
  40. ("under_score", True),
  41. ('" or ""="', False),
  42. ("", False),
  43. ],
  44. )
  45. def test_validate_node_id(node_id, valid):
  46. assert systemctl_mqtt._homeassistant.validate_node_id(node_id) == valid