forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_slackrequest.py
More file actions
114 lines (78 loc) · 3.74 KB
/
Copy pathtest_slackrequest.py
File metadata and controls
114 lines (78 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
from slackclient.slackrequest import SlackRequest
from slackclient.version import __version__
import os
def test_http_headers(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.do('xoxb-123', 'chat.postMessage', {'text': 'test', 'channel': '#general'})
args, kwargs = requests.post.call_args
assert kwargs['headers']['user-agent'] is not None
def test_custom_user_agent(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.append_user_agent("fooagent1", "0.1")
request.append_user_agent("baragent/2", "0.2")
request.do('xoxb-123', 'chat.postMessage', {'text': 'test', 'channel': '#general'})
args, kwargs = requests.post.call_args
# Verify user-agent includes both default and custom agent info
assert "slackclient/{}".format(__version__) in kwargs['headers']['user-agent']
assert "fooagent1/0.1" in kwargs['headers']['user-agent']
# verify escaping of slashes in custom agent name
assert "baragent:2/0.2" in kwargs['headers']['user-agent']
def test_auth_header(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.do('xoxb-123', 'chat.postMessage', {'text': 'test', 'channel': '#general'})
args, kwargs = requests.post.call_args
assert "Bearer xoxb-123" in kwargs['headers']['Authorization']
def test_token_override(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.do('xoxb-123', 'chat.postMessage',
{
'token': "newtoken",
'text': 'test',
'channel': '#general'
})
args, kwargs = requests.post.call_args
assert "Bearer newtoken" in kwargs['headers']['Authorization']
def test_plural_field(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.do('xoxb-123', 'conversations.open', {'users': ['U123', 'U234', 'U345']})
args, kwargs = requests.post.call_args
assert kwargs['data'] == {'users': 'U123,U234,U345'}
request.do('xoxb-123', 'conversations.open', {'users': "U123,U234,U345"})
args2, kwargs2 = requests.post.call_args
assert kwargs2['data'] == {'users': 'U123,U234,U345'}
def test_post_file(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.do('xoxb-123',
'files.upload',
{'file': open(os.path.join('.', 'tests', 'data', 'slack_logo.png'), 'rb'),
'filename': 'slack_logo.png'})
args, kwargs = requests.post.call_args
assert requests.post.call_count == 1
assert 'https://slack.com/api/files.upload' == args[0]
assert {'filename': 'slack_logo.png'} == kwargs['data']
assert kwargs['files'] is not None
def test_get_file(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.do('xoxb-123', 'files.info', {'file': 'myFavoriteFileID'})
args, kwargs = requests.post.call_args
assert requests.post.call_count == 1
assert 'https://slack.com/api/files.info' == args[0]
assert {'file': "myFavoriteFileID"} == kwargs['data']
assert kwargs['files'] is None
def test_post_attachements(mocker):
requests = mocker.patch('slackclient.slackrequest.requests')
request = SlackRequest()
request.do('xoxb-123',
'chat.postMessage',
{'attachments': [{'title': 'hello'}]})
args, kwargs = requests.post.call_args
assert requests.post.call_count == 1
assert 'https://slack.com/api/chat.postMessage' == args[0]
assert isinstance(kwargs["data"]["attachments"], str)