Skip to content

Commit

Permalink
bugfix; caught by @jdlcdl
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmukai committed Aug 31, 2023
1 parent b4e7009 commit a48620f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/seedsigner/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def parse_settingsqr(cls, data: str) -> tuple[str, dict]:
values = value
for v in values:
if v not in [opt[0] for opt in settings_entry.selection_options]:
if settings_entry.attr_name == SettingsConstants.SETTING__PERSISTENT_SETTINGS and v == SettingsConstants.OPTION__ENABLED:
# Special case: trying to enable Persistent Settings when
# DISABLED is the only option allowed (because the SD card is not
# inserted. Explicitly set to DISABLED.
value = SettingsConstants.OPTION__DISABLED
break
raise InvalidSettingsQRData(f"""{abbreviated_name} = '{v}' is not valid""")

updated_settings[settings_entry.attr_name] = value
Expand Down
7 changes: 0 additions & 7 deletions src/seedsigner/views/settings_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,6 @@ def __init__(self, data: str):
# May raise an Exception which will bubble up to the Controller to display to the
# user.
self.config_name, settings_update_dict = Settings.parse_settingsqr(data)

persistent_settings = settings_update_dict.get(SettingsConstants.SETTING__PERSISTENT_SETTINGS)
if persistent_settings == SettingsConstants.OPTION__ENABLED and not MicroSD.get_instance().is_inserted:
# SettingsQR wants to enable persistent settings, but no MicroSD is inserted.
# For the sake of simplicity we just ignore that setting for now.
# TODO: Can consider a warning screen instead that gives the user some options.
del settings_update_dict[SettingsConstants.SETTING__PERSISTENT_SETTINGS]

self.settings.update(settings_update_dict)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_flows_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from typing import Callable

from mock import PropertyMock, patch

# Must import test base before the Controller
from base import FlowTest, FlowStep

Expand Down Expand Up @@ -116,6 +118,19 @@ def _run_test(initial_setting_state: str, load_settingsqr_into_decoder: Callable
self.mock_microsd.is_inserted = False
assert MicroSD.get_instance().is_inserted is False

# Have to jump through some hoops to completely simulate the SD card being
# removed; we need Settings to restrict Persistent Settings to only allow
# DISABLED.
with patch('seedsigner.models.settings.Settings.HOSTNAME', new_callable=PropertyMock) as mock_hostname:
# Must identify itself as SeedSigner OS to trigger the SD card removal logic
mock_hostname.return_value = Settings.SEEDSIGNER_OS
Settings.handle_microsd_state_change(MicroSD.ACTION__REMOVED)

selection_options = SettingsDefinition.get_settings_entry(SettingsConstants.SETTING__PERSISTENT_SETTINGS).selection_options
assert len(selection_options) == 1
assert selection_options[0][0] == SettingsConstants.OPTION__DISABLED
assert self.settings.get_value(SettingsConstants.SETTING__PERSISTENT_SETTINGS) == SettingsConstants.OPTION__DISABLED

_run_test(
initial_setting_state=SettingsConstants.OPTION__DISABLED,
load_settingsqr_into_decoder=load_persistent_settingsqr_into_decoder,
Expand Down

0 comments on commit a48620f

Please sign in to comment.