Skip to content

feat(users): add approve and reject methods to User #2061

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 25, 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
7 changes: 7 additions & 0 deletions docs/gl_objects/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ List a user's starred projects

user.starred_projects.list()

If the GitLab instance has new user account approval enabled some users may
have ``user.state == 'blocked_pending_approval'``. Administrators can approve
and reject such users::

user.approve()
user.reject()

User custom attributes
======================

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


class GitlabUserApproveError(GitlabOperationError):
pass


class GitlabUserRejectError(GitlabOperationError):
pass


# For an explanation of how these type-hints work see:
# https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
#
Expand Down
36 changes: 36 additions & 0 deletions gitlab/v4/objects/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,42 @@ 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.GitlabUserApproveError)
def approve(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Approve a user creation request.

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

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabUserApproveError: If the user could not be activated

Returns:
The new object data (*not* a RESTObject)
"""
path = f"/users/{self.encoded_id}/approve"
return self.manager.gitlab.http_post(path, **kwargs)

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabUserRejectError)
def reject(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Reject a user creation request.

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

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabUserRejectError: If the user could not be rejected

Returns:
The new object data (*not* a RESTObject)
"""
path = f"/users/{self.encoded_id}/reject"
return self.manager.gitlab.http_post(path, **kwargs)

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabBanError)
def ban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
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_approve():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url="http://localhost/api/v4/users/1/approve",
json={"message": "Success"},
content_type="application/json",
status=201,
)
yield rsps


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


@pytest.fixture
def resp_ban():
with responses.RequestsMock() as rsps:
Expand Down Expand Up @@ -242,6 +268,14 @@ def test_user_activate_deactivate(user, resp_activate):
user.deactivate()


def test_user_approve_(user, resp_approve):
user.approve()


def test_user_approve_reject(user, resp_reject):
user.reject()


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

Expand Down