| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 384031c commit 0d44b11
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,6 +72,11 @@ Activate/Deactivate a user:: | |||
| 72 | 72 | user.activate() | |
| 73 | 73 | user.deactivate() | |
| 74 | 74 | ||
| 75 | + Ban/Unban a user:: | ||
| 76 | + | ||
| 77 | + user.ban() | ||
| 78 | + user.unban() | ||
| 79 | + | ||
| 75 | 80 | Follow/Unfollow a user:: | |
| 76 | 81 | ||
| 77 | 82 | user.follow() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -186,6 +186,14 @@ class GitlabActivateError(GitlabOperationError): | |||
| 186 | 186 | pass | |
| 187 | 187 | ||
| 188 | 188 | ||
| 189 | + class GitlabBanError(GitlabOperationError): | ||
| 190 | + pass | ||
| 191 | + | ||
| 192 | + | ||
| 193 | + class GitlabUnbanError(GitlabOperationError): | ||
| 194 | + pass | ||
| 195 | + | ||
| 196 | + | ||
| 189 | 197 | class GitlabSubscribeError(GitlabOperationError): | |
| 190 | 198 | pass | |
| 191 | 199 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -283,6 +283,48 @@ def activate(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]: | |||
| 283 | 283 | self._attrs["state"] = "active" | |
| 284 | 284 | return server_data | |
| 285 | 285 | ||
| 286 | + @cli.register_custom_action("User") | ||
| 287 | + @exc.on_http_error(exc.GitlabBanError) | ||
| 288 | + def ban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]: | ||
| 289 | + """Ban the user. | ||
| 290 | + | ||
| 291 | + Args: | ||
| 292 | + **kwargs: Extra options to send to the server (e.g. sudo) | ||
| 293 | + | ||
| 294 | + Raises: | ||
| 295 | + GitlabAuthenticationError: If authentication is not correct | ||
| 296 | + GitlabBanError: If the user could not be banned | ||
| 297 | + | ||
| 298 | + Returns: | ||
| 299 | + Whether the user has been banned | ||
| 300 | + """ | ||
| 301 | + path = f"/users/{self.encoded_id}/ban" | ||
| 302 | + server_data = self.manager.gitlab.http_post(path, **kwargs) | ||
| 303 | + if server_data: | ||
| 304 | + self._attrs["state"] = "banned" | ||
| 305 | + return server_data | ||
| 306 | + | ||
| 307 | + @cli.register_custom_action("User") | ||
| 308 | + @exc.on_http_error(exc.GitlabUnbanError) | ||
| 309 | + def unban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]: | ||
| 310 | + """Unban the user. | ||
| 311 | + | ||
| 312 | + Args: | ||
| 313 | + **kwargs: Extra options to send to the server (e.g. sudo) | ||
| 314 | + | ||
| 315 | + Raises: | ||
| 316 | + GitlabAuthenticationError: If authentication is not correct | ||
| 317 | + GitlabUnbanError: If the user could not be unbanned | ||
| 318 | + | ||
| 319 | + Returns: | ||
| 320 | + Whether the user has been unbanned | ||
| 321 | + """ | ||
| 322 | + path = f"/users/{self.encoded_id}/unban" | ||
| 323 | + server_data = self.manager.gitlab.http_post(path, **kwargs) | ||
| 324 | + if server_data: | ||
| 325 | + self._attrs["state"] = "active" | ||
| 326 | + return server_data | ||
| 327 | + | ||
| 286 | 328 | ||
| 287 | 329 | class UserManager(CRUDMixin, RESTManager): | |
| 288 | 330 | _path = "/users" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,16 @@ def test_block_user(gl, user): | |||
| 37 | 37 | assert user in users | |
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | + def test_ban_user(gl, user): | ||
| 41 | + user.ban() | ||
| 42 | + retrieved_user = gl.users.get(user.id) | ||
| 43 | + assert retrieved_user.state == "banned" | ||
| 44 | + | ||
| 45 | + user.unban() | ||
| 46 | + retrieved_user = gl.users.get(user.id) | ||
| 47 | + assert retrieved_user.state == "active" | ||
| 48 | + | ||
| 49 | + | ||
| 40 | 50 | def test_delete_user(gl, wait_for_sidekiq): | |
| 41 | 51 | new_user = gl.users.create( | |
| 42 | 52 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,6 +80,32 @@ def resp_activate(): | |||
| 80 | 80 | yield rsps | |
| 81 | 81 | ||
| 82 | 82 | ||
| 83 | + @pytest.fixture | ||
| 84 | + def resp_ban(): | ||
| 85 | + with responses.RequestsMock() as rsps: | ||
| 86 | + rsps.add( | ||
| 87 | + method=responses.POST, | ||
| 88 | + url="http://localhost/api/v4/users/1/ban", | ||
| 89 | + json={}, | ||
| 90 | + content_type="application/json", | ||
| 91 | + status=201, | ||
| 92 | + ) | ||
| 93 | + yield rsps | ||
| 94 | + | ||
| 95 | + | ||
| 96 | + @pytest.fixture | ||
| 97 | + def resp_unban(): | ||
| 98 | + with responses.RequestsMock() as rsps: | ||
| 99 | + rsps.add( | ||
| 100 | + method=responses.POST, | ||
| 101 | + url="http://localhost/api/v4/users/1/unban", | ||
| 102 | + json={}, | ||
| 103 | + content_type="application/json", | ||
| 104 | + status=201, | ||
| 105 | + ) | ||
| 106 | + yield rsps | ||
| 107 | + | ||
| 108 | + | ||
| 83 | 109 | @pytest.fixture | |
| 84 | 110 | def resp_get_user_status(): | |
| 85 | 111 | content = { | |
@@ -216,6 +242,14 @@ def test_user_activate_deactivate(user, resp_activate): | |||
| 216 | 242 | user.deactivate() | |
| 217 | 243 | ||
| 218 | 244 | ||
| 245 | + def test_user_ban(user, resp_ban): | ||
| 246 | + user.ban() | ||
| 247 | + | ||
| 248 | + | ||
| 249 | + def test_user_unban(user, resp_unban): | ||
| 250 | + user.unban() | ||
| 251 | + | ||
| 252 | + | ||
| 219 | 253 | def test_delete_user_identity(user, resp_delete_user_identity): | |
| 220 | 254 | user.identityproviders.delete("test_provider") | |
| 221 | 255 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments