-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_slack_tokens.py
More file actions
223 lines (170 loc) · 8.71 KB
/
Copy pathtest_slack_tokens.py
File metadata and controls
223 lines (170 loc) · 8.71 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""Tests for core.operations.slack_ops.tokens."""
import os
import pytest
from unittest.mock import patch
from django.conf import settings
from core.operations.slack_ops.tokens import (
get_slack_bot_token,
get_slack_app_token,
get_slack_client,
get_default_team_key,
)
from core.operations.slack_ops.client import SlackAPIClient
def test_get_slack_bot_token_from_env():
"""get_slack_bot_token returns value from settings dict when team_id is set."""
with patch.object(settings, "SLACK_BOT_TOKEN", {"T01234": "xoxb-from-env"}):
token = get_slack_bot_token("T01234")
assert token == "xoxb-from-env"
def test_get_slack_bot_token_no_args_uses_slack_team_id_fallback():
"""get_slack_bot_token() with no args uses SLACK_TEAM_ID fallback and returns token for that team."""
with patch.object(settings, "SLACK_TEAM_ID", "T99"):
with patch.object(settings, "SLACK_BOT_TOKEN", {"T99": "xoxb-fallback"}):
token = get_slack_bot_token()
assert token == "xoxb-fallback"
def test_get_slack_bot_token_raises_when_team_id_and_slack_team_id_fallback_missing():
"""get_slack_bot_token raises ValueError when team_id and SLACK_TEAM_ID fallback are missing."""
with patch.object(settings, "SLACK_TEAM_ID", ""):
with patch.object(settings, "SLACK_BOT_TOKEN", {}):
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_bot_token()
with patch.object(settings, "SLACK_TEAM_ID", ""):
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_bot_token(None)
with patch.object(settings, "SLACK_TEAM_ID", " "):
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_bot_token(" ")
def test_get_default_team_key_single():
"""get_default_team_key() returns SLACK_TEAM_ID when set."""
with patch.object(settings, "SLACK_TEAM_ID", "only"):
key = get_default_team_key()
assert key == "only"
def test_get_default_team_key_raises_when_missing():
"""get_default_team_key() raises ValueError when SLACK_TEAM_ID is not set."""
with patch.object(settings, "SLACK_TEAM_ID", ""):
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_default_team_key()
def test_get_slack_bot_token_missing_team_id_raises():
"""get_slack_bot_token raises ValueError when no team configured (SLACK_TEAM_ID empty and no team_id)."""
with patch.object(settings, "SLACK_TEAM_ID", ""):
with patch.object(settings, "SLACK_BOT_TOKEN", {}):
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_bot_token()
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_bot_token(None)
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_bot_token(" ")
def test_get_slack_bot_token_missing_raises():
"""get_slack_bot_token raises ValueError when token for team_id is not set."""
with patch.object(settings, "SLACK_BOT_TOKEN", {}):
with pytest.raises(ValueError, match="SLACK_BOT_TOKEN"):
get_slack_bot_token("T01234")
def test_get_slack_app_token_from_env():
"""get_slack_app_token returns value from settings dict when team_id is set."""
with patch.object(settings, "SLACK_APP_TOKEN", {"T01234": "xapp-from-env"}):
token = get_slack_app_token("T01234")
assert token == "xapp-from-env"
def test_get_slack_app_token_no_args_uses_slack_team_id_fallback():
"""get_slack_app_token() with no args uses SLACK_TEAM_ID fallback."""
with patch.object(settings, "SLACK_TEAM_ID", "T99"):
with patch.object(settings, "SLACK_APP_TOKEN", {"T99": "xapp-fallback"}):
token = get_slack_app_token()
assert token == "xapp-fallback"
def test_get_slack_app_token_missing_raises():
"""get_slack_app_token raises ValueError when token for team is not set."""
with patch.object(settings, "SLACK_APP_TOKEN", {}):
with pytest.raises(ValueError, match="SLACK_APP_TOKEN"):
get_slack_app_token("T01234")
with patch.object(settings, "SLACK_TEAM_ID", ""):
with patch.object(settings, "SLACK_APP_TOKEN", {}):
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_app_token()
def test_get_slack_client_with_explicit_token():
"""get_slack_client(bot_token='x') returns SlackAPIClient with that token."""
client = get_slack_client(bot_token="xoxb-explicit")
assert isinstance(client, SlackAPIClient)
assert client.token == "xoxb-explicit"
def test_get_slack_client_without_token_uses_get_slack_bot_token():
"""get_slack_client(team_id=...) uses get_slack_bot_token(team_id) when bot_token not set."""
with patch.object(settings, "SLACK_BOT_TOKEN", {"T01234": "xoxb-env-token"}):
client = get_slack_client(team_id="T01234")
assert isinstance(client, SlackAPIClient)
assert client.token == "xoxb-env-token"
def test_get_slack_client_no_args_uses_slack_team_id_fallback():
"""get_slack_client() with no args uses SLACK_TEAM_ID fallback and returns client with that token."""
with patch.object(settings, "SLACK_TEAM_ID", "T99"):
with patch.object(settings, "SLACK_BOT_TOKEN", {"T99": "xoxb-fallback-token"}):
client = get_slack_client()
assert isinstance(client, SlackAPIClient)
assert client.token == "xoxb-fallback-token"
def test_get_slack_client_no_args_fallback_from_os_environ():
"""get_slack_client() with no args uses SLACK_TEAM_ID (as from os.environ) for token lookup."""
with patch.dict(os.environ, {"SLACK_TEAM_ID": "T88"}, clear=False):
with patch.object(settings, "SLACK_TEAM_ID", "T88"):
with patch.object(
settings,
"SLACK_BOT_TOKEN",
{"T88": "xoxb-from-env-token"},
):
client = get_slack_client()
assert isinstance(client, SlackAPIClient)
assert client.token == "xoxb-from-env-token"
def test_get_slack_client_no_args_no_team_raises():
"""get_slack_client() with no args raises when SLACK_TEAM_ID is not set."""
with patch.object(settings, "SLACK_TEAM_ID", ""):
with patch.object(settings, "SLACK_BOT_TOKEN", {}):
with pytest.raises(ValueError, match="SLACK_TEAM_ID is required"):
get_slack_client()
def test_get_slack_bot_token_whitespace_only_raises():
with patch.object(settings, "SLACK_TEAM_ID", "T1"):
with patch.object(settings, "SLACK_BOT_TOKEN", {"T1": " "}):
with pytest.raises(ValueError, match="missing"):
get_slack_bot_token("T1")
def test_get_slack_app_token_whitespace_only_raises():
with patch.object(settings, "SLACK_TEAM_ID", "T1"):
with patch.object(settings, "SLACK_APP_TOKEN", {"T1": "\t"}):
with pytest.raises(ValueError, match="missing"):
get_slack_app_token("T1")
def test_slack_team_fallback_handles_settings_getattr_error():
class _BadSettings:
__slots__ = ()
def __getattr__(self, name):
raise RuntimeError("no")
with patch("django.conf.settings", _BadSettings()):
with pytest.raises(ValueError, match="SLACK_TEAM_ID"):
get_default_team_key()
def test_get_slack_bot_token_inner_settings_raises_returns_none_map():
class _BadSettings:
__slots__ = ()
def __getattr__(self, name):
raise RuntimeError("no")
with patch.object(settings, "SLACK_TEAM_ID", "T1"):
with patch("django.conf.settings", _BadSettings()):
with pytest.raises(ValueError, match="SLACK_BOT_TOKEN"):
get_slack_bot_token("T1")
def test_get_slack_app_token_inner_settings_raises():
class _BadSettings:
__slots__ = ()
def __getattr__(self, name):
raise RuntimeError("no")
with patch.object(settings, "SLACK_TEAM_ID", "T1"):
with patch("django.conf.settings", _BadSettings()):
with pytest.raises(ValueError, match="SLACK_APP_TOKEN"):
get_slack_app_token("T1")
def test_get_slack_bot_token_raises_when_fallback_team_id_empty():
with patch(
"core.operations.slack_ops.tokens._slack_team_fallback",
return_value="",
):
with pytest.raises(
ValueError, match="team id is required for get_slack_bot_token"
):
get_slack_bot_token("")
def test_get_slack_app_token_raises_when_fallback_team_id_empty():
with patch(
"core.operations.slack_ops.tokens._slack_team_fallback",
return_value="",
):
with pytest.raises(
ValueError, match="team id is required for get_slack_app_token"
):
get_slack_app_token("")