diff options
author | John L. Villalovos <john@sodarock.com> | 2022-07-23 10:17:12 -0700 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2022-07-23 10:17:12 -0700 |
commit | 00b3e263e06c94cdfbc47380e91df07dd11fa8a3 (patch) | |
tree | dedc3842330ec0658f4221ccc95c2963f7c0214f | |
parent | 4a8b4166dc238fde852cb85cc3d48f5bcb65191f (diff) | |
download | gitlab-jlvillal/find_non_dict.tar.gz |
chore: correct type-hints for `ban()` and `unban()`jlvillal/find_non_dict
The functions return Optional[bool].
Add tests for their behavior
-rw-r--r-- | gitlab/v4/objects/users.py | 16 | ||||
-rw-r--r-- | tests/functional/api/test_users.py | 19 |
2 files changed, 26 insertions, 9 deletions
diff --git a/gitlab/v4/objects/users.py b/gitlab/v4/objects/users.py index 08c7738..fada649 100644 --- a/gitlab/v4/objects/users.py +++ b/gitlab/v4/objects/users.py @@ -326,7 +326,7 @@ class User(SaveMixin, ObjectDeleteMixin, RESTObject): @cli.register_custom_action("User") @exc.on_http_error(exc.GitlabBanError) - def ban(self, **kwargs: Any) -> gitlab.client.HttpResponseType: + def ban(self, **kwargs: Any) -> bool: """Ban the user. Args: @@ -340,14 +340,16 @@ class User(SaveMixin, ObjectDeleteMixin, RESTObject): Whether the user has been banned """ path = f"/users/{self.encoded_id}/ban" - server_data = self.manager.gitlab.http_post(path, **kwargs) - if server_data: + # NOTE: Undocumented behavior of the GitLab API is that it returns True + # on success. + server_data = cast(bool, self.manager.gitlab.http_post(path, **kwargs)) + if server_data is True: self._attrs["state"] = "banned" return server_data @cli.register_custom_action("User") @exc.on_http_error(exc.GitlabUnbanError) - def unban(self, **kwargs: Any) -> gitlab.client.HttpResponseType: + def unban(self, **kwargs: Any) -> bool: """Unban the user. Args: @@ -361,8 +363,10 @@ class User(SaveMixin, ObjectDeleteMixin, RESTObject): Whether the user has been unbanned """ path = f"/users/{self.encoded_id}/unban" - server_data = self.manager.gitlab.http_post(path, **kwargs) - if server_data: + # NOTE: Undocumented behavior of the GitLab API is that it returns True + # on success. + server_data = cast(bool, self.manager.gitlab.http_post(path, **kwargs)) + if server_data is True: self._attrs["state"] = "active" return server_data diff --git a/tests/functional/api/test_users.py b/tests/functional/api/test_users.py index a099e8f..ba6f60c 100644 --- a/tests/functional/api/test_users.py +++ b/tests/functional/api/test_users.py @@ -3,8 +3,11 @@ GitLab API: https://docs.gitlab.com/ee/api/users.html https://docs.gitlab.com/ee/api/users.html#delete-authentication-identity-from-user """ +import pytest import requests +import gitlab.exceptions + def test_create_user(gl, fixture_dir): user = gl.users.create( @@ -45,19 +48,29 @@ def test_block_user(gl, user): # unblock again result = user.unblock() - # Trying to unblock an already blocked user returns False + # Trying to unblock an already un-blocked user returns False assert result is False def test_ban_user(gl, user): - user.ban() + result = user.ban() + assert result is True retrieved_user = gl.users.get(user.id) assert retrieved_user.state == "banned" - user.unban() + # ban an already banned user raises an exception + with pytest.raises(gitlab.exceptions.GitlabBanError): + user.ban() + + result = user.unban() + assert result is True retrieved_user = gl.users.get(user.id) assert retrieved_user.state == "active" + # unban an already un-banned user raises an exception + with pytest.raises(gitlab.exceptions.GitlabUnbanError): + user.unban() + def test_delete_user(gl, wait_for_sidekiq): new_user = gl.users.create( |