test_cli.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # switchbot-mqtt - MQTT client controlling SwitchBot button & curtain automators,
  2. # compatible with home-assistant.io's MQTT Switch & Cover platform
  3. #
  4. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import unittest.mock
  19. import pytest
  20. import switchbot_mqtt
  21. # pylint: disable=too-many-arguments; these are tests, no API
  22. @pytest.mark.parametrize(
  23. (
  24. "argv",
  25. "expected_mqtt_host",
  26. "expected_mqtt_port",
  27. "expected_username",
  28. "expected_password",
  29. "expected_retry_count",
  30. ),
  31. [
  32. (
  33. ["", "--mqtt-host", "mqtt-broker.local"],
  34. "mqtt-broker.local",
  35. 1883,
  36. None,
  37. None,
  38. 3,
  39. ),
  40. (
  41. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  42. "mqtt-broker.local",
  43. 8883,
  44. None,
  45. None,
  46. 3,
  47. ),
  48. (
  49. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  50. "mqtt-broker.local",
  51. 1883,
  52. "me",
  53. None,
  54. 3,
  55. ),
  56. (
  57. [
  58. "",
  59. "--mqtt-host",
  60. "mqtt-broker.local",
  61. "--mqtt-username",
  62. "me",
  63. "--mqtt-password",
  64. "secret",
  65. "--retries",
  66. "21",
  67. ],
  68. "mqtt-broker.local",
  69. 1883,
  70. "me",
  71. "secret",
  72. 21,
  73. ),
  74. ],
  75. )
  76. def test__main(
  77. argv,
  78. expected_mqtt_host,
  79. expected_mqtt_port,
  80. expected_username,
  81. expected_password,
  82. expected_retry_count,
  83. ):
  84. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  85. "sys.argv", argv
  86. ):
  87. # pylint: disable=protected-access
  88. switchbot_mqtt._main()
  89. run_mock.assert_called_once_with(
  90. mqtt_host=expected_mqtt_host,
  91. mqtt_port=expected_mqtt_port,
  92. mqtt_username=expected_username,
  93. mqtt_password=expected_password,
  94. retry_count=expected_retry_count,
  95. )
  96. @pytest.mark.parametrize(
  97. ("password_file_content", "expected_password"),
  98. [
  99. ("secret", "secret"),
  100. ("secret space", "secret space"),
  101. ("secret ", "secret "),
  102. (" secret ", " secret "),
  103. ("secret\n", "secret"),
  104. ("secret\n\n", "secret\n"),
  105. ("secret\r\n", "secret"),
  106. ("secret\n\r\n", "secret\n"),
  107. ("你好\n", "你好"),
  108. ],
  109. )
  110. def test__main_password_file(tmpdir, password_file_content, expected_password):
  111. mqtt_password_path = tmpdir.join("mqtt-password")
  112. with mqtt_password_path.open("w") as mqtt_password_file:
  113. mqtt_password_file.write(password_file_content)
  114. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  115. "sys.argv",
  116. [
  117. "",
  118. "--mqtt-host",
  119. "localhost",
  120. "--mqtt-username",
  121. "me",
  122. "--mqtt-password-file",
  123. str(mqtt_password_path),
  124. ],
  125. ):
  126. # pylint: disable=protected-access
  127. switchbot_mqtt._main()
  128. run_mock.assert_called_once_with(
  129. mqtt_host="localhost",
  130. mqtt_port=1883,
  131. mqtt_username="me",
  132. mqtt_password=expected_password,
  133. retry_count=3,
  134. )
  135. def test__main_password_file_collision(capsys):
  136. with unittest.mock.patch(
  137. "sys.argv",
  138. [
  139. "",
  140. "--mqtt-host",
  141. "localhost",
  142. "--mqtt-username",
  143. "me",
  144. "--mqtt-password",
  145. "secret",
  146. "--mqtt-password-file",
  147. "/var/lib/secrets/mqtt/password",
  148. ],
  149. ):
  150. with pytest.raises(SystemExit):
  151. # pylint: disable=protected-access
  152. switchbot_mqtt._main()
  153. out, err = capsys.readouterr()
  154. assert not out
  155. assert (
  156. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  157. in err
  158. )