From 8cbcd433e9d2866b26050bad7ebe30131f96a925 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 31 May 2024 21:36:43 -0700 Subject: [PATCH] Added client impl to fetch domains by team id --- src/controller/home.py | 4 +--- src/services/switcher_service.py | 13 +++++++++++++ tests/test_slack_events.py | 8 +++++++- tests/test_switcher_controller.py | 22 +++++++++++++++++----- tests/utils/mock_request.py | 15 +++++++++++++++ 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/controller/home.py b/src/controller/home.py index 54eedb8..a1c955b 100644 --- a/src/controller/home.py +++ b/src/controller/home.py @@ -28,9 +28,7 @@ def on_change_request_opened(ack, body, client, logger): team_id = body["team"]["id"] logger.warning(f"Team ID: {team_id}") - domains = [ - {"name": "Domain Name", "id": 1} - ] + domains = SwitcherService().get_domains(team_id) # Populate view populate_selection( diff --git a/src/services/switcher_service.py b/src/services/switcher_service.py index c1dbbe6..721b11d 100644 --- a/src/services/switcher_service.py +++ b/src/services/switcher_service.py @@ -14,6 +14,19 @@ def __init__(self, *, api_url: Optional[str] = None): api_url or os.environ.get("SWITCHER_API_URL") ) + def get_domains(self, team_id: str) -> [dict]: + response = self.do_get( + path = "/slack/v1/domains", + params = { + "team_id": team_id + } + ) + + if response.status_code != 200: + raise SwitcherValidationError("Error fetching domains") + + return json.loads(response.data.decode('UTF-8')) + def get_environments(self, team_id: str, domain_id: str) -> [str]: response: dict = self.do_graphql(f''' query {{ diff --git a/tests/test_slack_events.py b/tests/test_slack_events.py index 247ec1d..d30807d 100644 --- a/tests/test_slack_events.py +++ b/tests/test_slack_events.py @@ -10,7 +10,8 @@ mock_event_handler, mock_base_client, mock_gql_client, - mock_switcher_client + mock_switcher_client, + set_mock_response ) from tests.fixtures.change_request import ( SLACK_EVENT, @@ -48,6 +49,11 @@ def test_open_app_home(client): @mock_event_handler @mock_base_client(MODAL_REQUEST) def test_open_change_request_modal(client): + set_mock_response( + path = "api/slack/v1/domains", + fixture = [{ 'name': 'Domain Name', 'id': '1' }] + ) + response = client.post( SLACK_EVENT, json = build_request_view( actions_fixture = build_static_select_action_value( diff --git a/tests/test_switcher_controller.py b/tests/test_switcher_controller.py index 96033d8..f10d5a0 100644 --- a/tests/test_switcher_controller.py +++ b/tests/test_switcher_controller.py @@ -60,11 +60,7 @@ def test_open_app_home(client): assert result == APP_HOME -@mock_gql_client({ - 'configuration': { - 'domain': [{'name': 'Domain', 'id': '1'}] - } -}) +@mock_switcher_client('get', [{ 'name': 'Domain Name', 'id': '1' }]) def test_open_change_request_modal(client): result = on_change_request_opened( ack = Mock(), @@ -83,6 +79,22 @@ def test_open_change_request_modal(client): assert result == expected_result +@mock_switcher_client('get', { 'error': 'Server unavailable' }, 500) +def test_open_change_request_modal_with_error(client): + result = on_change_request_opened( + ack = Mock(), + body = build_request_view( + actions_fixture = build_buttom_action_value( + action_id = "change_request", + text = "Request Change" + ) + ), + client = client, + logger = logging.getLogger() + ) + + assert result == None + @mock_gql_client({ 'configuration': { 'environments': ['default'] diff --git a/tests/utils/mock_request.py b/tests/utils/mock_request.py index 1b60d0c..6e4461f 100644 --- a/tests/utils/mock_request.py +++ b/tests/utils/mock_request.py @@ -16,6 +16,16 @@ from tests.fixtures.installation import INSTALLATION_FIX1 from tests.fixtures.change_request import get_slack_events_response +mock_response = mock.Mock(**{}) +mock_response_path = "" + +def set_mock_response(path: str, fixture: dict, status_code: int = 200): + global mock_response + global mock_response_path + + mock_response_path = path + mock_response = mock_requests_factory(json.dumps(fixture), status_code) + def mock_requests_factory(response_stub: str, status_code: int = 200): return mock.Mock(**{ "json.return_value": json.loads(response_stub), @@ -32,6 +42,9 @@ def mock_source_api(*args, **kwargs): if kwargs["url"].endswith("slack/v1/findinstallation"): return mock_requests_factory(json.dumps(INSTALLATION_FIX1), 200) + if kwargs["url"].endswith(mock_response_path): + return mock_response + def mock_event_handler(fn): """Bypass content verification and find installation""" @wraps(fn) @@ -39,6 +52,8 @@ def wrapper(client, *args, **kwargs): WebClient.views_publish = Mock() WebClient.chat_postMessage = Mock() WebClient.chat_update = Mock() + WebClient.views_open = Mock() + with ( patch.object(SignatureVerifier, "is_valid", return_value = True), patch.object(requests, "get", side_effect = mock_source_api),