From d13412024c3c03521f1c7c95c922af877aa3adb9 Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Thu, 6 Jun 2019 19:21:26 +0530 Subject: [PATCH] Fixes #51 supports logout endpoint --- data/test-logout.yml | 22 ++++++++++++++++++++++ data/test_logout.json | 1 + sdclientapi/__init__.py | 26 ++++++++++++++++++++++++++ tests/test_api.py | 5 +++++ tests/test_apiproxy.py | 5 +++++ 5 files changed, 59 insertions(+) create mode 100644 data/test-logout.yml create mode 100644 data/test_logout.json diff --git a/data/test-logout.yml b/data/test-logout.yml new file mode 100644 index 000000000..e225fc487 --- /dev/null +++ b/data/test-logout.yml @@ -0,0 +1,22 @@ +interactions: +- request: + body: null + headers: + Accept: [application/json] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Token eyJhbGciOiJIUzI1NiIsImV4cCI6MTU1OTg1NjEzNiwiaWF0IjoxNTU5ODI3MzM2fQ.eyJpZCI6MX0.UlITSFSmIoMtloqyUzMJ0KplL_x5l8I_zJL-ROMpSIM] + Connection: [keep-alive] + Content-Length: ['0'] + Content-Type: [application/json] + User-Agent: [python-requests/2.20.0] + method: POST + uri: http://127.0.0.1:8081/api/v1/logout + response: + body: {string: "{\n \"message\": \"Your token has been revoked.\"\n}\n"} + headers: + Content-Length: ['48'] + Content-Type: [application/json] + Date: ['Thu, 06 Jun 2019 13:22:16 GMT'] + Server: [Werkzeug/0.14.1 Python/2.7.12] + status: {code: 200, message: OK} +version: 1 diff --git a/data/test_logout.json b/data/test_logout.json new file mode 100644 index 000000000..0cd9f1570 --- /dev/null +++ b/data/test_logout.json @@ -0,0 +1 @@ +{"{\"body\": null, \"headers\": \"{\\\"Accept\\\": \\\"application/json\\\", \\\"Content-Type\\\": \\\"application/json\\\"}\", \"method\": \"POST\", \"path_query\": \"api/v1/logout\"}+1": "{\"body\": \"{\\n \\\"message\\\": \\\"Your token has been revoked.\\\"\\n}\\n\", \"version\": \"0.1.1\", \"status\": 200, \"headers\": {\"Server\": \"Werkzeug/0.14.1 Python/2.7.12\", \"Content-Length\": \"48\", \"Content-Type\": \"application/json\", \"Date\": \"Thu, 06 Jun 2019 13:14:28 GMT\"}}"} \ No newline at end of file diff --git a/sdclientapi/__init__.py b/sdclientapi/__init__.py index 9cf34f24e..7d964b1e6 100644 --- a/sdclientapi/__init__.py +++ b/sdclientapi/__init__.py @@ -895,3 +895,29 @@ def delete_reply(self, reply: Reply) -> bool: return True # We should never reach here return False + + def logout(self) -> bool: + """ + Logsout the current user + """ + path_query = "api/v1/logout" + method = "POST" + + try: + data, status_code, headers = self._send_json_request( + method, + path_query, + headers=self.req_headers, + timeout=self.default_request_timeout, + ) + + except json.decoder.JSONDecodeError: + raise BaseError("Error in parsing JSON") + + if "error" in data: + raise AuthError(data["error"]) + + if "message" in data and data["message"] == "Your token has been revoked.": + return True + else: + return False diff --git a/tests/test_api.py b/tests/test_api.py index 9ebf8244d..97a5bf133 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -321,6 +321,11 @@ def test_delete_reply(self): # We deleted one, so there must be 1 less reply now self.assertEqual(len(self.api.get_all_replies()), number_of_replies_before - 1) + @vcr.use_cassette("data/test-logout.yml") + def test_logout(self): + r = self.api.logout() + self.assertTrue(r) + def test_request_connect_timeout(mocker): api = API("mock", "mock", "mock", "mock", proxy=False) diff --git a/tests/test_apiproxy.py b/tests/test_apiproxy.py index 8fe3a5ad8..5876cdbe8 100644 --- a/tests/test_apiproxy.py +++ b/tests/test_apiproxy.py @@ -308,6 +308,11 @@ def test_delete_reply(self): # We deleted one, so there must be 1 less reply now self.assertEqual(len(self.api.get_all_replies()), number_of_replies_before - 1) + @dastollervey_datasaver + def test_logout(self): + r = self.api.logout() + self.assertTrue(r) + def test_request_timeout(mocker): class MockedPopen: