Skip to content

Commit d5c51a1

Browse files
authored
Merge pull request #116 from relic-se/wifi_credentials_exception
Validate wifi credentials
2 parents d26e232 + 82f9671 commit d5c51a1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

adafruit_portalbase/network.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,21 @@ def connect(self, max_attempts=10):
386386
"'networks' must be a list/tuple of dicts of 'ssid' and 'password'"
387387
)
388388

389+
self._wifi_credentials = list(
390+
filter(
391+
lambda credentials: isinstance(credentials, dict)
392+
and "ssid" in credentials
393+
and "password" in credentials
394+
and isinstance(credentials["ssid"], str)
395+
and isinstance(credentials["password"], str)
396+
and len(credentials["ssid"]),
397+
self._wifi_credentials,
398+
)
399+
)
400+
if not len(self._wifi_credentials):
401+
self._wifi_credentials = None
402+
raise OSError("No wifi credentials provided")
403+
389404
for credentials in self._wifi_credentials:
390405
self._wifi.neo_status(STATUS_CONNECTING)
391406
attempt = 1

tests/test_get_settings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,15 @@ def test_value_stored(settings_toml_current):
125125
with mock.patch("os.getenv", return_value="test") as mock_getenv:
126126
assert network._get_setting("ADAFRUIT_AIO_KEY") == "test"
127127
mock_getenv.assert_not_called()
128+
129+
130+
def test_invalid_wifi_credentials():
131+
for key in ("CIRCUITPY_WIFI_SSID", "CIRCUITPY_WIFI_PASSWORD"):
132+
if os.getenv(key) is not None and os.getenv(key) != "":
133+
assert False
134+
network = NetworkBase(None)
135+
try:
136+
network.connect()
137+
assert False
138+
except OSError:
139+
assert True

0 commit comments

Comments
 (0)