Skip to content

Commit

Permalink
Merge pull request #2061 from bgamari/patch-1
Browse files Browse the repository at this point in the history
feat(users): add approve and reject methods to User
  • Loading branch information
nejch committed Jun 25, 2022
2 parents f555bfb + f57139d commit f9b7c7b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/gl_objects/users.rst
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
Expand Up @@ -298,6 +298,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
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
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

0 comments on commit f9b7c7b

Please sign in to comment.