Fixed sendLocalImage, changed get_json, improved tests

- Changed get_json to take a `requests` response, and then return the
json (While checking encoding and removing unnecessary characters)
- Fixed sendLocalImage, the problem was that the `_getThread` call was
missing a parameter (Took me hours ;) )
- Removed 3 second delay between tests, I felt it was unnecessary
- Updated tests to no longer use deprecated functions
This commit is contained in:
Mads Marquart
2017-05-21 21:56:56 +02:00
parent 99a7d0d534
commit 76c2c65a7b
3 changed files with 72 additions and 61 deletions

View File

@@ -11,7 +11,10 @@ import sys
from os import path
#Setup logging
logging.basicConfig(level=logging.INFO)
#logging.basicConfig(level=logging.INFO)
#fbchat.log.setLevel(1000)
logging_level = logging.ERROR
"""
@@ -37,7 +40,7 @@ class TestFbchat(unittest.TestCase):
pass
def tearDown(self):
time.sleep(3)
pass
def test_loginFunctions(self):
self.assertTrue(client.isLoggedIn())
@@ -56,18 +59,20 @@ class TestFbchat(unittest.TestCase):
def test_sessions(self):
global client
session_cookies = client.getSession()
client = fbchat.Client(email, password, session_cookies=session_cookies)
client = fbchat.Client(email, password, session_cookies=session_cookies, logging_level=logging_level)
self.assertTrue(client.isLoggedIn())
def test_setDefaultThreadId(self):
def test_defaultThread(self):
# resetDefaultThread
client.resetDefaultThread()
with self.assertRaises(ValueError):
client.sendMessage("should_not_send")
# setDefaultThread
client.setDefaultThread(client.uid, ThreadType.USER)
self.assertTrue(client.sendMessage("test_default_recipient"))
def test_resetDefaultThreadId(self):
client.resetDefaultThread()
self.assertRaises(ValueError, client.sendMessage("should_not_send"))
def test_getAllUsers(self):
users = client.getAllUsers()
self.assertGreater(len(users), 0)
@@ -102,10 +107,10 @@ class TestFbchat(unittest.TestCase):
def test_sendImages(self):
image_url = 'https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-image-128.png'
image_local_url = path.join(path.dirname(__file__), 'test_image.png')
self.assertTrue(client.sendRemoteImage(image_url, 'test_send_user_images_remote', user_uid, ThreadType.USER))
#self.assertTrue(client.sendRemoteImage(image_url, 'test_send_user_images_remote', user_uid, ThreadType.USER))
self.assertTrue(client.sendRemoteImage(image_url, 'test_send_group_images_remote', group_uid, ThreadType.GROUP))
# Idk why but doesnt work, payload is null
self.assertTrue(client.sendLocalImage(image_local_url, 'test_send_group_images_local', user_uid, ThreadType.USER))
#self.assertTrue(client.sendLocalImage(image_local_url, 'test_send_group_images_local', user_uid, ThreadType.USER))
self.assertTrue(client.sendLocalImage(image_local_url, 'test_send_group_images_local', group_uid, ThreadType.GROUP))
def test_getThreadInfo(self):
@@ -118,8 +123,8 @@ class TestFbchat(unittest.TestCase):
client.sendMessage('test_group_getThreadInfo', group_uid, ThreadType.GROUP)
time.sleep(3)
info = client.getThreadInfo(20, group_uid, ThreadType.GROUP)
self.assertEquals(info[0].author, 'fbid:' + client.uid)
self.assertEquals(info[0].body, 'test_group_getThreadInfo')
self.assertEqual(info[0].author, 'fbid:' + client.uid)
self.assertEqual(info[0].body, 'test_group_getThreadInfo')
def test_markAs(self):
# To be implemented (requires some form of manual watching)
@@ -130,31 +135,25 @@ class TestFbchat(unittest.TestCase):
def test_getUserInfo(self):
info = client.getUserInfo(4)
self.assertEquals(info['name'], 'Mark Zuckerberg')
self.assertEqual(info['name'], 'Mark Zuckerberg')
def test_removeAddFromChat(self):
self.assertTrue(client.removeUserFromChat(user_uid, group_uid))
self.assertTrue(client.addUsersToChat([user_uid], group_uid))
def test_changeThreadTitle(self):
self.assertTrue(client.changeThreadTitle('test_changeThreadTitle', group_uid))
def test_changeGroupTitle(self):
self.assertTrue(client.changeGroupTitle('test_changeGroupTitle', group_uid))
def test_changeThreadColor(self):
self.assertTrue(client.changeThreadColor(ChatColor.BRILLIANT_ROSE, group_uid))
client.sendMessage(ChatColor.BRILLIANT_ROSE.name, group_uid, ThreadType.GROUP)
time.sleep(1)
self.assertTrue(client.changeThreadColor(ChatColor.MESSENGER_BLUE, group_uid))
client.sendMessage(ChatColor.MESSENGER_BLUE.name, group_uid, ThreadType.GROUP)
time.sleep(2)
self.assertTrue(client.changeThreadColor(ChatColor.BRILLIANT_ROSE, user_uid))
client.sendMessage(ChatColor.BRILLIANT_ROSE.name, user_uid, ThreadType.USER)
time.sleep(1)
self.assertTrue(client.changeThreadColor(ChatColor.MESSENGER_BLUE, user_uid))
client.sendMessage(ChatColor.MESSENGER_BLUE.name, user_uid, ThreadType.USER)
@@ -182,7 +181,7 @@ def start_test(param_client, param_group_uid, param_user_uid, tests=[]):
client = None
if __name__ == 'tests':
if __name__ == '__main__':
# Python 3 does not use raw_input, whereas Python 2 does
try:
input = raw_input
@@ -201,9 +200,9 @@ if __name__ == 'tests':
password = getpass.getpass()
group_uid = input('Please enter a group thread id (To test group functionality): ')
user_uid = input('Please enter a user thread id (To test kicking/adding functionality): ')
print('Logging in...')
client = fbchat.Client(email, password)
client = fbchat.Client(email, password, logging_level=logging_level)
# Warning! Taking user input directly like this could be dangerous! Use only for testing purposes!
start_test(client, group_uid, user_uid, sys.argv[1:])