forked from slackapi/python-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_web_client.py
More file actions
141 lines (122 loc) · 5.01 KB
/
Copy pathtest_web_client.py
File metadata and controls
141 lines (122 loc) · 5.01 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
134
135
136
137
138
139
140
141
# Standard Imports
import unittest
from unittest import mock
import asyncio
import re
# Internal Imports
import slack
from tests.helpers import async_test, fake_req_args, mock_request
import slack.errors as err
@mock.patch("slack.WebClient._request", new_callable=mock_request)
class TestWebClient(unittest.TestCase):
def setUp(self):
self.client = slack.WebClient("xoxb-abc-123", loop=asyncio.get_event_loop())
def tearDown(self):
pass
pattern_for_language = re.compile("python/(\\S+)", re.IGNORECASE)
pattern_for_package_identifier = re.compile("slackclient/(\\S+)")
def test_api_calls_return_a_response_when_run_in_sync_mode(self, mock_request):
resp = self.client.api_test()
self.assertFalse(asyncio.isfuture(resp))
self.assertTrue(resp["ok"])
def test_api_calls_include_user_agent(self, mock_request):
self.client.api_test()
mock_call_kwargs = mock_request.call_args[1]
self.assertIn("req_args", mock_call_kwargs)
mock_call_req_args = mock_call_kwargs["req_args"]
self.assertIn("headers", mock_call_req_args)
mock_call_headers = mock_call_req_args["headers"]
self.assertIn("User-Agent", mock_call_headers)
mock_call_user_agent = mock_call_headers["User-Agent"]
self.assertRegex(
mock_call_user_agent,
self.pattern_for_package_identifier,
"User Agent contains slackclient and version",
)
self.assertRegex(
mock_call_user_agent,
self.pattern_for_language,
"User Agent contains Python and version",
)
@async_test
async def test_api_calls_return_a_future_when_run_in_async_mode(self, mock_request):
self.client.run_async = True
future = self.client.api_test()
self.assertTrue(asyncio.isfuture(future))
resp = await future
self.assertTrue(resp["ok"])
def test_builtin_api_methods_send_json(self, mock_request):
self.client.api_test(msg="bye")
mock_request.assert_called_once_with(
http_verb="POST",
api_url="https://www.slack.com/api/api.test",
req_args=fake_req_args(json={"msg": "bye"}),
)
def test_requests_can_be_paginated(self, mock_request):
mock_request.response.side_effect = [
{
"data": {
"ok": True,
"members": ["Bob", "cat"],
"response_metadata": {"next_cursor": 1},
},
"status_code": 200,
"headers": {},
},
{
"data": {"ok": True, "members": ["Kevin", "dog"]},
"status_code": 200,
"headers": {},
},
]
users = []
for page in self.client.users_list(limit=2):
users = users + page["members"]
self.assertTrue(len(users) == 4)
def test_request_pagination_stops_when_next_cursor_is_missing(self, mock_request):
mock_request.response.side_effect = [
{
"data": {"ok": True, "members": ["Bob", "cat"]},
"status_code": 200,
"headers": {},
},
{
"data": {"ok": True, "members": ["Kevin", "dog"]},
"status_code": 200,
"headers": {},
},
]
users = []
for page in self.client.users_list(limit=2):
users = users + page["members"]
self.assertTrue(len(users) == 2)
mock_request.assert_called_once_with(
http_verb="GET",
api_url="https://www.slack.com/api/users.list",
req_args=fake_req_args(params={"limit": 2}),
)
def test_xoxb_token_validation(self, mock_request):
with self.assertRaises(err.BotUserAccessError):
# Channels can only be created with xoxa tokens.
self.client.channels_create(name="test")
def test_json_can_only_be_sent_with_post_requests(self, mock_request):
with self.assertRaises(err.SlackRequestError):
self.client.api_call("fake.method", http_verb="GET", json={})
def test_slack_api_error_is_raised_on_unsuccessful_responses(self, mock_request):
mock_request.response.side_effect = [
{"data": {"ok": False}, "status_code": 200, "headers": {}},
{"data": {"ok": True}, "status_code": 500, "headers": {}},
]
with self.assertRaises(err.SlackApiError):
self.client.api_test()
with self.assertRaises(err.SlackApiError):
self.client.api_test()
def test_the_api_call_files_argument_creates_the_expected_data(self, mock_request):
self.client.token = "xoxa-123"
with mock.patch("builtins.open", mock.mock_open(read_data="fake")):
self.client.users_setPhoto(image="/fake/path")
mock_request.assert_called_once_with(
http_verb="POST",
api_url="https://www.slack.com/api/users.setPhoto",
req_args=fake_req_args(),
)