Skip to content

Commit 8e57152

Browse files
authored
Merge pull request #901 from CitrineInformatics/as_admin-listing
as_admin listing for users and teams
2 parents cdc0cdd + e4a21a4 commit 8e57152

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

src/citrine/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.40.0"
1+
__version__ = "2.41.0"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from functools import partial
2+
from typing import Iterator
3+
4+
from citrine._rest.collection import Collection, ResourceType
5+
6+
7+
class AdminCollection(Collection[ResourceType]):
8+
"""Abstract class for representing collections of REST resources with as_admin access."""
9+
10+
def list(
11+
self, *, per_page: int = 100, as_admin: bool = False
12+
) -> Iterator[ResourceType]:
13+
"""
14+
Paginate over the elements of the collection.
15+
16+
Leaving page and per_page as default values will yield all elements in the
17+
collection, paginating over all available pages.
18+
19+
Parameters
20+
---------
21+
per_page: int, optional
22+
Max number of results to return per page. Default is 100. This parameter
23+
is used when making requests to the backend service. If the page parameter
24+
is specified it limits the maximum number of elements in the response.
25+
26+
as_admin: bool, optional
27+
Whether this request should be made as an admin (returning all teams,
28+
rather than only those to which the user belongs).
29+
30+
Returns
31+
-------
32+
Iterator[ResourceType]
33+
An iterator that can be used to loop over all the resources in this collection.
34+
Use list() to force evaluation of all results into an in-memory list.
35+
36+
"""
37+
fetcher = partial(
38+
self._fetch_page, additional_params={"as_admin": "true"} if as_admin else {}
39+
)
40+
return self._paginator.paginate(
41+
page_fetcher=fetcher,
42+
collection_builder=self._build_collection_elements,
43+
per_page=per_page,
44+
)

src/citrine/resources/team.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional, Union, List
33
from uuid import UUID
44

5-
from citrine._rest.collection import Collection
5+
from citrine._rest.admin_collection import AdminCollection
66
from citrine._rest.resource import Resource, ResourceTypeEnum
77
from citrine._serialization import properties
88
from citrine._session import Session
@@ -394,7 +394,7 @@ def table_definition_ids(self) -> TeamResourceIDs:
394394
resource_type=ResourceTypeEnum.TABLE_DEFINITION.value)
395395

396396

397-
class TeamCollection(Collection[Team]):
397+
class TeamCollection(AdminCollection[Team]):
398398
"""
399399
Represents the collection of all teams as well as the resources belonging to it.
400400

src/citrine/resources/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Resources that represent both individual and collections of users."""
22
from typing import Optional
33

4-
from citrine._rest.collection import Collection
4+
from citrine._rest.admin_collection import AdminCollection
55
from citrine._rest.resource import Resource, ResourceTypeEnum
66
from citrine._serialization import properties
77
from citrine._session import Session
@@ -59,7 +59,7 @@ def get(self):
5959
raise NotImplementedError("Get Not Implemented in Citrine Platform")
6060

6161

62-
class UserCollection(Collection[User]):
62+
class UserCollection(AdminCollection[User]):
6363
"""Represents the collection of all users."""
6464

6565
_path_template = '/users'

tests/resources/test_team.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ def test_list_teams(collection, session):
148148
assert 5 == len(teams)
149149

150150

151+
def test_list_teams_as_admin(collection, session):
152+
# Given
153+
teams_data = TeamDataFactory.create_batch(5)
154+
session.set_response({"teams": teams_data})
155+
156+
# When
157+
teams = list(collection.list(as_admin=True))
158+
159+
# Then
160+
assert 1 == session.num_calls
161+
expected_call = FakeCall(
162+
method="GET",
163+
path="/teams",
164+
params={"per_page": 100, "page": 1, "as_admin": "true"},
165+
)
166+
assert expected_call == session.last_call
167+
assert 5 == len(teams)
168+
169+
151170
def test_update_team(collection: TeamCollection, team, session):
152171
team.name = "updated name"
153172
session.set_response({'team': team.dump()})

tests/resources/test_user.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ def test_list_users(collection, session):
9898
assert len(users) == 5
9999

100100

101+
def test_list_users_as_admin(collection, session):
102+
# Given
103+
user_data = UserDataFactory.create_batch(5)
104+
session.set_response({'users': user_data})
105+
106+
# When
107+
users = list(collection.list(as_admin=True))
108+
109+
# Then
110+
assert 1 == session.num_calls
111+
expected_call = FakeCall(
112+
method='GET',
113+
path='/users',
114+
params={'per_page': 100, 'page': 1, 'as_admin': 'true'}
115+
)
116+
117+
assert expected_call == session.last_call
118+
assert len(users) == 5
119+
120+
101121
def test_get_users(collection, session):
102122
# Given
103123
uid = '151199ec-e9aa-49a1-ac8e-da722aaf74c4'

0 commit comments

Comments
 (0)