|
| 1 | +import re |
| 2 | + |
| 3 | +from slack_sdk import WebClient |
| 4 | +from slack_sdk.oauth.installation_store import FileInstallationStore |
| 5 | +from slack_sdk.oauth.state_store import FileOAuthStateStore |
| 6 | + |
| 7 | +from slack_bolt import BoltRequest, BoltResponse |
| 8 | +from slack_bolt.oauth import OAuthFlow |
| 9 | +from slack_bolt.oauth.callback_options import CallbackOptions, SuccessArgs, FailureArgs |
| 10 | +from slack_bolt.oauth.oauth_settings import OAuthSettings |
| 11 | +from tests.mock_web_api_server import ( |
| 12 | + cleanup_mock_web_api_server, |
| 13 | + setup_mock_web_api_server, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class TestOAuthFlowSQLite3: |
| 18 | + mock_api_server_base_url = "http://localhost:8888" |
| 19 | + |
| 20 | + def setup_method(self): |
| 21 | + setup_mock_web_api_server(self) |
| 22 | + |
| 23 | + def teardown_method(self): |
| 24 | + cleanup_mock_web_api_server(self) |
| 25 | + |
| 26 | + def test_instantiation(self): |
| 27 | + oauth_flow = OAuthFlow.sqlite3( |
| 28 | + database="./logs/test_db", |
| 29 | + client_id="111.222", |
| 30 | + client_secret="xxx", |
| 31 | + scopes=["chat:write", "commands"], |
| 32 | + ) |
| 33 | + assert oauth_flow is not None |
| 34 | + assert oauth_flow.logger is not None |
| 35 | + assert oauth_flow.client is not None |
| 36 | + |
| 37 | + def test_handle_installation(self): |
| 38 | + oauth_flow = OAuthFlow.sqlite3( |
| 39 | + client=WebClient(base_url=self.mock_api_server_base_url), |
| 40 | + database="./logs/test_db", |
| 41 | + client_id="111.222", |
| 42 | + client_secret="xxx", |
| 43 | + scopes=["chat:write", "commands"], |
| 44 | + ) |
| 45 | + req = BoltRequest(body="") |
| 46 | + resp = oauth_flow.handle_installation(req) |
| 47 | + assert resp.status == 302 |
| 48 | + url = resp.headers["location"][0] |
| 49 | + assert ( |
| 50 | + re.compile( |
| 51 | + "https://slack.com/oauth/v2/authorize\\?state=[-0-9a-z]+." |
| 52 | + "&client_id=111\\.222" |
| 53 | + "&scope=chat:write,commands" |
| 54 | + "&user_scope=" |
| 55 | + ).match(url) |
| 56 | + is not None |
| 57 | + ) |
| 58 | + |
| 59 | + def test_handle_callback(self): |
| 60 | + oauth_flow = OAuthFlow.sqlite3( |
| 61 | + client=WebClient(base_url=self.mock_api_server_base_url), |
| 62 | + database="./logs/test_db", |
| 63 | + client_id="111.222", |
| 64 | + client_secret="xxx", |
| 65 | + scopes=["chat:write", "commands"], |
| 66 | + success_url="https://www.example.com/completion", |
| 67 | + failure_url="https://www.example.com/failure", |
| 68 | + ) |
| 69 | + state = oauth_flow.issue_new_state(None) |
| 70 | + req = BoltRequest( |
| 71 | + body="", |
| 72 | + query=f"code=foo&state={state}", |
| 73 | + headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]}, |
| 74 | + ) |
| 75 | + resp = oauth_flow.handle_callback(req) |
| 76 | + assert resp.status == 200 |
| 77 | + assert "https://www.example.com/completion" in resp.body |
| 78 | + |
| 79 | + def test_handle_callback_invalid_state(self): |
| 80 | + oauth_flow = OAuthFlow.sqlite3( |
| 81 | + client=WebClient(base_url=self.mock_api_server_base_url), |
| 82 | + database="./logs/test_db", |
| 83 | + client_id="111.222", |
| 84 | + client_secret="xxx", |
| 85 | + scopes=["chat:write", "commands"], |
| 86 | + ) |
| 87 | + state = oauth_flow.issue_new_state(None) |
| 88 | + req = BoltRequest( |
| 89 | + body="", |
| 90 | + query=f"code=foo&state=invalid", |
| 91 | + headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]}, |
| 92 | + ) |
| 93 | + resp = oauth_flow.handle_callback(req) |
| 94 | + assert resp.status == 400 |
| 95 | + |
| 96 | + def test_handle_callback_using_options(self): |
| 97 | + def success(args: SuccessArgs) -> BoltResponse: |
| 98 | + assert args.request is not None |
| 99 | + return BoltResponse(status=200, body="customized") |
| 100 | + |
| 101 | + def failure(args: FailureArgs) -> BoltResponse: |
| 102 | + assert args.request is not None |
| 103 | + assert args.reason is not None |
| 104 | + return BoltResponse(status=502, body="customized") |
| 105 | + |
| 106 | + oauth_flow = OAuthFlow.sqlite3( |
| 107 | + client=WebClient(base_url=self.mock_api_server_base_url), |
| 108 | + database="./logs/test_db", |
| 109 | + client_id="111.222", |
| 110 | + client_secret="xxx", |
| 111 | + scopes=["chat:write", "commands"], |
| 112 | + callback_options=CallbackOptions(success=success, failure=failure), |
| 113 | + ) |
| 114 | + state = oauth_flow.issue_new_state(None) |
| 115 | + req = BoltRequest( |
| 116 | + body="", |
| 117 | + query=f"code=foo&state={state}", |
| 118 | + headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]}, |
| 119 | + ) |
| 120 | + resp = oauth_flow.handle_callback(req) |
| 121 | + assert resp.status == 200 |
| 122 | + assert resp.body == "customized" |
| 123 | + |
| 124 | + state = oauth_flow.issue_new_state(None) |
| 125 | + req = BoltRequest( |
| 126 | + body="", |
| 127 | + query=f"code=foo&state=invalid", |
| 128 | + headers={"cookie": [f"{oauth_flow.settings.state_cookie_name}={state}"]}, |
| 129 | + ) |
| 130 | + resp = oauth_flow.handle_callback(req) |
| 131 | + assert resp.status == 502 |
| 132 | + assert resp.body == "customized" |
0 commit comments