diff --git a/adafruit_portalbase/network.py b/adafruit_portalbase/network.py index 6437b72..b500dc2 100755 --- a/adafruit_portalbase/network.py +++ b/adafruit_portalbase/network.py @@ -386,6 +386,21 @@ def connect(self, max_attempts=10): "'networks' must be a list/tuple of dicts of 'ssid' and 'password'" ) + self._wifi_credentials = list( + filter( + lambda credentials: isinstance(credentials, dict) + and "ssid" in credentials + and "password" in credentials + and isinstance(credentials["ssid"], str) + and isinstance(credentials["password"], str) + and len(credentials["ssid"]), + self._wifi_credentials, + ) + ) + if not len(self._wifi_credentials): + self._wifi_credentials = None + raise OSError("No wifi credentials provided") + for credentials in self._wifi_credentials: self._wifi.neo_status(STATUS_CONNECTING) attempt = 1 diff --git a/tests/test_get_settings.py b/tests/test_get_settings.py index d316cd0..b6e220b 100644 --- a/tests/test_get_settings.py +++ b/tests/test_get_settings.py @@ -125,3 +125,15 @@ def test_value_stored(settings_toml_current): with mock.patch("os.getenv", return_value="test") as mock_getenv: assert network._get_setting("ADAFRUIT_AIO_KEY") == "test" mock_getenv.assert_not_called() + + +def test_invalid_wifi_credentials(): + for key in ("CIRCUITPY_WIFI_SSID", "CIRCUITPY_WIFI_PASSWORD"): + if os.getenv(key) is not None and os.getenv(key) != "": + assert False + network = NetworkBase(None) + try: + network.connect() + assert False + except OSError: + assert True