1
0

test_homeassistant.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_object_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_discovery_object_id(hostname, expected_object_id):
  31. with unittest.mock.patch(
  32. "systemctl_mqtt._utils.get_hostname", return_value=hostname
  33. ):
  34. assert (
  35. systemctl_mqtt._homeassistant.get_default_discovery_object_id()
  36. == "systemctl-mqtt-" + expected_object_id
  37. )
  38. @pytest.mark.parametrize(
  39. ("object_id", "valid"),
  40. [
  41. ("raspberrypi", True),
  42. ("da-sh", True),
  43. ("under_score", True),
  44. ('" or ""="', False),
  45. ("", False),
  46. ],
  47. )
  48. def test_validate_discovery_object_id(object_id, valid):
  49. assert (
  50. systemctl_mqtt._homeassistant.validate_discovery_object_id(object_id) == valid
  51. )