Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_RTM_with_workspace_tokens():
def token_update_callback(update_data):
return update_data
with pytest.raises(TokenRefreshError):
sc = SlackClient(
client_id="12345",
client_secret="12345",
refresh_token="refresh_token",
token_update_callback=token_update_callback,
)
sc.rtm_connect()
def test_token_refresh_on_expired_token():
# Client should fetch and append an access token on the first API request
# When the token is refreshed, the client will call this callback
client_args = {}
def token_update_callback(update_data):
client_args[update_data["team_id"]] = update_data
sc = SlackClient(
client_id="12345",
client_secret="12345",
refresh_token="refresh_token",
token_update_callback=token_update_callback,
)
# Set the token TTL to some time in the past
sc.access_token_expires_at = 0
# Mock both the main API request and the token refresh request
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
"https://slack.com/api/auth.test",
status=200,
json={"ok": True},
def test_token_refresh_failed():
# Client should raise TokenRefreshError is token refresh returns error
def token_update_callback(update_data):
return update_data
sc = SlackClient(
client_id="12345",
client_secret="12345",
refresh_token="refresh_token",
token_update_callback=token_update_callback,
)
with pytest.raises(TokenRefreshError):
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
"https://slack.com/api/channels.list",
status=200,
json={"ok": False, "error": "invalid_auth"},
)
rsps.add(
def test_api_not_ok(slackclient):
# Testing for rate limit retry headers
client = SlackClient("xoxp-1234123412341234-12341234-1234")
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
"https://slack.com/api/im.open",
status=200,
json={"ok": False, "error": "invalid_auth"},
headers={},
)
client.api_call("im.open", user="UXXXX")
for call in rsps.calls:
assert call.response.status_code == 200
assert call.request.url in ["https://slack.com/api/im.open"]
def test_noncallable_refresh_callback():
with pytest.raises(TokenRefreshError):
SlackClient(
client_id="12345",
client_secret="12345",
refresh_token="refresh_token",
token_update_callback="THIS IS A STRING, NOT A CALLABLE METHOD",
)
def test_proxy():
proxies = {"http": "some-bad-proxy", "https": "some-bad-proxy"}
client = SlackClient("xoxp-1234123412341234-12341234-1234", proxies=proxies)
server = client.server
assert server.proxies == proxies
with pytest.raises(ProxyError):
server.rtm_connect()
with pytest.raises(SlackConnectionError):
server.connect_slack_websocket(
"wss://mpmulti-xw58.slack-msgs.com/websocket/bad-token"
)
api_requester = server.api_requester
assert api_requester.proxies == proxies
with pytest.raises(ProxyError):
api_requester.do("xoxp-1234123412341234-12341234-1234", request="channels.list")
def test_token_refresh_on_initial_api_request():
# Client should fetch and append an access token on the first API request
# When the token is refreshed, the client will call this callback
access_token = "xoxa-2-abcdef"
client_args = {}
def token_update_callback(update_data):
client_args[update_data["team_id"]] = update_data
sc = SlackClient(
client_id="12345",
client_secret="12345",
refresh_token="refresh_token",
token_update_callback=token_update_callback,
)
# The client starts out with an empty token
assert sc.token is None
# Mock both the main API request and the token refresh request
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
"https://slack.com/api/auth.test",
status=200,
json={"ok": True},
import threading
from slackclient.server import Server
from slackclient.client import SlackClient
class ThreadedSlackClient(SlackClient):
def __init__(self, *args, **kwargs):
super(ThreadedSlackClient, self).__init__(*args, **kwargs)
self.server = ThreadedServer(self.token, False)
class ThreadedServer(Server):
def __init__(self, *args, **kwargs):
super(ThreadedServer, self).__init__(*args, **kwargs)
self.api_call_lock = threading.RLock()
def api_call(self, *args, **kwargs):
with self.api_call_lock:
return super(ThreadedServer, self).api_call(*args, **kwargs)