Skip to content

Commit e2a6843

Browse files
authored
Fix #1038 by adding admin.auth.policy.* API support (#1039)
* Fix #1038 by adding admin.auth.policy.* API support * Update test comment
1 parent 3fcdbe1 commit e2a6843

6 files changed

Lines changed: 277 additions & 2 deletions

File tree

integration_tests/env_variable_names.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
SLACK_SDK_TEST_GRID_IDP_USERGROUP_ID_2 = "SLACK_SDK_TEST_GRID_IDP_USERGROUP_ID_2"
2828
SLACK_SDK_TEST_GRID_TEAM_ID = "SLACK_SDK_TEST_GRID_TEAM_ID"
2929
SLACK_SDK_TEST_GRID_USER_ID = "SLACK_SDK_TEST_GRID_USER_ID"
30+
# The following user must be a full member, who is not a primary owner
31+
SLACK_SDK_TEST_GRID_USER_ID_ADMIN_AUTH = "SLACK_SDK_TEST_GRID_USER_ID_ADMIN_AUTH"
3032

3133
# Webhook
3234
SLACK_SDK_TEST_INCOMING_WEBHOOK_URL = "SLACK_SDK_TEST_INCOMING_WEBHOOK_URL"
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import logging
2+
import os
3+
import unittest
4+
5+
from integration_tests.env_variable_names import (
6+
SLACK_SDK_TEST_GRID_ORG_ADMIN_USER_TOKEN, SLACK_SDK_TEST_GRID_USER_ID_ADMIN_AUTH,
7+
)
8+
from integration_tests.helpers import async_test
9+
from slack_sdk.web import WebClient
10+
from slack_sdk.web.async_client import AsyncWebClient
11+
12+
13+
class TestWebClient(unittest.TestCase):
14+
"""Runs integration tests with real Slack API"""
15+
16+
def setUp(self):
17+
self.logger = logging.getLogger(__name__)
18+
self.org_admin_token = os.environ[SLACK_SDK_TEST_GRID_ORG_ADMIN_USER_TOKEN]
19+
self.sync_client: WebClient = WebClient(token=self.org_admin_token)
20+
self.async_client: AsyncWebClient = AsyncWebClient(token=self.org_admin_token)
21+
self.user_ids = [os.environ[SLACK_SDK_TEST_GRID_USER_ID_ADMIN_AUTH]]
22+
23+
def tearDown(self):
24+
pass
25+
26+
def test_sync(self):
27+
client = self.sync_client
28+
29+
list = client.admin_auth_policy_getEntities(policy_name="email_password", limit=3)
30+
self.assertIsNotNone(list)
31+
32+
assignment = client.admin_auth_policy_assignEntities(
33+
entity_ids=self.user_ids,
34+
policy_name="email_password",
35+
entity_type="USER",
36+
)
37+
self.assertIsNotNone(assignment)
38+
self.assertEqual(list["entity_total_count"] + 1, assignment["entity_total_count"])
39+
40+
removal = client.admin_auth_policy_removeEntities(
41+
entity_ids=self.user_ids,
42+
policy_name="email_password",
43+
entity_type="USER",
44+
)
45+
self.assertIsNotNone(removal)
46+
self.assertEqual(list["entity_total_count"], removal["entity_total_count"])
47+
48+
@async_test
49+
async def test_async(self):
50+
client = self.async_client
51+
52+
list = await client.admin_auth_policy_getEntities(policy_name="email_password", limit=3)
53+
self.assertIsNotNone(list)
54+
55+
assignment = await client.admin_auth_policy_assignEntities(
56+
entity_ids=self.user_ids,
57+
policy_name="email_password",
58+
entity_type="USER",
59+
)
60+
self.assertIsNotNone(assignment)
61+
self.assertEqual(list["entity_total_count"] + 1, assignment["entity_total_count"])
62+
63+
removal = await client.admin_auth_policy_removeEntities(
64+
entity_ids=self.user_ids,
65+
policy_name="email_password",
66+
entity_type="USER",
67+
)
68+
self.assertIsNotNone(removal)
69+
self.assertEqual(list["entity_total_count"], removal["entity_total_count"])

