Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support updating all fields of collaboration #829

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion boxsdk/object/base_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def update_info(
The updated information about this object.
Must be JSON serializable.
Update the object attributes in data.keys(). The semantics of the
values depends on the the type and attributes of the object being
values depends on the type and attributes of the object being
updated. For details on particular semantics, refer to the Box
developer API documentation <https://developer.box.com/>.
:param params:
Expand Down
9 changes: 6 additions & 3 deletions boxsdk/object/collaboration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class Collaboration(BaseObject):
def update_info(
self,
*,
data: dict = None,
role: Optional[CollaborationRole] = None,
status: Optional[CollaborationStatus] = None,
**kwargs: Any
) -> 'BaseObject':
"""Edit an existing collaboration on Box

:param data:
The updated information about this object.
:param role:
The new role for this collaboration or None to leave unchanged
:param status:
Expand All @@ -51,12 +53,13 @@ def update_info(
:class:`BoxAPIException` if current user doesn't have permissions to edit the collaboration.
"""
# pylint:disable=arguments-differ
data = {}
if data is None:
data = {}
if role:
data['role'] = role
if status:
data['status'] = status
if role == CollaborationRole.OWNER:
if data.get('role', None) == CollaborationRole.OWNER:
return super().update_info(data=data, expect_json_response=False, **kwargs)

return super().update_info(data=data, **kwargs)
Expand Down
10 changes: 6 additions & 4 deletions docs/usage/collaboration.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ print(f'{collaborator.name} {has_accepted} accepted the collaboration to folder
Edit a Collaboration
--------------------

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

<!-- sample put_collaborations_id -->
```python
from boxsdk.object.collaboration import CollaborationRole, CollaborationStatus
from boxsdk.object.collaboration import CollaborationRole

collaboration_update = {'role': CollaborationRole.EDITOR, 'can_view_path': False}
collaboration = client.collaboration(collab_id='12345')
updated_collaboration = collaboration.update_info(role=CollaborationRole.EDITOR)
updated_collaboration = collaboration.update_info(data=collaboration_update)
```

[update_info]: https://box-python-sdk.readthedocs.io/en/latest/boxsdk.object.html#boxsdk.object.collaboration.Collaboration.update_info
Expand Down
35 changes: 35 additions & 0 deletions test/integration_new/object/collaboration_itest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from datetime import datetime

import pytest

from boxsdk.object.collaboration import CollaborationRole

from test.integration_new.context_managers.box_test_file import BoxTestFile
from test.integration_new.context_managers.box_test_folder import BoxTestFolder

FILE_TESTS_DIRECTORY_NAME = 'collaboration-integration-tests'


@pytest.fixture(scope="module", autouse=True)
def parent_folder():
with BoxTestFolder(name=f'{FILE_TESTS_DIRECTORY_NAME} {datetime.now()}') as folder:
yield folder


def test_collaboration(parent_folder, small_file_path, other_user,):
with BoxTestFile(parent_folder=parent_folder, file_path=small_file_path) as test_file:
collaboration = test_file.collaborate(other_user, CollaborationRole.VIEWER)
try:
assert collaboration.item.id == test_file.id
assert collaboration.accessible_by.id == other_user.id
assert collaboration.role == CollaborationRole.VIEWER

updated_expiration_date = '2088-01-01T00:00:00-08:00'
collaboration_update = {'role': CollaborationRole.EDITOR, 'expires_at': updated_expiration_date}
updated_collaboration = collaboration.update_info(data=collaboration_update)

assert updated_collaboration.role == CollaborationRole.EDITOR
assert updated_collaboration.expires_at != collaboration.expires_at
assert updated_collaboration.expires_at == updated_expiration_date
finally:
collaboration.delete()
28 changes: 27 additions & 1 deletion test/unit/object/test_collaboration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,40 @@ def test_update_info_returns_the_correct_response(
assert update_response.object_id == test_collaboration.object_id


@pytest.mark.parametrize('data', [
{},
{'role': CollaborationRole.EDITOR, 'status': CollaborationStatus.REJECTED},
{'role': CollaborationRole.EDITOR, 'status': CollaborationStatus.ACCEPTED},
{'role': CollaborationRole.EDITOR, 'expires_at': '2025-08-29T23:59:00-07:00'},
{'role': CollaborationRole.EDITOR, 'can_view_path': True},
])
def test_update_info_returns_the_correct_response_with_data_param(
test_collaboration,
mock_box_session,
mock_collab_response,
data):
# pylint:disable=protected-access
expected_url = test_collaboration.get_url()
mock_box_session.put.return_value = mock_collab_response
update_response = test_collaboration.update_info(data=data)
mock_box_session.put.assert_called_once_with(
expected_url,
data=json.dumps(data),
headers=None,
params=None,
)
assert isinstance(update_response, test_collaboration.__class__)
assert update_response.object_id == test_collaboration.object_id


def test_update_info_returns_204(
test_collaboration,
mock_box_session):
# pylint:disable=protected-access
data = {'role': CollaborationRole.OWNER, 'status': CollaborationStatus.ACCEPTED}
expected_url = test_collaboration.get_url()
mock_box_session.put.return_value.ok = True
is_success = test_collaboration.update_info(**data)
is_success = test_collaboration.update_info(data=data)
mock_box_session.put.assert_called_once_with(
expected_url,
data=json.dumps(data),
Expand Down