2023-01-09 14:53:54 +01:00
|
|
|
import json
|
2023-01-09 14:28:22 +01:00
|
|
|
|
2023-01-09 14:53:54 +01:00
|
|
|
import httpretty
|
|
|
|
|
import pytest
|
|
|
|
|
|
2023-01-09 16:03:32 +01:00
|
|
|
from lolafect.slack import send_message_to_slack_channel, SendSlackMessageTask
|
2023-01-09 14:53:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@httpretty.activate(allow_net_connect=False, verbose=True)
|
2023-01-09 14:28:22 +01:00
|
|
|
def test_send_message_to_slack_channel_works_properly():
|
2023-01-09 14:53:54 +01:00
|
|
|
mock_webhook_url = "http://the-webhook-url.com"
|
|
|
|
|
mock_message_to_channel = "Hi there!"
|
|
|
|
|
httpretty.register_uri(method=httpretty.POST, uri=mock_webhook_url, status=200)
|
2023-01-09 14:28:22 +01:00
|
|
|
|
2023-01-09 14:53:54 +01:00
|
|
|
send_message_to_slack_channel(
|
|
|
|
|
webhook_url=mock_webhook_url, text_to_send=mock_message_to_channel
|
|
|
|
|
)
|
2023-01-09 14:28:22 +01:00
|
|
|
|
2023-01-09 14:53:54 +01:00
|
|
|
request_observed_by_server = httpretty.last_request()
|
|
|
|
|
|
|
|
|
|
assert (request_observed_by_server.method == "POST") and (
|
|
|
|
|
json.loads(request_observed_by_server.body.decode("UTF-8"))
|
|
|
|
|
== {"text": mock_message_to_channel}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@httpretty.activate(allow_net_connect=False, verbose=True)
|
|
|
|
|
def test_send_message_to_slack_channel_response_400_raises_exception():
|
|
|
|
|
mock_webhook_url = "http://the-webhook-url.com"
|
|
|
|
|
mock_message_to_channel = "Hi there!"
|
|
|
|
|
httpretty.register_uri(method=httpretty.POST, uri=mock_webhook_url, status=400)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
send_message_to_slack_channel(
|
|
|
|
|
webhook_url=mock_webhook_url, text_to_send=mock_message_to_channel
|
|
|
|
|
)
|
2023-01-09 14:28:22 +01:00
|
|
|
|
|
|
|
|
|
2023-01-09 16:03:32 +01:00
|
|
|
@httpretty.activate(allow_net_connect=False, verbose=True)
|
|
|
|
|
def test_slack_task_reaches_server_successfully():
|
|
|
|
|
mock_webhook_url = "http://the-webhook-url.com"
|
|
|
|
|
mock_message_to_channel = "Hi there!"
|
|
|
|
|
httpretty.register_uri(method=httpretty.POST, uri=mock_webhook_url, status=200)
|
|
|
|
|
|
|
|
|
|
slack_task = SendSlackMessageTask()
|
|
|
|
|
slack_task.run(webhook_url=mock_webhook_url, text_to_send=mock_message_to_channel)
|
2023-01-09 14:28:22 +01:00
|
|
|
|
2023-01-09 16:03:32 +01:00
|
|
|
request_observed_by_server = httpretty.last_request()
|
|
|
|
|
assert (request_observed_by_server.method == "POST") and (
|
|
|
|
|
json.loads(request_observed_by_server.body.decode("UTF-8"))
|
|
|
|
|
== {"text": mock_message_to_channel}
|
|
|
|
|
)
|
2023-01-09 14:28:22 +01:00
|
|
|
|
2023-01-09 16:03:32 +01:00
|
|
|
|
|
|
|
|
@httpretty.activate(allow_net_connect=False, verbose=True)
|
|
|
|
|
def test_slack_task_raises_value_error():
|
|
|
|
|
mock_webhook_url = "http://the-webhook-url.com"
|
|
|
|
|
mock_message_to_channel = "Hi there!"
|
|
|
|
|
httpretty.register_uri(method=httpretty.POST, uri=mock_webhook_url, status=400)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
slack_task = SendSlackMessageTask()
|
|
|
|
|
slack_task.run(
|
|
|
|
|
webhook_url=mock_webhook_url, text_to_send=mock_message_to_channel
|
|
|
|
|
)
|