forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_slackclient.py
More file actions
133 lines (105 loc) · 3.74 KB
/
Copy pathtest_slackclient.py
File metadata and controls
133 lines (105 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import pytest
from requests.exceptions import ProxyError
import responses
from slackclient.channel import Channel
from slackclient.client import SlackClient
from slackclient.server import SlackConnectionError
@pytest.fixture
def channel_created_fixture():
file_channel_created_data = open('tests/data/channel.created.json', 'r').read()
json_channel_created_data = json.loads(file_channel_created_data)
return json_channel_created_data
@pytest.fixture
def im_created_fixture():
file_channel_created_data = open('tests/data/im.created.json', 'r').read()
json_channel_created_data = json.loads(file_channel_created_data)
return json_channel_created_data
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_SlackClient(slackclient):
assert type(slackclient) == SlackClient
def test_SlackClient_process_changes(slackclient, channel_created_fixture, im_created_fixture):
slackclient.process_changes(channel_created_fixture)
assert type(slackclient.server.channels.find('fun')) == Channel
slackclient.process_changes(im_created_fixture)
assert type(slackclient.server.channels.find('U123BL234')) == Channel
def test_api_not_ok(slackclient):
# Testing for rate limit retry headers
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
"https://slack.com/api/im.open",
status=200,
json={
"ok": False,
},
headers={}
)
slackclient.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_im_open(slackclient):
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
"https://slack.com/api/im.open",
status=200,
json={
"ok": True,
"channel": {"id":"CXXXXXX"}
},
headers={}
)
slackclient.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_channel_join(slackclient):
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
"https://slack.com/api/channels.join",
status=200,
json={
"ok": True,
"channel": {
"id": "CXXXX",
"name": "test",
"members": ("U0G9QF9C6", "U1QNSQB9U")
}
}
)
slackclient.api_call(
"channels.join",
channel="CXXXX"
)
for call in rsps.calls:
assert call.response.status_code == 200
assert call.request.url in [
"https://slack.com/api/channels.join"
]
response_json = call.response.json()
assert response_json["ok"] is True