Merge pull request #2 from lolamarket/slack-tests

Slack tests
This commit is contained in:
pablolola 2023-01-09 16:03:59 +01:00 committed by GitHub
commit b360256713
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 1 deletions

View file

@ -1,4 +1,5 @@
prefect==1.2.2
requests==2.28.1
boto3==1.26.40
pytest==7.2.0
pytest==7.2.0
httpretty==1.1.4

65
tests/test_slack.py Normal file
View file

@ -0,0 +1,65 @@
import json
import httpretty
import pytest
from lolafect.slack import send_message_to_slack_channel, SendSlackMessageTask
@httpretty.activate(allow_net_connect=False, verbose=True)
def test_send_message_to_slack_channel_works_properly():
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)
send_message_to_slack_channel(
webhook_url=mock_webhook_url, text_to_send=mock_message_to_channel
)
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
)
@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)
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_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
)