X Tutup
Skip to content

Commit 8566095

Browse files
committed
Add tests for Pyramid framework
1 parent a65c125 commit 8566095

File tree

2 files changed

+185
-3
lines changed

2 files changed

+185
-3
lines changed

slack_bolt/adapter/pyramid/handler.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99

1010
def to_bolt_request(request: Request) -> BoltRequest:
11+
body: str = ""
12+
if request.body is not None:
13+
if isinstance(request.body, bytes):
14+
body = request.body.decode("utf-8")
15+
else:
16+
body = request.body
1117
bolt_req = BoltRequest(
12-
body=request.body.decode("utf-8"),
13-
query=request.query_string,
14-
headers=request.headers,
18+
body=body, query=request.query_string, headers=request.headers,
1519
)
1620
return bolt_req
1721

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
import json
2+
from time import time
3+
from unittest import TestCase
4+
from urllib.parse import quote
5+
6+
from pyramid import testing
7+
from pyramid.request import Request
8+
from pyramid.response import Response
9+
from slack_sdk.signature import SignatureVerifier
10+
from slack_sdk.web import WebClient
11+
12+
from slack_bolt.adapter.pyramid import SlackRequestHandler
13+
from slack_bolt.app import App
14+
from slack_bolt.oauth.oauth_settings import OAuthSettings
15+
from tests.mock_web_api_server import (
16+
setup_mock_web_api_server,
17+
cleanup_mock_web_api_server,
18+
)
19+
from tests.utils import remove_os_env_temporarily, restore_os_env
20+
21+
22+
class TestPyramid(TestCase):
23+
signing_secret = "secret"
24+
valid_token = "xoxb-valid"
25+
mock_api_server_base_url = "http://localhost:8888"
26+
signature_verifier = SignatureVerifier(signing_secret)
27+
web_client = WebClient(token=valid_token, base_url=mock_api_server_base_url,)
28+
29+
def setUp(self):
30+
self.config = testing.setUp()
31+
self.old_os_env = remove_os_env_temporarily()
32+
setup_mock_web_api_server(self)
33+
34+
def tearDown(self):
35+
testing.tearDown()
36+
cleanup_mock_web_api_server(self)
37+
restore_os_env(self.old_os_env)
38+
39+
def generate_signature(self, body: str, timestamp: str):
40+
return self.signature_verifier.generate_signature(
41+
body=body, timestamp=timestamp,
42+
)
43+
44+
def build_headers(self, timestamp: str, body: str):
45+
content_type = (
46+
"application/json"
47+
if body.startswith("{")
48+
else "application/x-www-form-urlencoded"
49+
)
50+
return {
51+
"content-type": [content_type],
52+
"x-slack-signature": [self.generate_signature(body, timestamp)],
53+
"x-slack-request-timestamp": [timestamp],
54+
}
55+
56+
def test_events(self):
57+
app = App(client=self.web_client, signing_secret=self.signing_secret,)
58+
59+
def event_handler():
60+
pass
61+
62+
app.event("app_mention")(event_handler)
63+
64+
input = {
65+
"token": "verification_token",
66+
"team_id": "T111",
67+
"enterprise_id": "E111",
68+
"api_app_id": "A111",
69+
"event": {
70+
"client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63",
71+
"type": "app_mention",
72+
"text": "<@W111> Hi there!",
73+
"user": "W222",
74+
"ts": "1595926230.009600",
75+
"team": "T111",
76+
"channel": "C111",
77+
"event_ts": "1595926230.009600",
78+
},
79+
"type": "event_callback",
80+
"event_id": "Ev111",
81+
"event_time": 1595926230,
82+
"authed_users": ["W111"],
83+
}
84+
timestamp, body = str(int(time())), json.dumps(input)
85+
86+
request: Request = testing.DummyRequest()
87+
request.path = "/slack/events"
88+
request.method = "POST"
89+
request.body = body.encode("utf-8")
90+
request.headers = self.build_headers(timestamp, body)
91+
response: Response = SlackRequestHandler(app).handle(request)
92+
assert response.status_code == 200
93+
assert self.mock_received_requests["/auth.test"] == 1
94+
95+
def test_shortcuts(self):
96+
app = App(client=self.web_client, signing_secret=self.signing_secret,)
97+
98+
def shortcut_handler(ack):
99+
ack()
100+
101+
app.shortcut("test-shortcut")(shortcut_handler)
102+
103+
input = {
104+
"type": "shortcut",
105+
"token": "verification_token",
106+
"action_ts": "111.111",
107+
"team": {
108+
"id": "T111",
109+
"domain": "workspace-domain",
110+
"enterprise_id": "E111",
111+
"enterprise_name": "Org Name",
112+
},
113+
"user": {"id": "W111", "username": "primary-owner", "team_id": "T111"},
114+
"callback_id": "test-shortcut",
115+
"trigger_id": "111.111.xxxxxx",
116+
}
117+
118+
timestamp, body = str(int(time())), f"payload={quote(json.dumps(input))}"
119+
120+
request: Request = testing.DummyRequest()
121+
request.path = "/slack/events"
122+
request.method = "POST"
123+
request.body = body.encode("utf-8")
124+
request.headers = self.build_headers(timestamp, body)
125+
response: Response = SlackRequestHandler(app).handle(request)
126+
assert response.status_code == 200
127+
assert self.mock_received_requests["/auth.test"] == 1
128+
129+
def test_commands(self):
130+
app = App(client=self.web_client, signing_secret=self.signing_secret,)
131+
132+
def command_handler(ack):
133+
ack()
134+
135+
app.command("/hello-world")(command_handler)
136+
137+
input = (
138+
"token=verification_token"
139+
"&team_id=T111"
140+
"&team_domain=test-domain"
141+
"&channel_id=C111"
142+
"&channel_name=random"
143+
"&user_id=W111"
144+
"&user_name=primary-owner"
145+
"&command=%2Fhello-world"
146+
"&text=Hi"
147+
"&enterprise_id=E111"
148+
"&enterprise_name=Org+Name"
149+
"&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx"
150+
"&trigger_id=111.111.xxx"
151+
)
152+
timestamp, body = str(int(time())), input
153+
154+
request: Request = testing.DummyRequest()
155+
request.path = "/slack/events"
156+
request.method = "POST"
157+
request.body = body.encode("utf-8")
158+
request.headers = self.build_headers(timestamp, body)
159+
response: Response = SlackRequestHandler(app).handle(request)
160+
assert response.status_code == 200
161+
assert self.mock_received_requests["/auth.test"] == 1
162+
163+
def test_oauth(self):
164+
app = App(
165+
client=self.web_client,
166+
signing_secret=self.signing_secret,
167+
oauth_settings=OAuthSettings(
168+
client_id="111.111",
169+
client_secret="xxx",
170+
scopes=["chat:write", "commands"],
171+
),
172+
)
173+
174+
request: Request = testing.DummyRequest()
175+
request.path = "/slack/install"
176+
request.method = "GET"
177+
response: Response = SlackRequestHandler(app).handle(request)
178+
assert response.status_code == 302

0 commit comments

Comments
 (0)
X Tutup