updated spiffworkflow-connector-command
This commit is contained in:
parent
49477e0873
commit
2df500f83c
|
@ -326,7 +326,7 @@ typing-extensions = "^4.8.0"
|
||||||
type = "git"
|
type = "git"
|
||||||
url = "https://github.com/sartography/spiffworkflow-connector-command.git"
|
url = "https://github.com/sartography/spiffworkflow-connector-command.git"
|
||||||
reference = "main"
|
reference = "main"
|
||||||
resolved_reference = "88bcbafdb1ffbf3f85fa9f13e05e41ace48b0988"
|
resolved_reference = "022c01826e98fd3997ec21652c88c1343279b6f4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tomli"
|
name = "tomli"
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
"""Send message to a slack channel."""
|
"""Send message to a slack channel."""
|
||||||
|
import json
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import requests # type: ignore
|
import requests # type: ignore
|
||||||
from spiffworkflow_connector_command.command_interface import CommandErrorDict
|
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 ConnectorCommand
|
||||||
from spiffworkflow_connector_command.command_interface import ConnectorProxyResponseDict
|
from spiffworkflow_connector_command.command_interface import ConnectorProxyResponseDict
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ class PostMessage(ConnectorCommand):
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
self.message = message
|
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}",
|
headers = {"Authorization": f"Bearer {self.token}",
|
||||||
"Content-type": "application/json"}
|
"Content-type": "application/json"}
|
||||||
|
@ -60,14 +61,16 @@ class PostMessage(ConnectorCommand):
|
||||||
error = {"error_code": exception.__class__.__name__, "message": str(exception)}
|
error = {"error_code": exception.__class__.__name__, "message": str(exception)}
|
||||||
status = 500
|
status = 500
|
||||||
|
|
||||||
return_response: ConnectorProxyResponseDict = {
|
return_response: CommandResponseDict = {
|
||||||
"command_response": command_response,
|
"body": json.dumps(command_response),
|
||||||
"error": error,
|
|
||||||
}
|
|
||||||
result: CommandResultDictV2 = {
|
|
||||||
"response": return_response,
|
|
||||||
"status": status,
|
|
||||||
"mimetype": "application/json",
|
"mimetype": "application/json",
|
||||||
|
"http_status": status,
|
||||||
|
}
|
||||||
|
result: ConnectorProxyResponseDict = {
|
||||||
|
"command_response": return_response,
|
||||||
|
"error": error,
|
||||||
|
"command_response_version": 2,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
@ -32,9 +33,11 @@ class TestPostMessage:
|
||||||
mock_post.return_value.json = MagicMock(return_value=success_response)
|
mock_post.return_value.json = MagicMock(return_value=success_response)
|
||||||
poster = PostMessage('xxx', 'my_channel', 'hello world!')
|
poster = PostMessage('xxx', 'my_channel', 'hello world!')
|
||||||
response = poster.execute({}, {})
|
response = poster.execute({}, {})
|
||||||
assert response['status'] == 200
|
assert response['command_response'] == {
|
||||||
assert response['mimetype'] == "application/json"
|
"body": json.dumps(success_response),
|
||||||
assert response['response']['command_response'] == success_response
|
"mimetype": "application/json",
|
||||||
|
"http_status": 200
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_connection_error(self) -> None:
|
def test_connection_error(self) -> None:
|
||||||
|
@ -42,12 +45,15 @@ class TestPostMessage:
|
||||||
mock_post.return_value.status_code = 404
|
mock_post.return_value.status_code = 404
|
||||||
poster = PostMessage('xxx', 'my_channel', 'hello world!')
|
poster = PostMessage('xxx', 'my_channel', 'hello world!')
|
||||||
response = poster.execute({}, {})
|
response = poster.execute({}, {})
|
||||||
assert response['status'] == 404
|
assert response['command_response'] == {
|
||||||
assert response['mimetype'] == "application/json"
|
"body": '{}',
|
||||||
assert response["response"]["error"] is not None
|
"mimetype": "application/json",
|
||||||
assert "error_code" in response["response"]["error"]
|
"http_status": 404
|
||||||
assert response["response"]["error"]["error_code"] == "SlackMessageFailed"
|
}
|
||||||
assert response["response"]["error"]["message"] == "Unreadable (non JSON) response from Slack"
|
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:
|
def test_error_from_slack(self) -> None:
|
||||||
example_error = {'ok': False, 'error': 'invalid_arguments', 'warning': 'missing_charset',
|
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)
|
mock_post.return_value.json = MagicMock(return_value=example_error)
|
||||||
poster = PostMessage('xxx', 'my_channel', 'hello world!')
|
poster = PostMessage('xxx', 'my_channel', 'hello world!')
|
||||||
response = poster.execute({}, {})
|
response = poster.execute({}, {})
|
||||||
assert response['status'] == 400
|
assert response['command_response'] == {
|
||||||
assert response['mimetype'] == "application/json"
|
"body": '{}',
|
||||||
assert response["response"]["error"] is not None
|
"mimetype": "application/json",
|
||||||
assert "error_code" in response["response"]["error"]
|
"http_status": 400
|
||||||
assert response["response"]["error"]["error_code"] == "SlackMessageFailed"
|
}
|
||||||
assert response["response"]["error"]["message"] == "[ERROR] missing required field: channel"
|
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"
|
||||||
|
|
Loading…
Reference in New Issue