Debian Codegraph @

tests/test_plugin_fortysixelks.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.plugins.fortysixelks import Notify46Elks
37
38 logging.disable(logging.CRITICAL)
39
40 # Our Testing URLs
41 apprise_url_tests = (
42 ("46elks://", False),
43 ("46elks://user@/", False),
44 ("46elks://:pass@/", False),
45 (
46 "46elks://user:pass@/",
47 {
48 "instance": Notify46Elks,
49 # no target was specified
50 "notify_response": False,
51 },
52 ),
53 (
54 "46elks://user:pass@+15551234556",
55 {
56 "instance": Notify46Elks,
57 },
58 ),
59 (
60 "46elks://user:pass@+15551234567/+46701234534?from=Acme",
61 {
62 "instance": Notify46Elks,
63 },
64 ),
65 # Support elks:// too!
66 (
67 "elks://user:pass@+15551234123/",
68 {
69 "instance": Notify46Elks,
70 },
71 ),
72 # Privacy mode redacts password
73 (
74 "46elks://user:pass@+15551234512",
75 {
76 "privacy_url": "46elks://user:****@+15551234512",
77 "instance": Notify46Elks,
78 },
79 ),
80 # invalid phone no
81 (
82 "46elks://user:pass@Acme/234512",
83 {
84 "instance": Notify46Elks,
85 "notify_response": False,
86 },
87 ),
88 # Native URL reversal
89 (
90 ("https://user1:[email protected]/a1/sms?to=+15551234511&from=Acme"),
91 {
92 "instance": Notify46Elks,
93 "privacy_url": "46elks://user1:****@Acme/+15551234511",
94 },
95 ),
96 (
97 "46elks://user:pass@+15551234567",
98 {
99 "instance": Notify46Elks,
100 # throw a bizarre code forcing us to fail to look it up
101 "response": False,
102 "requests_response_code": 999,
103 },
104 ),
105 (
106 "46elks://user:pass@+15551234578",
107 {
108 "instance": Notify46Elks,
109 # Throws a series of i/o exceptions with this flag
110 # is set and tests that we gracefully handle them
111 "test_requests_exceptions": True,
112 },
113 ),
114 )
115
116
117 def test_plugin_46elks_urls():
118 """NotifyTemplate() Apprise URLs."""
119
120 # Run our general tests
121 AppriseURLTester(tests=apprise_url_tests).run_all()
122
123
124 @mock.patch("requests.post")
125 def test_plugin_46elks_edge_cases(mock_post):
126 """Notify46Elks() Edge Cases."""
127
128 user = "user1"
129 password = "pass123"
130 phone = "+15551234591"
131
132 response = requests.Request()
133 response.status_code = requests.codes.ok
134
135 # Prepare Mock
136 mock_post.return_value = response
137
138 obj = Apprise.instantiate(f"46elks://{user}:{password}@{phone}")
139 assert (
140 bool(
141 obj.notify(body="body", title="title", notify_type=NotifyType.INFO)
142 )
143 is True
144 )
145
146 # We know there is 1 (valid) targets
147 assert len(obj) == 1
148
149 # Test our call count
150 assert mock_post.call_count == 1
151
152 # Test
153 details = mock_post.call_args_list[0]
154 headers = details[1]["headers"]
155 assert headers["User-Agent"] == "Apprise"
156 payload = details[1]["data"]
157 assert payload["to"] == phone
158 assert payload["from"] == phone
159 assert payload["message"] == "title\r\nbody"
160
161 # Verify our URL looks good
162 assert obj.url().startswith(f"46elks://{user}:{password}@{phone}")