Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions adafruit_portalbase/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, couple questions:

  1. what about APs with no password?
  2. to isinstance is faster and generally better then type can we use those?
  3. can we add tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got you on 1 and 2, but I'm not very familiar with pytest. Let me know if I got it right.

By the way, I totally forgot that APs could have empty passwords. 😆

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look at the tests tomorrow. I'll make suggestions for anything to add/change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And right - who would want something without a password? But people still complain when they can't connect because of it ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justmobilize I think I've got the test figured out now, but let me know if it needs any adjustment.

filter(
lambda credentials: isinstance(credentials, (list, tuple))
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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_get_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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