diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d7a47888..807e96b49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ at anytime. * * +### Added + * Added validation of currencies. + * + ### Fixed * * diff --git a/lbrynet/conf.py b/lbrynet/conf.py index 22c9662b7..2e168d805 100644 --- a/lbrynet/conf.py +++ b/lbrynet/conf.py @@ -8,6 +8,7 @@ import yaml import envparse from appdirs import user_data_dir, user_config_dir from lbrynet.core import utils +from lbrynet.core.Error import InvalidCurrencyError try: from lbrynet.winhelpers.knownpaths import get_path, FOLDERID, UserHandle @@ -351,6 +352,10 @@ class Config(object): assert name not in self._fixed_defaults, \ ValueError('{} is not an editable setting'.format(name)) + def _validate_currency(self, currency): + if currency not in self._fixed_defaults['CURRENCIES'].keys(): + raise InvalidCurrencyError(currency) + def get(self, name, data_type=None): """Get a config value @@ -388,6 +393,10 @@ class Config(object): data types (e.g. PERSISTED values to save to a file, CLI values from parsed command-line options, etc), you can specify that with the data_types param """ + if name == "max_key_fee": + currency = str(value["currency"]).upper() + self._validate_currency(currency) + self._assert_editable_setting(name) for data_type in data_types: self._assert_valid_data_type(data_type) diff --git a/lbrynet/core/Error.py b/lbrynet/core/Error.py index 02316d6d2..d1e8bb785 100644 --- a/lbrynet/core/Error.py +++ b/lbrynet/core/Error.py @@ -139,3 +139,9 @@ class InvalidAuthenticationToken(Exception): class NegotiationError(Exception): pass + +class InvalidCurrencyError(Exception): + def __init__(self, currency): + self.currency = currency + Exception.__init__( + self, 'Invalid currency: {} is not a supported currency.'.format(currency)) diff --git a/tests/unit/test_conf.py b/tests/unit/test_conf.py index d7adeb174..194d565af 100644 --- a/tests/unit/test_conf.py +++ b/tests/unit/test_conf.py @@ -1,8 +1,9 @@ import os +import json from twisted.trial import unittest from lbrynet import conf - +from lbrynet.core.Error import InvalidCurrencyError class SettingsTest(unittest.TestCase): def setUp(self): @@ -54,6 +55,21 @@ class SettingsTest(unittest.TestCase): settings.set('test', 'runtime_takes_precedence', data_types=(conf.TYPE_RUNTIME,)) self.assertEqual('runtime_takes_precedence', settings['test']) + def test_max_key_fee_set(self): + fixed_default = {'CURRENCIES':{'BTC':{'type':'crypto'}}} + adjustable_settings = {'max_key_fee': (json.loads, {'currency':'USD', 'amount':1})} + env = conf.Env(**adjustable_settings) + settings = conf.Config(fixed_default, adjustable_settings, environment=env) + + with self.assertRaises(InvalidCurrencyError): + settings.set('max_key_fee', {'currency':'USD', 'amount':1}) + + valid_setting= {'currency':'BTC', 'amount':1} + settings.set('max_key_fee', valid_setting ) + out = settings.get('max_key_fee') + self.assertEqual(out, valid_setting) + + def test_data_dir(self): # check if these directories are returned as string and not unicode # otherwise there will be problems when calling os.path.join on