def test__main_device_password_file(tmpdir, device_passwords): device_passwords_path = tmpdir.join("passwords.json") device_passwords_path.write_text(json.dumps(device_passwords), encoding="utf8") with unittest.mock.patch( "switchbot_mqtt._run") as run_mock, unittest.mock.patch( "sys.argv", [ "", "--mqtt-host", "localhost", "--device-password-file", str(device_passwords_path), ], ): # pylint: disable=protected-access switchbot_mqtt._main() run_mock.assert_called_once_with( mqtt_host="localhost", mqtt_port=1883, mqtt_username=None, mqtt_password=None, retry_count=3, device_passwords=device_passwords, )
def test__main_mqtt_password_file(tmpdir, mqtt_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(mqtt_password_file_content) with unittest.mock.patch( "switchbot_mqtt._run") as run_mock, unittest.mock.patch( "sys.argv", [ "", "--mqtt-host", "localhost", "--mqtt-username", "me", "--mqtt-password-file", str(mqtt_password_path), ], ): # pylint: disable=protected-access switchbot_mqtt._main() run_mock.assert_called_once_with( mqtt_host="localhost", mqtt_port=1883, mqtt_username="******", mqtt_password=expected_password, retry_count=3, device_passwords={}, )
def test__main(argv, expected_mqtt_host, expected_mqtt_port, expected_username, expected_password): with unittest.mock.patch( "switchbot_mqtt._run") as run_mock, unittest.mock.patch( "sys.argv", argv): # pylint: disable=protected-access switchbot_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, )
def test__main_mqtt_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): # pylint: disable=protected-access switchbot_mqtt._main() out, err = capsys.readouterr() assert not out assert ( "argument --mqtt-password-file: not allowed with argument --mqtt-password\n" in err)