|
| 1 | +import asyncio |
| 2 | +import re |
| 3 | + |
| 4 | +import pytest |
| 5 | +from slack_sdk.oauth.installation_store import FileInstallationStore |
| 6 | +from slack_sdk.oauth.state_store import FileOAuthStateStore |
| 7 | +from slack_sdk.web.async_client import AsyncWebClient |
| 8 | + |
| 9 | +from slack_bolt.oauth.async_oauth_flow import AsyncOAuthFlow |
| 10 | +from slack_bolt.request.async_request import AsyncBoltRequest |
| 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 TestAsyncOAuthFlow: |
| 18 | + mock_api_server_base_url = "http://localhost:8888" |
| 19 | + |
| 20 | + @pytest.fixture |
| 21 | + def event_loop(self): |
| 22 | + setup_mock_web_api_server(self) |
| 23 | + loop = asyncio.get_event_loop() |
| 24 | + yield loop |
| 25 | + loop.close() |
| 26 | + cleanup_mock_web_api_server(self) |
| 27 | + |
| 28 | + def next(self): |
| 29 | + pass |
| 30 | + |
| 31 | + @pytest.mark.asyncio |
| 32 | + async def test_instantiation(self): |
| 33 | + oauth_flow = AsyncOAuthFlow( |
| 34 | + client_id="111.222", |
| 35 | + client_secret="xxx", |
| 36 | + scopes=["chat:write", "commands"], |
| 37 | + installation_store=FileInstallationStore(), |
| 38 | + oauth_state_store=FileOAuthStateStore(expiration_seconds=120), |
| 39 | + ) |
| 40 | + assert oauth_flow is not None |
| 41 | + |
| 42 | + @pytest.mark.asyncio |
| 43 | + async def test_handle_installation(self): |
| 44 | + oauth_flow = AsyncOAuthFlow( |
| 45 | + client_id="111.222", |
| 46 | + client_secret="xxx", |
| 47 | + scopes=["chat:write", "commands"], |
| 48 | + installation_store=FileInstallationStore(), |
| 49 | + oauth_state_store=FileOAuthStateStore(expiration_seconds=120), |
| 50 | + ) |
| 51 | + req = AsyncBoltRequest(body="") |
| 52 | + resp = await oauth_flow.handle_installation(req) |
| 53 | + assert resp.status == 302 |
| 54 | + url = resp.headers["location"][0] |
| 55 | + assert ( |
| 56 | + re.compile( |
| 57 | + "https://slack.com/oauth/v2/authorize\\?state=[-0-9a-z]+." |
| 58 | + "&client_id=111\\.222" |
| 59 | + "&scope=chat:write,commands" |
| 60 | + "&user_scope=" |
| 61 | + ).match(url) |
| 62 | + is not None |
| 63 | + ) |
| 64 | + |
| 65 | + @pytest.mark.asyncio |
| 66 | + async def test_handle_callback(self): |
| 67 | + oauth_flow = AsyncOAuthFlow( |
| 68 | + client=AsyncWebClient(base_url=self.mock_api_server_base_url), |
| 69 | + client_id="111.222", |
| 70 | + client_secret="xxx", |
| 71 | + scopes=["chat:write", "commands"], |
| 72 | + installation_store=FileInstallationStore(), |
| 73 | + oauth_state_store=FileOAuthStateStore(expiration_seconds=120), |
| 74 | + success_url="https://www.example.com/completion", |
| 75 | + failure_url="https://www.example.com/failure", |
| 76 | + ) |
| 77 | + state = await oauth_flow.issue_new_state(None) |
| 78 | + req = AsyncBoltRequest( |
| 79 | + body="", |
| 80 | + query=f"code=foo&state={state}", |
| 81 | + headers={"cookie": [f"{oauth_flow.oauth_state_cookie_name}={state}"]}, |
| 82 | + ) |
| 83 | + resp = await oauth_flow.handle_callback(req) |
| 84 | + assert resp.status == 200 |
| 85 | + assert "https://www.example.com/completion" in resp.body |
| 86 | + |
| 87 | + @pytest.mark.asyncio |
| 88 | + async def test_handle_callback_invalid_state(self): |
| 89 | + oauth_flow = AsyncOAuthFlow( |
| 90 | + client_id="111.222", |
| 91 | + client_secret="xxx", |
| 92 | + scopes=["chat:write", "commands"], |
| 93 | + installation_store=FileInstallationStore(), |
| 94 | + oauth_state_store=FileOAuthStateStore(expiration_seconds=120), |
| 95 | + ) |
| 96 | + state = await oauth_flow.issue_new_state(None) |
| 97 | + req = AsyncBoltRequest( |
| 98 | + body="", |
| 99 | + query=f"code=foo&state=invalid", |
| 100 | + headers={"cookie": [f"{oauth_flow.oauth_state_cookie_name}={state}"]}, |
| 101 | + ) |
| 102 | + resp = await oauth_flow.handle_callback(req) |
| 103 | + assert resp.status == 400 |
0 commit comments