Skip to content

added update user method #304

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

Merged
merged 8 commits into from
Mar 25, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta

- `sdk.legalhold.get_all_events()` to search for legal hold events.

- `sdk.users.update_user()` to update an existing user in Code42.

### Fixed

- Bug where proxy settings were not being applied correctly.
Expand Down
40 changes: 40 additions & 0 deletions src/py42/services/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,43 @@ def remove_role(self, user_id, role_name):
role_name = quote(role_name)
uri = u"/api/UserRole?userId={}&roleName={}".format(user_id, role_name)
return self._connection.delete(uri)

def update_user(
self,
user_uid,
username=None,
email=None,
password=None,
first_name=None,
last_name=None,
notes=None,
quota=None,
):
"""Updates an existing user.
`REST Documentation <https://console.us.code42.com/apidocviewer/#User-put>`__

Args:
user_uid (int): A Code42 user UID.
username (str, optional): The username to which the user's username will be changed. Defaults to None.
email (str, optional): The email to which the user's email will be changed. Defaults to None.
password (str, optional): The password to which the user's password will be changed. Defaults to None.
first_name (str, optional): The first name to which the user's first name will be changed. Defaults to None.
last_name (str, optional): The last name to which the user's last name will be changed. Defaults to None.
notes (str, optional): Descriptive information about the user. Defaults to None.
quota (int, optional): The quota in bytes that limits the user's archive size. Defaults to None.

Returns:
:class:`py42.response.Py42Response`
"""

uri = u"/api/User/{}?idType=uid".format(user_uid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, but usually we let the params= argument take care of query parameters

data = {
u"username": username,
u"email": email,
u"password": password,
u"firstName": first_name,
u"lastName": last_name,
u"notes": notes,
u"quota": quota,
}
return self._connection.put(uri, json=data)
63 changes: 63 additions & 0 deletions tests/services/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def post_api_mock_response(self, mocker):
response.text = MOCK_text
return Py42Response(response)

@pytest.fixture
def put_api_mock_response(self, mocker):
response = mocker.MagicMock(spec=Response)
response.status_code = 200
response.encoding = "utf-8"
response.text = MOCK_text
return Py42Response(response)

@pytest.fixture
def post_api_mock_error_response(self, mocker):
response = mocker.MagicMock(spec=Response)
Expand Down Expand Up @@ -215,3 +223,58 @@ def side_effect(url, json):

expected = u"Cannot deactivate the user with ID 1234 as the user is involved in a legal hold matter."
assert str(err.value) == expected

def test_update_user_calls_put_with_expected_url_and_params(
self, mock_connection, put_api_mock_response
):
user_service = UserService(mock_connection)
mock_connection.post.return_value = put_api_mock_response
user_id = "TEST_USER_ID"
expected_uri = "{}/{}?idType=uid".format(USER_URI, user_id)
username = "[email protected]"
email = "[email protected]"
password = "password"
first_name = "FIRSTNAME"
last_name = "LASTNAME"
note = "Test Note"
quota = 12345
user_service.update_user(
user_id,
username=username,
email=email,
password=password,
first_name=first_name,
last_name=last_name,
notes=note,
quota=quota,
)
expected_params = {
u"username": username,
u"email": email,
u"password": password,
u"firstName": first_name,
u"lastName": last_name,
u"notes": note,
u"quota": quota,
}
mock_connection.put.assert_called_once_with(expected_uri, json=expected_params)

def test_update_user_does_not_include_empty_params(
self, mock_connection, put_api_mock_response
):
user_service = UserService(mock_connection)
mock_connection.post.return_value = put_api_mock_response
user_id = "TEST_USER_ID"
expected_uri = "{}/{}?idType=uid".format(USER_URI, user_id)
username = "[email protected]"
user_service.update_user(user_id, username=username)
expected_params = {
u"username": username,
u"email": None,
u"password": None,
u"firstName": None,
u"lastName": None,
u"notes": None,
u"quota": None,
}
mock_connection.put.assert_called_once_with(expected_uri, json=expected_params)