Skip to content

Commit afdc533

Browse files
committed
Merged changes from main, fixed __version__.py conflict.
2 parents caae732 + b184548 commit afdc533

File tree

3 files changed

+111
-24
lines changed

3 files changed

+111
-24
lines changed

src/citrine/resources/team.py

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ def list_members(self) -> List[TeamMember]:
162162
"""
163163
List all of the members in the current team.
164164
165+
Requires admin privileges.
166+
165167
Returns
166168
-------
167169
List[TeamMember]
@@ -172,37 +174,81 @@ def list_members(self) -> List[TeamMember]:
172174
version=self._api_version)["users"]
173175
return [TeamMember(user=User.build(m), team=self, actions=m["actions"]) for m in members]
174176

175-
def remove_user(self, user_id: Union[str, UUID]) -> bool:
177+
def get_member(self, user_id: Union[str, UUID, User]) -> TeamMember:
176178
"""
177-
Remove a User from a Team.
179+
Get a particular member in the current team.
180+
181+
May require admin privileges depending on which user is being requested.
178182
179183
Parameters
180184
----------
181185
user_id: str or uuid
182186
The id of the user to remove from the team
183187
188+
Returns
189+
-------
190+
TeamMember
191+
The requested team member
192+
193+
"""
194+
if isinstance(user_id, User):
195+
user_id = user_id.uid
196+
path = self._path() + format_escaped_url('/users/{user_id}', user_id=user_id)
197+
member = self.session.get_resource(path=path, version=self._api_version)["user"]
198+
return TeamMember(user=User.build(member), team=self, actions=member["actions"])
199+
200+
def me(self) -> TeamMember:
201+
"""
202+
Get the member for the current user.
203+
204+
Returns
205+
-------
206+
TeamMember
207+
The TeamMember object representing the current user
208+
209+
"""
210+
me = UserCollection(self.session).me()
211+
return self.get_member(me)
212+
213+
def remove_user(self, user_id: Union[str, UUID, User]) -> bool:
214+
"""
215+
Remove a User from a Team.
216+
217+
Requires admin privileges.
218+
219+
Parameters
220+
----------
221+
user_id: User, str or uuid
222+
The id of the user to remove from the team
223+
184224
Returns
185225
-------
186226
bool
187227
Returns ``True`` if user successfully removed
188228
189229
"""
230+
if isinstance(user_id, User):
231+
user_id = user_id.uid
190232
self.session.checked_post(self._path() + "/users/batch-remove",
191233
json={"ids": [str(user_id)]}, version=self._api_version)
192234
return True # note: only get here if checked_post doesn't raise error
193235

194-
def add_user(self, user_id: Union[str, UUID], *,
236+
def add_user(self,
237+
user_id: Union[str, UUID, User],
238+
*,
195239
actions: Optional[List[TEAM_ACTIONS]] = None) -> bool:
196240
"""
197241
Add a User to a Team.
198242
243+
Requires admin privileges.
244+
199245
If no actions are specified, adds User with ``READ`` action to the Team.
200246
201247
Use the `update_user_action` method to change a User's actions.
202248
203249
Parameters
204250
----------
205-
user_id: str or uuid
251+
user_id: User, str or uuid
206252
The id of the user to add to the team
207253
actions: list of TEAM_ACTIONS
208254
The actions to give the new user in this team
@@ -215,18 +261,24 @@ def add_user(self, user_id: Union[str, UUID], *,
215261
Returns ``True`` if user successfully added
216262
217263
"""
264+
if isinstance(user_id, User):
265+
user_id = user_id.uid
218266
if actions is None:
219267
actions = [READ]
220268
return self.update_user_action(user_id, actions=actions)
221269

222-
def update_user_action(self, user_id: Union[str, UUID], *,
270+
def update_user_action(self,
271+
user_id: Union[str, UUID, User],
272+
*,
223273
actions: List[TEAM_ACTIONS]) -> bool:
224274
"""
225275
Overwrites a User's action permissions in the Team.
226276
277+
Requires admin privileges.
278+
227279
Parameters
228280
----------
229-
user_id: str or uuid
281+
user_id: User, str or uuid
230282
The id of the user to add to the team
231283
actions: list of TEAM_ACTIONS
232284
The actions to give the new user in this team
@@ -238,21 +290,26 @@ def update_user_action(self, user_id: Union[str, UUID], *,
238290
Returns ``True`` if user successfully added
239291
240292
"""
293+
if isinstance(user_id, User):
294+
user_id = user_id.uid
241295
self.session.checked_put(self._path() + "/users", version=self._api_version,
242296
json={'id': str(user_id), "actions": actions})
243297
return True
244298

245-
def share(self, *,
299+
def share(self,
300+
*,
246301
resource: Resource,
247-
target_team_id: Union[str, UUID]) -> bool:
302+
target_team_id: Union[str, UUID, "Team"]) -> bool:
248303
"""
249304
Share a resource with another team.
250305
306+
Requires SHARE action.
307+
251308
Parameters
252309
----------
253310
resource: Resource
254311
The resource owned by this team, which will be shared
255-
target_team_id: Union[str, UUID]
312+
target_team_id: Union[str, UUID, Team]
256313
The id of the team with which to share the resource
257314
258315
Returns
@@ -261,6 +318,8 @@ def share(self, *,
261318
Returns ``True`` if resource successfully shared
262319
263320
"""
321+
if isinstance(target_team_id, Team):
322+
target_team_id = target_team_id.uid
264323
resource_access = resource.access_control_dict()
265324
payload = {
266325
"resource_type": resource_access["type"],
@@ -271,15 +330,17 @@ def share(self, *,
271330
version=self._api_version, json=payload)
272331
return True
273332

274-
def un_share(self, *, resource: Resource, target_team_id: Union[str, UUID]) -> bool:
333+
def un_share(self, *, resource: Resource, target_team_id: Union[str, UUID, "Team"]) -> bool:
275334
"""
276335
Revoke the share of a particular resource to a secondary team.
277336
337+
Requires SHARE action.
338+
278339
Parameters
279340
----------
280341
resource: Resource
281342
The resource owned by this team, which will be un-shared
282-
target_team_id: Union[str, UUID]
343+
target_team_id: Union[str, UUID, Team]
283344
The id of the team which should not have access to the resource
284345
285346
Returns
@@ -288,6 +349,8 @@ def un_share(self, *, resource: Resource, target_team_id: Union[str, UUID]) -> b
288349
Returns ``True`` if resource successfully un-shared
289350
290351
"""
352+
if isinstance(target_team_id, Team):
353+
target_team_id = target_team_id.uid
291354
resource_type = resource.access_control_dict()["type"]
292355
resource_id = resource.access_control_dict()["id"]
293356
self.session.checked_delete(

tests/resources/test_team.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ def team(session) -> Team:
3636
return team
3737

3838

39+
@pytest.fixture
40+
def other_team(session) -> Team:
41+
team = Team(
42+
name='Test Team',
43+
session=session
44+
)
45+
team.uid = uuid.uuid4()
46+
return team
47+
48+
3949
@pytest.fixture
4050
def collection(session) -> TeamCollection:
4151
return TeamCollection(session)
@@ -162,13 +172,31 @@ def test_list_members(team, session):
162172
assert isinstance(members[0], TeamMember)
163173

164174

175+
def test_me(team, session):
176+
# Given
177+
user = UserDataFactory()
178+
member = user.copy()
179+
member["actions"] = [READ]
180+
member.pop("position")
181+
session.set_responses({**user}, {'user': member})
182+
183+
# When
184+
member = team.me()
185+
186+
# Then
187+
assert 2 == session.num_calls
188+
member_call = FakeCall(method='GET', path='/teams/{}/users/{}'.format(team.uid, user["id"]))
189+
assert member_call == session.last_call
190+
assert isinstance(member, TeamMember)
191+
192+
165193
def test_update_user_actions(team, session):
166194
# Given
167195
user = UserDataFactory()
168196
session.set_response({'id': user['id'], 'actions': ['READ']})
169197

170198
# When
171-
update_user_role_response = team.update_user_action(user_id=user["id"], actions=[WRITE, SHARE])
199+
update_user_role_response = team.update_user_action(user_id=User.build(user), actions=[WRITE, SHARE])
172200

173201
# Then
174202
assert 1 == session.num_calls
@@ -184,7 +212,7 @@ def test_add_user(team, session):
184212
session.set_response({'id': user["id"], 'actions': ['READ']})
185213

186214
# When
187-
add_user_response = team.add_user(user["id"])
215+
add_user_response = team.add_user(User.build(user))
188216

189217
# Then
190218
assert 1 == session.num_calls
@@ -220,7 +248,7 @@ def test_remove_user(team, session):
220248
session.set_response({'ids': [user["id"]]})
221249

222250
# When
223-
remove_user_response = team.remove_user(user["id"])
251+
remove_user_response = team.remove_user(User.build(user))
224252

225253
# Then
226254
assert 1 == session.num_calls
@@ -233,14 +261,13 @@ def test_remove_user(team, session):
233261
assert remove_user_response is True
234262

235263

236-
def test_share(team, session):
264+
def test_share(team, other_team, session):
237265
# Given
238-
target_team_id = uuid.uuid4()
239266
dataset = Dataset(name="foo", summary="", description="")
240267
dataset.uid = str(uuid.uuid4())
241268

242269
# When
243-
share_response = team.share(resource=dataset, target_team_id=target_team_id)
270+
share_response = team.share(resource=dataset, target_team_id=other_team)
244271

245272
# Then
246273
assert 1 == session.num_calls
@@ -250,29 +277,28 @@ def test_share(team, session):
250277
json={
251278
"resource_type": "DATASET",
252279
"resource_id": str(dataset.uid),
253-
"target_team_id": str(target_team_id)
280+
"target_team_id": str(other_team.uid)
254281
}
255282
)
256283
assert expect_call == session.last_call
257284
assert share_response is True
258285

259286

260-
def test_un_share(team, session):
287+
def test_un_share(team, other_team, session):
261288
# Given
262-
target_team_id = uuid.uuid4()
263289
dataset = Dataset(name="foo", summary="", description="")
264290
dataset.uid = str(uuid.uuid4())
265291

266292
# When
267-
share_response = team.un_share(resource=dataset, target_team_id=target_team_id)
293+
share_response = team.un_share(resource=dataset, target_team_id=other_team)
268294

269295
# Then
270296
assert 1 == session.num_calls
271297
expect_call = FakeCall(
272298
method="DELETE",
273299
path="/teams/{}/shared-resources/{}/{}".format(team.uid, "DATASET", str(dataset.uid)),
274300
json={
275-
"target_team_id": str(target_team_id)
301+
"target_team_id": str(other_team.uid)
276302
}
277303
)
278304
assert expect_call == session.last_call

tests/resources/test_user.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ def test_delete_user(collection, session):
127127
def test_get_me(collection, session):
128128
# Given
129129
user = UserDataFactory()
130-
user["id"] = user["id"]
131-
del user["id"]
132130
session.set_response(user)
133131

134132
# When

0 commit comments

Comments
 (0)