2017-04-09 11:21:56 +02:00
|
|
|
import six
|
|
|
|
import pytest
|
|
|
|
import itertools
|
2018-05-06 01:37:13 +02:00
|
|
|
from mastodon.streaming import StreamListener, CallbackStreamListener
|
2017-11-28 13:50:57 +01:00
|
|
|
from mastodon.Mastodon import MastodonMalformedEventError
|
2018-05-06 01:37:13 +02:00
|
|
|
from mastodon import Mastodon
|
|
|
|
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2018-07-14 01:04:06 +02:00
|
|
|
import select
|
|
|
|
|
2018-05-06 01:37:13 +02:00
|
|
|
# For monkeypatching so we can make vcrpy better
|
|
|
|
import vcr.stubs
|
|
|
|
|
2018-07-14 01:04:06 +02:00
|
|
|
streaming_is_patched = False
|
|
|
|
real_connections = []
|
|
|
|
close_connections = False
|
2018-05-06 01:37:13 +02:00
|
|
|
|
2019-10-11 22:32:51 +02:00
|
|
|
def patch_streaming():
|
2018-07-14 01:04:06 +02:00
|
|
|
global streaming_is_patched
|
|
|
|
global close_connections
|
|
|
|
if streaming_is_patched == True:
|
2018-05-06 01:37:13 +02:00
|
|
|
return
|
2018-07-14 01:04:06 +02:00
|
|
|
streaming_is_patched = True
|
2018-05-06 01:37:13 +02:00
|
|
|
|
2018-07-14 01:04:06 +02:00
|
|
|
real_get_response = vcr.stubs.VCRConnection.getresponse
|
|
|
|
def fake_get_response(*args, **kwargs):
|
|
|
|
global close_connections
|
|
|
|
close_connections = False
|
2018-05-06 01:37:13 +02:00
|
|
|
if args[0]._vcr_request.path.startswith("/api/v1/streaming/"):
|
2018-07-14 01:04:06 +02:00
|
|
|
real_connections.append(args[0].real_connection)
|
|
|
|
real_connection_real_get_response = args[0].real_connection.getresponse
|
2018-05-06 01:37:13 +02:00
|
|
|
def fakeRealConnectionGetresponse(*args, **kwargs):
|
2018-07-14 01:04:06 +02:00
|
|
|
response = real_connection_real_get_response(*args, **kwargs)
|
2018-05-06 01:37:13 +02:00
|
|
|
real_body = b""
|
|
|
|
try:
|
2018-07-14 01:04:06 +02:00
|
|
|
while close_connections == False:
|
|
|
|
if len(select.select([response], [], [], 0.01)[0]) > 0:
|
|
|
|
chunk = response.read(1)
|
|
|
|
real_body += chunk
|
2018-05-06 01:37:13 +02:00
|
|
|
except AttributeError:
|
|
|
|
pass # Connection closed
|
2018-07-14 01:04:06 +02:00
|
|
|
print(real_body)
|
2018-05-06 01:37:13 +02:00
|
|
|
response.read = (lambda: real_body)
|
|
|
|
return response
|
|
|
|
args[0].real_connection.getresponse = fakeRealConnectionGetresponse
|
2018-07-14 01:04:06 +02:00
|
|
|
return real_get_response(*args, **kwargs)
|
|
|
|
vcr.stubs.VCRConnection.getresponse = fake_get_response
|
2018-05-06 01:37:13 +02:00
|
|
|
|
2018-07-14 01:04:06 +02:00
|
|
|
def streaming_close():
|
|
|
|
global real_connections
|
|
|
|
for connection in real_connections:
|
2018-05-06 01:37:13 +02:00
|
|
|
connection.close()
|
2018-07-14 01:04:06 +02:00
|
|
|
real_connections = []
|
|
|
|
close_connections = True
|
2018-05-06 01:37:13 +02:00
|
|
|
|
2017-04-09 11:21:56 +02:00
|
|
|
class Listener(StreamListener):
|
|
|
|
def __init__(self):
|
|
|
|
self.updates = []
|
|
|
|
self.notifications = []
|
|
|
|
self.deletes = []
|
|
|
|
self.heartbeats = 0
|
|
|
|
|
|
|
|
def on_update(self, status):
|
|
|
|
self.updates.append(status)
|
|
|
|
|
|
|
|
def on_notification(self, notification):
|
|
|
|
self.notifications.append(notification)
|
|
|
|
|
|
|
|
def on_delete(self, status_id):
|
|
|
|
self.deletes.append(status_id)
|
|
|
|
|
2017-11-28 13:50:57 +01:00
|
|
|
def on_blahblah(self, data):
|
|
|
|
pass
|
|
|
|
|
2017-04-09 11:21:56 +02:00
|
|
|
def handle_heartbeat(self):
|
|
|
|
self.heartbeats += 1
|
|
|
|
|
|
|
|
def handle_stream_(self, lines):
|
2017-09-05 22:59:32 +02:00
|
|
|
"""Test helper to avoid littering all tests with six.b()."""
|
2017-12-21 11:06:13 +01:00
|
|
|
class MockResponse():
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def iter_content(self, chunk_size):
|
|
|
|
for line in self.data:
|
|
|
|
for byte in line:
|
|
|
|
bytearr = bytearray()
|
|
|
|
bytearr.append(byte)
|
|
|
|
yield(bytearr)
|
|
|
|
yield(b'\n')
|
|
|
|
return self.handle_stream(MockResponse(map(six.b, lines)))
|
2017-04-09 11:21:56 +02:00
|
|
|
|
2017-09-05 22:59:32 +02:00
|
|
|
|
2017-04-09 11:21:56 +02:00
|
|
|
def test_heartbeat():
|
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([':one', ':two'])
|
|
|
|
assert listener.heartbeats == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_status():
|
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: update',
|
|
|
|
'data: {"foo": "bar"}',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.updates == [{"foo": "bar"}]
|
|
|
|
|
|
|
|
|
|
|
|
def test_notification():
|
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: notification',
|
|
|
|
'data: {"foo": "bar"}',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.notifications == [{"foo": "bar"}]
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete():
|
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: delete',
|
|
|
|
'data: 123',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.deletes == [123]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('events', itertools.permutations([
|
|
|
|
['event: update', 'data: {"foo": "bar"}', ''],
|
|
|
|
['event: notification', 'data: {"foo": "bar"}', ''],
|
|
|
|
['event: delete', 'data: 123', ''],
|
|
|
|
[':toot toot'],
|
|
|
|
[':beep beep'],
|
|
|
|
]))
|
|
|
|
def test_many(events):
|
|
|
|
listener = Listener()
|
|
|
|
stream = [
|
|
|
|
line
|
|
|
|
for event in events
|
|
|
|
for line in event
|
|
|
|
]
|
|
|
|
listener.handle_stream_(stream)
|
|
|
|
assert listener.updates == [{"foo": "bar"}]
|
|
|
|
assert listener.notifications == [{"foo": "bar"}]
|
|
|
|
assert listener.deletes == [123]
|
|
|
|
assert listener.heartbeats == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_unknown_event():
|
2017-09-05 22:59:32 +02:00
|
|
|
"""Be tolerant of new event types"""
|
2017-04-09 11:21:56 +02:00
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: blahblah',
|
|
|
|
'data: {}',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.updates == []
|
|
|
|
assert listener.notifications == []
|
|
|
|
assert listener.deletes == []
|
|
|
|
assert listener.heartbeats == 0
|
|
|
|
|
2018-05-06 02:05:08 +02:00
|
|
|
def test_invalid_event():
|
|
|
|
"""But not too tolerant"""
|
|
|
|
listener = Listener()
|
|
|
|
with pytest.raises(MastodonMalformedEventError):
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: whatup',
|
|
|
|
'data: {}',
|
|
|
|
'',
|
|
|
|
])
|
2017-04-09 11:21:56 +02:00
|
|
|
|
2018-05-06 02:26:19 +02:00
|
|
|
def test_invalid_json():
|
|
|
|
"""But not too tolerant"""
|
|
|
|
listener = Listener()
|
|
|
|
with pytest.raises(MastodonMalformedEventError):
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: blahblah',
|
|
|
|
'data: {kjaslkdjalskdjasd asdkjhak ajdasldasd}',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
|
2017-04-09 11:21:56 +02:00
|
|
|
def test_missing_event_name():
|
|
|
|
listener = Listener()
|
2017-11-28 13:50:57 +01:00
|
|
|
with pytest.raises(MastodonMalformedEventError):
|
2017-04-09 11:21:56 +02:00
|
|
|
listener.handle_stream_([
|
|
|
|
'data: {}',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
|
|
|
|
assert listener.updates == []
|
|
|
|
assert listener.notifications == []
|
|
|
|
assert listener.deletes == []
|
|
|
|
assert listener.heartbeats == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_missing_data():
|
|
|
|
listener = Listener()
|
2017-11-28 13:50:57 +01:00
|
|
|
with pytest.raises(MastodonMalformedEventError):
|
2017-04-09 11:21:56 +02:00
|
|
|
listener.handle_stream_([
|
|
|
|
'event: update',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
|
|
|
|
assert listener.updates == []
|
|
|
|
assert listener.notifications == []
|
|
|
|
assert listener.deletes == []
|
|
|
|
assert listener.heartbeats == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_sse_order_doesnt_matter():
|
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'data: {"foo": "bar"}',
|
|
|
|
'event: update',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.updates == [{"foo": "bar"}]
|
|
|
|
|
|
|
|
|
|
|
|
def test_extra_keys_ignored():
|
2017-09-05 22:59:32 +02:00
|
|
|
"""
|
2017-04-09 11:21:56 +02:00
|
|
|
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
|
|
|
|
defines 'id' and 'retry' keys which the Mastodon streaming API doesn't use,
|
|
|
|
and alleges that "All other field names are ignored".
|
2017-09-05 22:59:32 +02:00
|
|
|
"""
|
2017-04-09 11:21:56 +02:00
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: update',
|
|
|
|
'data: {"foo": "bar"}',
|
|
|
|
'id: 123',
|
|
|
|
'retry: 456',
|
|
|
|
'ignoreme: blah blah blah',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.updates == [{"foo": "bar"}]
|
|
|
|
|
|
|
|
|
|
|
|
def test_valid_utf8():
|
2017-09-05 22:59:32 +02:00
|
|
|
"""Snowman Cat Face With Tears Of Joy"""
|
2017-04-09 11:21:56 +02:00
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: update',
|
|
|
|
'data: {"foo": "\xE2\x98\x83\xF0\x9F\x98\xB9"}',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.updates == [{"foo": u"\u2603\U0001F639"}]
|
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_utf8():
|
2017-09-05 22:59:32 +02:00
|
|
|
"""Cat Face With Tears O"""
|
2017-04-09 11:21:56 +02:00
|
|
|
listener = Listener()
|
2017-11-28 13:50:57 +01:00
|
|
|
with pytest.raises(MastodonMalformedEventError):
|
2017-04-09 11:21:56 +02:00
|
|
|
listener.handle_stream_([
|
|
|
|
'event: update',
|
|
|
|
'data: {"foo": "\xF0\x9F\x98"}',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
def test_multiline_payload():
|
2017-09-05 22:59:32 +02:00
|
|
|
"""
|
2017-04-09 11:21:56 +02:00
|
|
|
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Data-only_messages
|
|
|
|
says that newlines in the 'data' field can be encoded by sending the field
|
|
|
|
twice! This would be really pathological for Mastodon because the payload
|
|
|
|
is JSON, but technically literal newlines are permissible (outside strings)
|
|
|
|
so let's handle this case.
|
2017-09-05 22:59:32 +02:00
|
|
|
"""
|
2017-04-09 11:21:56 +02:00
|
|
|
listener = Listener()
|
|
|
|
listener.handle_stream_([
|
|
|
|
'event: update',
|
|
|
|
'data: {"foo":',
|
|
|
|
'data: "bar"',
|
|
|
|
'data: }',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
assert listener.updates == [{"foo": "bar"}]
|
2018-05-06 01:37:13 +02:00
|
|
|
|
|
|
|
@pytest.mark.vcr(match_on=['path'])
|
|
|
|
def test_stream_user(api, api2):
|
2019-10-11 22:32:51 +02:00
|
|
|
patch_streaming()
|
2018-05-06 01:37:13 +02:00
|
|
|
|
2018-05-07 00:53:13 +02:00
|
|
|
# Make sure we are in the right state to not receive updates from api2
|
|
|
|
user = api2.account_verify_credentials()
|
|
|
|
api2.account_unfollow(user)
|
2018-06-05 22:10:31 +02:00
|
|
|
time.sleep(2)
|
2018-05-07 00:53:13 +02:00
|
|
|
|
2018-05-06 01:37:13 +02:00
|
|
|
updates = []
|
|
|
|
notifications = []
|
2018-05-06 02:00:18 +02:00
|
|
|
deletes = []
|
2018-05-06 01:37:13 +02:00
|
|
|
listener = CallbackStreamListener(
|
|
|
|
update_handler = lambda x: updates.append(x),
|
2018-05-06 02:00:18 +02:00
|
|
|
notification_handler = lambda x: notifications.append(x),
|
|
|
|
delete_handler = lambda x: deletes.append(x)
|
2018-05-06 01:37:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
posted = []
|
|
|
|
def do_activities():
|
|
|
|
time.sleep(5)
|
|
|
|
posted.append(api.status_post("only real cars respond."))
|
|
|
|
posted.append(api2.status_post("@mastodonpy_test beep beep I'm a jeep"))
|
|
|
|
posted.append(api2.status_post("on the internet, nobody knows you're a plane"))
|
2018-05-06 02:00:18 +02:00
|
|
|
time.sleep(1)
|
|
|
|
api.status_delete(posted[0])
|
2018-07-14 01:04:06 +02:00
|
|
|
time.sleep(10)
|
|
|
|
streaming_close()
|
2018-05-06 01:37:13 +02:00
|
|
|
|
|
|
|
t = threading.Thread(args=(), target=do_activities)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
stream = None
|
|
|
|
try:
|
|
|
|
stream = api.stream_user(listener, run_async=True)
|
2018-07-14 01:04:06 +02:00
|
|
|
time.sleep(20)
|
2018-05-06 01:37:13 +02:00
|
|
|
finally:
|
|
|
|
if stream != None:
|
|
|
|
stream.close()
|
|
|
|
|
2018-05-06 02:00:18 +02:00
|
|
|
assert len(updates) == 1
|
2018-05-06 01:37:13 +02:00
|
|
|
assert len(notifications) == 1
|
2018-05-06 02:00:18 +02:00
|
|
|
assert len(deletes) == 1
|
2018-05-06 01:37:13 +02:00
|
|
|
|
|
|
|
assert updates[0].id == posted[0].id
|
2018-05-06 02:00:18 +02:00
|
|
|
assert deletes[0] == posted[0].id
|
2018-05-06 01:37:13 +02:00
|
|
|
assert notifications[0].status.id == posted[1].id
|
|
|
|
|
|
|
|
t.join()
|
|
|
|
|
2018-05-06 02:00:18 +02:00
|
|
|
@pytest.mark.vcr(match_on=['path'])
|
|
|
|
def test_stream_user_local(api, api2):
|
2019-10-11 22:32:51 +02:00
|
|
|
patch_streaming()
|
2018-05-06 02:00:18 +02:00
|
|
|
|
2018-05-07 00:53:13 +02:00
|
|
|
# Make sure we are in the right state to not receive updates from api2
|
|
|
|
user = api2.account_verify_credentials()
|
|
|
|
api2.account_unfollow(user)
|
|
|
|
|
2018-05-06 02:00:18 +02:00
|
|
|
updates = []
|
|
|
|
notifications = []
|
|
|
|
listener = CallbackStreamListener(
|
|
|
|
local_update_handler = lambda x: updates.append(x),
|
|
|
|
)
|
|
|
|
|
|
|
|
posted = []
|
|
|
|
def do_activities():
|
|
|
|
time.sleep(5)
|
|
|
|
posted.append(api.status_post("it's cool guy"))
|
2018-07-14 01:04:06 +02:00
|
|
|
time.sleep(10)
|
|
|
|
streaming_close()
|
2018-05-06 02:00:18 +02:00
|
|
|
|
|
|
|
t = threading.Thread(args=(), target=do_activities)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
stream = None
|
|
|
|
try:
|
|
|
|
stream = api.stream_user(listener, run_async=True)
|
2018-07-14 01:04:06 +02:00
|
|
|
time.sleep(20)
|
2018-05-06 02:00:18 +02:00
|
|
|
finally:
|
|
|
|
if stream != None:
|
|
|
|
stream.close()
|
|
|
|
|
|
|
|
assert len(updates) == 1
|
|
|
|
assert updates[0].id == posted[0].id
|
|
|
|
|
2018-07-14 01:04:06 +02:00
|
|
|
t.join()
|
2019-06-22 16:41:32 +02:00
|
|
|
|
2019-10-11 22:32:51 +02:00
|
|
|
@pytest.mark.vcr(match_on=['path'])
|
|
|
|
def test_stream_direct(api, api2):
|
|
|
|
patch_streaming()
|
|
|
|
|
|
|
|
conversations = []
|
|
|
|
listener = CallbackStreamListener(
|
|
|
|
conversation_handler = lambda x: conversations.append(x),
|
|
|
|
)
|
|
|
|
|
|
|
|
def do_activities():
|
|
|
|
time.sleep(5)
|
|
|
|
api2.status_post("@mastodonpy_test todo funny text here", visibility = "direct")
|
|
|
|
time.sleep(10)
|
|
|
|
streaming_close()
|
|
|
|
|
|
|
|
t = threading.Thread(args=(), target=do_activities)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
stream = None
|
|
|
|
try:
|
|
|
|
stream = api.stream_direct(listener, run_async=True)
|
|
|
|
time.sleep(20)
|
|
|
|
finally:
|
|
|
|
if stream != None:
|
|
|
|
stream.close()
|
|
|
|
|
|
|
|
assert len(conversations) == 1
|
|
|
|
|
2019-06-22 16:41:32 +02:00
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_stream_healthy(api_anonymous):
|
|
|
|
assert api_anonymous.stream_healthy()
|