Update BMConfigParser and add test to enable/disable identity

This commit is contained in:
shekhar-cis 2022-05-05 13:21:11 +05:30
parent 991e470a06
commit ae5a264df0
Signed by untrusted user: shekhar-cis
GPG Key ID: F4F00AB04E83F9A7
2 changed files with 72 additions and 0 deletions

View File

@ -154,6 +154,21 @@ class BMConfigParser(SafeConfigParser):
return False return False
return True 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): if not getattr(BMConfigParser, 'read_file', False):
BMConfigParser.read_file = BMConfigParser.readfp BMConfigParser.read_file = BMConfigParser.readfp

View File

@ -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')