Adjust for YAML config file; boolean settings are actually boolean
authorKarl O. Pinc <kop@karlpinc.com>
Fri, 21 Jun 2024 23:39:00 +0000 (18:39 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Fri, 21 Jun 2024 23:39:00 +0000 (18:39 -0500)
src/pgwui_common/check_settings.py
src/pgwui_common/checkset.py
tests/test_check_settings.py
tests/test_checkset.py

index 8330046bda5322135b1fb6ab8a4a4ee868221dcf..2ef7acdcc2e17b47589d87ad8201ce1cc38e0dfd 100644 (file)
@@ -24,7 +24,6 @@
 '''
 
 import re
-from ast import literal_eval
 
 from . import constants
 from . import exceptions as ex
@@ -49,15 +48,11 @@ def require_setting(errors, setting, pgwui_settings, formatter):
 
 
 def boolean_setting(errors, setting, pgwui_settings):
-    if setting in pgwui_settings:
-        try:
-            val = literal_eval(pgwui_settings[setting])
-        except ValueError:
-            val = None
-        if (val is not True
-                and val is not False):
-            errors.append(ex.NotBooleanSettingError(
-                key_to_ini(setting), pgwui_settings[setting]))
+    val = pgwui_settings.get(setting, True)
+    if (val is not True
+            and val is not False):
+        errors.append(ex.NotBooleanSettingError(
+            key_to_ini(setting), pgwui_settings[setting]))
 
 
 def validate_setting_values(errors, settings):
@@ -87,11 +82,11 @@ def validate_setting_values(errors, settings):
 
 
 def do_validate_hmac(settings):
-    '''True unless the user has specificly rejected hmac validation
+    '''True unless the user has specifically rejected hmac validation
     '''
     pgwui_settings = settings['pgwui']
     return ('validate_hmac' not in pgwui_settings
-            or literal_eval(pgwui_settings['validate_hmac']))
+            or pgwui_settings['validate_hmac'] is True)
 
 
 def validate_hmac(errors, settings):
@@ -235,6 +230,9 @@ def validate_page_setting(errors, settings, page_key):
 def validate_settings(errors, settings):
     '''Validate all core settings
     '''
+    # This is different from all the other component's check_settings().
+    # This takes all the settings, whereas the others take only their
+    # component's settings.
     validate_setting_values(errors, settings)
     validate_hmac(errors, settings)
     validate_page_setting(errors, settings, 'home_page')
index d6965410d9d5b863bd8d8eeb9ab0587924820a8b..238939ba38c2ee725dfd7a65fc623e3a3ef7f748 100644 (file)
@@ -1,4 +1,5 @@
-# Copyright (C) 2020, 2021 The Meme Factory, Inc.  http://www.karlpinc.com/
+# Copyright (C) 2020, 2021, 2024 The Meme Factory, Inc.
+#   http://www.karlpinc.com/
 
 # This file is part of PGWUI_Common.
 #
@@ -22,8 +23,6 @@
 '''Helper routines for checking a PGWUI component's settings
 '''
 
-from ast import literal_eval
-
 from . import exceptions
 
 
@@ -48,15 +47,11 @@ def unknown_settings(component, settings, conf):
 def boolean_settings(component, booleans, conf):
     errors = []
     for setting in booleans:
-        if setting in conf:
-            try:
-                val = literal_eval(conf[setting])
-            except ValueError:
-                val = None
-            if (val is not True
-                    and val is not False):
-                errors.append(exceptions.NotBooleanSettingError(
-                    '{}:{}'.format(component, setting), conf[setting]))
+        val = conf.get(setting, True)
+        if (val is not True
+                and val is not False):
+            errors.append(exceptions.NotBooleanSettingError(
+                '{}:{}'.format(component, setting), conf[setting]))
     return errors
 
 
index d4fbe9ab99646573a0146dea00225db7c7b2519a..c928b4766775b4eb1f477db396d48332c3619fa6 100644 (file)
@@ -85,17 +85,17 @@ def test_boolean_setting_missing():
 
 
 def test_boolean_setting_true():
-    '''Does nothing when the setting is "True"'''
+    '''Does nothing when the setting is True'''
     errors = []
-    check_settings.boolean_setting(errors, 'key', {'key': 'True'})
+    check_settings.boolean_setting(errors, 'key', {'key': True})
 
     assert errors == []
 
 
 def test_boolean_setting_false():
-    '''Does nothing when the setting is "False"'''
+    '''Does nothing when the setting is False'''
     errors = []
-    check_settings.boolean_setting(errors, 'key', {'key': 'False'})
+    check_settings.boolean_setting(errors, 'key', {'key': False})
 
     assert errors == []
 
@@ -149,14 +149,14 @@ def test_do_validate_hmac_none():
 def test_do_validate_hmac_True():
     '''Require hmac validation when pgwui.validate_hmac is True'''
     result = check_settings.do_validate_hmac(
-        {'pgwui': {'validate_hmac': 'True'}})
+        {'pgwui': {'validate_hmac': True}})
     assert result is True
 
 
 def test_do_validate_hmac_False():
     '''No hmac validation when pgwui.validate_hmac is False'''
     result = check_settings.do_validate_hmac(
-        {'pgwui': {'validate_hmac': 'False'}})
+        {'pgwui': {'validate_hmac': False}})
     assert result is False
 
 
index 54d55e8d7ff8def456504cea1424f825987a549f..f43551bfd8d65dd7e8878fcda1a32e821614ebc9 100644 (file)
@@ -87,7 +87,7 @@ def test_unknown_settings_bad():
 def test_boolean_settings_good():
     '''No errors when boolean settings are boolean
     '''
-    conf = {'settinga': 'a', 'settingb': 'True', 'settingc': 'False'}
+    conf = {'settinga': 'a', 'settingb': True, 'settingc': False}
     bools = ['settingc', 'settingb']
 
     result = checkset.boolean_settings('testcomp', bools, conf)
@@ -104,7 +104,7 @@ def test_boolean_settings_bad():
 
     result = checkset.boolean_settings('testcomp', bools, conf)
 
-    assert len(result) == 1
+    assert len(result) == 2
     for error in result:
         assert isinstance(error, ex.NotBooleanSettingError)