Skip to content

Commit d4c2b57

Browse files
chore: correct type-hints for ban() and unban()
The functions return Optional[bool]. Add tests for their behavior
1 parent badc916 commit d4c2b57

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

gitlab/v4/objects/users.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def reject(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
326326

327327
@cli.register_custom_action("User")
328328
@exc.on_http_error(exc.GitlabBanError)
329-
def ban(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
329+
def ban(self, **kwargs: Any) -> bool:
330330
"""Ban the user.
331331
332332
Args:
@@ -340,14 +340,16 @@ def ban(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
340340
Whether the user has been banned
341341
"""
342342
path = f"/users/{self.encoded_id}/ban"
343-
server_data = self.manager.gitlab.http_post(path, **kwargs)
344-
if server_data:
343+
# NOTE: Undocumented behavior of the GitLab API is that it returns True
344+
# on success.
345+
server_data = cast(bool, self.manager.gitlab.http_post(path, **kwargs))
346+
if server_data is True:
345347
self._attrs["state"] = "banned"
346348
return server_data
347349

348350
@cli.register_custom_action("User")
349351
@exc.on_http_error(exc.GitlabUnbanError)
350-
def unban(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
352+
def unban(self, **kwargs: Any) -> bool:
351353
"""Unban the user.
352354
353355
Args:
@@ -361,8 +363,10 @@ def unban(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
361363
Whether the user has been unbanned
362364
"""
363365
path = f"/users/{self.encoded_id}/unban"
364-
server_data = self.manager.gitlab.http_post(path, **kwargs)
365-
if server_data:
366+
# NOTE: Undocumented behavior of the GitLab API is that it returns True
367+
# on success.
368+
server_data = cast(bool, self.manager.gitlab.http_post(path, **kwargs))
369+
if server_data is True:
366370
self._attrs["state"] = "active"
367371
return server_data
368372

tests/functional/api/test_users.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
https://docs.gitlab.com/ee/api/users.html
44
https://docs.gitlab.com/ee/api/users.html#delete-authentication-identity-from-user
55
"""
6+
import pytest
67
import requests
78

9+
import gitlab.exceptions
10+
811

912
def test_create_user(gl, fixture_dir):
1013
user = gl.users.create(
@@ -45,19 +48,29 @@ def test_block_user(gl, user):
4548

4649
# unblock again
4750
result = user.unblock()
48-
# Trying to unblock an already blocked user returns False
51+
# Trying to unblock an already un-blocked user returns False
4952
assert result is False
5053

5154

5255
def test_ban_user(gl, user):
53-
user.ban()
56+
result = user.ban()
57+
assert result is True
5458
retrieved_user = gl.users.get(user.id)
5559
assert retrieved_user.state == "banned"
5660

57-
user.unban()
61+
# ban an already banned user raises an exception
62+
with pytest.raises(gitlab.exceptions.GitlabBanError):
63+
user.ban()
64+
65+
result = user.unban()
66+
assert result is True
5867
retrieved_user = gl.users.get(user.id)
5968
assert retrieved_user.state == "active"
6069

70+
# unban an already un-banned user raises an exception
71+
with pytest.raises(gitlab.exceptions.GitlabUnbanError):
72+
user.unban()
73+
6174

6275
def test_delete_user(gl, wait_for_sidekiq):
6376
new_user = gl.users.create(

0 commit comments

Comments
 (0)