Skip to content

feat(users): add ban and unban methods #2064

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 1 commit into from
Jun 13, 2022
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
5 changes: 5 additions & 0 deletions docs/gl_objects/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ Activate/Deactivate a user::
user.activate()
user.deactivate()

Ban/Unban a user::

user.ban()
user.unban()

Follow/Unfollow a user::

user.follow()
Expand Down
8 changes: 8 additions & 0 deletions gitlab/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ class GitlabActivateError(GitlabOperationError):
pass


class GitlabBanError(GitlabOperationError):
pass


class GitlabUnbanError(GitlabOperationError):
pass


class GitlabSubscribeError(GitlabOperationError):
pass

Expand Down
42 changes: 42 additions & 0 deletions gitlab/v4/objects/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,48 @@ def activate(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
self._attrs["state"] = "active"
return server_data

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabBanError)
def ban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Ban the user.

Args:
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabBanError: If the user could not be banned

Returns:
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:
self._attrs["state"] = "banned"
return server_data

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabUnbanError)
def unban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Unban the user.

Args:
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabUnbanError: If the user could not be unbanned

Returns:
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:
self._attrs["state"] = "active"
return server_data


class UserManager(CRUDMixin, RESTManager):
_path = "/users"
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/api/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ def test_block_user(gl, user):
assert user in users


def test_ban_user(gl, user):
user.ban()
retrieved_user = gl.users.get(user.id)
assert retrieved_user.state == "banned"

user.unban()
retrieved_user = gl.users.get(user.id)
assert retrieved_user.state == "active"


def test_delete_user(gl, wait_for_sidekiq):
new_user = gl.users.create(
{
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/objects/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ def resp_activate():
yield rsps


@pytest.fixture
def resp_ban():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/users/1/ban",
json={},
content_type="application/json",
status=201,
)
yield rsps


@pytest.fixture
def resp_unban():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/users/1/unban",
json={},
content_type="application/json",
status=201,
)
yield rsps


@pytest.fixture
def resp_get_user_status():
content = {
Expand Down Expand Up @@ -216,6 +242,14 @@ def test_user_activate_deactivate(user, resp_activate):
user.deactivate()


def test_user_ban(user, resp_ban):
user.ban()


def test_user_unban(user, resp_unban):
user.unban()


def test_delete_user_identity(user, resp_delete_user_identity):
user.identityproviders.delete("test_provider")

Expand Down