test_cli.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 logging
  20. import typing
  21. import unittest.mock
  22. import pytest
  23. import switchbot_mqtt
  24. # pylint: disable=too-many-arguments; these are tests, no API
  25. @pytest.mark.parametrize(
  26. (
  27. "argv",
  28. "expected_mqtt_host",
  29. "expected_mqtt_port",
  30. "expected_username",
  31. "expected_password",
  32. "expected_retry_count",
  33. ),
  34. [
  35. (
  36. ["", "--mqtt-host", "mqtt-broker.local"],
  37. "mqtt-broker.local",
  38. 1883,
  39. None,
  40. None,
  41. 3,
  42. ),
  43. (
  44. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  45. "mqtt-broker.local",
  46. 8883,
  47. None,
  48. None,
  49. 3,
  50. ),
  51. (
  52. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  53. "mqtt-broker.local",
  54. 1883,
  55. "me",
  56. None,
  57. 3,
  58. ),
  59. (
  60. [
  61. "",
  62. "--mqtt-host",
  63. "mqtt-broker.local",
  64. "--mqtt-username",
  65. "me",
  66. "--mqtt-password",
  67. "secret",
  68. "--retries",
  69. "21",
  70. ],
  71. "mqtt-broker.local",
  72. 1883,
  73. "me",
  74. "secret",
  75. 21,
  76. ),
  77. ],
  78. )
  79. def test__main(
  80. argv,
  81. expected_mqtt_host,
  82. expected_mqtt_port,
  83. expected_username,
  84. expected_password,
  85. expected_retry_count,
  86. ):
  87. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  88. "sys.argv", argv
  89. ):
  90. # pylint: disable=protected-access
  91. switchbot_mqtt._main()
  92. run_mock.assert_called_once_with(
  93. mqtt_host=expected_mqtt_host,
  94. mqtt_port=expected_mqtt_port,
  95. mqtt_username=expected_username,
  96. mqtt_password=expected_password,
  97. retry_count=expected_retry_count,
  98. device_passwords={},
  99. fetch_device_info=False,
  100. )
  101. @pytest.mark.parametrize(
  102. ("mqtt_password_file_content", "expected_password"),
  103. [
  104. ("secret", "secret"),
  105. ("secret space", "secret space"),
  106. ("secret ", "secret "),
  107. (" secret ", " secret "),
  108. ("secret\n", "secret"),
  109. ("secret\n\n", "secret\n"),
  110. ("secret\r\n", "secret"),
  111. ("secret\n\r\n", "secret\n"),
  112. ("你好\n", "你好"),
  113. ],
  114. )
  115. def test__main_mqtt_password_file(
  116. tmpdir, mqtt_password_file_content, expected_password
  117. ):
  118. mqtt_password_path = tmpdir.join("mqtt-password")
  119. with mqtt_password_path.open("w") as mqtt_password_file:
  120. mqtt_password_file.write(mqtt_password_file_content)
  121. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  122. "sys.argv",
  123. [
  124. "",
  125. "--mqtt-host",
  126. "localhost",
  127. "--mqtt-username",
  128. "me",
  129. "--mqtt-password-file",
  130. str(mqtt_password_path),
  131. ],
  132. ):
  133. # pylint: disable=protected-access
  134. switchbot_mqtt._main()
  135. run_mock.assert_called_once_with(
  136. mqtt_host="localhost",
  137. mqtt_port=1883,
  138. mqtt_username="me",
  139. mqtt_password=expected_password,
  140. retry_count=3,
  141. device_passwords={},
  142. fetch_device_info=False,
  143. )
  144. def test__main_mqtt_password_file_collision(capsys):
  145. with unittest.mock.patch(
  146. "sys.argv",
  147. [
  148. "",
  149. "--mqtt-host",
  150. "localhost",
  151. "--mqtt-username",
  152. "me",
  153. "--mqtt-password",
  154. "secret",
  155. "--mqtt-password-file",
  156. "/var/lib/secrets/mqtt/password",
  157. ],
  158. ):
  159. with pytest.raises(SystemExit):
  160. # pylint: disable=protected-access
  161. switchbot_mqtt._main()
  162. out, err = capsys.readouterr()
  163. assert not out
  164. assert (
  165. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  166. in err
  167. )
  168. @pytest.mark.parametrize(
  169. "device_passwords",
  170. [
  171. {},
  172. {"11:22:33:44:55:66": "password", "aa:bb:cc:dd:ee:ff": "secret"},
  173. ],
  174. )
  175. def test__main_device_password_file(tmpdir, device_passwords):
  176. device_passwords_path = tmpdir.join("passwords.json")
  177. device_passwords_path.write_text(json.dumps(device_passwords), encoding="utf8")
  178. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  179. "sys.argv",
  180. [
  181. "",
  182. "--mqtt-host",
  183. "localhost",
  184. "--device-password-file",
  185. str(device_passwords_path),
  186. ],
  187. ):
  188. # pylint: disable=protected-access
  189. switchbot_mqtt._main()
  190. run_mock.assert_called_once_with(
  191. mqtt_host="localhost",
  192. mqtt_port=1883,
  193. mqtt_username=None,
  194. mqtt_password=None,
  195. retry_count=3,
  196. device_passwords=device_passwords,
  197. fetch_device_info=False,
  198. )
  199. def test__main_fetch_device_info():
  200. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  201. "sys.argv",
  202. [
  203. "",
  204. "--mqtt-host",
  205. "localhost",
  206. ],
  207. ):
  208. # pylint: disable=protected-access
  209. switchbot_mqtt._main()
  210. default_kwargs = dict(
  211. mqtt_host="localhost",
  212. mqtt_port=1883,
  213. mqtt_username=None,
  214. mqtt_password=None,
  215. retry_count=3,
  216. device_passwords={},
  217. )
  218. run_mock.assert_called_once_with(fetch_device_info=False, **default_kwargs)
  219. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  220. "sys.argv",
  221. ["", "--mqtt-host", "localhost", "--fetch-device-info"],
  222. ):
  223. # pylint: disable=protected-access
  224. switchbot_mqtt._main()
  225. run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
  226. @pytest.mark.parametrize(
  227. ("additional_argv", "root_log_level", "log_format"),
  228. [
  229. ([], logging.INFO, "%(message)s"),
  230. (
  231. ["--debug"],
  232. logging.DEBUG,
  233. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
  234. ),
  235. ],
  236. )
  237. def test__main_log_config(
  238. additional_argv: typing.List[str], root_log_level: int, log_format: str
  239. ):
  240. with unittest.mock.patch(
  241. "sys.argv", ["", "--mqtt-host", "localhost"] + additional_argv
  242. ), unittest.mock.patch(
  243. "logging.basicConfig"
  244. ) as logging_basic_config_mock, unittest.mock.patch(
  245. "switchbot_mqtt._run"
  246. ):
  247. # pylint: disable=protected-access
  248. switchbot_mqtt._main()
  249. logging_basic_config_mock.assert_called_once_with(
  250. level=root_log_level, format=log_format, datefmt="%Y-%m-%dT%H:%M:%S%z"
  251. )