From ebca789932d678fa1b13c149b0297e6ef55b3b72 Mon Sep 17 00:00:00 2001 From: Guillaume Charest <1690085+gcharest@users.noreply.github.com> Date: Fri, 9 Feb 2024 12:00:22 -0500 Subject: [PATCH] Refactor/module atip (#405) * feat: move atip to its own module * feat: register atip module * fix: module tests imports --- app/main.py | 12 ++++------- app/modules/atip/__init__.py | 1 + app/{commands => modules/atip}/atip.py | 21 ++++++++++++++++++++ app/tests/{commands => modules}/test_atip.py | 10 +++++----- 4 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 app/modules/atip/__init__.py rename app/{commands => modules/atip}/atip.py (96%) rename app/tests/{commands => modules}/test_atip.py (98%) diff --git a/app/main.py b/app/main.py index 4f0fdf44..4e34f18e 100644 --- a/app/main.py +++ b/app/main.py @@ -5,8 +5,8 @@ from slack_bolt.adapter.socket_mode import SocketModeHandler from slack_bolt import App from dotenv import load_dotenv -from commands import atip, aws, incident, sre, role, google_service -from modules import secret +from commands import aws, incident, sre, role, google_service +from modules import secret, atip from commands.helpers import incident_helper, webhook_helper from server import bot_middleware, server @@ -32,12 +32,8 @@ def main(bot): bot.view("role_view")(role.role_view_handler) bot.action("role_change_locale")(role.update_modal_locale) - # Register ATIP commands - bot.command(f"/{PREFIX}atip")(atip.atip_command) - bot.command(f"/{PREFIX}aiprp")(atip.atip_command) - bot.action("ati_search_width")(atip.atip_width_action) - bot.view("atip_view")(atip.atip_view_handler) - bot.action("atip_change_locale")(atip.update_modal_locale) + # Register ATIP module + atip.register(bot) # Register AWS commands bot.command(f"/{PREFIX}aws")(aws.aws_command) diff --git a/app/modules/atip/__init__.py b/app/modules/atip/__init__.py new file mode 100644 index 00000000..c7ffa399 --- /dev/null +++ b/app/modules/atip/__init__.py @@ -0,0 +1 @@ +from .atip import register # noqa: F401 diff --git a/app/commands/atip.py b/app/modules/atip/atip.py similarity index 96% rename from app/commands/atip.py rename to app/modules/atip/atip.py index 3a8b1a74..3c1abb23 100644 --- a/app/commands/atip.py +++ b/app/modules/atip/atip.py @@ -11,6 +11,27 @@ i18n.set("locale", "en-US") i18n.set("fallback", "en-US") +PREFIX = os.environ.get("PREFIX", "") + + +def register(bot): + """ + Registers the ATIP commands and events in the Slack Bot. Import the ATIP module in the main.py file and call the register function. + + Example in main.py: + ``` + from modules import atip + + def main(bot): + atip.register(bot) + ``` + """ + bot.command(f"/{PREFIX}atip")(atip_command) + bot.command(f"/{PREFIX}aiprp")(atip_command) + bot.action("ati_search_width")(atip_width_action) + bot.view("atip_view")(atip_view_handler) + bot.action("atip_change_locale")(update_modal_locale) + def atip_command(ack, command, logger, respond, client, body): ack() diff --git a/app/tests/commands/test_atip.py b/app/tests/modules/test_atip.py similarity index 98% rename from app/tests/commands/test_atip.py rename to app/tests/modules/test_atip.py index f00d6e48..4447605d 100644 --- a/app/tests/commands/test_atip.py +++ b/app/tests/modules/test_atip.py @@ -1,4 +1,4 @@ -from commands import atip +from modules.atip import atip from unittest.mock import ANY, MagicMock, patch @@ -134,7 +134,7 @@ def test_atip_command_handles_unknown_command_FR_client(): ) -@patch("commands.atip.request_start_modal") +@patch("modules.atip.atip.request_start_modal") def test_atip_command_handles_access_EN_command(request_start_modal): ack = MagicMock() respond = MagicMock() @@ -147,7 +147,7 @@ def test_atip_command_handles_access_EN_command(request_start_modal): request_start_modal.assert_called_with(client, body, "en-US") -@patch("commands.atip.request_start_modal") +@patch("modules.atip.atip.request_start_modal") def test_atip_command_handles_access_FR_command(request_start_modal): ack = MagicMock() respond = MagicMock() @@ -160,7 +160,7 @@ def test_atip_command_handles_access_FR_command(request_start_modal): request_start_modal.assert_called_with(client, body, "fr-FR") -@patch("commands.atip.atip_modal_view") +@patch("modules.atip.atip.atip_modal_view") def test_atip_action_update_locale_to_FR(atip_modal_view): ack = MagicMock() client = MagicMock() @@ -171,7 +171,7 @@ def test_atip_action_update_locale_to_FR(atip_modal_view): atip_modal_view.assert_called_with("user_id", "", "fr-FR") -@patch("commands.atip.atip_modal_view") +@patch("modules.atip.atip.atip_modal_view") def test_atip_action_update_locale_to_EN(atip_modal_view): ack = MagicMock() client = MagicMock()