diff --git a/app.py b/app.py index 78c530f..6945faa 100644 --- a/app.py +++ b/app.py @@ -23,8 +23,23 @@ app = Flask(__name__) -slack_events_adapter = SlackEventAdapter(os.environ["SIGNING_SECRET"], "/slack/events", app) -client = WebClient(token=os.environ["SLACK_TOKEN"]) + +def create_slack_event_adapter(signing_secret, endpoint, app): + if not signing_secret: + raise ValueError("SIGNING_SECRET environment variable is not set.") + return SlackEventAdapter(signing_secret, endpoint, app) + + +def create_slack_client(token): + if not token: + raise ValueError("SLACK_TOKEN environment variable is not set.") + return WebClient(token=token) + + +slack_events_adapter = create_slack_event_adapter( + os.getenv("SIGNING_SECRET"), "/slack/events", app +) +client = create_slack_client(os.getenv("SLACK_TOKEN")) client.chat_postMessage(channel=DEPLOYS_CHANNEL_NAME, text="bot started v1.9 240611-1 top") diff --git a/tests/test_welcome_plugin.py b/tests/test_welcome_plugin.py index 82f16e7..b331b60 100644 --- a/tests/test_welcome_plugin.py +++ b/tests/test_welcome_plugin.py @@ -3,7 +3,7 @@ import pytest -from app import handle_team_join +from app import create_slack_client, create_slack_event_adapter, handle_team_join JOINS_CHANNEL_ID = "C06RMMRMGHE" @@ -60,36 +60,60 @@ def expected_message(): ) -def test_handle_team_join_successful(mocker, event_data, expected_message): - # event_data={ - # "event": { - # "user": { - # "id": "D0730R9KFC2" - # } - # } - # } - mock_client = mocker.patch("app.client") - # Mock responses for chat_postMessage and conversations_open - mock_client.chat_postMessage.return_value = {"ok": True} - mock_client.conversations_open.return_value = {"channel": {"id": "C06RBJ779CH"}} +@pytest.fixture +def slack_event_adapter_mock(): + with mock.patch("app.SlackEventAdapter") as mock_adapter: + yield mock_adapter + + +@pytest.fixture +def slack_client_mock(): + with mock.patch("app.WebClient") as mock_client: + yield mock_client + + +@pytest.fixture +def slack_event_adapter(): + return create_slack_event_adapter("xapp-token", "/slack/events", None) - mock_open_file = mocker.mock_open(read_data=expected_message) - mocker.patch("builtins.open", mock_open_file) +@pytest.fixture +def slack_client(): + return create_slack_client("xoxb-token") + + +def test_handle_team_join( + setenvvar, + slack_event_adapter_mock, + slack_client_mock, + event_data, + slack_client, +): + # Configure the mock Slack client to return a specific response for conversations_open + slack_client_mock.conversations_open.return_value = {"channel": {"id": "mock_channel_id"}} + + with open("welcome_message.txt", "r", encoding="utf-8") as file: + welcome_message_template = file.read() + + # Call the handle_team_join function with the event data handle_team_join(event_data) - # Assert that the chat_postMessage was called with the correct parameters - mock_client.chat_postMessage.assert_any_call( + # Check that the Slack client methods were called as expected + slack_client_mock.chat_postMessage.assert_called_once_with( channel=JOINS_CHANNEL_ID, text="<@D0730R9KFC2> joined the team." ) - mock_client.conversations_open.assert_called_once_with(users=["D0730R9KFC2"]) - welcome_message = expected_message.format(user_id="D0730R9KFC2") - blocks = [{"type": "section", "text": {"type": "mrkdwn", "text": welcome_message.strip()}}] - mock_client.chat_postMessage.assert_any_call( - channel="C06RBJ779CH", text="Welcome to the OWASP Slack Community!", blocks=blocks + # Check that the direct message was sent + slack_client_mock.chat_postMessage.assert_any_call( + channel="mock_channel_id", + text="Welcome to the OWASP Slack Community!", + blocks=[ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": welcome_message_template.format(user_id="D0730R9KFC2").strip(), + }, + } + ], ) - - -if __name__ == "__main__": - pytest.main()