1
0

test_cli.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. @pytest.mark.parametrize(
  22. (
  23. "argv",
  24. "expected_mqtt_host",
  25. "expected_mqtt_port",
  26. "expected_username",
  27. "expected_password",
  28. ),
  29. [
  30. (
  31. ["", "--mqtt-host", "mqtt-broker.local"],
  32. "mqtt-broker.local",
  33. 1883,
  34. None,
  35. None,
  36. ),
  37. (
  38. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  39. "mqtt-broker.local",
  40. 8883,
  41. None,
  42. None,
  43. ),
  44. (
  45. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  46. "mqtt-broker.local",
  47. 1883,
  48. "me",
  49. None,
  50. ),
  51. (
  52. [
  53. "",
  54. "--mqtt-host",
  55. "mqtt-broker.local",
  56. "--mqtt-username",
  57. "me",
  58. "--mqtt-password",
  59. "secret",
  60. ],
  61. "mqtt-broker.local",
  62. 1883,
  63. "me",
  64. "secret",
  65. ),
  66. ],
  67. )
  68. def test__main(
  69. argv, expected_mqtt_host, expected_mqtt_port, expected_username, expected_password
  70. ):
  71. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  72. "sys.argv", argv
  73. ):
  74. # pylint: disable=protected-access
  75. switchbot_mqtt._main()
  76. run_mock.assert_called_once_with(
  77. mqtt_host=expected_mqtt_host,
  78. mqtt_port=expected_mqtt_port,
  79. mqtt_username=expected_username,
  80. mqtt_password=expected_password,
  81. )
  82. @pytest.mark.parametrize(
  83. ("password_file_content", "expected_password"),
  84. [
  85. ("secret", "secret"),
  86. ("secret space", "secret space"),
  87. ("secret ", "secret "),
  88. (" secret ", " secret "),
  89. ("secret\n", "secret"),
  90. ("secret\n\n", "secret\n"),
  91. ("secret\r\n", "secret"),
  92. ("secret\n\r\n", "secret\n"),
  93. ("你好\n", "你好"),
  94. ],
  95. )
  96. def test__main_password_file(tmpdir, password_file_content, expected_password):
  97. mqtt_password_path = tmpdir.join("mqtt-password")
  98. with mqtt_password_path.open("w") as mqtt_password_file:
  99. mqtt_password_file.write(password_file_content)
  100. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  101. "sys.argv",
  102. [
  103. "",
  104. "--mqtt-host",
  105. "localhost",
  106. "--mqtt-username",
  107. "me",
  108. "--mqtt-password-file",
  109. str(mqtt_password_path),
  110. ],
  111. ):
  112. # pylint: disable=protected-access
  113. switchbot_mqtt._main()
  114. run_mock.assert_called_once_with(
  115. mqtt_host="localhost",
  116. mqtt_port=1883,
  117. mqtt_username="me",
  118. mqtt_password=expected_password,
  119. )
  120. def test__main_password_file_collision(capsys):
  121. with unittest.mock.patch(
  122. "sys.argv",
  123. [
  124. "",
  125. "--mqtt-host",
  126. "localhost",
  127. "--mqtt-username",
  128. "me",
  129. "--mqtt-password",
  130. "secret",
  131. "--mqtt-password-file",
  132. "/var/lib/secrets/mqtt/password",
  133. ],
  134. ):
  135. with pytest.raises(SystemExit):
  136. # pylint: disable=protected-access
  137. switchbot_mqtt._main()
  138. out, err = capsys.readouterr()
  139. assert not out
  140. assert (
  141. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  142. in err
  143. )