forked from LBRYCommunity/lbry-sdk
Added validation for currencies.
Now while setting currency via API/CLI it is checked that the user has not entered any currency other that LBC, BTC or USD. Fixes #778
This commit is contained in:
parent
bf6bc02828
commit
ecd602e5fb
4 changed files with 36 additions and 1 deletions
|
@ -12,6 +12,10 @@ at anytime.
|
|||
*
|
||||
*
|
||||
|
||||
### Added
|
||||
* Added validation of currencies.
|
||||
*
|
||||
|
||||
### Fixed
|
||||
*
|
||||
*
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue