From ae5a264df0ec30184945ac79c88ec4e0bb19f050 Mon Sep 17 00:00:00 2001 From: shekhar-cis Date: Thu, 5 May 2022 13:21:11 +0530 Subject: [PATCH] Update BMConfigParser and add test to enable/disable identity --- src/bmconfigparser.py | 15 +++++++++ src/tests/test_config_address.py | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/tests/test_config_address.py diff --git a/src/bmconfigparser.py b/src/bmconfigparser.py index 0a2ec558..dc26783d 100644 --- a/src/bmconfigparser.py +++ b/src/bmconfigparser.py @@ -154,6 +154,21 @@ class BMConfigParser(SafeConfigParser): return False return True + def search_addresses(self, address, searched_text): + """Return the searched label of MyAddress""" + return [x for x in [self.get(address, 'label').lower(), + address.lower()] if searched_text in x] + + def disable_address(self, address): + """"Disabling the specific Address""" + self.set(str(address), 'enabled', 'false') + self.save() + + def enable_address(self, address): + """"Enabling the specific Address""" + self.set(address, 'enabled', 'true') + self.save() + if not getattr(BMConfigParser, 'read_file', False): BMConfigParser.read_file = BMConfigParser.readfp diff --git a/src/tests/test_config_address.py b/src/tests/test_config_address.py new file mode 100644 index 00000000..b76df7ec --- /dev/null +++ b/src/tests/test_config_address.py @@ -0,0 +1,57 @@ +""" +Various tests to Enable and Disable the identity +""" + +import unittest +from six import StringIO +from six.moves import configparser +from pybitmessage.bmconfigparser import BMConfigParser + + +address_obj = """[BM-enabled_identity] +label = Test_address_1 +enabled = true + +[BM-disabled_identity] +label = Test_address_2 +enabled = false +""" + + +# pylint: disable=protected-access +class TestAddressEnableDisable(unittest.TestCase): + """A test case for bmconfigparser""" + + def setUp(self): + self.config = BMConfigParser() + self.config.read_file(StringIO(address_obj)) + + def test_enable_enabled_identity(self): + """Test enabling already enabled identity""" + self.config.enable_address('BM-enabled_identity') + self.assertEqual(self.config.safeGet('BM-enabled_identity', 'enabled'), 'true') + + def test_enable_disabled_identity(self): + """Test enabling the Disabled identity""" + self.config.enable_address('BM-disabled_identity') + self.assertEqual(self.config.safeGet('BM-disabled_identity', 'enabled'), 'true') + + def test_enable_non_existent_identity(self): + """Test enable non-existent address""" + with self.assertRaises(configparser.NoSectionError): + self.config.enable_address('non_existent_address') + + def test_disable_disabled_identity(self): + """Test disabling already disabled identity""" + self.config.disable_address('BM-disabled_identity') + self.assertEqual(self.config.safeGet('BM-disabled_identity', 'enabled'), 'false') + + def test_disable_enabled_identity(self): + """Test Disabling the Enabled identity""" + self.config.disable_address('BM-enabled_identity') + self.assertEqual(self.config.safeGet('BM-enabled_identity', 'enabled'), 'false') + + def test_disable_non_existent_identity(self): + """Test dsiable non-existent address""" + with self.assertRaises(configparser.NoSectionError): + self.config.disable_address('non_existent_address')