1
0

test_cli.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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=protected-access; tests
  25. # pylint: disable=too-many-arguments; these are tests, no API
  26. @pytest.mark.parametrize(
  27. (
  28. "argv",
  29. "expected_mqtt_host",
  30. "expected_mqtt_port",
  31. "expected_username",
  32. "expected_password",
  33. "expected_retry_count",
  34. ),
  35. [
  36. (
  37. ["", "--mqtt-host", "mqtt-broker.local"],
  38. "mqtt-broker.local",
  39. 1883,
  40. None,
  41. None,
  42. 3,
  43. ),
  44. (
  45. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  46. "mqtt-broker.local",
  47. 8883,
  48. None,
  49. None,
  50. 3,
  51. ),
  52. (
  53. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  54. "mqtt-broker.local",
  55. 1883,
  56. "me",
  57. None,
  58. 3,
  59. ),
  60. (
  61. [
  62. "",
  63. "--mqtt-host",
  64. "mqtt-broker.local",
  65. "--mqtt-username",
  66. "me",
  67. "--mqtt-password",
  68. "secret",
  69. "--retries",
  70. "21",
  71. ],
  72. "mqtt-broker.local",
  73. 1883,
  74. "me",
  75. "secret",
  76. 21,
  77. ),
  78. ],
  79. )
  80. def test__main(
  81. argv,
  82. expected_mqtt_host,
  83. expected_mqtt_port,
  84. expected_username,
  85. expected_password,
  86. expected_retry_count,
  87. ):
  88. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  89. "sys.argv", argv
  90. ):
  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. switchbot_mqtt._main()
  134. run_mock.assert_called_once_with(
  135. mqtt_host="localhost",
  136. mqtt_port=1883,
  137. mqtt_username="me",
  138. mqtt_password=expected_password,
  139. retry_count=3,
  140. device_passwords={},
  141. fetch_device_info=False,
  142. )
  143. def test__main_mqtt_password_file_collision(capsys):
  144. with unittest.mock.patch(
  145. "sys.argv",
  146. [
  147. "",
  148. "--mqtt-host",
  149. "localhost",
  150. "--mqtt-username",
  151. "me",
  152. "--mqtt-password",
  153. "secret",
  154. "--mqtt-password-file",
  155. "/var/lib/secrets/mqtt/password",
  156. ],
  157. ):
  158. with pytest.raises(SystemExit):
  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. switchbot_mqtt._main()
  187. run_mock.assert_called_once_with(
  188. mqtt_host="localhost",
  189. mqtt_port=1883,
  190. mqtt_username=None,
  191. mqtt_password=None,
  192. retry_count=3,
  193. device_passwords=device_passwords,
  194. fetch_device_info=False,
  195. )
  196. def test__main_fetch_device_info():
  197. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  198. "sys.argv",
  199. [
  200. "",
  201. "--mqtt-host",
  202. "localhost",
  203. ],
  204. ):
  205. switchbot_mqtt._main()
  206. default_kwargs = dict(
  207. mqtt_host="localhost",
  208. mqtt_port=1883,
  209. mqtt_username=None,
  210. mqtt_password=None,
  211. retry_count=3,
  212. device_passwords={},
  213. )
  214. run_mock.assert_called_once_with(fetch_device_info=False, **default_kwargs)
  215. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  216. "sys.argv",
  217. ["", "--mqtt-host", "localhost", "--fetch-device-info"],
  218. ):
  219. switchbot_mqtt._main()
  220. run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
  221. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  222. "sys.argv",
  223. ["", "--mqtt-host", "localhost"],
  224. ), unittest.mock.patch.dict("os.environ", {"FETCH_DEVICE_INFO": "21"}):
  225. switchbot_mqtt._main()
  226. run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
  227. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  228. "sys.argv",
  229. ["", "--mqtt-host", "localhost"],
  230. ), unittest.mock.patch.dict("os.environ", {"FETCH_DEVICE_INFO": ""}):
  231. switchbot_mqtt._main()
  232. run_mock.assert_called_once_with(fetch_device_info=False, **default_kwargs)
  233. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  234. "sys.argv",
  235. ["", "--mqtt-host", "localhost"],
  236. ), unittest.mock.patch.dict("os.environ", {"FETCH_DEVICE_INFO": " "}):
  237. switchbot_mqtt._main()
  238. run_mock.assert_called_once_with(fetch_device_info=True, **default_kwargs)
  239. @pytest.mark.parametrize(
  240. ("additional_argv", "root_log_level", "log_format"),
  241. [
  242. ([], logging.INFO, "%(message)s"),
  243. (
  244. ["--debug"],
  245. logging.DEBUG,
  246. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
  247. ),
  248. ],
  249. )
  250. def test__main_log_config(
  251. additional_argv: typing.List[str], root_log_level: int, log_format: str
  252. ):
  253. with unittest.mock.patch(
  254. "sys.argv", ["", "--mqtt-host", "localhost"] + additional_argv
  255. ), unittest.mock.patch(
  256. "logging.basicConfig"
  257. ) as logging_basic_config_mock, unittest.mock.patch(
  258. "switchbot_mqtt._run"
  259. ):
  260. switchbot_mqtt._main()
  261. logging_basic_config_mock.assert_called_once_with(
  262. level=root_log_level, format=log_format, datefmt="%Y-%m-%dT%H:%M:%S%z"
  263. )