Skip to content

Commit ca98d88

Browse files
authored
Merge pull request #2064 from antoineauger/feat/user-ban-unban
feat(users): add ban and unban methods
2 parents 384031c + 0d44b11 commit ca98d88

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

docs/gl_objects/users.rst

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ Activate/Deactivate a user::
7272
user.activate()
7373
user.deactivate()
7474

75+
Ban/Unban a user::
76+
77+
user.ban()
78+
user.unban()
79+
7580
Follow/Unfollow a user::
7681

7782
user.follow()

gitlab/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ class GitlabActivateError(GitlabOperationError):
186186
pass
187187

188188

189+
class GitlabBanError(GitlabOperationError):
190+
pass
191+
192+
193+
class GitlabUnbanError(GitlabOperationError):
194+
pass
195+
196+
189197
class GitlabSubscribeError(GitlabOperationError):
190198
pass
191199

gitlab/v4/objects/users.py

+42
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,48 @@ def activate(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
283283
self._attrs["state"] = "active"
284284
return server_data
285285

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+
286328

287329
class UserManager(CRUDMixin, RESTManager):
288330
_path = "/users"

tests/functional/api/test_users.py

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def test_block_user(gl, user):
3737
assert user in users
3838

3939

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+
4050
def test_delete_user(gl, wait_for_sidekiq):
4151
new_user = gl.users.create(
4252
{

tests/unit/objects/test_users.py

+34
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ def resp_activate():
8080
yield rsps
8181

8282

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+
83109
@pytest.fixture
84110
def resp_get_user_status():
85111
content = {
@@ -216,6 +242,14 @@ def test_user_activate_deactivate(user, resp_activate):
216242
user.deactivate()
217243

218244

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+
219253
def test_delete_user_identity(user, resp_delete_user_identity):
220254
user.identityproviders.delete("test_provider")
221255

0 commit comments

Comments
 (0)