forked from slackapi/bolt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_oauth_flow.py
More file actions
182 lines (166 loc) · 6.9 KB
/
test_oauth_flow.py
File metadata and controls
182 lines (166 loc) · 6.9 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
import json
from time import time
from urllib.parse import quote
from slack_sdk import WebClient
from slack_sdk.oauth.installation_store import FileInstallationStore
from slack_sdk.oauth.state_store import FileOAuthStateStore
from slack_sdk.signature import SignatureVerifier
from slack_bolt import BoltRequest, BoltResponse, App
from slack_bolt.oauth import OAuthFlow
from slack_bolt.oauth.callback_options import CallbackOptions, SuccessArgs, FailureArgs
from slack_bolt.oauth.oauth_settings import OAuthSettings
from tests.mock_web_api_server import (
cleanup_mock_web_api_server,
setup_mock_web_api_server,
)
class TestOAuthFlow:
mock_api_server_base_url = "http://localhost:8888"
def setup_method(self):
setup_mock_web_api_server(self)
def teardown_method(self):
cleanup_mock_web_api_server(self)
def test_instantiation(self):
oauth_flow = OAuthFlow(
settings=OAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes=["chat:write", "commands"],
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
)
)
assert oauth_flow is not None
assert oauth_flow.logger is not None
assert oauth_flow.client is not None
def test_handle_installation(self):
oauth_flow = OAuthFlow(
settings=OAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes=["chat:write", "commands"],
user_scopes=["search:read"],
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
)
)
req = BoltRequest(body="")
resp = oauth_flow.handle_installation(req)
assert resp.status == 200
assert resp.headers.get("content-type") == ["text/html; charset=utf-8"]
assert resp.headers.get("content-length") == ["576"]
assert "https://slack.com/oauth/v2/authorize?state=" in resp.body
def test_scopes_as_str(self):
settings = OAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes="chat:write,commands",
user_scopes="search:read",
)
assert settings.scopes == ["chat:write", "commands"]
assert settings.user_scopes == ["search:read"]
def test_handle_callback(self):
oauth_flow = OAuthFlow(
client=WebClient(base_url=self.mock_api_server_base_url),
settings=OAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes=["chat:write", "commands"],
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
success_url="https://www.example.com/completion",
failure_url="https://www.example.com/failure",
),
)
state = oauth_flow.issue_new_state(None)
req = BoltRequest(
body="",
query=f"code=foo&state={state}",
headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]},
)
resp = oauth_flow.handle_callback(req)
assert resp.status == 200
assert "https://www.example.com/completion" in resp.body
app = App(signing_secret="signing_secret", oauth_flow=oauth_flow)
global_shortcut_body = {
"type": "shortcut",
"token": "verification_token",
"action_ts": "111.111",
"team": {
"id": "T111",
"domain": "workspace-domain",
"enterprise_id": "E111",
"enterprise_name": "Org Name",
},
"user": {"id": "W111", "username": "primary-owner", "team_id": "T111"},
"callback_id": "test-shortcut",
"trigger_id": "111.111.xxxxxx",
}
body = f"payload={quote(json.dumps(global_shortcut_body))}"
timestamp = str(int(time()))
signature_verifier = SignatureVerifier("signing_secret")
headers = {
"content-type": ["application/x-www-form-urlencoded"],
"x-slack-signature": [
signature_verifier.generate_signature(body=body, timestamp=timestamp)
],
"x-slack-request-timestamp": [timestamp],
}
request = BoltRequest(body=body, headers=headers)
response = app.dispatch(request)
assert response.status == 200
assert self.mock_received_requests["/auth.test"] == 1
def test_handle_callback_invalid_state(self):
oauth_flow = OAuthFlow(
settings=OAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes=["chat:write", "commands"],
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
)
)
state = oauth_flow.issue_new_state(None)
req = BoltRequest(
body="",
query=f"code=foo&state=invalid",
headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]},
)
resp = oauth_flow.handle_callback(req)
assert resp.status == 400
def test_handle_callback_using_options(self):
def success(args: SuccessArgs) -> BoltResponse:
assert args.request is not None
return BoltResponse(status=200, body="customized")
def failure(args: FailureArgs) -> BoltResponse:
assert args.request is not None
assert args.reason is not None
return BoltResponse(status=502, body="customized")
oauth_flow = OAuthFlow(
client=WebClient(base_url=self.mock_api_server_base_url),
settings=OAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes=["chat:write", "commands"],
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
callback_options=CallbackOptions(success=success, failure=failure),
),
)
state = oauth_flow.issue_new_state(None)
req = BoltRequest(
body="",
query=f"code=foo&state={state}",
headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]},
)
resp = oauth_flow.handle_callback(req)
assert resp.status == 200
assert resp.body == "customized"
state = oauth_flow.issue_new_state(None)
req = BoltRequest(
body="",
query=f"code=foo&state=invalid",
headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]},
)
resp = oauth_flow.handle_callback(req)
assert resp.status == 502
assert resp.body == "customized"