Skip to content
This repository was archived by the owner on Jan 7, 2024. It is now read-only.

Commit 5c6c7e2

Browse files
committed
Add support for /users endpoint
1 parent cc64e2c commit 5c6c7e2

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

data/test-get-users.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- application/json
7+
Accept-Encoding:
8+
- gzip, deflate
9+
Authorization:
10+
- Token eyJleHAiOjE2MDIxNDYxMDIsImFsZyI6IkhTMjU2IiwiaWF0IjoxNjAyMTE3MzAyfQ.eyJpZCI6MX0.z7UGeKx0191qRhU97f0bJJDuY4y4fJi9njDGD0OXrpA
11+
Connection:
12+
- keep-alive
13+
Content-Type:
14+
- application/json
15+
User-Agent:
16+
- python-requests/2.22.0
17+
method: GET
18+
uri: http://127.0.0.1:8081/api/v1/users
19+
response:
20+
body:
21+
string: '{"users":[{"first_name":"","last_name":"","username":"journalist","uuid":"0a5db1b6-a003-4aa9-812b-53c6abbf1124"},{"first_name":"","last_name":"","username":"dellsberg","uuid":"3325fde8-a339-4847-aa3e-d2be412fbaf7"}]}
22+
23+
'
24+
headers:
25+
Content-Length:
26+
- '217'
27+
Content-Type:
28+
- application/json
29+
Date:
30+
- Thu, 08 Oct 2020 00:35:02 GMT
31+
Server:
32+
- Werkzeug/0.16.0 Python/3.5.2
33+
status:
34+
code: 200
35+
message: OK
36+
version: 1

sdclientapi/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ReplyError,
1818
Source,
1919
Submission,
20+
User,
2021
WrongUUIDError,
2122
)
2223

@@ -598,6 +599,29 @@ def get_current_user(self) -> Any:
598599

599600
return data
600601

602+
def get_users(self) -> List[User]:
603+
"""
604+
Returns a list of all the journalist and admin users registered on the
605+
server.
606+
607+
:returns: List of User objects.
608+
"""
609+
path_query = "api/v1/users"
610+
method = "GET"
611+
612+
data, status_code, headers = self._send_json_request(
613+
method, path_query, headers=self.req_headers, timeout=self.default_request_timeout,
614+
)
615+
616+
users = data["users"]
617+
result = [] # type: List[User]
618+
619+
for user in users:
620+
u = User(**user)
621+
result.append(u)
622+
623+
return result
624+
601625
def reply_source(self, source: Source, msg: str, reply_uuid: Optional[str] = None) -> Reply:
602626
"""
603627
This method is used to reply to a given source. The message should be preencrypted with the

sdclientapi/sdlocalobjects.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,26 @@ def __init__(self, **kwargs) -> None: # type: ignore
177177
if key not in kwargs:
178178
AttributeError("Missing key {}".format(key))
179179
setattr(self, key, kwargs[key])
180+
181+
182+
class User:
183+
"""
184+
This class represents a user (journalist or admin) of the Journalist
185+
Interface.
186+
"""
187+
188+
def __init__(self, **kwargs) -> None: # type: ignore
189+
self.first_name = "" # type: str
190+
self.last_name = "" # type: str
191+
self.username = "" # type: str
192+
self.uuid = "" # type: str
193+
194+
for key in [
195+
"first_name",
196+
"last_name",
197+
"username",
198+
"uuid",
199+
]:
200+
if key not in kwargs:
201+
AttributeError("Missing key {}".format(key))
202+
setattr(self, key, kwargs[key])

tests/test_api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,20 @@ def test_get_current_user(self):
224224
self.assertTrue("first_name" in user)
225225
self.assertTrue("last_name" in user)
226226

227+
@vcr.use_cassette("data/test-get-users.yml")
228+
def test_get_users(self):
229+
users = self.api.get_users()
230+
for user in users:
231+
# Assert expected fields are present
232+
assert hasattr(user, "first_name")
233+
assert hasattr(user, "last_name")
234+
# Every user has a non-empty name and UUID
235+
assert user.username
236+
assert user.uuid
237+
# The API should never return these fields
238+
assert not hasattr(user, "last_login")
239+
assert not hasattr(user, "is_admin")
240+
227241
@vcr.use_cassette("data/test-error-unencrypted-reply.yml")
228242
def test_error_unencrypted_reply(self):
229243
s = self.api.get_sources()[0]

0 commit comments

Comments
 (0)