Skip to content

Commit

Permalink
updated spiffworkflow-connector-command
Browse files Browse the repository at this point in the history
  • Loading branch information
jasquat committed Oct 18, 2023
1 parent 49477e0 commit 2df500f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions src/connector_slack/commands/post_message.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -25,7 +26,7 @@ def __init__(self, token: str, channel: str, message: str):
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"}
Expand Down Expand Up @@ -60,14 +61,16 @@ def execute(self, _config: Any, _task_data: Any) -> CommandResultDictV2:
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
39 changes: 24 additions & 15 deletions tests/connector_slack/unit/test_post_message.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from unittest.mock import MagicMock
from unittest.mock import patch

Expand Down Expand Up @@ -32,22 +33,27 @@ def test_successful_post(self) -> None:
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:
with patch("requests.post") as mock_post:
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',
Expand All @@ -59,9 +65,12 @@ def test_error_from_slack(self) -> None:
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"

0 comments on commit 2df500f

Please sign in to comment.