test_cli.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. fetch_device_info=False,
  98. )
  99. @pytest.mark.parametrize(
  100. ("mqtt_password_file_content", "expected_password"),
  101. [
  102. ("secret", "secret"),
  103. ("secret space", "secret space"),
  104. ("secret ", "secret "),
  105. (" secret ", " secret "),
  106. ("secret\n", "secret"),
  107. ("secret\n\n", "secret\n"),
  108. ("secret\r\n", "secret"),
  109. ("secret\n\r\n", "secret\n"),
  110. ("你好\n", "你好"),
  111. ],
  112. )
  113. def test__main_mqtt_password_file(
  114. tmpdir, mqtt_password_file_content, expected_password
  115. ):
  116. mqtt_password_path = tmpdir.join("mqtt-password")
  117. with mqtt_password_path.open("w") as mqtt_password_file:
  118. mqtt_password_file.write(mqtt_password_file_content)
  119. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  120. "sys.argv",
  121. [
  122. "",
  123. "--mqtt-host",
  124. "localhost",
  125. "--mqtt-username",
  126. "me",
  127. "--mqtt-password-file",
  128. str(mqtt_password_path),
  129. ],
  130. ):
  131. # pylint: disable=protected-access
  132. switchbot_mqtt._main()
  133. run_mock.assert_called_once_with(
  134. mqtt_host="localhost",
  135. mqtt_port=1883,
  136. mqtt_username="me",
  137. mqtt_password=expected_password,
  138. retry_count=3,
  139. device_passwords={},
  140. fetch_device_info=False,
  141. )
  142. def test__main_mqtt_password_file_collision(capsys):
  143. with unittest.mock.patch(
  144. "sys.argv",
  145. [
  146. "",
  147. "--mqtt-host",
  148. "localhost",
  149. "--mqtt-username",
  150. "me",
  151. "--mqtt-password",
  152. "secret",
  153. "--mqtt-password-file",
  154. "/var/lib/secrets/mqtt/password",
  155. ],
  156. ):
  157. with pytest.raises(SystemExit):
  158. # pylint: disable=protected-access
  159. switchbot_mqtt._main()
  160. out, err = capsys.readouterr()
  161. assert not out
  162. assert (
  163. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  164. in err
  165. )
  166. @pytest.mark.parametrize(
  167. "device_passwords",
  168. [
  169. {},
  170. {"11:22:33:44:55:66": "password", "aa:bb:cc:dd:ee:ff": "secret"},
  171. ],
  172. )
  173. def test__main_device_password_file(tmpdir, device_passwords):
  174. device_passwords_path = tmpdir.join("passwords.json")
  175. device_passwords_path.write_text(json.dumps(device_passwords), encoding="utf8")
  176. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  177. "sys.argv",
  178. [
  179. "",
  180. "--mqtt-host",
  181. "localhost",
  182. "--device-password-file",
  183. str(device_passwords_path),
  184. ],
  185. ):
  186. # pylint: disable=protected-access
  187. switchbot_mqtt._main()
  188. run_mock.assert_called_once_with(
  189. mqtt_host="localhost",
  190. mqtt_port=1883,
  191. mqtt_username=None,
  192. mqtt_password=None,
  193. retry_count=3,
  194. device_passwords=device_passwords,
  195. fetch_device_info=False,
  196. )
  197. def test__main_fetch_device_info():
  198. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  199. "sys.argv",
  200. [
  201. "",
  202. "--mqtt-host",
  203. "localhost",
  204. ],
  205. ):
  206. # pylint: disable=protected-access
  207. switchbot_mqtt._main()
  208. default_kwargs = dict(
  209. mqtt_host="localhost",
  210. mqtt_port=1883,
  211. mqtt_username=None,
  212. mqtt_password=None,
  213. retry_count=3,
  214. device_passwords={},
  215. )
  216. run_mock.assert_called_once_with(fetch_device_info=False, **default_kwargs)
  217. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  218. "sys.argv",
  219. ["", "--mqtt-host", "localhost", "--fetch-device-info"],
  220. ):
  221. # pylint: disable=protected-access
  222. switchbot_mqtt._main()
  223. run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)