Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc44f50d

Browse files
committed
feat(users): add ban and unban methods
1 parent384031c commitc44f50d

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

‎docs/gl_objects/users.rst

Lines changed: 5 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ class GitlabActivateError(GitlabOperationError):
186186
pass
187187

188188

189+
classGitlabBanError(GitlabOperationError):
190+
pass
191+
192+
193+
classGitlabUnbanError(GitlabOperationError):
194+
pass
195+
196+
189197
classGitlabSubscribeError(GitlabOperationError):
190198
pass
191199

‎gitlab/v4/objects/users.py

Lines changed: 42 additions & 0 deletions
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
returnserver_data
285285

286+
@cli.register_custom_action("User")
287+
@exc.on_http_error(exc.GitlabBanError)
288+
defban(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+
ifserver_data:
304+
self._attrs["state"]="banned"
305+
returnserver_data
306+
307+
@cli.register_custom_action("User")
308+
@exc.on_http_error(exc.GitlabUnbanError)
309+
defunban(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+
ifserver_data:
325+
self._attrs["state"]="active"
326+
returnserver_data
327+
286328

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

‎tests/functional/api/test_users.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def test_block_user(gl, user):
3737
assertuserinusers
3838

3939

40+
deftest_ban_user(gl,user):
41+
user.ban()
42+
retrieved_user=gl.users.get(id=user.id)
43+
assertretrieved_user.state=="banned"
44+
45+
user.unban()
46+
retrieved_user=gl.users.get(id=user.id)
47+
assertretrieved_user.state=="active"
48+
49+
4050
deftest_delete_user(gl,wait_for_sidekiq):
4151
new_user=gl.users.create(
4252
{

‎tests/unit/objects/test_users.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ def resp_activate():
8080
yieldrsps
8181

8282

83+
@pytest.fixture
84+
defresp_ban():
85+
withresponses.RequestsMock()asrsps:
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+
yieldrsps
94+
95+
96+
@pytest.fixture
97+
defresp_unban():
98+
withresponses.RequestsMock()asrsps:
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+
yieldrsps
107+
108+
83109
@pytest.fixture
84110
defresp_get_user_status():
85111
content= {
@@ -216,6 +242,14 @@ def test_user_activate_deactivate(user, resp_activate):
216242
user.deactivate()
217243

218244

245+
deftest_user_ban(user,resp_ban):
246+
user.ban()
247+
248+
249+
deftest_user_unban(user,resp_unban):
250+
user.unban()
251+
252+
219253
deftest_delete_user_identity(user,resp_delete_user_identity):
220254
user.identityproviders.delete("test_provider")
221255

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp