import pathlib import unittest.mock import pytest import intertechno_cc1101_mqtt # pylint: disable=protected-access @pytest.mark.parametrize( ( "argv", "expected_mqtt_host", "expected_mqtt_port", "expected_username", "expected_password", ), [ ( ["", "--mqtt-host", "mqtt-broker.local"], "mqtt-broker.local", 1883, None, None, ), ( ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"], "mqtt-broker.local", 8883, None, None, ), ( ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"], "mqtt-broker.local", 1883, "me", None, ), ( [ "", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me", "--mqtt-password", "secret", ], "mqtt-broker.local", 1883, "me", "secret", ), ], ) def test__main( argv, expected_mqtt_host, expected_mqtt_port, expected_username, expected_password ): with unittest.mock.patch( "intertechno_cc1101_mqtt._run" ) as run_mock, unittest.mock.patch("sys.argv", argv): intertechno_cc1101_mqtt._main() run_mock.assert_called_once_with( mqtt_host=expected_mqtt_host, mqtt_port=expected_mqtt_port, mqtt_username=expected_username, mqtt_password=expected_password, alias_file_path=None, power_setting=0xC6, ) @pytest.mark.parametrize( ("password_file_content", "expected_password"), [ ("secret", "secret"), ("secret space", "secret space"), ("secret ", "secret "), (" secret ", " secret "), ("secret\n", "secret"), ("secret\n\n", "secret\n"), ("secret\r\n", "secret"), ("secret\n\r\n", "secret\n"), ("你好\n", "你好"), ], ) def test__main_password_file(tmpdir, password_file_content, expected_password): mqtt_password_path = tmpdir.join("mqtt-password") with mqtt_password_path.open("w") as mqtt_password_file: mqtt_password_file.write(password_file_content) with unittest.mock.patch( "intertechno_cc1101_mqtt._run" ) as run_mock, unittest.mock.patch( "sys.argv", [ "", "--mqtt-host", "localhost", "--mqtt-username", "me", "--mqtt-password-file", str(mqtt_password_path), ], ): intertechno_cc1101_mqtt._main() run_mock.assert_called_once_with( mqtt_host="localhost", mqtt_port=1883, mqtt_username="me", mqtt_password=expected_password, alias_file_path=None, power_setting=0xC6, ) def test__main_password_file_collision(capsys): with unittest.mock.patch( "sys.argv", [ "", "--mqtt-host", "localhost", "--mqtt-username", "me", "--mqtt-password", "secret", "--mqtt-password-file", "/var/lib/secrets/mqtt/password", ], ): with pytest.raises(SystemExit): intertechno_cc1101_mqtt._main() out, err = capsys.readouterr() assert not out assert ( "argument --mqtt-password-file: not allowed with argument --mqtt-password\n" in err ) def test__main_alias_file(): with unittest.mock.patch( "intertechno_cc1101_mqtt._run" ) as run_mock, unittest.mock.patch( "sys.argv", [ "", "--mqtt-host", "broker", "--alias-file", "/etc/intertechno-cc1101-mqtt/aliases.json", ], ): intertechno_cc1101_mqtt._main() run_mock.assert_called_once_with( mqtt_host="broker", mqtt_port=1883, mqtt_username=None, mqtt_password=None, alias_file_path=pathlib.Path("/etc/intertechno-cc1101-mqtt/aliases.json"), power_setting=0xC6, ) def test__main_power_setting(): with unittest.mock.patch( "intertechno_cc1101_mqtt._run" ) as run_mock, unittest.mock.patch( "sys.argv", ["", "--mqtt-host", "broker", "--power-setting", "29"] ): intertechno_cc1101_mqtt._main() run_mock.assert_called_once_with( mqtt_host="broker", mqtt_port=1883, mqtt_username=None, mqtt_password=None, alias_file_path=None, power_setting=0x1D, # -15dBm )