-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslack_test.py
144 lines (134 loc) · 4.02 KB
/
slack_test.py
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
142
143
144
"""Tests for slack functions"""
import pytest
from slack import get_channels_info, get_doofs_id
pytestmark = pytest.mark.asyncio
async def test_get_channels_info(mocker):
"""get_channels_info should obtain information for all public and private channels that doof knows about"""
post_patch = mocker.async_patch("client_wrapper.ClientWrapper.post")
next_cursor = "some cursor"
post_patch.return_value.json.side_effect = [
{
"channels": [
{
"name": "a",
"id": "public channel",
},
{
"name": "and a",
"id": "private one",
},
],
"response_metadata": {
"next_cursor": next_cursor,
},
},
{"channels": [{"name": "two", "id": "a channel in the next page"}]},
]
token = "token"
assert await get_channels_info(token) == {
"a": "public channel",
"and a": "private one",
"two": "a channel in the next page",
}
post_patch.assert_any_call(
mocker.ANY,
"https://slack.com/api/conversations.list",
data={
"token": token,
"types": "public_channel,private_channel",
"limit": 200,
"exclude_archived": "true",
},
)
post_patch.assert_any_call(
mocker.ANY,
"https://slack.com/api/conversations.list",
data={
"token": token,
"types": "public_channel,private_channel",
"limit": 200,
"cursor": next_cursor,
"exclude_archived": "true",
},
)
async def test_get_doofs_id(mocker):
"""get_doofs_id should contact the user API which gets slack info"""
post_patch = mocker.async_patch("client_wrapper.ClientWrapper.post")
doof_id = "It's doof"
token = "It's a token"
next_cursor = "some cursor"
post_patch.return_value.json.side_effect = [
{
"members": [
{
"name": "someone",
"id": "their id",
},
{
"name": "other person",
"id": "other id",
},
],
"response_metadata": {
"next_cursor": next_cursor,
},
},
{"members": [{"name": "doof", "id": doof_id}]},
]
assert await get_doofs_id(token) == doof_id
post_patch.assert_any_call(
mocker.ANY,
"https://slack.com/api/users.list",
data={
"token": token,
"cursor": next_cursor,
},
)
post_patch.assert_any_call(
mocker.ANY,
"https://slack.com/api/users.list",
data={
"token": token,
},
)
async def test_get_doofs_id_missing(mocker):
"""get_doofs_id should raise an exception if the user id can't be found"""
post_patch = mocker.async_patch("client_wrapper.ClientWrapper.post")
token = "It's a token"
next_cursor = "some cursor"
post_patch.return_value.json.side_effect = [
{
"members": [
{
"name": "someone",
"id": "their id",
},
{
"name": "other person",
"id": "other id",
},
],
"response_metadata": {
"next_cursor": next_cursor,
},
},
{"members": [{"name": "other person", "id": "someone else"}]},
]
with pytest.raises(Exception) as ex:
await get_doofs_id(token)
assert ex.value.args[0] == "Unable to find Doof's user id"
post_patch.assert_any_call(
mocker.ANY,
"https://slack.com/api/users.list",
data={
"token": token,
"cursor": next_cursor,
},
)
post_patch.assert_any_call(
mocker.ANY,
"https://slack.com/api/users.list",
data={
"token": token,
},
)