test_cli.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. power_setting=0xC6,
  67. )
  68. @pytest.mark.parametrize(
  69. ("password_file_content", "expected_password"),
  70. [
  71. ("secret", "secret"),
  72. ("secret space", "secret space"),
  73. ("secret ", "secret "),
  74. (" secret ", " secret "),
  75. ("secret\n", "secret"),
  76. ("secret\n\n", "secret\n"),
  77. ("secret\r\n", "secret"),
  78. ("secret\n\r\n", "secret\n"),
  79. ("你好\n", "你好"),
  80. ],
  81. )
  82. def test__main_password_file(tmpdir, password_file_content, expected_password):
  83. mqtt_password_path = tmpdir.join("mqtt-password")
  84. with mqtt_password_path.open("w") as mqtt_password_file:
  85. mqtt_password_file.write(password_file_content)
  86. with unittest.mock.patch(
  87. "intertechno_cc1101_mqtt._run"
  88. ) as run_mock, unittest.mock.patch(
  89. "sys.argv",
  90. [
  91. "",
  92. "--mqtt-host",
  93. "localhost",
  94. "--mqtt-username",
  95. "me",
  96. "--mqtt-password-file",
  97. str(mqtt_password_path),
  98. ],
  99. ):
  100. intertechno_cc1101_mqtt._main()
  101. run_mock.assert_called_once_with(
  102. mqtt_host="localhost",
  103. mqtt_port=1883,
  104. mqtt_username="me",
  105. mqtt_password=expected_password,
  106. alias_file_path=None,
  107. power_setting=0xC6,
  108. )
  109. def test__main_password_file_collision(capsys):
  110. with unittest.mock.patch(
  111. "sys.argv",
  112. [
  113. "",
  114. "--mqtt-host",
  115. "localhost",
  116. "--mqtt-username",
  117. "me",
  118. "--mqtt-password",
  119. "secret",
  120. "--mqtt-password-file",
  121. "/var/lib/secrets/mqtt/password",
  122. ],
  123. ):
  124. with pytest.raises(SystemExit):
  125. intertechno_cc1101_mqtt._main()
  126. out, err = capsys.readouterr()
  127. assert not out
  128. assert (
  129. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  130. in err
  131. )
  132. def test__main_alias_file():
  133. with unittest.mock.patch(
  134. "intertechno_cc1101_mqtt._run"
  135. ) as run_mock, unittest.mock.patch(
  136. "sys.argv",
  137. [
  138. "",
  139. "--mqtt-host",
  140. "broker",
  141. "--alias-file",
  142. "/etc/intertechno-cc1101-mqtt/aliases.json",
  143. ],
  144. ):
  145. intertechno_cc1101_mqtt._main()
  146. run_mock.assert_called_once_with(
  147. mqtt_host="broker",
  148. mqtt_port=1883,
  149. mqtt_username=None,
  150. mqtt_password=None,
  151. alias_file_path=pathlib.Path("/etc/intertechno-cc1101-mqtt/aliases.json"),
  152. power_setting=0xC6,
  153. )
  154. def test__main_power_setting():
  155. with unittest.mock.patch(
  156. "intertechno_cc1101_mqtt._run"
  157. ) as run_mock, unittest.mock.patch(
  158. "sys.argv", ["", "--mqtt-host", "broker", "--power-setting", "29"]
  159. ):
  160. intertechno_cc1101_mqtt._main()
  161. run_mock.assert_called_once_with(
  162. mqtt_host="broker",
  163. mqtt_port=1883,
  164. mqtt_username=None,
  165. mqtt_password=None,
  166. alias_file_path=None,
  167. power_setting=0x1D, # -15dBm
  168. )