Debian Codegraph @

tests/test_plugin_syslog.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 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.exception import AppriseImproperlyConfigured # noqa E402
79 from apprise.plugins.syslog import NotifySyslog # noqa E402
80
81
82 @mock.patch("syslog.syslog")
83 @mock.patch("syslog.openlog")
84 def test_plugin_syslog_by_url(openlog, syslog):
85 """NotifySyslog() Apprise URLs."""
86 # an invalid URL
87 assert NotifySyslog.parse_url(object) is None
88 assert NotifySyslog.parse_url(42) is None
89 assert NotifySyslog.parse_url(None) is None
90
91 obj = apprise.Apprise.instantiate("syslog://")
92 assert obj.url().startswith("syslog://user")
93 assert r"logpid=yes" in obj.url()
94 assert r"logperror=no" in obj.url()
95
96 # We do not support generation of a URL ID
97 assert obj.url_id() is None
98
99 assert isinstance(
100 apprise.Apprise.instantiate("syslog://:@/"), NotifySyslog
101 )
102
103 obj = apprise.Apprise.instantiate("syslog://?logpid=no&logperror=yes")
104 assert isinstance(obj, NotifySyslog)
105 assert obj.url().startswith("syslog://user")
106 assert r"logpid=no" in obj.url()
107 assert r"logperror=yes" in obj.url()
108
109 # Test sending a notification
110 assert bool(obj.notify("body")) is True
111 assert bool(obj.notify(title="title", body="body")) is True
112
113 # Invalid Notification Type
114 assert bool(obj.notify("body", notify_type="invalid")) is False
115
116 obj = apprise.Apprise.instantiate("syslog://_/?facility=local5")
117 assert isinstance(obj, NotifySyslog)
118 assert obj.url().startswith("syslog://local5")
119 assert r"logpid=yes" in obj.url()
120 assert r"logperror=no" in obj.url()
121
122 # Invalid instantiation
123 assert apprise.Apprise.instantiate("syslog://_/?facility=invalid") is None
124
125 # j will cause a search to take place and match to daemon
126 obj = apprise.Apprise.instantiate("syslog://_/?facility=d")
127 assert isinstance(obj, NotifySyslog)
128 assert obj.url().startswith("syslog://daemon")
129 assert r"logpid=yes" in obj.url()
130 assert r"logperror=no" in obj.url()
131
132 # Facility can also be specified on the url as a hostname
133 obj = apprise.Apprise.instantiate("syslog://kern?logpid=no&logperror=y")
134 assert isinstance(obj, NotifySyslog)
135 assert obj.url().startswith("syslog://kern")
136 assert r"logpid=no" in obj.url()
137 assert r"logperror=yes" in obj.url()
138
139 # Facilities specified as an argument always over-ride host
140 obj = apprise.Apprise.instantiate("syslog://kern?facility=d")
141 assert isinstance(obj, NotifySyslog)
142 assert obj.url().startswith("syslog://daemon")
143
144
145 @mock.patch("syslog.syslog")
146 @mock.patch("syslog.openlog")
147 def test_plugin_syslog_edge_cases(openlog, syslog):
148 """NotifySyslog() Edge Cases."""
149
150 # Default
151 obj = NotifySyslog(facility=None)
152 assert isinstance(obj, NotifySyslog)
153 assert obj.url().startswith("syslog://user")
154 assert r"logpid=yes" in obj.url()
155 assert r"logperror=no" in obj.url()
156
157 # Exception should be thrown about the fact no bot token was specified
158 with pytest.raises(AppriseImproperlyConfigured):
159 NotifySyslog(facility="invalid")
160
161 with pytest.raises(AppriseImproperlyConfigured):
162 NotifySyslog(facility=object)