From f146500b5897884cae4ba8ff40e75ace9b2823dc Mon Sep 17 00:00:00 2001 From: navjot Date: Thu, 24 Dec 2020 01:12:18 +0530 Subject: [PATCH] written test case for addressbook --- src/bitmessageqt/__init__.py | 12 +++++------ src/helper_addressbook.py | 9 ++++++-- src/tests/core.py | 40 ++++++++++++++++++++++++++++++++++- src/tests/test_addressbook.py | 20 ------------------ 4 files changed, 51 insertions(+), 30 deletions(-) delete mode 100644 src/tests/test_addressbook.py diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 76290de5..25f5cb8d 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -2436,17 +2436,15 @@ class MyForm(settingsmixin.SMainWindow): )) return - if address in BMConfigParser().addresses(): + if helper_addressbook.insert(label=label, address=address): + self.rerenderMessagelistFromLabels() + self.rerenderMessagelistToLabels() + self.rerenderAddressBook() + else: self.updateStatusBar(_translate( "MainWindow", "Error: You cannot add your own address in the address book." )) - return - - helper_addressbook.insert(label=label, address=address) - self.rerenderMessagelistFromLabels() - self.rerenderMessagelistToLabels() - self.rerenderAddressBook() def addSubscription(self, address, label): # This should be handled outside of this function, for error displaying diff --git a/src/helper_addressbook.py b/src/helper_addressbook.py index 8e36b745..a55f479a 100644 --- a/src/helper_addressbook.py +++ b/src/helper_addressbook.py @@ -2,10 +2,15 @@ Insert value into addressbook """ -from helper_sql import sqlExecute +from bmconfigparser import BMConfigParser +from helper_sql import sqlExecute, sqlQuery def insert(address, label): """perform insert into addressbook""" + queryreturn = sqlQuery( + '''SELECT count(*) FROM addressbook WHERE address=?''', address) - sqlExecute('''INSERT INTO addressbook VALUES (?,?)''', label, address) + if address not in BMConfigParser().addresses() and not queryreturn[0][0]: + return sqlExecute('''INSERT INTO addressbook VALUES (?,?)''', label, address) == 1 + return False diff --git a/src/tests/core.py b/src/tests/core.py index 03e8b948..90b342a2 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -16,6 +16,7 @@ import unittest import protocol import state import helper_sent +import helper_addressbook from bmconfigparser import BMConfigParser from helper_msgcoding import MsgEncode, MsgDecode @@ -57,6 +58,7 @@ def pickle_knownnodes(): class TestCore(unittest.TestCase): """Test case, which runs in main pybitmessage thread""" + addr = 'BM-2cVvkzJuQDsQHLqxRXc6HZGPLZnkBLzEZY' def test_msgcoding(self): """test encoding and decoding (originally from helper_msgcoding)""" @@ -251,7 +253,7 @@ class TestCore(unittest.TestCase): subject = 'test subject' result = helper_sent.insert( toAddress=toAddress, fromAddress=fromAddress, - subject=subject, message=message, + subject=subject, message=message ) queryreturn = sqlQuery( '''select msgid from sent where ackdata=?''', result) @@ -274,6 +276,42 @@ class TestCore(unittest.TestCase): finally: cleanup(files=('knownnodes.dat',)) + @staticmethod + def generate_random_address(): + """Generating random address""" + import queues + streamNumberForAddress = 1 + queues.addressGeneratorQueue.put(( + 'createRandomAddress', 4, streamNumberForAddress, + "test1", 1, "", False)) + + def delete_address_from_database(self): + """Deleting random address""" + sqlQuery('''delete from addressbook where address=?''', self.addr) + + def test_add_same_address_twice_in_addressbook(self): + """checking same address is added twice in addressbook""" + self.assertTrue(helper_addressbook.insert(label='test1', address=self.addr)) + self.assertFalse(helper_addressbook.insert(label='test1', address=self.addr)) + self.delete_address_from_database() + + def test_is_address_present_in_addressbook(self): + """checking is address added in addressbook or not""" + helper_addressbook.insert(label='test1', address=self.addr) + queryreturn = sqlQuery('''select count(*) from addressbook where address=?''', self.addr) + self.assertTrue(bool(queryreturn[0][0])) + self.delete_address_from_database() + + def test_is_own_address_add_to_addressbook(self): + """Checking own address adding in addressbook""" + self.generate_random_address() + time.sleep(.5) + try: + all_addresses = BMConfigParser().addresses() + self.assertFalse(helper_addressbook.insert(label='test', address=all_addresses[0])) + except IndexError: + self.fail("Can't generate addresses") + def run(): """Starts all tests defined in this module""" diff --git a/src/tests/test_addressbook.py b/src/tests/test_addressbook.py deleted file mode 100644 index 061af374..00000000 --- a/src/tests/test_addressbook.py +++ /dev/null @@ -1,20 +0,0 @@ -""" -Tests for addresbook validation -""" - -import unittest -from pybitmessage.bmconfigparser import BMConfigParser - - -class TestAddAddressBook(unittest.TestCase): - """this class is used for testting add address feature""" - - def test_is_own_address_add_to_addressbook(self): - """Check the logic of TCPConnection.local""" - own_addresses = BMConfigParser().addresses() - if own_addresses: - address = own_addresses[0] - else: - address = 'BM-2cWiUsWMo4Po2UyNB3VyQdF3gxGrDX9gNm' - - self.assertTrue(address not in own_addresses)