From a15d28d9d60128fd5551f05eccd8999b46f70f5e Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 6 May 2018 01:37:13 +0200 Subject: Testing updates, can now test streaming API --- tests/test_streaming.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 4 deletions(-) (limited to 'tests/test_streaming.py') diff --git a/tests/test_streaming.py b/tests/test_streaming.py index ac8e691..8c2160a 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -1,11 +1,51 @@ import six import pytest import itertools -from mastodon.streaming import StreamListener +from mastodon.streaming import StreamListener, CallbackStreamListener from mastodon.Mastodon import MastodonMalformedEventError - - - +from mastodon import Mastodon + +import threading +import time + +# For monkeypatching so we can make vcrpy better +import vcr.stubs + +streamingIsPatched = False +realConnections = [] + +def patchStreaming(): + global streamingIsPatched + if streamingIsPatched == True: + return + streamingIsPatched = True + + realGetResponse = vcr.stubs.VCRConnection.getresponse + def fakeGetResponse(*args, **kwargs): + if args[0]._vcr_request.path.startswith("/api/v1/streaming/"): + realConnections.append(args[0].real_connection) + realConnectionRealGetresponse = args[0].real_connection.getresponse + def fakeRealConnectionGetresponse(*args, **kwargs): + response = realConnectionRealGetresponse(*args, **kwargs) + real_body = b"" + try: + while True: + chunk = response.read(1) + real_body += chunk + except AttributeError: + pass # Connection closed + response.read = (lambda: real_body) + return response + args[0].real_connection.getresponse = fakeRealConnectionGetresponse + return realGetResponse(*args, **kwargs) + vcr.stubs.VCRConnection.getresponse = fakeGetResponse + +def streamingClose(): + global realConnections + for connection in realConnections: + connection.close() + realConnections = [] + class Listener(StreamListener): def __init__(self): self.updates = [] @@ -210,3 +250,45 @@ def test_multiline_payload(): '', ]) assert listener.updates == [{"foo": "bar"}] + +@pytest.mark.vcr(match_on=['path']) +def test_stream_user(api, api2): + patchStreaming() + + updates = [] + notifications = [] + listener = CallbackStreamListener( + update_handler = lambda x: updates.append(x), + notification_handler = lambda x: notifications.append(x) + ) + + 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")) + time.sleep(3) + streamingClose() + + t = threading.Thread(args=(), target=do_activities) + t.start() + + stream = None + try: + stream = api.stream_user(listener, run_async=True) + time.sleep(13) + finally: + if stream != None: + stream.close() + + assert len(updates) == 2 + assert len(notifications) == 1 + + assert updates[0].id == posted[0].id + assert updates[1].id == posted[0].id + assert notifications[0].status.id == posted[1].id + + t.join() + + \ No newline at end of file -- cgit v1.2.3 From 3864bbe0859e6c010a3a796467fd48d896734644 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 6 May 2018 02:00:18 +0200 Subject: More streaming tests --- tests/test_streaming.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'tests/test_streaming.py') diff --git a/tests/test_streaming.py b/tests/test_streaming.py index 8c2160a..08d05b3 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -257,9 +257,11 @@ def test_stream_user(api, api2): updates = [] notifications = [] + deletes = [] listener = CallbackStreamListener( update_handler = lambda x: updates.append(x), - notification_handler = lambda x: notifications.append(x) + notification_handler = lambda x: notifications.append(x), + delete_handler = lambda x: deletes.append(x) ) posted = [] @@ -268,7 +270,9 @@ def test_stream_user(api, api2): 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")) - time.sleep(3) + time.sleep(1) + api.status_delete(posted[0]) + time.sleep(2) streamingClose() t = threading.Thread(args=(), target=do_activities) @@ -282,13 +286,45 @@ def test_stream_user(api, api2): if stream != None: stream.close() - assert len(updates) == 2 + assert len(updates) == 1 assert len(notifications) == 1 + assert len(deletes) == 1 assert updates[0].id == posted[0].id - assert updates[1].id == posted[0].id + assert deletes[0] == posted[0].id assert notifications[0].status.id == posted[1].id t.join() - \ No newline at end of file +@pytest.mark.vcr(match_on=['path']) +def test_stream_user_local(api, api2): + patchStreaming() + + 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")) + time.sleep(3) + streamingClose() + + t = threading.Thread(args=(), target=do_activities) + t.start() + + stream = None + try: + stream = api.stream_user(listener, run_async=True) + time.sleep(13) + finally: + if stream != None: + stream.close() + + assert len(updates) == 1 + assert updates[0].id == posted[0].id + + t.join() \ No newline at end of file -- cgit v1.2.3 From 9e368f3814bd5cc77c66869e4487695b493ef5c9 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 6 May 2018 02:05:08 +0200 Subject: Add more streaming tests --- tests/test_streaming.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests/test_streaming.py') diff --git a/tests/test_streaming.py b/tests/test_streaming.py index 08d05b3..883b773 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -154,6 +154,15 @@ def test_unknown_event(): assert listener.deletes == [] assert listener.heartbeats == 0 +def test_invalid_event(): + """But not too tolerant""" + listener = Listener() + with pytest.raises(MastodonMalformedEventError): + listener.handle_stream_([ + 'event: whatup', + 'data: {}', + '', + ]) def test_missing_event_name(): listener = Listener() -- cgit v1.2.3 From f8e209f6ff70d3be08b71b05f4cec753a9dac926 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sun, 6 May 2018 02:26:19 +0200 Subject: Add more tests --- tests/test_streaming.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests/test_streaming.py') diff --git a/tests/test_streaming.py b/tests/test_streaming.py index 883b773..33d1381 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -164,6 +164,16 @@ def test_invalid_event(): '', ]) +def test_invalid_json(): + """But not too tolerant""" + listener = Listener() + with pytest.raises(MastodonMalformedEventError): + listener.handle_stream_([ + 'event: blahblah', + 'data: {kjaslkdjalskdjasd asdkjhak ajdasldasd}', + '', + ]) + def test_missing_event_name(): listener = Listener() with pytest.raises(MastodonMalformedEventError): -- cgit v1.2.3