From 2e7dc799b123897fcd726ec28c58c3c50e34ed44 Mon Sep 17 00:00:00 2001 From: Filip Maj Date: Fri, 22 Nov 2024 13:23:11 -0500 Subject: [PATCH] test: try to implement websocket server 409 integration test --- .../socket_mode/mock_socket_mode_server.py | 4 + .../test_websocket_server_failures.py | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tests/adapter_tests/socket_mode/test_websocket_server_failures.py diff --git a/tests/adapter_tests/socket_mode/mock_socket_mode_server.py b/tests/adapter_tests/socket_mode/mock_socket_mode_server.py index 997657368..116072276 100644 --- a/tests/adapter_tests/socket_mode/mock_socket_mode_server.py +++ b/tests/adapter_tests/socket_mode/mock_socket_mode_server.py @@ -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) @@ -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), ] ) diff --git a/tests/adapter_tests/socket_mode/test_websocket_server_failures.py b/tests/adapter_tests/socket_mode/test_websocket_server_failures.py new file mode 100644 index 000000000..11fc0274d --- /dev/null +++ b/tests/adapter_tests/socket_mode/test_websocket_server_failures.py @@ -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()