Skip to content

Commit

Permalink
test: try to implement websocket server 409 integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Maj committed Nov 22, 2024
1 parent f2bb2d2 commit 2e7dc79
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/adapter_tests/socket_mode/mock_socket_mode_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def reset_server_state():

test.reset_server_state = reset_server_state

async def error(request: web.Request):
return web.Response(status=409, text="Unexpected server response: 409")

async def health(request: web.Request):
wr = web.Response()
await wr.prepare(request)
Expand Down Expand Up @@ -56,6 +59,7 @@ async def link(request: web.Request):
app.add_routes(
[
web.get("/link", link),
web.get("/error", error),
web.get("/health", health),
]
)
Expand Down
73 changes: 73 additions & 0 deletions tests/adapter_tests/socket_mode/test_websocket_server_failures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import logging
import time

from slack_sdk import WebClient

from slack_bolt import App
from slack_bolt.adapter.socket_mode.websocket_client import SocketModeHandler
from .mock_socket_mode_server import (
start_socket_mode_server,
stop_socket_mode_server,
)
from .mock_web_api_server import (
setup_mock_web_api_server,
cleanup_mock_web_api_server,
)
from ...utils import remove_os_env_temporarily, restore_os_env


class TestSocketModeServerFailures:
logger = logging.getLogger(__name__)

def setup_method(self):
self.old_os_env = remove_os_env_temporarily()
setup_mock_web_api_server(self)
self.web_client = WebClient(
token="xoxb-api_test",
base_url="http://localhost:8888",
)
start_socket_mode_server(self, 3012)

def teardown_method(self):
cleanup_mock_web_api_server(self)
restore_os_env(self.old_os_env)
stop_socket_mode_server(self)

def test_interactions(self):

app = App(client=self.web_client)

result = {"shortcut": False, "command": False}

@app.shortcut("do-something")
def shortcut_handler(ack):
result["shortcut"] = True
ack()

@app.command("/hello-socket-mode")
def command_handler(ack):
result["command"] = True
ack()

handler = SocketModeHandler(
app_token="xapp-A111-222-xyz",
app=app,
trace_enabled=True,
)
try:
handler.client.wss_uri = "ws://localhost:3012/error"
handler.client.default_auto_reconnect_enabled = True

handler.connect()
time.sleep(2) # wait for the message receiver
assert handler.client.is_connected() is True
logging.info("is client connected?")
logging.info(handler.client.is_connected())

# handler.client.send_message("foo")

# time.sleep(2)
# assert result["shortcut"] is True
# assert result["command"] is True
finally:
handler.client.close()

0 comments on commit 2e7dc79

Please sign in to comment.