test_cli.py 4.1 KB

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