From d810d12d64be659feb14ba6c7427f847f92cb541 Mon Sep 17 00:00:00 2001 From: kdmukai Date: Thu, 31 Aug 2023 18:44:40 -0500 Subject: [PATCH] bugfix; caught by @jdlcdl --- src/seedsigner/models/settings.py | 6 ++++++ src/seedsigner/views/settings_views.py | 7 ------- tests/test_flows_settings.py | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/seedsigner/models/settings.py b/src/seedsigner/models/settings.py index 7b5f4602..f0b1c97f 100644 --- a/src/seedsigner/models/settings.py +++ b/src/seedsigner/models/settings.py @@ -87,6 +87,12 @@ class Settings(Singleton): 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 diff --git a/src/seedsigner/views/settings_views.py b/src/seedsigner/views/settings_views.py index 8eca039c..e2a742ca 100644 --- a/src/seedsigner/views/settings_views.py +++ b/src/seedsigner/views/settings_views.py @@ -196,13 +196,6 @@ class SettingsIngestSettingsQRView(View): # 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) diff --git a/tests/test_flows_settings.py b/tests/test_flows_settings.py index d9a62574..a6eb34d9 100644 --- a/tests/test_flows_settings.py +++ b/tests/test_flows_settings.py @@ -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 @@ -116,6 +118,19 @@ class TestSettingsFlows(FlowTest): 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,