test_cli.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ],
  77. )
  78. def test__main_password_file(tmpdir, password_file_content, expected_password):
  79. mqtt_password_path = tmpdir.join("mqtt-password")
  80. with mqtt_password_path.open("w") as mqtt_password_file:
  81. mqtt_password_file.write(password_file_content)
  82. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  83. "sys.argv",
  84. [
  85. "",
  86. "--mqtt-host",
  87. "localhost",
  88. "--mqtt-username",
  89. "me",
  90. "--mqtt-password-file",
  91. str(mqtt_password_path),
  92. ],
  93. ):
  94. # pylint: disable=protected-access
  95. switchbot_mqtt._main()
  96. run_mock.assert_called_once_with(
  97. mqtt_host="localhost",
  98. mqtt_port=1883,
  99. mqtt_username="me",
  100. mqtt_password=expected_password,
  101. )
  102. def test__main_password_file_collision(capsys):
  103. with unittest.mock.patch(
  104. "sys.argv",
  105. [
  106. "",
  107. "--mqtt-host",
  108. "localhost",
  109. "--mqtt-username",
  110. "me",
  111. "--mqtt-password",
  112. "secret",
  113. "--mqtt-password-file",
  114. "/var/lib/secrets/mqtt/password",
  115. ],
  116. ):
  117. with pytest.raises(SystemExit):
  118. # pylint: disable=protected-access
  119. switchbot_mqtt._main()
  120. out, err = capsys.readouterr()
  121. assert not out
  122. assert (
  123. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  124. in err
  125. )