test_cli.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 json
  19. import unittest.mock
  20. import pytest
  21. import switchbot_mqtt
  22. # pylint: disable=too-many-arguments; these are tests, no API
  23. @pytest.mark.parametrize(
  24. (
  25. "argv",
  26. "expected_mqtt_host",
  27. "expected_mqtt_port",
  28. "expected_username",
  29. "expected_password",
  30. "expected_retry_count",
  31. ),
  32. [
  33. (
  34. ["", "--mqtt-host", "mqtt-broker.local"],
  35. "mqtt-broker.local",
  36. 1883,
  37. None,
  38. None,
  39. 3,
  40. ),
  41. (
  42. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  43. "mqtt-broker.local",
  44. 8883,
  45. None,
  46. None,
  47. 3,
  48. ),
  49. (
  50. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  51. "mqtt-broker.local",
  52. 1883,
  53. "me",
  54. None,
  55. 3,
  56. ),
  57. (
  58. [
  59. "",
  60. "--mqtt-host",
  61. "mqtt-broker.local",
  62. "--mqtt-username",
  63. "me",
  64. "--mqtt-password",
  65. "secret",
  66. "--retries",
  67. "21",
  68. ],
  69. "mqtt-broker.local",
  70. 1883,
  71. "me",
  72. "secret",
  73. 21,
  74. ),
  75. ],
  76. )
  77. def test__main(
  78. argv,
  79. expected_mqtt_host,
  80. expected_mqtt_port,
  81. expected_username,
  82. expected_password,
  83. expected_retry_count,
  84. ):
  85. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  86. "sys.argv", argv
  87. ):
  88. # pylint: disable=protected-access
  89. switchbot_mqtt._main()
  90. run_mock.assert_called_once_with(
  91. mqtt_host=expected_mqtt_host,
  92. mqtt_port=expected_mqtt_port,
  93. mqtt_username=expected_username,
  94. mqtt_password=expected_password,
  95. retry_count=expected_retry_count,
  96. device_passwords={},
  97. )
  98. @pytest.mark.parametrize(
  99. ("mqtt_password_file_content", "expected_password"),
  100. [
  101. ("secret", "secret"),
  102. ("secret space", "secret space"),
  103. ("secret ", "secret "),
  104. (" secret ", " secret "),
  105. ("secret\n", "secret"),
  106. ("secret\n\n", "secret\n"),
  107. ("secret\r\n", "secret"),
  108. ("secret\n\r\n", "secret\n"),
  109. ("你好\n", "你好"),
  110. ],
  111. )
  112. def test__main_mqtt_password_file(
  113. tmpdir, mqtt_password_file_content, expected_password
  114. ):
  115. mqtt_password_path = tmpdir.join("mqtt-password")
  116. with mqtt_password_path.open("w") as mqtt_password_file:
  117. mqtt_password_file.write(mqtt_password_file_content)
  118. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  119. "sys.argv",
  120. [
  121. "",
  122. "--mqtt-host",
  123. "localhost",
  124. "--mqtt-username",
  125. "me",
  126. "--mqtt-password-file",
  127. str(mqtt_password_path),
  128. ],
  129. ):
  130. # pylint: disable=protected-access
  131. switchbot_mqtt._main()
  132. run_mock.assert_called_once_with(
  133. mqtt_host="localhost",
  134. mqtt_port=1883,
  135. mqtt_username="me",
  136. mqtt_password=expected_password,
  137. retry_count=3,
  138. device_passwords={},
  139. )
  140. def test__main_mqtt_password_file_collision(capsys):
  141. with unittest.mock.patch(
  142. "sys.argv",
  143. [
  144. "",
  145. "--mqtt-host",
  146. "localhost",
  147. "--mqtt-username",
  148. "me",
  149. "--mqtt-password",
  150. "secret",
  151. "--mqtt-password-file",
  152. "/var/lib/secrets/mqtt/password",
  153. ],
  154. ):
  155. with pytest.raises(SystemExit):
  156. # pylint: disable=protected-access
  157. switchbot_mqtt._main()
  158. out, err = capsys.readouterr()
  159. assert not out
  160. assert (
  161. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  162. in err
  163. )
  164. @pytest.mark.parametrize(
  165. "device_passwords",
  166. [
  167. {},
  168. {"11:22:33:44:55:66": "password", "aa:bb:cc:dd:ee:ff": "secret"},
  169. ],
  170. )
  171. def test__main_device_password_file(tmpdir, device_passwords):
  172. device_passwords_path = tmpdir.join("passwords.json")
  173. device_passwords_path.write_text(json.dumps(device_passwords), encoding="utf8")
  174. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  175. "sys.argv",
  176. [
  177. "",
  178. "--mqtt-host",
  179. "localhost",
  180. "--device-password-file",
  181. str(device_passwords_path),
  182. ],
  183. ):
  184. # pylint: disable=protected-access
  185. switchbot_mqtt._main()
  186. run_mock.assert_called_once_with(
  187. mqtt_host="localhost",
  188. mqtt_port=1883,
  189. mqtt_username=None,
  190. mqtt_password=None,
  191. retry_count=3,
  192. device_passwords=device_passwords,
  193. )