From eed6f8f0a4498bf6393c4f79a5dc662c48e98d73 Mon Sep 17 00:00:00 2001 From: codl Date: Mon, 27 Nov 2017 20:43:27 +0100 Subject: add tests for log_in --- tests/cassettes/test_log_in_password.yaml | 27 +++++++++++ .../cassettes/test_log_in_password_incorrect.yaml | 31 +++++++++++++ tests/cassettes/test_log_in_password_to_file.yaml | 52 ++++++++++++++++++++++ tests/test_auth.py | 44 +++++++++++++++++- 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 tests/cassettes/test_log_in_password.yaml create mode 100644 tests/cassettes/test_log_in_password_incorrect.yaml create mode 100644 tests/cassettes/test_log_in_password_to_file.yaml (limited to 'tests') diff --git a/tests/cassettes/test_log_in_password.yaml b/tests/cassettes/test_log_in_password.yaml new file mode 100644 index 0000000..c44bed6 --- /dev/null +++ b/tests/cassettes/test_log_in_password.yaml @@ -0,0 +1,27 @@ +interactions: +- request: + body: redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&password=mastodonadmin&username=admin%40localhost%3A3000&grant_type=password&client_id=__MASTODON_PY_TEST_ID&client_secret=__MASTODON_PY_TEST_SECRET&scope=read+write+follow + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Connection: [keep-alive] + Content-Length: ['221'] + Content-Type: [application/x-www-form-urlencoded] + User-Agent: [python-requests/2.18.4] + method: POST + uri: http://localhost:3000/oauth/token + response: + body: {string: '{"access_token":"__MASTODON_PY_TEST_TOKEN","token_type":"bearer","scope":"read + write follow","created_at":1511814180}'} + headers: + Cache-Control: [no-store] + Content-Type: [application/json; charset=utf-8] + ETag: [W/"f6a22ef91276cdcde8f09c8186d2d41d"] + Pragma: [no-cache] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + X-Request-Id: [85cb59f1-b92a-4ff8-8a49-c09bed55eb98] + X-Runtime: ['0.077197'] + content-length: ['117'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/cassettes/test_log_in_password_incorrect.yaml b/tests/cassettes/test_log_in_password_incorrect.yaml new file mode 100644 index 0000000..a66fe07 --- /dev/null +++ b/tests/cassettes/test_log_in_password_incorrect.yaml @@ -0,0 +1,31 @@ +interactions: +- request: + body: redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&password=hunter2&username=admin%40localhost%3A3000&grant_type=password&client_id=__MASTODON_PY_TEST_ID&client_secret=__MASTODON_PY_TEST_SECRET&scope=read+write+follow + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Connection: [keep-alive] + Content-Length: ['215'] + Content-Type: [application/x-www-form-urlencoded] + User-Agent: [python-requests/2.18.4] + method: POST + uri: http://localhost:3000/oauth/token + response: + body: {string: '{"error":"invalid_grant","error_description":"The provided authorization + grant is invalid, expired, revoked, does not match the redirection URI used + in the authorization request, or was issued to another client."}'} + headers: + Cache-Control: [no-store] + Content-Type: [application/json; charset=utf-8] + Pragma: [no-cache] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + WWW-Authenticate: ['Bearer realm="Doorkeeper", error="invalid_grant", error_description="The + provided authorization grant is invalid, expired, revoked, does not match + the redirection URI used in the authorization request, or was issued to + another client."'] + X-Request-Id: [ece30cd8-c6c1-4bc7-af1a-19bbd997745e] + X-Runtime: ['0.077864'] + content-length: ['213'] + status: {code: 401, message: Unauthorized} +version: 1 diff --git a/tests/cassettes/test_log_in_password_to_file.yaml b/tests/cassettes/test_log_in_password_to_file.yaml new file mode 100644 index 0000000..f26590c --- /dev/null +++ b/tests/cassettes/test_log_in_password_to_file.yaml @@ -0,0 +1,52 @@ +interactions: +- request: + body: redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&password=mastodonadmin&username=admin%40localhost%3A3000&grant_type=password&client_id=__MASTODON_PY_TEST_ID&client_secret=__MASTODON_PY_TEST_SECRET&scope=read+write+follow + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Connection: [keep-alive] + Content-Length: ['221'] + Content-Type: [application/x-www-form-urlencoded] + User-Agent: [python-requests/2.18.4] + method: POST + uri: http://localhost:3000/oauth/token + response: + body: {string: '{"access_token":"__MASTODON_PY_TEST_TOKEN","token_type":"bearer","scope":"read + write follow","created_at":1511814180}'} + headers: + Cache-Control: [no-store] + Content-Type: [application/json; charset=utf-8] + ETag: [W/"f77400f50590c94b55a861fb6d432464"] + Pragma: [no-cache] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + X-Request-Id: [b6168343-dd9e-4a0f-9ec4-69a1de8f879a] + X-Runtime: ['0.080069'] + content-length: ['117'] + status: {code: 200, message: OK} +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.18.4] + method: GET + uri: http://localhost:3000/api/v1/accounts/verify_credentials + response: + body: {string: '{"id":"1","username":"admin","acct":"admin","display_name":"admin","locked":false,"created_at":"2017-08-02T04:15:27.248Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost:3000/@admin","avatar":"http://localhost:3000/system/accounts/avatars/000/000/001/original/69910b47243ddb47.png","avatar_static":"http://localhost:3000/system/accounts/avatars/000/000/001/original/69910b47243ddb47.png","header":"http://localhost:3000/headers/original/missing.png","header_static":"http://localhost:3000/headers/original/missing.png","followers_count":2,"following_count":1,"statuses_count":16,"source":{"privacy":"public","sensitive":false,"note":""}}'} + headers: + Cache-Control: ['max-age=0, private, must-revalidate'] + Content-Type: [application/json; charset=utf-8] + ETag: [W/"622f72a29f8398775e2abff7cd51dbac"] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Frame-Options: [SAMEORIGIN] + X-Request-Id: [3852d894-50be-4342-a0e6-f9df2ddf5926] + X-Runtime: ['0.029510'] + X-XSS-Protection: [1; mode=block] + content-length: ['650'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/test_auth.py b/tests/test_auth.py index 3b62a2f..502d825 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,7 +1,11 @@ +import pytest +from mastodon.Mastodon import MastodonIllegalArgumentError +from mastodon import Mastodon try: from urllib.parse import urlparse, parse_qs except ImportError: - from urlparse import urlparse, parse_qs + from urlparse import urlparse, parse_qs + def test_auth_request_url(api): url = api.auth_request_url() @@ -14,3 +18,41 @@ def test_auth_request_url(api): assert set(query['scope'][0].split()) == set(('read', 'write', 'follow')) +@pytest.mark.vcr() +def test_log_in_password(api_anonymous): + token = api_anonymous.log_in( + username='admin@localhost:3000', + password='mastodonadmin') + assert token + + +@pytest.mark.vcr() +def test_log_in_password_incorrect(api_anonymous): + with pytest.raises(MastodonIllegalArgumentError): + api_anonymous.log_in( + username='admin@localhost:3000', + password='hunter2') + + +@pytest.mark.vcr() +def test_log_in_password_to_file(api_anonymous, tmpdir): + filepath = tmpdir.join('token') + api_anonymous.log_in( + username='admin@localhost:3000', + password='mastodonadmin', + to_file=str(filepath)) + token = filepath.read_text('UTF-8').rstrip() + assert token + api = api_anonymous + api.access_token = token + assert api.account_verify_credentials() + + +@pytest.mark.skip(reason="Not sure how to test this without setting up selenium or a similar browser automation suite to click on the allow button") +def test_log_in_code(api_anonymous): + pass + + +@pytest.mark.skip(reason="Not supported by Mastodon 😬 (yet?)") +def test_log_in_refresh(api_anonymous): + pass -- cgit v1.2.3