forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_dingding.py
219 lines (202 loc) · 8.49 KB
/
test_dingding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import unittest
import pytest
from airflow.models import Connection
from airflow.providers.dingding.hooks.dingding import DingdingHook
from airflow.utils import db
class TestDingdingHook(unittest.TestCase):
conn_id = 'dingding_conn_id_test'
def setUp(self):
db.merge_conn(
Connection(
conn_id=self.conn_id,
conn_type='dingding',
host='https://oapi.dingtalk.com',
password='you_token_here',
)
)
def test_get_endpoint_conn_id(self):
hook = DingdingHook(dingding_conn_id=self.conn_id)
endpoint = hook._get_endpoint()
assert 'robot/send?access_token=you_token_here' == endpoint
def test_build_text_message_not_remind(self):
config = {
'dingding_conn_id': self.conn_id,
'message_type': 'text',
'message': 'Airflow dingding text message remind no one',
'at_mobiles': False,
'at_all': False,
}
expect = {
'msgtype': 'text',
'text': {'content': 'Airflow dingding text message remind no one'},
'at': {'atMobiles': False, 'isAtAll': False},
}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_text_message_remind_specific(self):
config = {
'dingding_conn_id': self.conn_id,
'message_type': 'text',
'message': 'Airflow dingding text message remind specific users',
'at_mobiles': ['1234', '5768'],
'at_all': False,
}
expect = {
'msgtype': 'text',
'text': {'content': 'Airflow dingding text message remind specific users'},
'at': {'atMobiles': ['1234', '5768'], 'isAtAll': False},
}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_text_message_remind_all(self):
config = {
'dingding_conn_id': self.conn_id,
'message_type': 'text',
'message': 'Airflow dingding text message remind all user in group',
'at_all': True,
}
expect = {
'msgtype': 'text',
'text': {'content': 'Airflow dingding text message remind all user in group'},
'at': {'atMobiles': None, 'isAtAll': True},
}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_markdown_message_remind_specific(self):
msg = {
'title': 'Airflow dingding markdown message',
'text': '# Markdown message title\ncontent content .. \n### sub-title\n'
'![logo](https://airflow.apache.org/_images/pin_large.png)',
}
config = {
'dingding_conn_id': self.conn_id,
'message_type': 'markdown',
'message': msg,
'at_mobiles': ['1234', '5678'],
'at_all': False,
}
expect = {
'msgtype': 'markdown',
'markdown': msg,
'at': {'atMobiles': ['1234', '5678'], 'isAtAll': False},
}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_markdown_message_remind_all(self):
msg = {
'title': 'Airflow dingding markdown message',
'text': '# Markdown message title\ncontent content .. \n### sub-title\n'
'![logo](https://airflow.apache.org/_images/pin_large.png)',
}
config = {
'dingding_conn_id': self.conn_id,
'message_type': 'markdown',
'message': msg,
'at_all': True,
}
expect = {'msgtype': 'markdown', 'markdown': msg, 'at': {'atMobiles': None, 'isAtAll': True}}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_link_message(self):
msg = {
'title': 'Airflow dingding link message',
'text': 'Airflow official documentation link',
'messageUrl': 'https://airflow.apache.org',
'picURL': 'https://airflow.apache.org/_images/pin_large.png',
}
config = {'dingding_conn_id': self.conn_id, 'message_type': 'link', 'message': msg}
expect = {'msgtype': 'link', 'link': msg}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_single_action_card_message(self):
msg = {
'title': 'Airflow dingding single actionCard message',
'text': 'Airflow dingding single actionCard message\n'
'![logo](https://airflow.apache.org/_images/pin_large.png)\n'
'This is a official logo in Airflow website.',
'hideAvatar': '0',
'btnOrientation': '0',
'singleTitle': 'read more',
'singleURL': 'https://airflow.apache.org',
}
config = {'dingding_conn_id': self.conn_id, 'message_type': 'actionCard', 'message': msg}
expect = {'msgtype': 'actionCard', 'actionCard': msg}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_multi_action_card_message(self):
msg = {
'title': 'Airflow dingding multi actionCard message',
'text': 'Airflow dingding multi actionCard message\n'
'![logo](https://airflow.apache.org/_images/pin_large.png)\n'
'Airflow documentation and GitHub',
'hideAvatar': '0',
'btnOrientation': '0',
'btns': [
{'title': 'Airflow Documentation', 'actionURL': 'https://airflow.apache.org'},
{'title': 'Airflow GitHub', 'actionURL': 'https://github.com/apache/airflow'},
],
}
config = {'dingding_conn_id': self.conn_id, 'message_type': 'actionCard', 'message': msg}
expect = {'msgtype': 'actionCard', 'actionCard': msg}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_build_feed_card_message(self):
msg = {
"links": [
{
"title": "Airflow DAG feed card",
"messageURL": "https://airflow.apache.org/docs/apache-airflow/stable/ui.html",
"picURL": "https://airflow.apache.org/_images/dags.png",
},
{
"title": "Airflow tree feed card",
"messageURL": "https://airflow.apache.org/docs/apache-airflow/stable/ui.html",
"picURL": "https://airflow.apache.org/_images/tree.png",
},
{
"title": "Airflow graph feed card",
"messageURL": "https://airflow.apache.org/docs/apache-airflow/stable/ui.html",
"picURL": "https://airflow.apache.org/_images/graph.png",
},
]
}
config = {'dingding_conn_id': self.conn_id, 'message_type': 'feedCard', 'message': msg}
expect = {'msgtype': 'feedCard', 'feedCard': msg}
hook = DingdingHook(**config)
message = hook._build_message()
assert json.dumps(expect) == message
def test_send_not_support_type(self):
config = {
'dingding_conn_id': self.conn_id,
'message_type': 'not_support_type',
'message': 'Airflow dingding text message remind no one',
}
hook = DingdingHook(**config)
with pytest.raises(ValueError):
hook.send()