From f57139d8f1dafa6eb19d0d954b3634c19de6413c Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Thu, 9 Jun 2022 11:11:35 -0400 Subject: [PATCH] feat(users): add approve and reject methods to User As requested in #1604. Co-authored-by: John Villalovos --- docs/gl_objects/users.rst | 7 +++++++ gitlab/exceptions.py | 8 +++++++ gitlab/v4/objects/users.py | 36 ++++++++++++++++++++++++++++++++ tests/unit/objects/test_users.py | 34 ++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) diff --git a/docs/gl_objects/users.rst b/docs/gl_objects/users.rst index 53b00e277..996459d69 100644 --- a/docs/gl_objects/users.rst +++ b/docs/gl_objects/users.rst @@ -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 ====================== diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 602a45276..1f7999ed1 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -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 # diff --git a/gitlab/v4/objects/users.py b/gitlab/v4/objects/users.py index 9e76bd5d0..bef9fbec0 100644 --- a/gitlab/v4/objects/users.py +++ b/gitlab/v4/objects/users.py @@ -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]: diff --git a/tests/unit/objects/test_users.py b/tests/unit/objects/test_users.py index 1c8959375..392cc3eca 100644 --- a/tests/unit/objects/test_users.py +++ b/tests/unit/objects/test_users.py @@ -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: @@ -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()