From f42032dafcefd422d3819ace721accf75f2d0c91 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Fri, 11 Oct 2019 22:07:51 +0200 Subject: Add new card behaviour, freeze old test --- mastodon/Mastodon.py | 16 ++- tests/cassettes_pre_2_9_2/test_status_card.yaml | 144 ++++++++++++++++++++++++ tests/test_status.py | 16 +++ 3 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 tests/cassettes_pre_2_9_2/test_status_card.yaml diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 91f5125..7f9a008 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -784,7 +784,7 @@ class Mastodon: url = '/api/v1/statuses/{0}'.format(str(id)) return self.__api_request('GET', url) - @api_version("1.0.0", "1.0.0", __DICT_VERSION_CARD) + @api_version("1.0.0", "3.0.0", __DICT_VERSION_CARD) def status_card(self, id): """ Fetch a card associated with a status. A card describes an object (such as an @@ -792,11 +792,19 @@ class Mastodon: Does not require authentication for publicly visible statuses. + This function is deprecated as of 3.0.0 and the endpoint does not + exist anymore - you should just use the "card" field of the status dicts + instead. Mastodon.py will try to mimick the old behaviour, but this + is somewhat inefficient and not guaranteed to be the case forever. + Returns a `card dict`_. """ - id = self.__unpack_id(id) - url = '/api/v1/statuses/{0}/card'.format(str(id)) - return self.__api_request('GET', url) + if self.verify_minimum_version("3.0.0"): + return self.status(id).card + else: + id = self.__unpack_id(id) + url = '/api/v1/statuses/{0}/card'.format(str(id)) + return self.__api_request('GET', url) @api_version("1.0.0", "1.0.0", __DICT_VERSION_CONTEXT) def status_context(self, id): diff --git a/tests/cassettes_pre_2_9_2/test_status_card.yaml b/tests/cassettes_pre_2_9_2/test_status_card.yaml new file mode 100644 index 0000000..fdaccff --- /dev/null +++ b/tests/cassettes_pre_2_9_2/test_status_card.yaml @@ -0,0 +1,144 @@ +interactions: +- request: + body: status=http%3A%2F%2Fexample.org%2F + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + Content-Length: ['34'] + Content-Type: [application/x-www-form-urlencoded] + User-Agent: [python-requests/2.18.4] + method: POST + uri: http://localhost:3000/api/v1/statuses + response: + body: + string: !!binary | + H4sIAHLfoF0AA5RU227bMAz9lUDPTXyJ08UGBuwH9tanLYPByIyjVZYESU4bBPv3Ub5kTute9mTx + kIeiyENfmKhYwZI4zbPNJs3ydbrNk3SzZXeMWwSPVQmeItI4yZdJvEyShzQu4rjI8tV2m/+gOKFK + i0aeS6/LkE21Ut6iwLlulZ94HSonvDghKw4gHRJitJBoS4/P4T7KexJO7IUU/ky2afdScEIlqLqF + GsdMrRXkPnpviiiSmoM8auej1qF1UQPO60orQ2Ugoc6DJ4+LZh/cWjmX6tvLLLPk8FiBruxeyoo4 + IHup6ylygJOmej3OgtW1Fz2zniBNO/UbodTE5Fp5DNnYro3jNTfdB3sDFkeLh6+78V34DI2RuNK2 + jnZsYVGST+mDllI/LZTWBhVa8niwNXpylnvq+eOOTbM6A2rBJThHAUJ1o5I4xgxX9aFRiH2TO1Ju + yvoE79Wdb5AiuLGG1rCxx6OKwND0OHihFSsuTEFDAmPfh8GvzHkRJr9wLc2JyE+4d+HUsf8QnQ/z + vAz7lK6zzf2XbR73pyAuEuSQ9oWeWMf3s45KOCPhXA7MoH/NHyez32v/TwevFja+X6bpQ7oukqTY + pKssS8LCKh1KnxPLpD+fXIZQ/AlIK3PBvcdF2opaKJBRI5wTql4ZVV+JZVhKWu3/5x8RKpy9uPd8 + QHzn4g/4/bJQwHSNO4xiJtj4uxmh9I5ho38Lx4qfv4giUFbdmSTUYCWARueBHxva5iEmHEmUg+Wh + Hk7TPBzs9cdqqIxBln8BAAD//wMAq8iDptwFAAA= + headers: + Cache-Control: ['no-cache, no-store'] + Content-Encoding: [gzip] + Content-Type: [application/json; charset=utf-8] + Referrer-Policy: [strict-origin-when-cross-origin] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Download-Options: [noopen] + X-Frame-Options: [SAMEORIGIN] + X-Permitted-Cross-Domain-Policies: [none] + X-Request-Id: [5695c474-1f03-4198-934a-063f525d1421] + X-Runtime: ['0.206173'] + X-XSS-Protection: [1; mode=block] + status: {code: 200, message: OK} +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.18.4] + method: GET + uri: http://localhost:3000/api/v1/instance/ + response: + body: + string: !!binary | + H4sIAHffoF0AA0yQXU7EMAyE7+Ln0vRnabe5AydAqDKJaQNpEiXO7sNq705KWcSb/WlGM/YNcjQg + wXqFdvWJoQI2bKmwF0zstXcFaUoqmsCmbBIKoA2NPcYLxXTwrp7qtpAcbQJ5g8SRcDNumTHsIdck + hfhLkqemaeBeFRnyjz4nirPy2THI/uA5PUDXlhq+xLoHaYqX17y9u6PLyhz+B4iA6iuJjbRBYTZc + KIkQ6WLo+jRhO+q++1Cncz9MdFZKT22D43M3NDSMuv4MS7nEolvybgT5CuTgrYJIiymH4f6Lgjlm + qkB5x6h4RvXbzWVr798AAAD//wMA7+lJTl0BAAA= + headers: + Cache-Control: ['max-age=300, public'] + Content-Encoding: [gzip] + Content-Type: [application/json; charset=utf-8] + Date: ['Fri, 11 Oct 2019 20:00:55 GMT'] + ETag: [W/"d02697950b63ada2556058876a3603cd"] + Referrer-Policy: [strict-origin-when-cross-origin] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Download-Options: [noopen] + X-Frame-Options: [SAMEORIGIN] + X-Permitted-Cross-Domain-Policies: [none] + X-Request-Id: [cdbcdbaf-3317-4d46-88dd-a2108d9d8644] + X-Runtime: ['0.050483'] + X-XSS-Protection: [1; mode=block] + status: {code: 200, message: OK} +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.18.4] + method: GET + uri: http://localhost:3000/api/v1/statuses/102945524932891258/card + response: + body: + string: !!binary | + H4sIAHffoF0AA0yNyw4CIQxFf8V0PRHXrPU7DEoDjRQIFh8x/rsdGSfues+5N31BbwksRJFqjcGH + 45pwW1owMIGQJFR7GHizL+woq/B4PTeqQiWrnpvPOhcT5Ysm1yWWdsyOcegFjF+aays38vhfWdFa + isLLdScvEexOGVKI8j2JXdBt7ilNgHxC/5u+PwAAAP//AwBs2irL1gAAAA== + headers: + Cache-Control: ['no-cache, no-store'] + Content-Encoding: [gzip] + Content-Type: [application/json; charset=utf-8] + Referrer-Policy: [strict-origin-when-cross-origin] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Download-Options: [noopen] + X-Frame-Options: [SAMEORIGIN] + X-Permitted-Cross-Domain-Policies: [none] + X-Request-Id: [8d8488d2-f693-4edb-ba17-a9b4bb524b59] + X-Runtime: ['0.070177'] + X-XSS-Protection: [1; mode=block] + status: {code: 200, message: OK} +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + Content-Length: ['0'] + User-Agent: [python-requests/2.18.4] + method: DELETE + uri: http://localhost:3000/api/v1/statuses/102945524932891258 + response: + body: + string: !!binary | + H4sIAHffoF0AA5RUy47bMAz8lYXOm1hWnG3sU3+gtz31AYOxGYetLAmWnN0g6L+XcpTA3Rrd9hRq + yKGG1MQXQa2oRC5VWWy3qig3alfmarsTj6IZEAK2NQSuUDIvV7lc5fmzkpWUVVGud7vyM9eRqQd0 + +lwHW8duZtT6dxSaxo4mzLIejadAJxTVAbRHRpwljUMd8DXex31P5GlPmsKZz27ca2oY1WC6ETq8 + dRoH4vQxBFdlmbYN6KP1IRs9Dj7rwQfbWuNYBjLqAwTO+Gxx4HHQS60+vu2ySI7DEvp6mlRUMiJ7 + bbs5coCTZb0BF8H2vosrs5sh/TjPOzJmdkwrS8LxFXqncW2HLhO3XrdtgWOVDQSyRlQXYaDnRYpP + acC1Oz/ECR/8yHqY/IJ7H6OJ/ZPpTdJ9Sb5Rm2L79GFXymsUl8iLT23f7E1M/LCYaMk7Dec6MeM7 + 2+bHbMa9Dff4T2PKp5VSz2pT5Xm1VeuiyKMxjY3SxddRyk3jph+8HrJ0+vdHj+JPEGBYKr5mfGYH + 6siAznrynky3dqa7E+toPrbw//OPCC0uXnzNvEP8y8Xv8A9Wa/vCBXO7ThjXzLDb3+oGqUeBvf1O + XlRfvjGFULdTzBbqsSXgpwvQHHs0IdXEkE2ZTgG6FM37NDDcPyCOZSRb/gIAAP//AwC9GyasxAQA + AA== + headers: + Cache-Control: ['no-cache, no-store'] + Content-Encoding: [gzip] + Content-Type: [application/json; charset=utf-8] + Referrer-Policy: [strict-origin-when-cross-origin] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Download-Options: [noopen] + X-Frame-Options: [SAMEORIGIN] + X-Permitted-Cross-Domain-Policies: [none] + X-Request-Id: [ae14d39d-fe87-49ff-8072-5e43f83ef722] + X-Runtime: ['0.109924'] + X-XSS-Protection: [1; mode=block] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/test_status.py b/tests/test_status.py index ef27f9c..32f57a4 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -2,6 +2,7 @@ import pytest from mastodon.Mastodon import MastodonAPIError, MastodonNotFoundError import datetime import pytz +import vcr @pytest.mark.vcr() def test_status(status, api): @@ -28,6 +29,8 @@ def test_status_missing(api): api.status(0) # Messy and will only work if there is an internet connection that is decent, obviously. +# Also, deprecated, but still a good test (Mastodon.py tries to fake the old behaviour +# internally) @pytest.mark.vcr() def test_status_card(api): import time @@ -39,6 +42,19 @@ def test_status_card(api): finally: api.status_delete(status['id']) +# Old-version card api +def test_status_card_pre_2_9_2(api): + with vcr.use_cassette('test_status_card.yaml', cassette_library_dir='tests/cassettes_pre_2_9_2', record_mode='none'): + import time + status = api.status_post("http://example.org/") + time.sleep(5) # Card generation may take time + card = api.status_card(status['id']) + try: + assert card + finally: + api.status_delete(status['id']) + + @pytest.mark.vcr() def test_status_context(status, api): context = api.status_context(status['id']) -- cgit v1.2.3