Skip to content

Commit b6c2f1c

Browse files
committed
Fix #123 by adding a validation in constructors (App/AsyncApp)
1 parent 736f764 commit b6c2f1c

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

slack_bolt/app/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
debug_running_listener,
4242
error_unexpected_listener_middleware,
4343
error_client_invalid_type,
44+
error_authorize_conflicts,
4445
)
4546
from slack_bolt.middleware import (
4647
Middleware,
@@ -131,6 +132,8 @@ def __init__(
131132

132133
self._authorize: Optional[Authorize] = None
133134
if authorize is not None:
135+
if oauth_settings is not None or oauth_flow is not None:
136+
raise BoltError(error_authorize_conflicts())
134137
self._authorize = CallableAuthorize(
135138
logger=self._framework_logger, func=authorize
136139
)

slack_bolt/app/async_app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
error_unexpected_listener_middleware,
3333
error_listener_function_must_be_coro_func,
3434
error_client_invalid_type_async,
35+
error_authorize_conflicts,
3536
)
3637
from slack_bolt.lazy_listener.asyncio_runner import AsyncioLazyListenerRunner
3738
from slack_bolt.listener.async_listener import AsyncListener, AsyncCustomListener
@@ -138,6 +139,9 @@ def __init__(
138139

139140
self._async_authorize: Optional[AsyncAuthorize] = None
140141
if authorize is not None:
142+
if oauth_settings is not None or oauth_flow is not None:
143+
raise BoltError(error_authorize_conflicts())
144+
141145
self._async_authorize = AsyncCallableAuthorize(
142146
logger=self._framework_logger, func=authorize
143147
)

slack_bolt/logger/messages.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def error_listener_function_must_be_coro_func(func_name: str) -> str:
4545
return f"The listener function ({func_name}) is not a coroutine function."
4646

4747

48+
def error_authorize_conflicts() -> str:
49+
return "`authorize` in the top-level arguments is not allowed when you pass either `oauth_settings` or `oauth_flow`"
50+
51+
4852
# -------------------------------
4953
# Warning
5054
# -------------------------------

tests/scenario_tests/test_app.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from slack_sdk.oauth.state_store import FileOAuthStateStore
55

66
from slack_bolt import App
7+
from slack_bolt.authorization import AuthorizeResult
78
from slack_bolt.error import BoltError
89
from slack_bolt.oauth import OAuthFlow
910
from slack_bolt.oauth.oauth_settings import OAuthSettings
@@ -92,3 +93,31 @@ def test_valid_multi_auth_secret_absence(self):
9293
signing_secret="valid",
9394
oauth_settings=OAuthSettings(client_id="111.222", client_secret=None),
9495
)
96+
97+
def test_authorize_conflicts(self):
98+
oauth_settings = OAuthSettings(
99+
client_id="111.222",
100+
client_secret="valid",
101+
installation_store=FileInstallationStore(),
102+
state_store=FileOAuthStateStore(expiration_seconds=120),
103+
)
104+
105+
# no error with this
106+
App(signing_secret="valid", oauth_settings=oauth_settings)
107+
108+
def authorize() -> AuthorizeResult:
109+
return AuthorizeResult(enterprise_id="E111", team_id="T111")
110+
111+
with pytest.raises(BoltError):
112+
App(
113+
signing_secret="valid",
114+
authorize=authorize,
115+
oauth_settings=oauth_settings,
116+
)
117+
118+
oauth_flow = OAuthFlow(settings=oauth_settings)
119+
# no error with this
120+
App(signing_secret="valid", oauth_flow=oauth_flow)
121+
122+
with pytest.raises(BoltError):
123+
App(signing_secret="valid", authorize=authorize, oauth_flow=oauth_flow)

tests/scenario_tests_async/test_app.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from slack_sdk.oauth.state_store import FileOAuthStateStore
55

66
from slack_bolt.async_app import AsyncApp
7+
from slack_bolt.authorization import AuthorizeResult
78
from slack_bolt.error import BoltError
89
from slack_bolt.oauth.async_oauth_flow import AsyncOAuthFlow
910
from slack_bolt.oauth.async_oauth_settings import AsyncOAuthSettings
@@ -98,3 +99,31 @@ def test_valid_multi_auth_secret_absence(self):
9899
signing_secret="valid",
99100
oauth_settings=OAuthSettings(client_id="111.222", client_secret=None),
100101
)
102+
103+
def test_authorize_conflicts(self):
104+
oauth_settings = OAuthSettings(
105+
client_id="111.222",
106+
client_secret="valid",
107+
installation_store=FileInstallationStore(),
108+
state_store=FileOAuthStateStore(expiration_seconds=120),
109+
)
110+
111+
# no error with this
112+
AsyncApp(signing_secret="valid", oauth_settings=oauth_settings)
113+
114+
def authorize() -> AuthorizeResult:
115+
return AuthorizeResult(enterprise_id="E111", team_id="T111")
116+
117+
with pytest.raises(BoltError):
118+
AsyncApp(
119+
signing_secret="valid",
120+
authorize=authorize,
121+
oauth_settings=oauth_settings,
122+
)
123+
124+
oauth_flow = AsyncOAuthFlow(settings=oauth_settings)
125+
# no error with this
126+
AsyncApp(signing_secret="valid", oauth_flow=oauth_flow)
127+
128+
with pytest.raises(BoltError):
129+
AsyncApp(signing_secret="valid", authorize=authorize, oauth_flow=oauth_flow)

0 commit comments

Comments
 (0)