Skip to content

Commit 5971d0d

Browse files
authored
Merge pull request #710 from seratch/issue-536
Fix #536 Allow Tokens to be specified per request
2 parents db08ccc + 23d7867 commit 5971d0d

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

integration_tests/samples/oauth/oauth_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def oauth_callback():
9797
state = request.args["state"]
9898
if state_service.consume(state):
9999
code = request.args["code"]
100-
client = WebClient(token="") # not prepared token needed for this app
100+
client = WebClient() # no prepared token needed for this app
101101
response = client.oauth_v2_access(
102102
client_id=client_id,
103103
client_secret=client_secret,

integration_tests/web/test_web_client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,45 @@ async def test_uploading_binary_files_async(self):
220220
deletion = client.files_delete(file=upload["file"]["id"])
221221
self.assertIsNotNone(deletion)
222222

223+
def test_uploading_file_with_token_param(self):
224+
client = WebClient()
225+
current_dir = os.path.dirname(__file__)
226+
file = f"{current_dir}/../../tests/data/slack_logo.png"
227+
upload = client.files_upload(
228+
token=self.bot_token,
229+
channels=self.channel_id,
230+
title="Good Old Slack Logo",
231+
filename="slack_logo.png",
232+
file=file,
233+
)
234+
self.assertIsNotNone(upload)
235+
236+
deletion = client.files_delete(
237+
token=self.bot_token,
238+
file=upload["file"]["id"],
239+
)
240+
self.assertIsNotNone(deletion)
241+
242+
@async_test
243+
async def test_uploading_file_with_token_param_async(self):
244+
client = WebClient(run_async=True)
245+
current_dir = os.path.dirname(__file__)
246+
file = f"{current_dir}/../../tests/data/slack_logo.png"
247+
upload = await client.files_upload(
248+
token=self.bot_token,
249+
channels=self.channel_id,
250+
title="Good Old Slack Logo",
251+
filename="slack_logo.png",
252+
file=file,
253+
)
254+
self.assertIsNotNone(upload)
255+
256+
deletion = client.files_delete(
257+
token=self.bot_token,
258+
file=upload["file"]["id"],
259+
)
260+
self.assertIsNotNone(deletion)
261+
223262
# -------------------------
224263
# pagination
225264

slack/web/base_client.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ def _get_event_loop(self):
6969
return loop
7070

7171
def _get_headers(
72-
self, has_json: bool, has_files: bool, request_specific_headers: Optional[dict]
72+
self,
73+
*,
74+
token: Optional[str],
75+
has_json: bool,
76+
has_files: bool,
77+
request_specific_headers: Optional[dict],
7378
):
7479
"""Constructs the headers need for a request.
7580
Args:
@@ -90,8 +95,8 @@ def _get_headers(
9095
"Content-Type": "application/x-www-form-urlencoded",
9196
}
9297

93-
if self.token:
94-
final_headers.update({"Authorization": "Bearer {}".format(self.token)})
98+
if token:
99+
final_headers.update({"Authorization": "Bearer {}".format(token)})
95100

96101
# Merge headers specified at client initialization.
97102
final_headers.update(self.headers)
@@ -169,8 +174,18 @@ def api_call(
169174
if params is not None and isinstance(params, dict):
170175
params = {k: v for k, v in params.items() if v is not None}
171176

177+
token: Optional[str] = self.token
178+
if params is not None and "token" in params:
179+
token = params.pop("token")
180+
if json is not None and "token" in json:
181+
token = json.pop("token")
172182
req_args = {
173-
"headers": self._get_headers(has_json, has_files, headers),
183+
"headers": self._get_headers(
184+
token=token,
185+
has_json=has_json,
186+
has_files=has_files,
187+
request_specific_headers=headers,
188+
),
174189
"data": data,
175190
"files": files,
176191
"params": params,

tests/web/test_web_client.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,18 @@ def test_issue_690_oauth_v2_access(self):
158158
@async_test
159159
async def test_issue_690_oauth_v2_access_async(self):
160160
self.async_client.token = ""
161-
resp = await self.async_client.oauth_v2_access(client_id="111.222", client_secret="secret", code="codeeeeeeeeee")
161+
resp = await self.async_client.oauth_v2_access(
162+
client_id="111.222",
163+
client_secret="secret",
164+
code="codeeeeeeeeee",
165+
)
162166
self.assertIsNone(resp["error"])
163167
with self.assertRaises(err.SlackApiError):
164-
await self.async_client.oauth_v2_access(client_id="999.999", client_secret="secret", code="codeeeeeeeeee")
168+
await self.async_client.oauth_v2_access(
169+
client_id="999.999",
170+
client_secret="secret",
171+
code="codeeeeeeeeee",
172+
)
165173

166174
def test_issue_690_oauth_access(self):
167175
self.client.token = ""
@@ -184,3 +192,22 @@ def test_issue_705_no_param_request_pagination(self):
184192
for page in self.client.users_list():
185193
users = users + page["members"]
186194
self.assertTrue(len(users) == 4)
195+
196+
def test_token_param(self):
197+
client = WebClient(base_url="http://localhost:8888")
198+
with self.assertRaises(err.SlackApiError):
199+
client.users_list()
200+
resp = client.users_list(token="xoxb-users_list_pagination")
201+
self.assertIsNone(resp["error"])
202+
with self.assertRaises(err.SlackApiError):
203+
client.users_list()
204+
205+
@async_test
206+
async def test_token_param_async(self):
207+
client = WebClient(base_url="http://localhost:8888", run_async=True)
208+
with self.assertRaises(err.SlackApiError):
209+
await client.users_list()
210+
resp = await client.users_list(token="xoxb-users_list_pagination")
211+
self.assertIsNone(resp["error"])
212+
with self.assertRaises(err.SlackApiError):
213+
await client.users_list()

0 commit comments

Comments
 (0)