slack_sdk/web/async_client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,65 @@ async def admin_apps_uninstall(
182182
"admin.apps.uninstall", http_verb="POST", params=kwargs
183183
)
184184

185+
async def admin_auth_policy_getEntities(
186+
self,
187+
*,
188+
policy_name: str,
189+
cursor: Optional[str] = None,
190+
entity_type: Optional[str] = None,
191+
limit: Optional[int] = None,
192+
**kwargs
193+
) -> AsyncSlackResponse:
194+
"""Fetch all the entities assigned to a particular authentication policy by name."""
195+
kwargs.update({"policy_name": policy_name})
196+
if cursor is not None:
197+
kwargs.update({"cursor": cursor})
198+
if entity_type is not None:
199+
kwargs.update({"entity_type": entity_type})
200+
if limit is not None:
201+
kwargs.update({"limit": limit})
202+
return await self.api_call(
203+
"admin.auth.policy.getEntities", http_verb="POST", params=kwargs
204+
)
205+
206+
async def admin_auth_policy_assignEntities(
207+
self,
208+
*,
209+
entity_ids: Union[str, Sequence[str]],
210+
policy_name: str,
211+
entity_type: str,
212+
**kwargs
213+
) -> AsyncSlackResponse:
214+
"""Assign entities to a particular authentication policy."""
215+
if isinstance(entity_ids, (list, Tuple)):
216+
kwargs.update({"entity_ids": ",".join(entity_ids)})
217+
else:
218+
kwargs.update({"entity_ids": entity_ids})
219+
kwargs.update({"policy_name": policy_name})
220+
kwargs.update({"entity_type": entity_type})
221+
return await self.api_call(
222+
"admin.auth.policy.assignEntities", http_verb="POST", params=kwargs
223+
)
224+
225+
async def admin_auth_policy_removeEntities(
226+
self,
227+
*,
228+
entity_ids: Union[str, Sequence[str]],
229+
policy_name: str,
230+
entity_type: str,
231+
**kwargs
232+
) -> AsyncSlackResponse:
233+
"""Remove specified entities from a specified authentication policy."""
234+
if isinstance(entity_ids, (list, Tuple)):
235+
kwargs.update({"entity_ids": ",".join(entity_ids)})
236+
else:
237+
kwargs.update({"entity_ids": entity_ids})
238+
kwargs.update({"policy_name": policy_name})
239+
kwargs.update({"entity_type": entity_type})
240+
return await self.api_call(
241+
"admin.auth.policy.removeEntities", http_verb="POST", params=kwargs
242+
)
243+
185244
async def admin_barriers_create(
186245
self,
187246
*,

slack_sdk/web/client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,65 @@ def admin_apps_uninstall(
165165
kwargs.update({"team_ids": team_ids})
166166
return self.api_call("admin.apps.uninstall", http_verb="POST", params=kwargs)
167167

168+
def admin_auth_policy_getEntities(
169+
self,
170+
*,
171+
policy_name: str,
172+
cursor: Optional[str] = None,
173+
entity_type: Optional[str] = None,
174+
limit: Optional[int] = None,
175+
**kwargs
176+
) -> SlackResponse:
177+
"""Fetch all the entities assigned to a particular authentication policy by name."""
178+
kwargs.update({"policy_name": policy_name})
179+
if cursor is not None:
180+
kwargs.update({"cursor": cursor})
181+
if entity_type is not None:
182+
kwargs.update({"entity_type": entity_type})
183+
if limit is not None:
184+
kwargs.update({"limit": limit})
185+
return self.api_call(
186+
"admin.auth.policy.getEntities", http_verb="POST", params=kwargs
187+
)
188+
189+
def admin_auth_policy_assignEntities(
190+
self,
191+
*,
192+
entity_ids: Union[str, Sequence[str]],
193+
policy_name: str,
194+
entity_type: str,
195+
**kwargs
196+
) -> SlackResponse:
197+
"""Assign entities to a particular authentication policy."""
198+
if isinstance(entity_ids, (list, Tuple)):
199+
kwargs.update({"entity_ids": ",".join(entity_ids)})
200+
else:
201+
kwargs.update({"entity_ids": entity_ids})
202+
kwargs.update({"policy_name": policy_name})
203+
kwargs.update({"entity_type": entity_type})
204+
return self.api_call(
205+
"admin.auth.policy.assignEntities", http_verb="POST", params=kwargs
206+
)
207+
208+
def admin_auth_policy_removeEntities(
209+
self,
210+
*,
211+
entity_ids: Union[str, Sequence[str]],
212+
policy_name: str,
213+
entity_type: str,
214+
**kwargs
215+
) -> SlackResponse:
216+
"""Remove specified entities from a specified authentication policy."""
217+
if isinstance(entity_ids, (list, Tuple)):
218+
kwargs.update({"entity_ids": ",".join(entity_ids)})
219+
else:
220+
kwargs.update({"entity_ids": entity_ids})
221+
kwargs.update({"policy_name": policy_name})
222+
kwargs.update({"entity_type": entity_type})
223+
return self.api_call(
224+
"admin.auth.policy.removeEntities", http_verb="POST", params=kwargs
225+
)
226+
168227
def admin_barriers_create(
169228
self,
170229
*,

slack_sdk/web/legacy_client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,65 @@ def admin_apps_uninstall(
178178
kwargs.update({"team_ids": team_ids})
179179
return self.api_call("admin.apps.uninstall", http_verb="POST", params=kwargs)
180180

181+
def admin_auth_policy_getEntities(
182+
self,
183+
*,
184+
policy_name: str,
185+
cursor: Optional[str] = None,
186+
entity_type: Optional[str] = None,
187+
limit: Optional[int] = None,
188+
**kwargs
189+
) -> Union[Future, SlackResponse]:
190+
"""Fetch all the entities assigned to a particular authentication policy by name."""
191+
kwargs.update({"policy_name": policy_name})
192+
if cursor is not None:
193+
kwargs.update({"cursor": cursor})
194+
if entity_type is not None:
195+
kwargs.update({"entity_type": entity_type})
196+
if limit is not None:
197+
kwargs.update({"limit": limit})
198+
return self.api_call(
199+
"admin.auth.policy.getEntities", http_verb="POST", params=kwargs
200+
)
201+
202+
def admin_auth_policy_assignEntities(
203+
self,
204+
*,
205+
entity_ids: Union[str, Sequence[str]],
206+
policy_name: str,
207+
entity_type: str,
208+
**kwargs
209+
) -> Union[Future, SlackResponse]:
210+
"""Assign entities to a particular authentication policy."""
211+
if isinstance(entity_ids, (list, Tuple)):
212+
kwargs.update({"entity_ids": ",".join(entity_ids)})
213+
else:
214+
kwargs.update({"entity_ids": entity_ids})
215+
kwargs.update({"policy_name": policy_name})
216+
kwargs.update({"entity_type": entity_type})
217+
return self.api_call(
218+
"admin.auth.policy.assignEntities", http_verb="POST", params=kwargs
219+
)
220+
221+
def admin_auth_policy_removeEntities(
222+
self,
223+
*,
224+
entity_ids: Union[str, Sequence[str]],
225+
policy_name: str,
226+
entity_type: str,
227+
**kwargs
228+
) -> Union[Future, SlackResponse]:
229+
"""Remove specified entities from a specified authentication policy."""
230+
if isinstance(entity_ids, (list, Tuple)):
231+
kwargs.update({"entity_ids": ",".join(entity_ids)})
232+
else:
233+
kwargs.update({"entity_ids": entity_ids})
234+
kwargs.update({"policy_name": policy_name})
235+
kwargs.update({"entity_type": entity_type})
236+
return self.api_call(
237+
"admin.auth.policy.removeEntities", http_verb="POST", params=kwargs
238+
)
239+
181240
def admin_barriers_create(
182241
self,
183242
*,

0 commit comments

Comments
 (0)