Skip to content

Commit 6dc7ecc

Browse files
authored
feat: Support updating all fields of collaboration (#829)
Closes: SDK-3253
1 parent ed03eb8 commit 6dc7ecc

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-9
lines changed

boxsdk/object/base_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def update_info(
9191
The updated information about this object.
9292
Must be JSON serializable.
9393
Update the object attributes in data.keys(). The semantics of the
94-
values depends on the the type and attributes of the object being
94+
values depends on the type and attributes of the object being
9595
updated. For details on particular semantics, refer to the Box
9696
developer API documentation <https://developer.box.com/>.
9797
:param params:

boxsdk/object/collaboration.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ class Collaboration(BaseObject):
3434
def update_info(
3535
self,
3636
*,
37+
data: dict = None,
3738
role: Optional[CollaborationRole] = None,
3839
status: Optional[CollaborationStatus] = None,
3940
**kwargs: Any
4041
) -> 'BaseObject':
4142
"""Edit an existing collaboration on Box
42-
43+
:param data:
44+
The updated information about this object.
4345
:param role:
4446
The new role for this collaboration or None to leave unchanged
4547
:param status:
@@ -51,12 +53,13 @@ def update_info(
5153
:class:`BoxAPIException` if current user doesn't have permissions to edit the collaboration.
5254
"""
5355
# pylint:disable=arguments-differ
54-
data = {}
56+
if data is None:
57+
data = {}
5558
if role:
5659
data['role'] = role
5760
if status:
5861
data['status'] = status
59-
if role == CollaborationRole.OWNER:
62+
if data.get('role', None) == CollaborationRole.OWNER:
6063
return super().update_info(data=data, expect_json_response=False, **kwargs)
6164

6265
return super().update_info(data=data, **kwargs)

docs/usage/collaboration.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,17 @@ print(f'{collaborator.name} {has_accepted} accepted the collaboration to folder
7474
Edit a Collaboration
7575
--------------------
7676

77-
A collaboration can be edited by calling [`collaboration.update_info(*, role=None, status=None, **kwargs)`][update_info]. This method
78-
returns an updated [`Collaboration`][collaboration_class] object, leaving the original unmodified.
77+
A collaboration can be edited by calling [`collaboration.update_info(*, data=None, role=None, status=None, **kwargs)`][update_info].
78+
Note that `role` fields is always required when updating a collaboration. This method returns an updated
79+
[`Collaboration`][collaboration_class] object, leaving the original unmodified.
7980

8081
<!-- sample put_collaborations_id -->
8182
```python
82-
from boxsdk.object.collaboration import CollaborationRole, CollaborationStatus
83+
from boxsdk.object.collaboration import CollaborationRole
8384

85+
collaboration_update = {'role': CollaborationRole.EDITOR, 'can_view_path': False}
8486
collaboration = client.collaboration(collab_id='12345')
85-
updated_collaboration = collaboration.update_info(role=CollaborationRole.EDITOR)
87+
updated_collaboration = collaboration.update_info(data=collaboration_update)
8688
```
8789

8890
[update_info]: https://box-python-sdk.readthedocs.io/en/latest/boxsdk.object.html#boxsdk.object.collaboration.Collaboration.update_info
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from datetime import datetime
2+
3+
import pytest
4+
5+
from boxsdk.object.collaboration import CollaborationRole
6+
7+
from test.integration_new.context_managers.box_test_file import BoxTestFile
8+
from test.integration_new.context_managers.box_test_folder import BoxTestFolder
9+
10+
FILE_TESTS_DIRECTORY_NAME = 'collaboration-integration-tests'
11+
12+
13+
@pytest.fixture(scope="module", autouse=True)
14+
def parent_folder():
15+
with BoxTestFolder(name=f'{FILE_TESTS_DIRECTORY_NAME} {datetime.now()}') as folder:
16+
yield folder
17+
18+
19+
def test_collaboration(parent_folder, small_file_path, other_user,):
20+
with BoxTestFile(parent_folder=parent_folder, file_path=small_file_path) as test_file:
21+
collaboration = test_file.collaborate(other_user, CollaborationRole.VIEWER)
22+
try:
23+
assert collaboration.item.id == test_file.id
24+
assert collaboration.accessible_by.id == other_user.id
25+
assert collaboration.role == CollaborationRole.VIEWER
26+
27+
updated_expiration_date = '2088-01-01T00:00:00-08:00'
28+
collaboration_update = {'role': CollaborationRole.EDITOR, 'expires_at': updated_expiration_date}
29+
updated_collaboration = collaboration.update_info(data=collaboration_update)
30+
31+
assert updated_collaboration.role == CollaborationRole.EDITOR
32+
assert updated_collaboration.expires_at != collaboration.expires_at
33+
assert updated_collaboration.expires_at == updated_expiration_date
34+
finally:
35+
collaboration.delete()

test/unit/object/test_collaboration.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,40 @@ def test_update_info_returns_the_correct_response(
3232
assert update_response.object_id == test_collaboration.object_id
3333

3434

35+
@pytest.mark.parametrize('data', [
36+
{},
37+
{'role': CollaborationRole.EDITOR, 'status': CollaborationStatus.REJECTED},
38+
{'role': CollaborationRole.EDITOR, 'status': CollaborationStatus.ACCEPTED},
39+
{'role': CollaborationRole.EDITOR, 'expires_at': '2025-08-29T23:59:00-07:00'},
40+
{'role': CollaborationRole.EDITOR, 'can_view_path': True},
41+
])
42+
def test_update_info_returns_the_correct_response_with_data_param(
43+
test_collaboration,
44+
mock_box_session,
45+
mock_collab_response,
46+
data):
47+
# pylint:disable=protected-access
48+
expected_url = test_collaboration.get_url()
49+
mock_box_session.put.return_value = mock_collab_response
50+
update_response = test_collaboration.update_info(data=data)
51+
mock_box_session.put.assert_called_once_with(
52+
expected_url,
53+
data=json.dumps(data),
54+
headers=None,
55+
params=None,
56+
)
57+
assert isinstance(update_response, test_collaboration.__class__)
58+
assert update_response.object_id == test_collaboration.object_id
59+
60+
3561
def test_update_info_returns_204(
3662
test_collaboration,
3763
mock_box_session):
3864
# pylint:disable=protected-access
3965
data = {'role': CollaborationRole.OWNER, 'status': CollaborationStatus.ACCEPTED}
4066
expected_url = test_collaboration.get_url()
4167
mock_box_session.put.return_value.ok = True
42-
is_success = test_collaboration.update_info(**data)
68+
is_success = test_collaboration.update_info(data=data)
4369
mock_box_session.put.assert_called_once_with(
4470
expected_url,
4571
data=json.dumps(data),

0 commit comments

Comments
 (0)