Skip to content

Commit

Permalink
Added client impl to fetch domains by team id (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki authored Jun 1, 2024
1 parent 45a24c7 commit f2f144e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/controller/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions src/services/switcher_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {{
Expand Down
8 changes: 7 additions & 1 deletion tests/test_slack_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 17 additions & 5 deletions tests/test_switcher_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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']
Expand Down
15 changes: 15 additions & 0 deletions tests/utils/mock_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -32,13 +42,18 @@ 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)
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),
Expand Down

0 comments on commit f2f144e

Please sign in to comment.