1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 02:09:17 +02:00

make set_config|get_config throw an informative error if you try to get/set non-existing config keys

This commit is contained in:
holger krekel 2018-10-09 15:04:25 +02:00
parent 987dc80033
commit 0bbeb65d94
2 changed files with 16 additions and 0 deletions

View file

@ -45,6 +45,12 @@ class Account(object):
self._evlogger = EventLogger(self._dc_context, logid) self._evlogger = EventLogger(self._dc_context, logid)
deltachat.set_context_callback(self._dc_context, self._process_event) deltachat.set_context_callback(self._dc_context, self._process_event)
self._threads = IOThreads(self._dc_context) self._threads = IOThreads(self._dc_context)
self._configkeys = self.get_config("sys.config_keys").split()
def _check_config_key(self, name):
if name not in self._configkeys:
raise KeyError("{!r} not a valid config key, existing keys: {!r}".format(
name, self._configkeys))
def set_config(self, name, value): def set_config(self, name, value):
""" set configuration values. """ set configuration values.
@ -53,6 +59,7 @@ class Account(object):
:param value: value to set (unicode) :param value: value to set (unicode)
:returns: None :returns: None
""" """
self._check_config_key(name)
name = name.encode("utf8") name = name.encode("utf8")
value = value.encode("utf8") value = value.encode("utf8")
if name == b"addr" and self.is_configured(): if name == b"addr" and self.is_configured():
@ -66,6 +73,8 @@ class Account(object):
:returns: unicode value :returns: unicode value
:raises: KeyError if no config value was found. :raises: KeyError if no config value was found.
""" """
if name != "sys.config_keys":
self._check_config_key(name)
name = name.encode("utf8") name = name.encode("utf8")
res = lib.dc_get_config(self._dc_context, name, ffi.NULL) res = lib.dc_get_config(self._dc_context, name, ffi.NULL)
if res == ffi.NULL: if res == ffi.NULL:

View file

@ -13,6 +13,13 @@ class TestOfflineAccount:
with pytest.raises(ValueError): with pytest.raises(ValueError):
ac1.check_is_configured() ac1.check_is_configured()
def test_wrong_config_keys(self, acfactory):
ac1 = acfactory.get_unconfigured_account()
with pytest.raises(KeyError):
ac1.set_config("lqkwje", "value")
with pytest.raises(KeyError):
ac1.get_config("lqkwje")
def test_selfcontact_if_unconfigured(self, acfactory): def test_selfcontact_if_unconfigured(self, acfactory):
ac1 = acfactory.get_unconfigured_account() ac1 = acfactory.get_unconfigured_account()
with pytest.raises(ValueError): with pytest.raises(ValueError):