Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 1 commit intopython-gitlab:mainfromantoineauger:feat/user-ban-unban
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsdocs/gl_objects/users.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsgitlab/exceptions.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsgitlab/v4/objects/users.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/functional/api/test_users.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/unit/objects/test_users.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp