diff --git a/poetry.lock b/poetry.lock index abf9375..40aec2f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -326,7 +326,7 @@ typing-extensions = "^4.8.0" type = "git" url = "https://github.com/sartography/spiffworkflow-connector-command.git" reference = "main" -resolved_reference = "88bcbafdb1ffbf3f85fa9f13e05e41ace48b0988" +resolved_reference = "022c01826e98fd3997ec21652c88c1343279b6f4" [[package]] name = "tomli" diff --git a/src/connector_slack/commands/post_message.py b/src/connector_slack/commands/post_message.py index c53d26d..2038f9e 100644 --- a/src/connector_slack/commands/post_message.py +++ b/src/connector_slack/commands/post_message.py @@ -1,9 +1,10 @@ """Send message to a slack channel.""" +import json from typing import Any import requests # type: ignore from spiffworkflow_connector_command.command_interface import CommandErrorDict -from spiffworkflow_connector_command.command_interface import CommandResultDictV2 +from spiffworkflow_connector_command.command_interface import CommandResponseDict from spiffworkflow_connector_command.command_interface import ConnectorCommand from spiffworkflow_connector_command.command_interface import ConnectorProxyResponseDict @@ -25,7 +26,7 @@ class PostMessage(ConnectorCommand): self.channel = channel self.message = message - def execute(self, _config: Any, _task_data: Any) -> CommandResultDictV2: + def execute(self, _config: Any, _task_data: Any) -> ConnectorProxyResponseDict: headers = {"Authorization": f"Bearer {self.token}", "Content-type": "application/json"} @@ -60,14 +61,16 @@ class PostMessage(ConnectorCommand): error = {"error_code": exception.__class__.__name__, "message": str(exception)} status = 500 - return_response: ConnectorProxyResponseDict = { - "command_response": command_response, - "error": error, - } - result: CommandResultDictV2 = { - "response": return_response, - "status": status, + return_response: CommandResponseDict = { + "body": json.dumps(command_response), "mimetype": "application/json", + "http_status": status, + } + result: ConnectorProxyResponseDict = { + "command_response": return_response, + "error": error, + "command_response_version": 2, + } return result diff --git a/tests/connector_slack/unit/test_post_message.py b/tests/connector_slack/unit/test_post_message.py index e620253..c64079f 100644 --- a/tests/connector_slack/unit/test_post_message.py +++ b/tests/connector_slack/unit/test_post_message.py @@ -1,3 +1,4 @@ +import json from unittest.mock import MagicMock from unittest.mock import patch @@ -32,9 +33,11 @@ class TestPostMessage: mock_post.return_value.json = MagicMock(return_value=success_response) poster = PostMessage('xxx', 'my_channel', 'hello world!') response = poster.execute({}, {}) - assert response['status'] == 200 - assert response['mimetype'] == "application/json" - assert response['response']['command_response'] == success_response + assert response['command_response'] == { + "body": json.dumps(success_response), + "mimetype": "application/json", + "http_status": 200 + } def test_connection_error(self) -> None: @@ -42,12 +45,15 @@ class TestPostMessage: mock_post.return_value.status_code = 404 poster = PostMessage('xxx', 'my_channel', 'hello world!') response = poster.execute({}, {}) - assert response['status'] == 404 - assert response['mimetype'] == "application/json" - assert response["response"]["error"] is not None - assert "error_code" in response["response"]["error"] - assert response["response"]["error"]["error_code"] == "SlackMessageFailed" - assert response["response"]["error"]["message"] == "Unreadable (non JSON) response from Slack" + assert response['command_response'] == { + "body": '{}', + "mimetype": "application/json", + "http_status": 404 + } + assert response["error"] is not None + assert "error_code" in response["error"] + assert response["error"]["error_code"] == "SlackMessageFailed" + assert response["error"]["message"] == "Unreadable (non JSON) response from Slack" def test_error_from_slack(self) -> None: example_error = {'ok': False, 'error': 'invalid_arguments', 'warning': 'missing_charset', @@ -59,9 +65,12 @@ class TestPostMessage: mock_post.return_value.json = MagicMock(return_value=example_error) poster = PostMessage('xxx', 'my_channel', 'hello world!') response = poster.execute({}, {}) - assert response['status'] == 400 - assert response['mimetype'] == "application/json" - assert response["response"]["error"] is not None - assert "error_code" in response["response"]["error"] - assert response["response"]["error"]["error_code"] == "SlackMessageFailed" - assert response["response"]["error"]["message"] == "[ERROR] missing required field: channel" + assert response['command_response'] == { + "body": '{}', + "mimetype": "application/json", + "http_status": 400 + } + assert response["error"] is not None + assert "error_code" in response["error"] + assert response["error"]["error_code"] == "SlackMessageFailed" + assert response["error"]["message"] == "[ERROR] missing required field: channel"