'''
import re
-from ast import literal_eval
from . import constants
from . import exceptions as ex
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):
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):
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')
-# 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.
#
'''Helper routines for checking a PGWUI component's settings
'''
-from ast import literal_eval
-
from . import exceptions
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
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 == []
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
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)
result = checkset.boolean_settings('testcomp', bools, conf)
- assert len(result) == 1
+ assert len(result) == 2
for error in result:
assert isinstance(error, ex.NotBooleanSettingError)