Covered more API commands by tests, listed uncovered in TODO

This commit is contained in:
Dmitri Bogomolov 2019-11-11 17:31:16 +02:00
parent ef5593b3d5
commit 85f306e5f6
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -41,6 +41,19 @@ class TestAPIShutdown(TestAPIProto, TestProcessShutdown):
'%s has not stopped in 10 sec' % ' '.join(self._process_cmd)) '%s has not stopped in 10 sec' % ' '.join(self._process_cmd))
# TODO: uncovered API commands
# getAllInboxMessages
# getAllInboxMessageIds
# getInboxMessageById
# getInboxMessagesByReceiver
# trashMessage
# trashInboxMessage
# addSubscription
# disseminatePreEncryptedMsg
# disseminatePubkey
# getMessageDataByDestinationHash
# statusBar
class TestAPI(TestAPIProto): class TestAPI(TestAPIProto):
"""Main API test case""" """Main API test case"""
_seed = base64.encodestring( _seed = base64.encodestring(
@ -98,24 +111,45 @@ class TestAPI(TestAPIProto):
self.assertEqual(result['streamNumber'], 1) self.assertEqual(result['streamNumber'], 1)
def test_create_deterministic_addresses(self): def test_create_deterministic_addresses(self):
"""API command 'getDeterministicAddress': with various params""" """Test creation of deterministic addresses"""
self.assertEqual( self.assertEqual(
self.api.getDeterministicAddress(self._seed, 4, 1), self.api.getDeterministicAddress(self._seed, 4, 1),
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK')
)
self.assertEqual( self.assertEqual(
self.api.getDeterministicAddress(self._seed, 3, 1), self.api.getDeterministicAddress(self._seed, 3, 1),
'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN' 'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN')
)
self.assertRegexpMatches( self.assertRegexpMatches(
self.api.getDeterministicAddress(self._seed, 2, 1), self.api.getDeterministicAddress(self._seed, 2, 1),
r'^API Error 0002:' r'^API Error 0002:')
)
# This is here until the streams will be implemented # This is here until the streams will be implemented
self.assertRegexpMatches( self.assertRegexpMatches(
self.api.getDeterministicAddress(self._seed, 3, 2), self.api.getDeterministicAddress(self._seed, 3, 2),
r'API Error 0003:' r'API Error 0003:')
) self.assertRegexpMatches(
self.api.createDeterministicAddresses(self._seed, 1, 4, 2),
r'API Error 0003:')
self.assertRegexpMatches(
self.api.createDeterministicAddresses('', 1),
r'API Error 0001:')
self.assertRegexpMatches(
self.api.createDeterministicAddresses(self._seed, 1, 2),
r'API Error 0002:')
self.assertRegexpMatches(
self.api.createDeterministicAddresses(self._seed, 0),
r'API Error 0004:')
self.assertRegexpMatches(
self.api.createDeterministicAddresses(self._seed, 1000),
r'API Error 0005:')
addresses = json.loads(
self.api.createDeterministicAddresses(self._seed, 2, 4)
)['addresses']
self.assertEqual(len(addresses), 2)
self.assertEqual(addresses[0], 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK')
for addr in addresses:
self.assertEqual(self.api.deleteAddress(addr), 'success')
def test_create_random_address(self): def test_create_random_address(self):
"""API command 'createRandomAddress': basic BM-address validation""" """API command 'createRandomAddress': basic BM-address validation"""
@ -154,10 +188,90 @@ class TestAPI(TestAPIProto):
[] []
) )
def test_send_broadcast(self): def test_subscriptions(self):
"""API command 'sendBroadcast': ensure it returns ackData""" """Testing the API commands related to subscriptions"""
for s in json.loads(self.api.listSubscriptions())['subscriptions']:
# special address, added when sqlThread starts
if s['address'] == 'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw':
self.assertEqual(
base64.decodestring(s['label']),
'Bitmessage new releases/announcements')
self.assertTrue(s['enabled'])
break
else:
self.fail(
'Could not find Bitmessage new releases/announcements'
' in subscriptions')
self.assertEqual(
self.api.deleteSubscription('BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw'),
'Deleted subscription if it existed.')
self.assertEqual(
json.loads(self.api.listSubscriptions())['subscriptions'], [])
def test_send(self):
"""Test message sending"""
# self.api.createDeterministicAddresses(self._seed, 1, 4)
addr = self._add_random_address('random_2') addr = self._add_random_address('random_2')
msg = base64.encodestring('test message') msg = base64.encodestring('test message')
msg_subject = base64.encodestring('test_subject')
ackdata = self.api.sendMessage(
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK', addr, msg_subject, msg)
try:
# Check ackdata and message status
int(ackdata, 16)
status = self.api.getStatus(ackdata)
if status == 'notfound':
raise KeyError
self.assertIn(
status, (
'msgqueued', 'awaitingpubkey', 'msgsent', 'ackreceived',
'doingpubkeypow', 'doingmsgpow', 'msgsentnoackexpected'
))
# Find the message in sent
for m in json.loads(
self.api.getSentMessagesByAddress(addr))['sentMessages']:
if m['ackData'] == ackdata:
sent_msg = m['message']
break
else:
raise KeyError
# Find the message in inbox
# for m in json.loads(
# self.api.getInboxMessagesByReceiver(
# 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK'))['inboxMessages']:
# if m['subject'] == msg_subject:
# inbox_msg = m['message']
# break
except ValueError:
self.fail('sendMessage returned error or ackData is not hex')
except KeyError:
self.fail('Could not find sent message in sent messages')
else:
# Check found message
try:
self.assertEqual(sent_msg, msg.strip())
except UnboundLocalError:
self.fail('Could not find sent message in sent messages')
# self.assertEqual(inbox_msg, msg.strip())
self.assertEqual(json.loads(
self.api.getSentMessageByAckData(ackdata)
)['sentMessage'][0]['message'], sent_msg)
# Trash the message
self.assertEqual(
self.api.trashSentMessageByAckData(ackdata),
'Trashed sent message (assuming message existed).')
# Empty trash
self.assertEqual(self.api.deleteAndVacuum(), 'done')
# The message should disappear
self.assertIsNone(json.loads(
self.api.getSentMessageByAckData(ackdata)))
finally:
self.assertEqual(self.api.deleteAddress(addr), 'success')
def test_send_broadcast(self):
"""Test broadcast sending"""
addr = self._add_random_address('random_2')
msg = base64.encodestring('test broadcast')
ackdata = self.api.sendBroadcast( ackdata = self.api.sendBroadcast(
addr, base64.encodestring('test_subject'), msg) addr, base64.encodestring('test_subject'), msg)
try: try:
@ -167,9 +281,11 @@ class TestAPI(TestAPIProto):
raise KeyError raise KeyError
self.assertIn( self.assertIn(
status, ('broadcastqueued', 'broadcastsent', 'doingmsgpow')) status, ('broadcastqueued', 'broadcastsent', 'doingmsgpow'))
# Find the message and its ID in sent
for m in json.loads(self.api.getAllSentMessages())['sentMessages']: for m in json.loads(self.api.getAllSentMessages())['sentMessages']:
if m['ackData'] == ackdata: if m['ackData'] == ackdata:
sent_msg = m['message'] sent_msg = m['message']
sent_msgid = m['msgid']
break break
else: else:
raise KeyError raise KeyError
@ -178,13 +294,30 @@ class TestAPI(TestAPIProto):
except KeyError: except KeyError:
self.fail('Could not find sent broadcast in sent messages') self.fail('Could not find sent broadcast in sent messages')
else: else:
self.assertEqual(sent_msg, msg.strip()) # Check found message and its ID
try:
self.assertEqual(sent_msg, msg.strip())
except UnboundLocalError:
self.fail('Could not find sent message in sent messages')
self.assertEqual(json.loads(
self.api.getSentMessageById(sent_msgid)
)['sentMessage'][0]['message'], sent_msg)
self.assertIn(
{'msgid': sent_msgid}, json.loads(
self.api.getAllSentMessageIds())['sentMessageIds'])
# Trash the message by ID
self.assertEqual(
self.api.trashSentMessage(sent_msgid),
'Trashed sent message (assuming message existed).')
self.assertEqual(self.api.deleteAndVacuum(), 'done')
self.assertIsNone(json.loads(
self.api.getSentMessageById(sent_msgid)))
finally: finally:
self.assertEqual(self.api.deleteAddress(addr), 'success') self.assertEqual(self.api.deleteAddress(addr), 'success')
def test_chan(self): def test_chan(self):
"""Testing chan creation/joining""" """Testing chan creation/joining"""
# Cheate chan with known address # Create chan with known address
self.assertEqual( self.assertEqual(
self.api.createChan(self._seed), self.api.createChan(self._seed),
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK' 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK'
@ -196,8 +329,8 @@ class TestAPI(TestAPIProto):
) )
# Join chan with addresses of version 3 or 4 # Join chan with addresses of version 3 or 4
for addr in ( for addr in (
'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK', 'BM-2cWzSnwjJ7yRP3nLEWUV5LisTZyREWSzUK',
'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN' 'BM-2DBPTgeSawWYZceFD69AbDT5q4iUWtj1ZN'
): ):
self.assertEqual(self.api.joinChan(self._seed, addr), 'success') self.assertEqual(self.api.joinChan(self._seed, addr), 'success')
self.assertEqual(self.api.leaveChan(addr), 'success') self.assertEqual(self.api.leaveChan(addr), 'success')