Debian Codegraph @

tests/test_plugin_pagerduty.py

raw
1 # BSD 2-Clause License
2 #
3 # Apprise - Push Notification Library.
4 # Copyright (c) 2026, Chris Caron <[email protected]>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation
14 # and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 # Disable logging for a cleaner testing output
29 import logging
30 from unittest import mock
31
32 from helpers import AppriseURLTester
33 import requests
34
35 from apprise import Apprise, NotifyType
36 from apprise.exception import AppriseImproperlyConfigured
37 from apprise.plugins.pagerduty import NotifyPagerDuty
38
39 logging.disable(logging.CRITICAL)
40
41 # Our Testing URLs
42 apprise_url_tests = (
43 (
44 "pagerduty://",
45 {
46 # No Access Token or Integration/Routing Key specified
47 "instance": AppriseImproperlyConfigured,
48 },
49 ),
50 (
51 "pagerduty://%20@%20/",
52 {
53 # invalid Access Token and Integration/Routing Key
54 "instance": AppriseImproperlyConfigured,
55 },
56 ),
57 (
58 "pagerduty://%20/",
59 {
60 # invalid Access Token; no Integration/Routing Key
61 "instance": AppriseImproperlyConfigured,
62 },
63 ),
64 (
65 "pagerduty://%20@abcd/",
66 {
67 # Invalid Integration/Routing Key (but valid Access Token)
68 "instance": AppriseImproperlyConfigured,
69 },
70 ),
71 (
72 "pagerduty://myroutekey@myapikey/%20",
73 {
74 # bad source
75 "instance": AppriseImproperlyConfigured,
76 },
77 ),
78 (
79 "pagerduty://myroutekey@myapikey/mysource/%20",
80 {
81 # bad component
82 "instance": AppriseImproperlyConfigured,
83 },
84 ),
85 (
86 "pagerduty://myroutekey@myapikey?region=invalid",
87 {
88 # invalid region
89 "instance": AppriseImproperlyConfigured,
90 },
91 ),
92 (
93 "pagerduty://myroutekey@myapikey?severity=invalid",
94 {
95 # invalid severity
96 "instance": AppriseImproperlyConfigured,
97 },
98 ),
99 (
100 "pagerduty://myroutekey@myapikey",
101 {
102 # minimum requirements met
103 "instance": NotifyPagerDuty,
104 # Our expected url(privacy=True) startswith() response:
105 "privacy_url": "pagerduty://****@****/A...e/N...n?",
106 },
107 ),
108 (
109 "pagerduty://myroutekey@myapikey?image=no",
110 {
111 # minimum requirements met and disable images
112 "instance": NotifyPagerDuty,
113 },
114 ),
115 (
116 "pagerduty://myroutekey@myapikey?region=eu",
117 {
118 # european region
119 "instance": NotifyPagerDuty,
120 },
121 ),
122 (
123 "pagerduty://myroutekey@myapikey?severity=critical",
124 {
125 # Severity over-ride
126 "instance": NotifyPagerDuty,
127 },
128 ),
129 (
130 "pagerduty://myroutekey@myapikey?severity=err",
131 {
132 # Severity over-ride (short-form)
133 "instance": NotifyPagerDuty,
134 },
135 ),
136 # Custom values
137 (
138 "pagerduty://myroutekey@myapikey?+key=value&+key2=value2",
139 {
140 # minimum requirements and support custom key/value pairs
141 "instance": NotifyPagerDuty,
142 },
143 ),
144 (
145 "pagerduty://myroutekey@myapikey/mysource/mycomponent",
146 {
147 # a valid url
148 "instance": NotifyPagerDuty,
149 # Our expected url(privacy=True) startswith() response:
150 "privacy_url": "pagerduty://****@****/m...e/m...t?",
151 },
152 ),
153 (
154 "pagerduty://routekey@apikey/ms/mc?group=mygroup&class=myclass",
155 {
156 # class/group testing
157 "instance": NotifyPagerDuty,
158 },
159 ),
160 (
161 (
162 "pagerduty://?integrationkey=r&apikey=a&source=s&component=c"
163 "&group=g&class=c&image=no&click=http://localhost"
164 ),
165 {
166 # all parameters
167 "instance": NotifyPagerDuty
168 },
169 ),
170 (
171 "pagerduty://somerkey@someapikey/bizarre/code",
172 {
173 "instance": NotifyPagerDuty,
174 # throw a bizarre code forcing us to fail to look it up
175 "response": False,
176 "requests_response_code": 999,
177 },
178 ),
179 (
180 "pagerduty://myroutekey@myapikey/mysource/mycomponent",
181 {
182 "instance": NotifyPagerDuty,
183 # Throws a series of i/o exceptions with this flag
184 # is set and tests that we gracefully handle them
185 "test_requests_exceptions": True,
186 },
187 ),
188 )
189
190
191 def test_plugin_pagerduty_urls():
192 """NotifyPagerDuty() Apprise URLs."""
193
194 # Run our general tests
195 AppriseURLTester(tests=apprise_url_tests).run_all()
196
197
198 @mock.patch("requests.post")
199 def test_plugin_pagerduty_notify_type_is_string(mock_post):
200 """Verify string notification types are accepted and delivered."""
201 response = mock.Mock()
202 response.status_code = requests.codes.ok
203 response.content = ""
204 mock_post.return_value = response
205
206 obj = Apprise.instantiate("pagerduty://myroutekey@myapikey")
207 assert isinstance(obj, NotifyPagerDuty)
208
209 assert (
210 bool(
211 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
212 )
213 is True
214 )
215 assert mock_post.call_count == 1
216
217 call = mock_post.call_args_list[0]
218 # PagerDuty uses JSON payload
219 payload = call[1].get("data") or call[1].get("json")
220 payload_text = payload if isinstance(payload, str) else str(payload)
221
222 assert "NotifyType." not in payload_text