From 6a630202db718c5ed2924ad3a3863d7eee391b88 Mon Sep 17 00:00:00 2001 From: halcy Date: Sat, 19 Nov 2022 00:53:25 +0200 Subject: [PATCH] add server rules API --- TODO.md | 2 +- mastodon/Mastodon.py | 12 ++++- tests/cassettes/test_instance_rules.yaml | 60 ++++++++++++++++++++++++ tests/test_instance.py | 4 +- 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 tests/cassettes/test_instance_rules.yaml diff --git a/TODO.md b/TODO.md index 7f27e51..ee85789 100644 --- a/TODO.md +++ b/TODO.md @@ -29,7 +29,7 @@ Refer to mastodon changelog and API docs for details when implementing, add or m 3.4.0 ----- -* [ ] Add server rules +* [x] Add server rules * [ ] Add POST /api/v1/emails/confirmations to REST API * [ ] Add GET /api/v1/accounts/lookup to REST API * [ ] Add policy param to POST /api/v1/push/subscriptions in REST API diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 4462253..14cf174 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -774,8 +774,7 @@ class Mastodon: """ Basic health check. Returns True if healthy, False if not. """ - status = self.__api_request( - 'GET', '/health', parse=False).decode("utf-8") + status = self.__api_request('GET', '/health', parse=False).decode("utf-8") return status in ["OK", "success"] @api_version("3.0.0", "3.0.0", "3.0.0") @@ -807,6 +806,15 @@ class Mastodon: parse = urlparse(schema_url) return self.__api_request('GET', parse.path + parse.params + parse.query + parse.fragment) + @api_version("3.4.0", "3.4.0", __DICT_VERSION_INSTANCE) + def instance_rules(self): + """ + Retrieve instance rules. + + Returns a list of `id` + `text` dicts, same as the `rules` field in the `instance dicts`_. + """ + return self.__api_request('GET', '/api/v1/instance/rules') + ### # Reading data: Timelines ## diff --git a/tests/cassettes/test_instance_rules.yaml b/tests/cassettes/test_instance_rules.yaml new file mode 100644 index 0000000..6e144b6 --- /dev/null +++ b/tests/cassettes/test_instance_rules.yaml @@ -0,0 +1,60 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Authorization: + - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN + Connection: + - keep-alive + User-Agent: + - tests/v311 + method: GET + uri: http://localhost:3000/api/v1/instance/rules + response: + body: + string: '[]' + headers: + Cache-Control: + - no-store + Content-Security-Policy: + - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src + ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000; + style-src ''self'' http://localhost:3000 ''nonce-VfLfKge8CLCQB9dJyA6/cA==''; + media-src ''self'' https: data: http://localhost:3000; frame-src ''self'' + https:; manifest-src ''self'' http://localhost:3000; connect-src ''self'' + data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000 + ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline'' + ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000; + worker-src ''self'' blob: http://localhost:3000' + Content-Type: + - application/json; charset=utf-8 + ETag: + - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5" + Referrer-Policy: + - strict-origin-when-cross-origin + Transfer-Encoding: + - chunked + Vary: + - Accept, Origin + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Frame-Options: + - SAMEORIGIN + X-Permitted-Cross-Domain-Policies: + - none + X-Request-Id: + - 48fd3107-1ed2-4db1-8ae6-a62ac4897b98 + X-Runtime: + - '0.036615' + X-XSS-Protection: + - 1; mode=block + status: + code: 200 + message: OK +version: 1 diff --git a/tests/test_instance.py b/tests/test_instance.py index 3b42c3e..5f451c7 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -72,4 +72,6 @@ def test_directory(api): assert isinstance(directory, list) assert len(directory) > 0 - +@pytest.mark.vcr() +def test_instance_rules(api): + assert isinstance(api.instance_rules(), list)