Debian Codegraph @

tests/test_plugin_syslog.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 import sys
31 from unittest import mock
32
33 import pytest
34
35 import apprise
36
37 try:
38 import syslog
39
40 except ImportError:
41 # Shim so that test cases can run in environments that
42 # do not have syslog
43 import types
44
45 syslog = types.SimpleNamespace(
46 LOG_PID=0x01,
47 LOG_PERROR=0x02,
48 LOG_INFO=6,
49 LOG_NOTICE=5,
50 LOG_CRIT=2,
51 LOG_WARNING=4,
52 LOG_KERN=0,
53 LOG_USER=1,
54 LOG_MAIL=2,
55 LOG_DAEMON=3,
56 LOG_AUTH=4,
57 LOG_SYSLOG=5,
58 LOG_LPR=6,
59 LOG_NEWS=7,
60 LOG_UUCP=8,
61 LOG_CRON=9,
62 LOG_LOCAL0=16,
63 LOG_LOCAL1=17,
64 LOG_LOCAL2=18,
65 LOG_LOCAL3=19,
66 LOG_LOCAL4=20,
67 LOG_LOCAL5=21,
68 LOG_LOCAL6=22,
69 LOG_LOCAL7=23,
70 openlog=lambda *a, **kw: None,
71 syslog=lambda *a, **kw: None,
72 )
73 sys.modules["syslog"] = syslog
74
75
76 logging.disable(logging.CRITICAL)
77
78 from apprise.plugins.syslog import NotifySyslog # noqa E402
79
80
81 @mock.patch("syslog.syslog")
82 @mock.patch("syslog.openlog")
83 def test_plugin_syslog_by_url(openlog, syslog):
84 """NotifySyslog() Apprise URLs."""
85 # an invalid URL
86 assert NotifySyslog.parse_url(object) is None
87 assert NotifySyslog.parse_url(42) is None
88 assert NotifySyslog.parse_url(None) is None
89
90 obj = apprise.Apprise.instantiate("syslog://")
91 assert obj.url().startswith("syslog://user")
92 assert r"logpid=yes" in obj.url()
93 assert r"logperror=no" in obj.url()
94
95 # We do not support generation of a URL ID
96 assert obj.url_id() is None
97
98 assert isinstance(
99 apprise.Apprise.instantiate("syslog://:@/"), NotifySyslog
100 )
101
102 obj = apprise.Apprise.instantiate("syslog://?logpid=no&logperror=yes")
103 assert isinstance(obj, NotifySyslog)
104 assert obj.url().startswith("syslog://user")
105 assert r"logpid=no" in obj.url()
106 assert r"logperror=yes" in obj.url()
107
108 # Test sending a notification
109 assert obj.notify("body") is True
110 assert obj.notify(title="title", body="body") is True
111
112 # Invalid Notification Type
113 assert obj.notify("body", notify_type="invalid") is False
114
115 obj = apprise.Apprise.instantiate("syslog://_/?facility=local5")
116 assert isinstance(obj, NotifySyslog)
117 assert obj.url().startswith("syslog://local5")
118 assert r"logpid=yes" in obj.url()
119 assert r"logperror=no" in obj.url()
120
121 # Invalid instantiation
122 assert apprise.Apprise.instantiate("syslog://_/?facility=invalid") is None
123
124 # j will cause a search to take place and match to daemon
125 obj = apprise.Apprise.instantiate("syslog://_/?facility=d")
126 assert isinstance(obj, NotifySyslog)
127 assert obj.url().startswith("syslog://daemon")
128 assert r"logpid=yes" in obj.url()
129 assert r"logperror=no" in obj.url()
130
131 # Facility can also be specified on the url as a hostname
132 obj = apprise.Apprise.instantiate("syslog://kern?logpid=no&logperror=y")
133 assert isinstance(obj, NotifySyslog)
134 assert obj.url().startswith("syslog://kern")
135 assert r"logpid=no" in obj.url()
136 assert r"logperror=yes" in obj.url()
137
138 # Facilities specified as an argument always over-ride host
139 obj = apprise.Apprise.instantiate("syslog://kern?facility=d")
140 assert isinstance(obj, NotifySyslog)
141 assert obj.url().startswith("syslog://daemon")
142
143
144 @mock.patch("syslog.syslog")
145 @mock.patch("syslog.openlog")
146 def test_plugin_syslog_edge_cases(openlog, syslog):
147 """NotifySyslog() Edge Cases."""
148
149 # Default
150 obj = NotifySyslog(facility=None)
151 assert isinstance(obj, NotifySyslog)
152 assert obj.url().startswith("syslog://user")
153 assert r"logpid=yes" in obj.url()
154 assert r"logperror=no" in obj.url()
155
156 # Exception should be thrown about the fact no bot token was specified
157 with pytest.raises(TypeError):
158 NotifySyslog(facility="invalid")
159
160 with pytest.raises(TypeError):
161 NotifySyslog(facility=object)