Skip to content

Commit

Permalink
Fix url_verification error with the Flask adapter (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch authored Dec 14, 2021
1 parent e8556c1 commit d5289c9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/app_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ def event_test(body, say, logger):
# pip install slack_bolt
# export SLACK_SIGNING_SECRET=***
# export MY_TOKEN=xoxb-***
# python app.py
# python app_authorize.py
2 changes: 1 addition & 1 deletion examples/async_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ async def command(ack, body):
# pip install slack_bolt
# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***
# python app.py
# python async_app.py
3 changes: 3 additions & 0 deletions slack_bolt/adapter/flask/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def to_bolt_request(req: Request) -> BoltRequest:
def to_flask_response(bolt_resp: BoltResponse) -> Response:
resp: Response = make_response(bolt_resp.body, bolt_resp.status)
for k, values in bolt_resp.headers.items():
if k.lower() == "content-type" and resp.headers.get("content-type") is not None:
# Remove the one set by Flask
resp.headers.pop("content-type")
for v in values:
resp.headers.add_header(k, v)
return resp
Expand Down
34 changes: 34 additions & 0 deletions tests/adapter_tests/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def endpoint():
headers=self.build_headers(timestamp, body),
)
assert rv.status_code == 200
assert rv.headers.get("content-type") == "text/plain;charset=utf-8"
assert_auth_test_count(self, 1)

def test_shortcuts(self):
Expand Down Expand Up @@ -142,6 +143,7 @@ def endpoint():
headers=self.build_headers(timestamp, body),
)
assert rv.status_code == 200
assert rv.headers.get("content-type") == "text/plain;charset=utf-8"
assert_auth_test_count(self, 1)

def test_commands(self):
Expand Down Expand Up @@ -185,6 +187,7 @@ def endpoint():
headers=self.build_headers(timestamp, body),
)
assert rv.status_code == 200
assert rv.headers.get("content-type") == "text/plain;charset=utf-8"
assert_auth_test_count(self, 1)

def test_oauth(self):
Expand All @@ -205,4 +208,35 @@ def endpoint():

with flask_app.test_client() as client:
rv = client.get("/slack/install")
assert rv.headers.get("content-type") == "text/html; charset=utf-8"
assert rv.status_code == 200

def test_url_verification(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)

input = {
"token": "Jhj5dZrVaK7ZwHHjRyZWjbDl",
"challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P",
"type": "url_verification",
}

timestamp, body = str(int(time())), f"payload={quote(json.dumps(input))}"

flask_app = Flask(__name__)

@flask_app.route("/slack/events", methods=["POST"])
def endpoint():
return SlackRequestHandler(app).handle(request)

with flask_app.test_client() as client:
rv = client.post(
"/slack/events",
data=body,
headers=self.build_headers(timestamp, body),
)
assert rv.status_code == 200
assert rv.headers.get("content-type") == "application/json;charset=utf-8"
assert_auth_test_count(self, 1)

0 comments on commit d5289c9

Please sign in to comment.