Browse Source

fix parsing values in setconfig (#4225)

3.2.x
ghost43 7 years ago
committed by GitHub
parent
commit
cf88e239d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      lib/commands.py
  2. 33
      lib/tests/test_commands.py

13
lib/commands.py

@ -150,11 +150,20 @@ class Commands:
"""Return a configuration variable. """
return self.config.get(key)
@classmethod
def _setconfig_normalize_value(cls, key, value):
if key not in ('rpcuser', 'rpcpassword'):
value = json_decode(value)
try:
value = ast.literal_eval(value)
except:
pass
return value
@command('')
def setconfig(self, key, value):
"""Set a configuration variable. 'value' may be a string or a Python expression."""
if key not in ('rpcuser', 'rpcpassword'):
value = json_decode(value)
value = self._setconfig_normalize_value(key, value)
self.config.set_key(key, value)
return True

33
lib/tests/test_commands.py

@ -0,0 +1,33 @@
import unittest
from decimal import Decimal
from lib.commands import Commands
class TestCommands(unittest.TestCase):
def test_setconfig_non_auth_number(self):
self.assertEqual(7777, Commands._setconfig_normalize_value('rpcport', "7777"))
self.assertEqual(7777, Commands._setconfig_normalize_value('rpcport', '7777'))
self.assertAlmostEqual(Decimal(2.3), Commands._setconfig_normalize_value('somekey', '2.3'))
def test_setconfig_non_auth_number_as_string(self):
self.assertEqual("7777", Commands._setconfig_normalize_value('somekey', "'7777'"))
def test_setconfig_non_auth_boolean(self):
self.assertEqual(True, Commands._setconfig_normalize_value('show_console_tab', "true"))
self.assertEqual(True, Commands._setconfig_normalize_value('show_console_tab', "True"))
def test_setconfig_non_auth_list(self):
self.assertEqual(['file:///var/www/', 'https://electrum.org'],
Commands._setconfig_normalize_value('url_rewrite', "['file:///var/www/','https://electrum.org']"))
self.assertEqual(['file:///var/www/', 'https://electrum.org'],
Commands._setconfig_normalize_value('url_rewrite', '["file:///var/www/","https://electrum.org"]'))
def test_setconfig_auth(self):
self.assertEqual("7777", Commands._setconfig_normalize_value('rpcuser', "7777"))
self.assertEqual("7777", Commands._setconfig_normalize_value('rpcuser', '7777'))
self.assertEqual("7777", Commands._setconfig_normalize_value('rpcpassword', '7777'))
self.assertEqual("2asd", Commands._setconfig_normalize_value('rpcpassword', '2asd'))
self.assertEqual("['file:///var/www/','https://electrum.org']",
Commands._setconfig_normalize_value('rpcpassword', "['file:///var/www/','https://electrum.org']"))
Loading…
Cancel
Save