diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index a4d704d..2b27df3 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -119,6 +119,10 @@ def setPref(attributes, name, valStr): val = meshtastic.util.fromStr(valStr) logging.debug(f'valStr:{valStr} val:{val}') + if snake_name == 'wifi_password' and len(valStr) < 8: + print(f"Warning: wifi_password must be 8 or more characters.") + return + enumType = field.enum_type # pylint: disable=C0123 if enumType and type(val) == str: diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 58d1721..8298f5e 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -902,6 +902,27 @@ def test_main_set_valid_wifi_passwd(capsys): mo.assert_called() +@pytest.mark.unit +@pytest.mark.usefixtures("reset_globals") +def test_main_set_invalid_wifi_passwd(capsys): + """Test --set with an invalid value (password must be 8 or more characters)""" + sys.argv = ['', '--set', 'wifi_password', '1234567'] + Globals.getInstance().set_args(sys.argv) + + mocked_node = MagicMock(autospec=Node) + + iface = MagicMock(autospec=SerialInterface) + iface.getNode.return_value = mocked_node + + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + main() + out, err = capsys.readouterr() + assert re.search(r'Connected to radio', out, re.MULTILINE) + assert not re.search(r'Set wifi_password to 1234567', out, re.MULTILINE) + assert re.search(r'Warning: wifi_password must be 8 or more characters.', out, re.MULTILINE) + assert err == '' + mo.assert_called() + @pytest.mark.unit @pytest.mark.usefixtures("reset_globals") def test_main_set_valid_camel_case(capsys):