|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +import uuid |
| 18 | +from unittest.mock import MagicMock, patch |
| 19 | + |
| 20 | +import pandas as pd |
| 21 | +from flask import g |
| 22 | + |
| 23 | + |
| 24 | +@patch("superset.reports.notifications.slack.logger") |
| 25 | +def test_send_slack( |
| 26 | + logger_mock: MagicMock, |
| 27 | +) -> None: |
| 28 | + # `superset.models.helpers`, a dependency of following imports, |
| 29 | + # requires app context |
| 30 | + from superset.reports.models import ReportRecipients, ReportRecipientType |
| 31 | + from superset.reports.notifications.base import NotificationContent |
| 32 | + from superset.reports.notifications.slack import SlackNotification, WebClient |
| 33 | + |
| 34 | + execution_id = uuid.uuid4() |
| 35 | + g.logs_context = {"execution_id": execution_id} |
| 36 | + content = NotificationContent( |
| 37 | + name="test alert", |
| 38 | + header_data={ |
| 39 | + "notification_format": "PNG", |
| 40 | + "notification_type": "Alert", |
| 41 | + "owners": [1], |
| 42 | + "notification_source": None, |
| 43 | + "chart_id": None, |
| 44 | + "dashboard_id": None, |
| 45 | + }, |
| 46 | + embedded_data=pd.DataFrame( |
| 47 | + { |
| 48 | + "A": [1, 2, 3], |
| 49 | + "B": [4, 5, 6], |
| 50 | + "C": ["111", "222", '<a href="http://www.example.com">333</a>'], |
| 51 | + } |
| 52 | + ), |
| 53 | + description='<p>This is <a href="#">a test</a> alert</p><br />', |
| 54 | + ) |
| 55 | + with patch.object( |
| 56 | + WebClient, "chat_postMessage", return_value=True |
| 57 | + ) as chat_post_message_mock: |
| 58 | + SlackNotification( |
| 59 | + recipient=ReportRecipients( |
| 60 | + type=ReportRecipientType.SLACK, |
| 61 | + recipient_config_json='{"target": "some_channel"}', |
| 62 | + ), |
| 63 | + content=content, |
| 64 | + ).send() |
| 65 | + logger_mock.info.assert_called_with( |
| 66 | + "Report sent to slack", extra={"execution_id": execution_id} |
| 67 | + ) |
| 68 | + chat_post_message_mock.assert_called_with( |
| 69 | + channel="some_channel", |
| 70 | + text="""*test alert* |
| 71 | +
|
| 72 | +<p>This is <a href="#">a test</a> alert</p><br /> |
| 73 | +
|
| 74 | +<None|Explore in Superset> |
| 75 | +
|
| 76 | +``` |
| 77 | +| | A | B | C | |
| 78 | +|---:|----:|----:|:-----------------------------------------| |
| 79 | +| 0 | 1 | 4 | 111 | |
| 80 | +| 1 | 2 | 5 | 222 | |
| 81 | +| 2 | 3 | 6 | <a href="http://www.example.com">333</a> | |
| 82 | +``` |
| 83 | +""", |
| 84 | + ) |
| 85 | + |
| 86 | + # reset g.logs_context |
| 87 | + g.logs_context = None |
0 commit comments