Skip to content

Commit

Permalink
fix: handle cases where attachements may be None
Browse files Browse the repository at this point in the history
  • Loading branch information
gcharest authored Sep 17, 2024
1 parent 4937cdc commit 9d1fd3d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
14 changes: 9 additions & 5 deletions app/server/server.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down
76 changes: 74 additions & 2 deletions app/tests/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,18 @@ 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
type(payload).text = PropertyMock(return_value="text")
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(
[
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9d1fd3d

Please sign in to comment.