"""Tests for check_attachments_statuses script.""" import re from unittest.mock import patch from httpx import Response import pytest from scripts.check_attachments_statuses import post_message_to_slack from scripts.utils import ScriptException from moneyhub.constants.constants import StatementAttachmentType from tests.utils.factories import ReportCustomFactory from tests.utils.factories import StatementAttachmentFactory @patch('scripts.check_attachments_statuses.httpx.post') @patch('scripts.check_attachments_statuses.logging.info') def test_post_custom_reports_message_to_slack(mock_logging, mock_post): """Test post message with a valid text.""" mock_post.return_value = Response(status_code=200, text='ok') reports = [ ReportCustomFactory.build( report_custom_id=123, created_at='2010-09-08 07:06:05', ), ReportCustomFactory.build( report_custom_id=456, created_at='2011-10-09 08:07:06', ), ] link_to_post = 'mock_slack_endpoint' post_message_to_slack('custom reports', reports, link_to_post) expected_payload = { 'blocks': [ { 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': '*⚠️ The following custom reports are stuck:*\n' '* `123` (created: 2010-09-08 07:06:05)\n' '* `456` (created: 2011-10-09 08:07:06)\n' } } ] } mock_post.assert_called_once_with( link_to_post, json=expected_payload, ) mock_logging.assert_called_once_with('Message sent to Slack: ok') @patch('scripts.check_attachments_statuses.httpx.post') @patch('scripts.check_attachments_statuses.logging.info') def test_post_message_to_slack(mock_logging, mock_post): """Test post message with a valid text.""" mock_post.return_value = Response(status_code=200, text='ok') attachments = [ StatementAttachmentFactory.build( statement_attachment_type=StatementAttachmentType.REVENUE_DETAIL, statement_attachment_id=1, created_by='me', created_at='2022-07-26T17:23:44+00:00' ), StatementAttachmentFactory.build( statement_attachment_type=StatementAttachmentType.COLLECTION_SUMMARY_LABEL, statement_attachment_id=2, created_by='me', created_at='2022-07-26T17:23:44+00:00' ) ] link_to_post = 'mock_slack_endpoint' post_message_to_slack('statement attachments', attachments, link_to_post) expected_payload = { 'blocks': [ { 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': '*⚠️ The following statement attachments are stuck:*\n' '* `1` (created: 2022-07-26T17:23:44+00:00)\n' '* `2` (created: 2022-07-26T17:23:44+00:00)\n' } } ] } mock_post.assert_called_once_with( link_to_post, json=expected_payload, ) mock_logging.assert_called_once_with('Message sent to Slack: ok') @patch('scripts.check_attachments_statuses.httpx.post') @patch('scripts.check_attachments_statuses.logging.info') def test_post_message_to_slack_fail(mock_logging, mock_post): """Test post message with a valid text.""" attachments = [ StatementAttachmentFactory.build( statement_attachment_type=StatementAttachmentType.REVENUE_DETAIL, statement_attachment_id=1, created_by='me', created_at='2022-07-26T17:23:44+00:00' ), ] mock_post.return_value = Response(status_code=403, text='invalid token') with pytest.raises( ScriptException, match=re.escape('Slack message failed (403): invalid token') ): post_message_to_slack('statement attachments', attachments, 'slack')