test_cli.py 3.5 KB

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