diff --git a/app/server/server.py b/app/server/server.py index 519b54a2..f74af1b4 100644 --- a/app/server/server.py +++ b/app/server/server.py @@ -1,10 +1,10 @@ import json import logging import os -import requests +import requests # type: ignore from starlette.config import Config -from authlib.integrations.starlette_client import OAuth, OAuthError +from authlib.integrations.starlette_client import OAuth, OAuthError # type: ignore from starlette.middleware.sessions import SessionMiddleware from starlette.responses import RedirectResponse, HTMLResponse from fastapi.responses import JSONResponse @@ -26,7 +26,7 @@ from integrations.sentinel import log_to_sentinel from integrations import maxmind from server.event_handlers import aws -from sns_message_validator import ( +from sns_message_validator import ( # type: ignore SNSMessageValidator, ) from fastapi import Depends @@ -424,8 +424,12 @@ def get_version(request: Request): return {"version": os.environ.get("GIT_SHA", "unknown")} -def append_incident_buttons(payload, webhook_id): - payload.attachments = payload.attachments + [ +def append_incident_buttons(payload: WebhookPayload, webhook_id): + if payload.attachments is None: + payload.attachments = [] + elif isinstance(payload.attachments, str): + payload.attachments = [payload.attachments] + payload.attachments += [ { "fallback": "Incident", "callback_id": "handle_incident_action_buttons", diff --git a/app/tests/server/test_server.py b/app/tests/server/test_server.py index 24726600..e3af3c59 100644 --- a/app/tests/server/test_server.py +++ b/app/tests/server/test_server.py @@ -367,7 +367,7 @@ def test_get_version_known(): assert response.json() == {"version": "foo"} -def test_append_incident_buttons(): +def test_append_incident_buttons_with_list_attachments(): payload = MagicMock() attachments = PropertyMock(return_value=[]) type(payload).attachments = attachments @@ -375,8 +375,10 @@ def test_append_incident_buttons(): webhook_id = "bar" resp = server.append_incident_buttons(payload, webhook_id) assert payload == resp - assert attachments.call_count == 2 + assert attachments.call_count == 4 assert attachments.call_args_list == [ + call(), + call(), call(), call( [ @@ -407,6 +409,76 @@ def test_append_incident_buttons(): ] +def test_append_incident_buttons_with_none_attachments(): + payload = MagicMock() + payload.attachments = None + payload.text = "text" + webhook_id = "bar" + + resp = server.append_incident_buttons(payload, webhook_id) + + assert payload == resp + assert payload.attachments == [ + { + "fallback": "Incident", + "callback_id": "handle_incident_action_buttons", + "color": "#3AA3E3", + "attachment_type": "default", + "actions": [ + { + "name": "call-incident", + "text": "🎉 Call incident ", + "type": "button", + "value": "text", + "style": "primary", + }, + { + "name": "ignore-incident", + "text": "🙈 Acknowledge and ignore", + "type": "button", + "value": "bar", + "style": "default", + }, + ], + } + ] + + +def test_append_incident_buttons_with_str_attachments(): + payload = MagicMock() + payload.attachments = "existing_attachment" + payload.text = "text" + webhook_id = "bar" + + resp = server.append_incident_buttons(payload, webhook_id) + assert payload == resp + assert payload.attachments == [ + "existing_attachment", + { + "fallback": "Incident", + "callback_id": "handle_incident_action_buttons", + "color": "#3AA3E3", + "attachment_type": "default", + "actions": [ + { + "name": "call-incident", + "text": "🎉 Call incident ", + "type": "button", + "value": "text", + "style": "primary", + }, + { + "name": "ignore-incident", + "text": "🙈 Acknowledge and ignore", + "type": "button", + "value": "bar", + "style": "default", + }, + ], + }, + ] + + # Unit test the react app def test_react_app(): # test the react app