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/cassettes/test_instance_activity.yaml | 28 ++++++++++++++++++++++++++++ tests/cassettes/test_instance_peers.yaml | 28 ++++++++++++++++++++++++++++ tests/conftest.py | 8 ++++++-- tests/test_instance.py | 21 +++++++++++++++++++++ tests/test_streaming.py | 10 ++++++++++ 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 tests/cassettes/test_instance_activity.yaml create mode 100644 tests/cassettes/test_instance_peers.yaml (limited to 'tests') diff --git a/tests/cassettes/test_instance_activity.yaml b/tests/cassettes/test_instance_activity.yaml new file mode 100644 index 0000000..f981c19 --- /dev/null +++ b/tests/cassettes/test_instance_activity.yaml @@ -0,0 +1,28 @@ +interactions: +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.9.1] + method: GET + uri: http://localhost:3000/api/v1/instance/activity + response: + body: {string: '[{"week":"1525039200","statuses":"402","logins":"2","registrations":"0"},{"week":"1524434400","statuses":"0","logins":"0","registrations":"0"},{"week":"1523829600","statuses":"1514","logins":"2","registrations":"0"},{"week":"1523224800","statuses":"0","logins":"0","registrations":"0"},{"week":"1522620000","statuses":"0","logins":"0","registrations":"0"},{"week":"1522015200","statuses":"0","logins":"0","registrations":"0"},{"week":"1521414000","statuses":"0","logins":"0","registrations":"0"},{"week":"1520809200","statuses":"0","logins":"0","registrations":"0"},{"week":"1520204400","statuses":"0","logins":"0","registrations":"0"},{"week":"1519599600","statuses":"0","logins":"0","registrations":"0"},{"week":"1518994800","statuses":"0","logins":"0","registrations":"0"},{"week":"1518390000","statuses":"0","logins":"0","registrations":"0"}]'} + headers: + Cache-Control: ['max-age=86400, public'] + Content-Type: [application/json; charset=utf-8] + Date: ['Sun, 06 May 2018 00:22:05 GMT'] + ETag: [W/"58f08c236168987e365c17c22d5d7788"] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Frame-Options: [SAMEORIGIN] + X-Request-Id: [1155ee76-f23a-407c-a833-26e70c8efbe2] + X-Runtime: ['0.070592'] + X-XSS-Protection: [1; mode=block] + content-length: ['846'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/cassettes/test_instance_peers.yaml b/tests/cassettes/test_instance_peers.yaml new file mode 100644 index 0000000..0f70fc0 --- /dev/null +++ b/tests/cassettes/test_instance_peers.yaml @@ -0,0 +1,28 @@ +interactions: +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.9.1] + method: GET + uri: http://localhost:3000/api/v1/instance/peers + response: + body: {string: '[]'} + headers: + Cache-Control: ['max-age=86400, public'] + Content-Type: [application/json; charset=utf-8] + Date: ['Sun, 06 May 2018 00:24:52 GMT'] + ETag: [W/"3c4a13455f997cff85b40946e8a0eb9d"] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Frame-Options: [SAMEORIGIN] + X-Request-Id: [98dc4df6-9979-4d50-8473-96f9253d65b9] + X-Runtime: ['0.031372'] + X-XSS-Protection: [1; mode=block] + content-length: ['2'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/conftest.py b/tests/conftest.py index b1fc8c5..a1dc161 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,19 +1,23 @@ import pytest -def _api(access_token='__MASTODON_PY_TEST_ACCESS_TOKEN'): +def _api(access_token='__MASTODON_PY_TEST_ACCESS_TOKEN', version="2.3.0", version_check_mode="created"): import mastodon return mastodon.Mastodon( api_base_url='http://localhost:3000', client_id='__MASTODON_PY_TEST_CLIENT_ID', client_secret='__MASTODON_PY_TEST_CLIENT_SECRET', access_token=access_token, - mastodon_version="2.3.0") + mastodon_version=version, + version_check_mode=version_check_mode) @pytest.fixture def api(): return _api() +@pytest.fixture +def api_low_version(): + return _api(version="1.2.0", version_check_mode="changed") @pytest.fixture def api2(): diff --git a/tests/test_instance.py b/tests/test_instance.py index 7b6dc49..e8be86f 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -1,5 +1,7 @@ import pytest +from mastodon.Mastodon import MastodonVersionError + @pytest.mark.vcr() def test_instance(api): instance = api.instance() @@ -8,3 +10,22 @@ def test_instance(api): expected_keys = set(('description', 'email', 'title', 'uri', 'version', 'urls')) assert set(instance.keys()) >= expected_keys + +@pytest.mark.vcr() +def test_instance_activity(api): + activity = api.instance_activity() + + assert len(activity) > 0 + assert "statuses" in activity[0] + assert "logins" in activity[0] + assert "week" in activity[0] + +@pytest.mark.vcr() +def test_instance_peers(api): + assert len(api.instance_peers()) == 0 + +@pytest.mark.vcr() +def test_low_version(api_low_version): + with pytest.raises(MastodonVersionError): + instance = api_low_version.instance() + \ No newline at end of file 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