Debian Codegraph @

tests/test_plugin_pagerduty.py

raw symbols SCIP
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.plugins.pagerduty import NotifyPagerDuty
37
38 logging.disable(logging.CRITICAL)
39
40 # Our Testing URLs
41 apprise_url_tests = (
42 (
43 "pagerduty://",
44 {
45 # No Access Token or Integration/Routing Key specified
46 "instance": TypeError,
47 },
48 ),
49 (
50 "pagerduty://%20@%20/",
51 {
52 # invalid Access Token and Integration/Routing Key
53 "instance": TypeError,
54 },
55 ),
56 (
57 "pagerduty://%20/",
58 {
59 # invalid Access Token; no Integration/Routing Key
60 "instance": TypeError,
61 },
62 ),
63 (
64 "pagerduty://%20@abcd/",
65 {
66 # Invalid Integration/Routing Key (but valid Access Token)
67 "instance": TypeError,
68 },
69 ),
70 (
71 "pagerduty://myroutekey@myapikey/%20",
72 {
73 # bad source
74 "instance": TypeError,
75 },
76 ),
77 (
78 "pagerduty://myroutekey@myapikey/mysource/%20",
79 {
80 # bad component
81 "instance": TypeError,
82 },
83 ),
84 (
85 "pagerduty://myroutekey@myapikey?region=invalid",
86 {
87 # invalid region
88 "instance": TypeError,
89 },
90 ),
91 (
92 "pagerduty://myroutekey@myapikey?severity=invalid",
93 {
94 # invalid severity
95 "instance": TypeError,
96 },
97 ),
98 (
99 "pagerduty://myroutekey@myapikey",
100 {
101 # minimum requirements met
102 "instance": NotifyPagerDuty,
103 # Our expected url(privacy=True) startswith() response:
104 "privacy_url": "pagerduty://****@****/A...e/N...n?",
105 },
106 ),
107 (
108 "pagerduty://myroutekey@myapikey?image=no",
109 {
110 # minimum requirements met and disable images
111 "instance": NotifyPagerDuty,
112 },
113 ),
114 (
115 "pagerduty://myroutekey@myapikey?region=eu",
116 {
117 # european region
118 "instance": NotifyPagerDuty,
119 },
120 ),
121 (
122 "pagerduty://myroutekey@myapikey?severity=critical",
123 {
124 # Severity over-ride
125 "instance": NotifyPagerDuty,
126 },
127 ),
128 (
129 "pagerduty://myroutekey@myapikey?severity=err",
130 {
131 # Severity over-ride (short-form)
132 "instance": NotifyPagerDuty,
133 },
134 ),
135 # Custom values
136 (
137 "pagerduty://myroutekey@myapikey?+key=value&+key2=value2",
138 {
139 # minimum requirements and support custom key/value pairs
140 "instance": NotifyPagerDuty,
141 },
142 ),
143 (
144 "pagerduty://myroutekey@myapikey/mysource/mycomponent",
145 {
146 # a valid url
147 "instance": NotifyPagerDuty,
148 # Our expected url(privacy=True) startswith() response:
149 "privacy_url": "pagerduty://****@****/m...e/m...t?",
150 },
151 ),
152 (
153 "pagerduty://routekey@apikey/ms/mc?group=mygroup&class=myclass",
154 {
155 # class/group testing
156 "instance": NotifyPagerDuty,
157 },
158 ),
159 (
160 (
161 "pagerduty://?integrationkey=r&apikey=a&source=s&component=c"
162 "&group=g&class=c&image=no&click=http://localhost"
163 ),
164 {
165 # all parameters
166 "instance": NotifyPagerDuty
167 },
168 ),
169 (
170 "pagerduty://somerkey@someapikey/bizarre/code",
171 {
172 "instance": NotifyPagerDuty,
173 # throw a bizarre code forcing us to fail to look it up
174 "response": False,
175 "requests_response_code": 999,
176 },
177 ),
178 (
179 "pagerduty://myroutekey@myapikey/mysource/mycomponent",
180 {
181 "instance": NotifyPagerDuty,
182 # Throws a series of i/o exceptions with this flag
183 # is set and tests that we gracefully handle them
184 "test_requests_exceptions": True,
185 },
186 ),
187 )
188
189
190 def test_plugin_pagerduty_urls():
191 """NotifyPagerDuty() Apprise URLs."""
192
193 # Run our general tests
194 AppriseURLTester(tests=apprise_url_tests).run_all()
195
196
197 @mock.patch("requests.post")
198 def test_plugin_pagerduty_notify_type_is_string(mock_post):
199 response = mock.Mock()
200 response.status_code = requests.codes.ok
201 response.content = ""
202 mock_post.return_value = response
203
204 obj = Apprise.instantiate("pagerduty://myroutekey@myapikey")
205 assert isinstance(obj, NotifyPagerDuty)
206
207 assert (
208 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
209 is True
210 )
211 assert mock_post.call_count == 1
212
213 call = mock_post.call_args_list[0]
214 # PagerDuty uses JSON payload
215 payload = call[1].get("data") or call[1].get("json")
216 payload_text = payload if isinstance(payload, str) else str(payload)
217
218 assert "NotifyType." not in payload_text