updated spiffworkflow-connector-command

This commit is contained in:
jasquat 2023-10-18 09:37:32 -04:00
parent 49477e0873
commit 2df500f83c
No known key found for this signature in database
3 changed files with 37 additions and 25 deletions

2
poetry.lock generated
View File

@ -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"

View File

@ -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

View File

@ -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"