Skip to content

Commit

Permalink
Merge pull request #88 from freedomofpress/logout
Browse files Browse the repository at this point in the history
Supports logout endpoint (fixes #51)
  • Loading branch information
kushaldas authored Jun 11, 2019
2 parents 5309592 + 880635d commit 73c164a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions data/test-logout.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions data/test_logout.json
Original file line number Diff line number Diff line change
@@ -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\"}}"}
26 changes: 26 additions & 0 deletions sdclientapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_zlogout(self):
r = self.api.logout()
self.assertTrue(r)


def test_request_connect_timeout(mocker):
api = API("mock", "mock", "mock", "mock", proxy=False)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_apiproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 73c164a

Please sign in to comment.