From 9a194f0bae1cb2fc520ba0e358774e02a97baa48 Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Fri, 15 Oct 2021 19:30:19 +0530 Subject: [PATCH 1/7] Added statusBar, getAllInboxMessages, getAllInboxMessageIds, getInboxMessageById, getInboxMessagesByReceiver, trashMessage, trashInboxMessage & addSubscription, methods testcases & updated related functions --- src/api.py | 18 ++++++++- src/bitmessagemain.py | 5 +++ src/testmode_init.py | 40 ++++++++++++++++++++ src/tests/samples.py | 7 ++++ src/tests/test_api.py | 86 +++++++++++++++++++++++++++++++++++++------ 5 files changed, 144 insertions(+), 12 deletions(-) create mode 100644 src/testmode_init.py diff --git a/src/api.py b/src/api.py index 0ce12bb32b..0dfa3e7d50 100644 --- a/src/api.py +++ b/src/api.py @@ -89,9 +89,10 @@ ) from bmconfigparser import BMConfigParser from debug import logger -from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure +from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure, sql_ready from inventory import Inventory from network.threads import StoppableThread +from six.moves import queue from version import softwareVersion try: # TODO: write tests for XML vulnerabilities @@ -230,6 +231,7 @@ class StoppableRPCServer(RPCServerBase): def serve_forever(self, poll_interval=None): """Start the RPCServer""" + sql_ready.wait() while state.shutdown == 0: self.handle_request() @@ -1422,11 +1424,25 @@ def HandleAdd(self, a, b): """Test two numeric params""" return a + b + @command('clearUISignalQueue') + def HandleclearUISignalQueue(self): + """clear UISignalQueue""" + queues.UISignalQueue.queue.clear() + @command('statusBar') def HandleStatusBar(self, message): """Update GUI statusbar message""" queues.UISignalQueue.put(('updateStatusBar', message)) + @command('getStatusBar') + def HandleGetStatusBar(self): + """Get GUI statusbar message""" + try: + _, data = queues.UISignalQueue.get(block=False) + except queue.Empty: + return None + return data + @command('deleteAndVacuum') def HandleDeleteAndVacuum(self): """Cleanup trashes and vacuum messages database""" diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index dc7426ac93..84313ab973 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -33,6 +33,8 @@ import shared import shutdown import state + +from testmode_init import populate_api_test_data from bmconfigparser import BMConfigParser from debug import logger # this should go before any threads from helper_startup import ( @@ -295,6 +297,9 @@ def start(self): else: config.remove_option('bitmessagesettings', 'dontconnect') + if state.testmode: + populate_api_test_data() + if daemon: while state.shutdown == 0: time.sleep(1) diff --git a/src/testmode_init.py b/src/testmode_init.py new file mode 100644 index 0000000000..4be3626c0a --- /dev/null +++ b/src/testmode_init.py @@ -0,0 +1,40 @@ +import time +import uuid + +import helper_inbox +import helper_sql + +# from .tests.samples import sample_inbox_msg_ids, sample_inbox_msg_receiver_address +sample_inbox_msg_receiver_address = 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' +sample_inbox_msg_ids = ['27e644765a3e4b2e973ee7ccf958ea20', '51fc5531-3989-4d69-bbb5-68d64b756f5b', + '2c975c515f8b414db5eea60ba57ba455', 'bc1f2d8a-681c-4cc0-9a12-6067c7e1ac24'] + + +def populate_api_test_data(): + '''Adding test records in inbox table''' + helper_sql.sql_ready.wait() + + test1 = ( + sample_inbox_msg_ids[0], sample_inbox_msg_receiver_address, + sample_inbox_msg_receiver_address, 'Test1 subject', int(time.time()), + 'Test1 body', 'inbox', 2, 0, uuid.uuid4().bytes + ) + test2 = ( + sample_inbox_msg_ids[1], sample_inbox_msg_receiver_address, + sample_inbox_msg_receiver_address, 'Test2 subject', int(time.time()), + 'Test2 body', 'inbox', 2, 0, uuid.uuid4().bytes + ) + test3 = ( + sample_inbox_msg_ids[2], sample_inbox_msg_receiver_address, + sample_inbox_msg_receiver_address, 'Test3 subject', int(time.time()), + 'Test3 body', 'inbox', 2, 0, uuid.uuid4().bytes + ) + test4 = ( + sample_inbox_msg_ids[3], sample_inbox_msg_receiver_address, + sample_inbox_msg_receiver_address, 'Test4 subject', int(time.time()), + 'Test4 body', 'inbox', 2, 0, uuid.uuid4().bytes + ) + helper_inbox.insert(test1) + helper_inbox.insert(test2) + helper_inbox.insert(test3) + helper_inbox.insert(test4) diff --git a/src/tests/samples.py b/src/tests/samples.py index 17ec03bd47..94bef71cf0 100644 --- a/src/tests/samples.py +++ b/src/tests/samples.py @@ -35,3 +35,10 @@ sample_deterministic_addr4 = 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' sample_daddr3_512 = 18875720106589866286514488037355423395410802084648916523381 sample_daddr4_512 = 25152821841976547050350277460563089811513157529113201589004 + +sample_statusbar_msg = "new status bar message" +sample_inbox_msg_receiver_address = 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' +sample_inbox_msg_ids = ['27e644765a3e4b2e973ee7ccf958ea20', '51fc5531-3989-4d69-bbb5-68d64b756f5b', + '2c975c515f8b414db5eea60ba57ba455', 'bc1f2d8a-681c-4cc0-9a12-6067c7e1ac24'] +sample_test_subscription_address = ['BM-2cWQLCBGorT9pUGkYSuGGVr9LzE4mRnQaq', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw'] +sample_subscription_name = 'test sub' diff --git a/src/tests/test_api.py b/src/tests/test_api.py index 6e71c90bfc..71ef91575a 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -6,12 +6,15 @@ import json import time +from binascii import hexlify from six.moves import xmlrpc_client # nosec import psutil from .samples import ( - sample_seed, sample_deterministic_addr3, sample_deterministic_addr4) + sample_seed, sample_deterministic_addr3, sample_deterministic_addr4, sample_statusbar_msg, + sample_inbox_msg_receiver_address, sample_inbox_msg_ids, sample_test_subscription_address, + sample_subscription_name) from .test_process import TestProcessProto @@ -46,17 +49,9 @@ def test_shutdown(self): # TODO: uncovered API commands -# getAllInboxMessages -# getAllInboxMessageIds -# getInboxMessageById -# getInboxMessagesByReceiver -# trashMessage -# trashInboxMessage -# addSubscription # disseminatePreEncryptedMsg # disseminatePubkey # getMessageDataByDestinationHash -# statusBar class TestAPI(TestAPIProto): @@ -91,6 +86,53 @@ def test_invalid_method(self): 'API Error 0020: Invalid method: test' ) + def test_statusbar_method(self): + """Test statusbar method""" + self.api.clearUISignalQueue() + self.assertEqual( + self.api.statusBar(sample_statusbar_msg), + 'null' + ) + self.assertEqual( + self.api.getStatusBar(), + sample_statusbar_msg + ) + + def test_message_inbox(self): + """Test message inbox methods""" + self.assertTrue( + len(json.loads( + self.api.getAllInboxMessages())["inboxMessages"]) == 2 + ) + self.assertTrue( + len(json.loads( + self.api.getAllInboxMessageIds())["inboxMessageIds"]) == 2 + ) + self.assertTrue( + len(json.loads( + self.api.getInboxMessageById(hexlify(sample_inbox_msg_ids[2])))["inboxMessage"]) == 1 + ) + self.assertTrue( + len(json.loads( + self.api.getInboxMessagesByReceiver(sample_inbox_msg_receiver_address))["inboxMessages"]) == 2 + ) + + def test_delete_message(self): + """Test delete message methods""" + messages_before_delete = len(json.loads(self.api.getAllInboxMessageIds())["inboxMessageIds"]) + self.assertEqual( + self.api.trashMessage(hexlify(sample_inbox_msg_ids[0])), + 'Trashed message (assuming message existed).' + ) + self.assertEqual( + self.api.trashInboxMessage(hexlify(sample_inbox_msg_ids[1])), + 'Trashed inbox message (assuming message existed).' + ) + self.assertEqual( + len(json.loads(self.api.getAllInboxMessageIds())["inboxMessageIds"]), + messages_before_delete - 2 + ) + def test_clientstatus_consistency(self): """If networkStatus is notConnected networkConnections should be 0""" status = json.loads(self.api.clientStatus()) @@ -193,9 +235,28 @@ def test_addressbook(self): def test_subscriptions(self): """Testing the API commands related to subscriptions""" + + self.assertEqual( + self.api.addSubscription(sample_test_subscription_address[0], sample_subscription_name.encode('base64')), + 'Added subscription.' + ) + + added_subscription = {'label': None, 'enabled': False} + # check_address + for sub in json.loads(self.api.listSubscriptions())['subscriptions']: + # special address, added when sqlThread starts + if sub['address'] == sample_test_subscription_address[0]: + added_subscription = sub + break + + self.assertEqual( + base64.decodestring(added_subscription['label']) if added_subscription['label'] else None, + sample_subscription_name) + self.assertTrue(added_subscription['enabled']) + for s in json.loads(self.api.listSubscriptions())['subscriptions']: # special address, added when sqlThread starts - if s['address'] == 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw': + if s['address'] == sample_test_subscription_address[1]: self.assertEqual( base64.decodestring(s['label']), 'Bitmessage new releases/announcements') @@ -206,7 +267,10 @@ def test_subscriptions(self): 'Could not find Bitmessage new releases/announcements' ' in subscriptions') self.assertEqual( - self.api.deleteSubscription('BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw'), + self.api.deleteSubscription(sample_test_subscription_address[0]), + 'Deleted subscription if it existed.') + self.assertEqual( + self.api.deleteSubscription(sample_test_subscription_address[1]), 'Deleted subscription if it existed.') self.assertEqual( json.loads(self.api.listSubscriptions())['subscriptions'], []) From 21cb4e516da2db08aa4dfc08d9ac86ccd443cbea Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Thu, 18 Nov 2021 21:24:05 +0530 Subject: [PATCH 2/7] Added testmode check for HandleclearUISignalQueue & HandleGetStatusBar functions, replaced sample_inbox_msg_receiver_address by sample_deterministic_addr4 --- src/api.py | 4 ++++ src/testmode_init.py | 20 ++++++++++---------- src/tests/samples.py | 1 - src/tests/test_api.py | 5 ++--- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/api.py b/src/api.py index 0dfa3e7d50..c5e390550c 100644 --- a/src/api.py +++ b/src/api.py @@ -310,6 +310,9 @@ def __init__(self, *aliases): self.aliases = aliases def __call__(self, func): + if func.__name__ in ["HandleclearUISignalQueue", "HandleGetStatusBar"] \ + and not state.testmode: + return None if BMConfigParser().safeGet( 'bitmessagesettings', 'apivariant') == 'legacy': def wrapper(*args): @@ -1428,6 +1431,7 @@ def HandleAdd(self, a, b): def HandleclearUISignalQueue(self): """clear UISignalQueue""" queues.UISignalQueue.queue.clear() + return "success" @command('statusBar') def HandleStatusBar(self, message): diff --git a/src/testmode_init.py b/src/testmode_init.py index 4be3626c0a..a088afc197 100644 --- a/src/testmode_init.py +++ b/src/testmode_init.py @@ -4,8 +4,8 @@ import helper_inbox import helper_sql -# from .tests.samples import sample_inbox_msg_ids, sample_inbox_msg_receiver_address -sample_inbox_msg_receiver_address = 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' +# from .tests.samples import sample_inbox_msg_ids, sample_deterministic_addr4 +sample_deterministic_addr4 = 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' sample_inbox_msg_ids = ['27e644765a3e4b2e973ee7ccf958ea20', '51fc5531-3989-4d69-bbb5-68d64b756f5b', '2c975c515f8b414db5eea60ba57ba455', 'bc1f2d8a-681c-4cc0-9a12-6067c7e1ac24'] @@ -15,23 +15,23 @@ def populate_api_test_data(): helper_sql.sql_ready.wait() test1 = ( - sample_inbox_msg_ids[0], sample_inbox_msg_receiver_address, - sample_inbox_msg_receiver_address, 'Test1 subject', int(time.time()), + sample_inbox_msg_ids[0], sample_deterministic_addr4, + sample_deterministic_addr4, 'Test1 subject', int(time.time()), 'Test1 body', 'inbox', 2, 0, uuid.uuid4().bytes ) test2 = ( - sample_inbox_msg_ids[1], sample_inbox_msg_receiver_address, - sample_inbox_msg_receiver_address, 'Test2 subject', int(time.time()), + sample_inbox_msg_ids[1], sample_deterministic_addr4, + sample_deterministic_addr4, 'Test2 subject', int(time.time()), 'Test2 body', 'inbox', 2, 0, uuid.uuid4().bytes ) test3 = ( - sample_inbox_msg_ids[2], sample_inbox_msg_receiver_address, - sample_inbox_msg_receiver_address, 'Test3 subject', int(time.time()), + sample_inbox_msg_ids[2], sample_deterministic_addr4, + sample_deterministic_addr4, 'Test3 subject', int(time.time()), 'Test3 body', 'inbox', 2, 0, uuid.uuid4().bytes ) test4 = ( - sample_inbox_msg_ids[3], sample_inbox_msg_receiver_address, - sample_inbox_msg_receiver_address, 'Test4 subject', int(time.time()), + sample_inbox_msg_ids[3], sample_deterministic_addr4, + sample_deterministic_addr4, 'Test4 subject', int(time.time()), 'Test4 body', 'inbox', 2, 0, uuid.uuid4().bytes ) helper_inbox.insert(test1) diff --git a/src/tests/samples.py b/src/tests/samples.py index 94bef71cf0..8c36dd8910 100644 --- a/src/tests/samples.py +++ b/src/tests/samples.py @@ -37,7 +37,6 @@ sample_daddr4_512 = 25152821841976547050350277460563089811513157529113201589004 sample_statusbar_msg = "new status bar message" -sample_inbox_msg_receiver_address = 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' sample_inbox_msg_ids = ['27e644765a3e4b2e973ee7ccf958ea20', '51fc5531-3989-4d69-bbb5-68d64b756f5b', '2c975c515f8b414db5eea60ba57ba455', 'bc1f2d8a-681c-4cc0-9a12-6067c7e1ac24'] sample_test_subscription_address = ['BM-2cWQLCBGorT9pUGkYSuGGVr9LzE4mRnQaq', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw'] diff --git a/src/tests/test_api.py b/src/tests/test_api.py index 71ef91575a..167ff2986f 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -13,8 +13,7 @@ from .samples import ( sample_seed, sample_deterministic_addr3, sample_deterministic_addr4, sample_statusbar_msg, - sample_inbox_msg_receiver_address, sample_inbox_msg_ids, sample_test_subscription_address, - sample_subscription_name) + sample_inbox_msg_ids, sample_test_subscription_address, sample_subscription_name) from .test_process import TestProcessProto @@ -114,7 +113,7 @@ def test_message_inbox(self): ) self.assertTrue( len(json.loads( - self.api.getInboxMessagesByReceiver(sample_inbox_msg_receiver_address))["inboxMessages"]) == 2 + self.api.getInboxMessagesByReceiver(sample_deterministic_addr4))["inboxMessages"]) == 2 ) def test_delete_message(self): From 56ec5b8a03209ca6b39e64c5340873f5d5fb5eed Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Thu, 25 Nov 2021 21:43:22 +0530 Subject: [PATCH 3/7] Created testmode decorator & routed command decorator through it --- src/api.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/api.py b/src/api.py index c5e390550c..ad5a142ae9 100644 --- a/src/api.py +++ b/src/api.py @@ -304,15 +304,27 @@ def __new__(mcs, name, bases, namespace): return result +class testmode(object): # pylint: disable=too-few-public-methods + """Decorator to check testmode & route to command decorator""" + + def __init__(self, *aliases): + self.aliases = aliases + + def __call__(self, func): + """Testmode call method""" + + if not state.testmode: + return None + return command(self.aliases[0]).__call__(func) + + class command(object): # pylint: disable=too-few-public-methods """Decorator for API command method""" def __init__(self, *aliases): self.aliases = aliases def __call__(self, func): - if func.__name__ in ["HandleclearUISignalQueue", "HandleGetStatusBar"] \ - and not state.testmode: - return None + if BMConfigParser().safeGet( 'bitmessagesettings', 'apivariant') == 'legacy': def wrapper(*args): @@ -1427,7 +1439,7 @@ def HandleAdd(self, a, b): """Test two numeric params""" return a + b - @command('clearUISignalQueue') + @testmode('clearUISignalQueue') def HandleclearUISignalQueue(self): """clear UISignalQueue""" queues.UISignalQueue.queue.clear() @@ -1438,7 +1450,7 @@ def HandleStatusBar(self, message): """Update GUI statusbar message""" queues.UISignalQueue.put(('updateStatusBar', message)) - @command('getStatusBar') + @testmode('getStatusBar') def HandleGetStatusBar(self): """Get GUI statusbar message""" try: From 62ffe96bc8d8ad8931cd356f126776fc7c1dc7f5 Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Fri, 26 Nov 2021 20:08:51 +0530 Subject: [PATCH 4/7] Added anouncement addr comment, replaces assertTrue with assertEqual & added print for getAllInboxMessages to check build failure --- src/tests/samples.py | 1 + src/tests/test_api.py | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/tests/samples.py b/src/tests/samples.py index 8c36dd8910..e1a3e676ff 100644 --- a/src/tests/samples.py +++ b/src/tests/samples.py @@ -39,5 +39,6 @@ sample_statusbar_msg = "new status bar message" sample_inbox_msg_ids = ['27e644765a3e4b2e973ee7ccf958ea20', '51fc5531-3989-4d69-bbb5-68d64b756f5b', '2c975c515f8b414db5eea60ba57ba455', 'bc1f2d8a-681c-4cc0-9a12-6067c7e1ac24'] +# second address in sample_test_subscription_address is for the announcement broadcast sample_test_subscription_address = ['BM-2cWQLCBGorT9pUGkYSuGGVr9LzE4mRnQaq', 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw'] sample_subscription_name = 'test sub' diff --git a/src/tests/test_api.py b/src/tests/test_api.py index 167ff2986f..75a4290be2 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -99,21 +99,27 @@ def test_statusbar_method(self): def test_message_inbox(self): """Test message inbox methods""" - self.assertTrue( + print("getAllInboxMessages: ", json.loads( + self.api.getAllInboxMessages())["inboxMessages"]) + self.assertEqual( len(json.loads( - self.api.getAllInboxMessages())["inboxMessages"]) == 2 + self.api.getAllInboxMessages())["inboxMessages"]), + 2 ) - self.assertTrue( + self.assertEqual( len(json.loads( - self.api.getAllInboxMessageIds())["inboxMessageIds"]) == 2 + self.api.getAllInboxMessageIds())["inboxMessageIds"]), + 2 ) - self.assertTrue( + self.assertEqual( len(json.loads( - self.api.getInboxMessageById(hexlify(sample_inbox_msg_ids[2])))["inboxMessage"]) == 1 + self.api.getInboxMessageById(hexlify(sample_inbox_msg_ids[2])))["inboxMessage"]), + 1 ) - self.assertTrue( + self.assertEqual( len(json.loads( - self.api.getInboxMessagesByReceiver(sample_deterministic_addr4))["inboxMessages"]) == 2 + self.api.getInboxMessagesByReceiver(sample_deterministic_addr4))["inboxMessages"]), + 2 ) def test_delete_message(self): From 21d5a6ce8f29a75a585d1c9d50b7fbe760d3317f Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Fri, 26 Nov 2021 20:23:55 +0530 Subject: [PATCH 5/7] Instead of print added getAllInboxMessages values in assertEqual message parameter --- src/tests/test_api.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/tests/test_api.py b/src/tests/test_api.py index 75a4290be2..6648aabdd4 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -99,12 +99,11 @@ def test_statusbar_method(self): def test_message_inbox(self): """Test message inbox methods""" - print("getAllInboxMessages: ", json.loads( - self.api.getAllInboxMessages())["inboxMessages"]) self.assertEqual( len(json.loads( self.api.getAllInboxMessages())["inboxMessages"]), - 2 + 2, + json.loads(self.api.getAllInboxMessages())["inboxMessages"] ) self.assertEqual( len(json.loads( From 16175278a87b85de71c5e1f8d7fb8cbb3269a453 Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Mon, 29 Nov 2021 22:51:02 +0530 Subject: [PATCH 6/7] Merged inbox & trash functions related testcases into single testcase method --- src/tests/test_api.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tests/test_api.py b/src/tests/test_api.py index 6648aabdd4..f0d52c5c10 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -97,18 +97,19 @@ def test_statusbar_method(self): sample_statusbar_msg ) - def test_message_inbox(self): + def test_message_inbox_and_trash(self): """Test message inbox methods""" + # Inbox function related tests self.assertEqual( len(json.loads( self.api.getAllInboxMessages())["inboxMessages"]), - 2, + 4, json.loads(self.api.getAllInboxMessages())["inboxMessages"] ) self.assertEqual( len(json.loads( self.api.getAllInboxMessageIds())["inboxMessageIds"]), - 2 + 4 ) self.assertEqual( len(json.loads( @@ -118,11 +119,10 @@ def test_message_inbox(self): self.assertEqual( len(json.loads( self.api.getInboxMessagesByReceiver(sample_deterministic_addr4))["inboxMessages"]), - 2 + 4 ) - def test_delete_message(self): - """Test delete message methods""" + # Trash function related tests messages_before_delete = len(json.loads(self.api.getAllInboxMessageIds())["inboxMessageIds"]) self.assertEqual( self.api.trashMessage(hexlify(sample_inbox_msg_ids[0])), From 5771f21cfbbf268a56773885bd48347620066c48 Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Tue, 30 Nov 2021 19:46:18 +0530 Subject: [PATCH 7/7] Added undeleteMessage function in helper_inbox & respective method in api.py and separated inbox & trash testcases --- src/api.py | 7 +++++++ src/helper_inbox.py | 5 +++++ src/tests/test_api.py | 19 ++++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/api.py b/src/api.py index ad5a142ae9..dd883d9acf 100644 --- a/src/api.py +++ b/src/api.py @@ -1459,6 +1459,13 @@ def HandleGetStatusBar(self): return None return data + @testmode('undeleteMessage') + def HandleUndeleteMessage(self, msgid): + """Undelete message""" + msgid = self._decode(msgid, "hex") + helper_inbox.undeleteMessage(msgid) + return "Undeleted message" + @command('deleteAndVacuum') def HandleDeleteAndVacuum(self): """Cleanup trashes and vacuum messages database""" diff --git a/src/helper_inbox.py b/src/helper_inbox.py index 654dd59d46..d99e954498 100644 --- a/src/helper_inbox.py +++ b/src/helper_inbox.py @@ -18,6 +18,11 @@ def trash(msgid): queues.UISignalQueue.put(('removeInboxRowByMsgid', msgid)) +def undeleteMessage(msgid): + """Undelte the message""" + sqlExecute('''UPDATE inbox SET folder='inbox' WHERE msgid=?''', msgid) + + def isMessageAlreadyInInbox(sigHash): """Check for previous instances of this message""" queryReturn = sqlQuery( diff --git a/src/tests/test_api.py b/src/tests/test_api.py index f0d52c5c10..ba19712707 100644 --- a/src/tests/test_api.py +++ b/src/tests/test_api.py @@ -97,9 +97,8 @@ def test_statusbar_method(self): sample_statusbar_msg ) - def test_message_inbox_and_trash(self): + def test_message_inbox(self): """Test message inbox methods""" - # Inbox function related tests self.assertEqual( len(json.loads( self.api.getAllInboxMessages())["inboxMessages"]), @@ -122,7 +121,9 @@ def test_message_inbox_and_trash(self): 4 ) - # Trash function related tests + def test_message_trash(self): + """Test message inbox methods""" + messages_before_delete = len(json.loads(self.api.getAllInboxMessageIds())["inboxMessageIds"]) self.assertEqual( self.api.trashMessage(hexlify(sample_inbox_msg_ids[0])), @@ -136,6 +137,18 @@ def test_message_inbox_and_trash(self): len(json.loads(self.api.getAllInboxMessageIds())["inboxMessageIds"]), messages_before_delete - 2 ) + self.assertEqual( + self.api.undeleteMessage(hexlify(sample_inbox_msg_ids[0])), + 'Undeleted message' + ) + self.assertEqual( + self.api.undeleteMessage(hexlify(sample_inbox_msg_ids[1])), + 'Undeleted message' + ) + self.assertEqual( + len(json.loads(self.api.getAllInboxMessageIds())["inboxMessageIds"]), + messages_before_delete + ) def test_clientstatus_consistency(self): """If networkStatus is notConnected networkConnections should be 0"""