forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_discord_webhook.py
113 lines (92 loc) · 3.78 KB
/
test_discord_webhook.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
#
# 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.exceptions import AirflowException
from airflow.models import Connection
from airflow.providers.discord.hooks.discord_webhook import DiscordWebhookHook
from airflow.utils import db
class TestDiscordWebhookHook(unittest.TestCase):
_config = {
'http_conn_id': 'default-discord-webhook',
'webhook_endpoint': 'webhooks/11111/some-discord-token_111',
'message': 'your message here',
'username': 'Airflow Webhook',
'avatar_url': 'https://static-cdn.avatars.com/my-avatar-path',
'tts': False,
'proxy': 'https://proxy.proxy.com:8888',
}
expected_payload_dict = {
'username': _config['username'],
'avatar_url': _config['avatar_url'],
'tts': _config['tts'],
'content': _config['message'],
}
expected_payload = json.dumps(expected_payload_dict)
def setUp(self):
db.merge_conn(
Connection(
conn_id='default-discord-webhook',
conn_type='discord',
host='https://discordapp.com/api/',
extra='{"webhook_endpoint": "webhooks/00000/some-discord-token_000"}',
)
)
def test_get_webhook_endpoint_manual_token(self):
# Given
provided_endpoint = 'webhooks/11111/some-discord-token_111'
hook = DiscordWebhookHook(webhook_endpoint=provided_endpoint)
# When
webhook_endpoint = hook._get_webhook_endpoint(None, provided_endpoint)
# Then
assert webhook_endpoint == provided_endpoint
def test_get_webhook_endpoint_invalid_url(self):
# Given
provided_endpoint = 'https://discordapp.com/some-invalid-webhook-url'
# When/Then
expected_message = 'Expected Discord webhook endpoint in the form of'
with pytest.raises(AirflowException, match=expected_message):
DiscordWebhookHook(webhook_endpoint=provided_endpoint)
def test_get_webhook_endpoint_conn_id(self):
# Given
conn_id = 'default-discord-webhook'
hook = DiscordWebhookHook(http_conn_id=conn_id)
expected_webhook_endpoint = 'webhooks/00000/some-discord-token_000'
# When
webhook_endpoint = hook._get_webhook_endpoint(conn_id, None)
# Then
assert webhook_endpoint == expected_webhook_endpoint
def test_build_discord_payload(self):
# Given
hook = DiscordWebhookHook(**self._config)
# When
payload = hook._build_discord_payload()
# Then
assert self.expected_payload == payload
def test_build_discord_payload_message_length(self):
# Given
config = self._config.copy()
# create message over the character limit
config["message"] = 'c' * 2001
hook = DiscordWebhookHook(**config)
# When/Then
expected_message = 'Discord message length must be 2000 or fewer characters'
with pytest.raises(AirflowException, match=expected_message):
hook._build_discord_payload()