Debian Codegraph @

tests/test_plugin_vapid.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 import json
29
30 # Disable logging for a cleaner testing output
31 import logging
32 import os
33 import sys
34 from unittest import mock
35
36 from helpers import AppriseURLTester
37 import pytest
38 import requests
39
40 from apprise import asset, exception, url
41 from apprise.common import PersistentStoreMode
42 from apprise.plugins.vapid import NotifyVapid
43 from apprise.plugins.vapid.subscription import (
44 WebPushSubscription,
45 WebPushSubscriptionManager,
46 )
47 from apprise.utils.pem import ApprisePEMController
48
49 logging.disable(logging.CRITICAL)
50
51 # Attachment Directory
52 TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var")
53
54 # a test UUID we can use
55 SUBSCRIBER = "[email protected]"
56
57 PLUGIN_ID = "vapid"
58
59 # Our Testing URLs
60 apprise_url_tests = (
61 (
62 "vapid://",
63 {
64 "instance": TypeError,
65 },
66 ),
67 (
68 "vapid://:@/",
69 {
70 "instance": TypeError,
71 },
72 ),
73 (
74 "vapid://invalid-subscriber",
75 {
76 # An invalid Subscriber
77 "instance": TypeError,
78 },
79 ),
80 (
81 "vapid://[email protected]",
82 {
83 # bare bone requirements met, but we don't have our subscription
84 # file or our private key (pem)
85 "instance": NotifyVapid,
86 # We'll fail to respond because we would not have found any
87 # configuration to load
88 "notify_response": False,
89 },
90 ),
91 (
92 "vapid://[email protected]?keyfile=invalid&subfile=invalid",
93 {
94 # Test passing keyfile and subfile on our path (even if invalid)
95 "instance": NotifyVapid,
96 # We'll fail to respond because we would not have found any
97 # configuration to load
98 "notify_response": False,
99 },
100 ),
101 (
102 "vapid://[email protected]/[email protected]",
103 {
104 # we don't have our subscription file or private key
105 "instance": NotifyVapid,
106 "notify_response": False,
107 },
108 ),
109 (
110 "vapid://[email protected]/[email protected]",
111 {
112 "instance": NotifyVapid,
113 # force a failure
114 "response": False,
115 "requests_response_code": requests.codes.internal_server_error,
116 },
117 ),
118 (
119 "vapid://[email protected]/[email protected]",
120 {
121 "instance": NotifyVapid,
122 # throw a bizarre code forcing us to fail to look it up
123 "response": False,
124 "requests_response_code": 999,
125 },
126 ),
127 (
128 "vapid://[email protected]/[email protected]",
129 {
130 "instance": NotifyVapid,
131 # Throws a series of i/o exceptions with this flag
132 # is set and tests that we gracefully handle them
133 "test_requests_exceptions": True,
134 },
135 ),
136 )
137
138
139 @pytest.fixture
140 def patch_persistent_store_namespace(tmpdir):
141 """Force an easy to test environment."""
142 with (
143 mock.patch.object(url.URLBase, "url_id", return_value=PLUGIN_ID),
144 mock.patch.object(
145 asset.AppriseAsset, "storage_mode", PersistentStoreMode.AUTO
146 ),
147 mock.patch.object(asset.AppriseAsset, "storage_path", str(tmpdir)),
148 ):
149 tmp_dir = tmpdir.mkdir(PLUGIN_ID)
150 # Return the directory name
151 yield str(tmp_dir)
152
153
154 @pytest.fixture
155 def subscription_reference():
156 return {
157 "[email protected]": {
158 "endpoint": "https://fcm.googleapis.com/fcm/send/default",
159 "keys": {
160 "p256dh": (
161 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
162 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
163 ),
164 "auth": "k9Xzm43nBGo=",
165 },
166 },
167 "user1": {
168 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
169 "keys": {
170 "p256dh": (
171 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
172 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
173 ),
174 "auth": "k9Xzm43nBGo=",
175 },
176 },
177 "user2": {
178 "endpoint": "https://fcm.googleapis.com/fcm/send/def456",
179 "keys": {
180 "p256dh": (
181 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
182 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
183 ),
184 "auth": "k9Xzm43nBGo=",
185 },
186 },
187 }
188
189
190 @pytest.mark.skipif(
191 "cryptography" not in sys.modules, reason="Requires cryptography"
192 )
193 def test_plugin_vapid_urls():
194 """
195 NotifyVapid() Apprise URLs - No Config
196
197 """
198
199 # Run our general tests
200 AppriseURLTester(tests=apprise_url_tests).run_all()
201
202
203 @pytest.mark.skipif(
204 "cryptography" not in sys.modules, reason="Requires cryptography"
205 )
206 def test_plugin_vapid_urls_with_required_assets(
207 patch_persistent_store_namespace, subscription_reference
208 ):
209 """NotifyVapid() Apprise URLs With Config."""
210
211 # Determine our store
212 pc = ApprisePEMController(path=patch_persistent_store_namespace)
213 assert pc.keygen() is True
214
215 # Write our subscriptions file to disk
216 subscription_file = os.path.join(
217 patch_persistent_store_namespace, NotifyVapid.vapid_subscription_file
218 )
219
220 with open(subscription_file, "w") as f:
221 f.write(json.dumps(subscription_reference))
222
223 tests = (
224 (
225 "vapid://[email protected]",
226 {
227 # [email protected] loaded (also used as subscriber id)
228 "instance": NotifyVapid,
229 },
230 ),
231 (
232 "vapid://[email protected]/[email protected]",
233 {
234 # no [email protected] key entry
235 "instance": NotifyVapid,
236 "notify_response": False,
237 },
238 ),
239 (
240 "vapid://[email protected]/user1?to=user2",
241 {
242 # We'll succesfully notify 2 users
243 "instance": NotifyVapid,
244 },
245 ),
246 (
247 "vapid://user1?to=user2&[email protected]",
248 {
249 # We'll succesfully notify 2 users
250 "instance": NotifyVapid,
251 },
252 ),
253 (
254 "vapid://?to=user2&[email protected]",
255 {
256 # No host provided
257 "instance": NotifyVapid,
258 },
259 ),
260 (
261 "vapid://[email protected]?to=user2&[email protected]",
262 {
263 # We'll succesfully notify 2 users
264 "instance": NotifyVapid,
265 },
266 ),
267 (
268 "vapid://[email protected]/user1?to=user2&ttl=15",
269 {
270 # test ttl
271 "instance": NotifyVapid,
272 },
273 ),
274 (
275 "vapid://[email protected]/user1?to=user2&ttl=",
276 {
277 # test ttl
278 "instance": NotifyVapid,
279 },
280 ),
281 (
282 "vapid://[email protected]/user1?to=user2&ttl=invalid",
283 {
284 # test ttl
285 "instance": NotifyVapid,
286 },
287 ),
288 (
289 "vapid://[email protected]/user1?to=user2&ttl=-4000",
290 {
291 # bad ttl
292 "instance": TypeError,
293 },
294 ),
295 (
296 "vapid://[email protected]/user1?to=user2&mode=edge",
297 {
298 # test mode
299 "instance": NotifyVapid,
300 },
301 ),
302 (
303 "vapid://[email protected]/user1?to=user2&mode=",
304 {
305 # test mode
306 "instance": TypeError,
307 },
308 ),
309 (
310 "vapid://[email protected]/user1?to=user2&mode=invalid",
311 {
312 # test mode more
313 "instance": TypeError,
314 },
315 ),
316 (
317 "vapid://[email protected]/user1",
318 {
319 "instance": NotifyVapid,
320 # force a failure
321 "response": False,
322 "requests_response_code": requests.codes.internal_server_error,
323 },
324 ),
325 (
326 "vapid://[email protected]/user1",
327 {
328 "instance": NotifyVapid,
329 # throw a bizarre code forcing us to fail to look it up
330 "response": False,
331 "requests_response_code": 999,
332 },
333 ),
334 (
335 "vapid://[email protected]/user1",
336 {
337 "instance": NotifyVapid,
338 # Throws a series of connection and transfer exceptions
339 # when this flag is set and tests that we gracefully handle
340 # them
341 "test_requests_exceptions": True,
342 },
343 ),
344 )
345
346 AppriseURLTester(tests=tests).run_all()
347
348
349 @pytest.mark.skipif(
350 "cryptography" not in sys.modules, reason="Requires cryptography"
351 )
352 def test_plugin_vapid_subscriptions(tmpdir):
353 """NotifyVapid() Subscriptions."""
354
355 # Temporary directory
356 tmpdir0 = tmpdir.mkdir("tmp00")
357
358 with pytest.raises(exception.AppriseInvalidData):
359 # Integer not supported
360 WebPushSubscription(42)
361
362 with pytest.raises(exception.AppriseInvalidData):
363 # Not the correct format
364 WebPushSubscription("bad-content")
365
366 with pytest.raises(exception.AppriseInvalidData):
367 # Invalid JSON
368 WebPushSubscription("{")
369
370 with pytest.raises(exception.AppriseInvalidData):
371 # Empty Dictionary
372 WebPushSubscription({})
373
374 with pytest.raises(exception.AppriseInvalidData):
375 WebPushSubscription(
376 {
377 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
378 "keys": {
379 "p256dh": "BNcW4oA7zq5H9TKIrA3XfKclN2fX9P_7NR=",
380 "auth": 42,
381 },
382 }
383 )
384
385 with pytest.raises(exception.AppriseInvalidData):
386 WebPushSubscription(
387 {
388 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
389 "keys": {
390 "p256dh": 42,
391 "auth": "k9Xzm43nBGo=",
392 },
393 }
394 )
395
396 with pytest.raises(exception.AppriseInvalidData):
397 WebPushSubscription(
398 {
399 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
400 }
401 )
402
403 with pytest.raises(exception.AppriseInvalidData):
404 WebPushSubscription(
405 {
406 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
407 "keys": {},
408 }
409 )
410
411 with pytest.raises(exception.AppriseInvalidData):
412 # Invalid p256dh public key provided
413 wps = WebPushSubscription(
414 {
415 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
416 "keys": {
417 "p256dh": "BNcW4oA7zq5H9TKIrA3XfKclN2fX9P_7NR=",
418 "auth": "k9Xzm43nBGo=",
419 },
420 }
421 )
422
423 # An empty object
424 wps = WebPushSubscription()
425 assert bool(wps) is False
426 assert isinstance(wps.json(), str)
427 assert json.loads(wps.json())
428 assert str(wps) == ""
429 assert wps.auth is None
430 assert wps.endpoint is None
431 assert wps.p256dh is None
432 assert wps.public_key is None
433 # We can't write anything as there is nothing loaded
434 assert wps.write(os.path.join(str(tmpdir0), "subscriptions.json")) is False
435
436 # A valid key
437 wps = WebPushSubscription(
438 {
439 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
440 "keys": {
441 "p256dh": (
442 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
443 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
444 ),
445 "auth": "k9Xzm43nBGo=",
446 },
447 }
448 )
449
450 assert bool(wps) is True
451 assert isinstance(wps.json(), str)
452 assert json.loads(wps.json())
453 assert str(wps) == "abc123"
454 assert wps.auth == "k9Xzm43nBGo="
455 assert wps.endpoint == "https://fcm.googleapis.com/fcm/send/abc123"
456 assert (
457 wps.p256dh == "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
458 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
459 )
460 assert wps.public_key is not None
461
462 # Currently no files here
463 assert os.listdir(str(tmpdir0)) == []
464
465 # Bad content
466 assert wps.write(object) is False
467 assert wps.write(None) is False
468 # Can't write to a name already taken by as a directory
469 assert wps.write(str(tmpdir0)) is False
470 # Can't write to a name already taken by as a directory
471 assert wps.write(os.path.join(str(tmpdir0), "subscriptions.json")) is True
472 assert os.listdir(str(tmpdir0)) == ["subscriptions.json"]
473
474
475 @pytest.mark.skipif(
476 "cryptography" in sys.modules,
477 reason="Requires that cryptography NOT be installed",
478 )
479 def test_plugin_vapid_subscriptions_without_c():
480 """NotifyVapid() Subscriptions (no Cryptography)"""
481 with pytest.raises(exception.AppriseInvalidData):
482 # A valid key that can't be loaded because crytography is missing
483 WebPushSubscription(
484 {
485 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
486 "keys": {
487 "p256dh": (
488 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
489 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
490 ),
491 "auth": "k9Xzm43nBGo=",
492 },
493 }
494 )
495
496
497 @pytest.mark.skipif(
498 "cryptography" not in sys.modules, reason="Requires cryptography"
499 )
500 def test_plugin_vapid_subscription_manager(tmpdir):
501 """NotifyVapid() Subscription Manager."""
502
503 # Temporary directory
504 tmpdir0 = tmpdir.mkdir("tmp00")
505
506 with pytest.raises(exception.AppriseInvalidData):
507 # An invalid object
508 smgr = WebPushSubscriptionManager()
509 smgr["abc"] = "invalid"
510
511 with pytest.raises(exception.AppriseInvalidData):
512 # An invalid object
513 smgr = WebPushSubscriptionManager()
514 smgr += "invalid"
515
516 smgr = WebPushSubscriptionManager()
517
518 assert bool(smgr) is False
519 assert len(smgr) == 0
520
521 sub = {
522 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
523 "keys": {
524 "p256dh": (
525 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
526 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
527 ),
528 "auth": "k9Xzm43nBGo=",
529 },
530 }
531
532 assert smgr.add(sub) is True
533 assert bool(smgr) is True
534 assert len(smgr) == 1
535
536 # Same sub (overwrites same slot)
537 smgr += sub
538 assert bool(smgr) is True
539 assert len(smgr) == 1
540
541 # This makes a copy
542 smgr["abc"] = smgr["abc123"]
543 assert bool(smgr) is True
544 assert len(smgr) == 2
545
546 assert isinstance(smgr["abc123"], WebPushSubscription)
547
548 # Currently no files here
549 assert os.listdir(str(tmpdir0)) == []
550
551 # Write our content
552 assert smgr.write(os.path.join(str(tmpdir0), "subscriptions.json")) is True
553
554 assert os.listdir(str(tmpdir0)) == ["subscriptions.json"]
555
556 # Reset our object
557 smgr.clear()
558 assert bool(smgr) is False
559 assert len(smgr) == 0
560
561 # Load our content back
562 assert smgr.load(os.path.join(str(tmpdir0), "subscriptions.json")) is True
563 assert bool(smgr) is True
564 assert len(smgr) == 2
565
566 # Write over our file using the standard Subscription format
567 assert (
568 smgr["abc123"].write(os.path.join(str(tmpdir0), "subscriptions.json"))
569 is True
570 )
571
572 # We can still open this type as well
573 assert smgr.load(os.path.join(str(tmpdir0), "subscriptions.json")) is True
574 assert bool(smgr) is True
575 assert len(smgr) == 1
576
577 smgr.clear()
578 bad_entry = {
579 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
580 "keys": {
581 "p256dh": "invalid",
582 "auth": "garbage",
583 },
584 }
585
586 subscriptions = os.path.join(str(tmpdir0), "subscriptions.json")
587 with open(subscriptions, "w", encoding="utf-8") as f:
588 # A bad JSON file
589 f.write("{")
590 assert smgr.load(subscriptions) is False
591
592 with open(subscriptions, "w", encoding="utf-8") as f:
593 # not expected dictionary
594 f.write("null")
595 assert smgr.load(subscriptions) is False
596
597 subscriptions = os.path.join(str(tmpdir0), "subscriptions.json")
598 with open(subscriptions, "w", encoding="utf-8") as f:
599 json.dump(bad_entry, f)
600 assert smgr.load(subscriptions) is False
601
602 # Create bad data
603 bad_data = {
604 "bad1": bad_entry,
605 "bad2": bad_entry,
606 "bad3": bad_entry,
607 "bad4": bad_entry,
608 }
609 subscriptions = os.path.join(str(tmpdir0), "subscriptions.json")
610 with open(subscriptions, "w", encoding="utf-8") as f:
611 json.dump(bad_data, f)
612 assert smgr.load(subscriptions) is False
613 assert smgr.load("invalid-file") is False
614
615
616 @pytest.mark.skipif(
617 "cryptography" not in sys.modules, reason="Requires cryptography"
618 )
619 @mock.patch("requests.post")
620 def test_plugin_vapid_initializations(mock_post, tmpdir):
621 """NotifyVapid() Initializations."""
622
623 # Assign our mock object our return value
624 okay_response = requests.Request()
625 okay_response.status_code = requests.codes.ok
626 okay_response.content = ""
627 mock_post.return_value = okay_response
628
629 # Temporary directory
630 tmpdir0 = tmpdir.mkdir("tmp00")
631
632 # Write our subfile
633 smgr = WebPushSubscriptionManager()
634 sub = {
635 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
636 "keys": {
637 "p256dh": (
638 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
639 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
640 ),
641 "auth": "k9Xzm43nBGo=",
642 },
643 }
644 subfile = os.path.join(str(tmpdir0), "subscriptions.json")
645 assert smgr.add(sub) is True
646 assert smgr.add(smgr["abc123"]) is True
647 assert os.listdir(str(tmpdir0)) == []
648
649 with mock.patch("json.dump", side_effect=OSError):
650 # We will fial to write
651 assert smgr.write(subfile) is False
652
653 assert smgr.write(subfile) is True
654 assert os.listdir(str(tmpdir0)) == ["subscriptions.json"]
655 assert isinstance(smgr.json(), str)
656
657 asset_ = asset.AppriseAsset(
658 storage_mode=PersistentStoreMode.FLUSH,
659 storage_path=str(tmpdir0),
660 # Auto-gen our private/public key pair
661 pem_autogen=True,
662 )
663
664 # Auto-Key Generation
665 obj = NotifyVapid(
666 "[email protected]",
667 targets=[
668 "abc123",
669 ],
670 subfile=subfile,
671 asset=asset_,
672 )
673 assert isinstance(obj, NotifyVapid)
674 # Our subscription directory + our
675 # persistent store where our keys were generated
676 assert len(os.listdir(str(tmpdir0))) == 2
677
678 # Second call re-references keys previously generated
679 obj = NotifyVapid(
680 "[email protected]",
681 targets=[
682 "abc123",
683 ],
684 subfile=subfile,
685 asset=asset_,
686 )
687 assert isinstance(obj, NotifyVapid)
688 assert isinstance(obj.url(), str)
689 assert obj.send("test") is True
690 # A second message makes no difference; what is loaded into memory is used
691 assert obj.send("test") is True
692
693 obj = NotifyVapid(
694 "[email protected]",
695 targets=[
696 "abc123",
697 ],
698 subfile="/a/bad/path",
699 asset=asset_,
700 )
701 assert isinstance(obj, NotifyVapid)
702 assert isinstance(obj.url(), str)
703 assert obj.send("test") is False
704 # A second message makes no difference; what is loaded into memory is used
705 assert obj.send("test") is False
706
707 # Detect our keyfile
708 cache_dir = next(
709 x
710 for x in os.listdir(str(tmpdir0))
711 if not x.endswith("subscriptions.json")
712 )
713
714 # Test fixed assignment to our keyfile
715 keyfile = os.path.join(str(tmpdir0), cache_dir, "private_key.pem")
716 assert os.path.exists(keyfile)
717 obj = NotifyVapid(
718 "[email protected]",
719 targets=[
720 "abc123",
721 ],
722 keyfile=keyfile,
723 subfile=subfile,
724 asset=asset_,
725 )
726 assert isinstance(obj, NotifyVapid)
727 assert isinstance(obj.url(), str)
728 assert obj.send("test") is True
729 # A second message makes no difference; what is loaded into memory is used
730 assert obj.send("test") is True
731
732 # Invalid Keyfile
733 obj = NotifyVapid(
734 "[email protected]",
735 targets=[
736 "abc123",
737 ],
738 keyfile=subfile,
739 subfile=subfile,
740 asset=asset_,
741 )
742 assert isinstance(obj, NotifyVapid)
743 assert isinstance(obj.url(), str)
744 assert obj.send("test") is False
745 # A second message makes no difference; what is loaded into memory is used
746 assert obj.send("test") is False
747
748 # AutoGen Temporary directory
749 tmpdir1 = tmpdir.mkdir("tmp01")
750 asset2 = asset.AppriseAsset(
751 storage_mode=PersistentStoreMode.FLUSH,
752 storage_path=str(tmpdir1),
753 # Auto-gen our private/public key pair
754 pem_autogen=True,
755 )
756
757 assert os.listdir(str(tmpdir1)) == []
758 obj = NotifyVapid(
759 "[email protected]",
760 targets=[
761 "abc123",
762 ],
763 keyfile=keyfile,
764 asset=asset2,
765 )
766 assert isinstance(obj, NotifyVapid)
767 assert isinstance(obj.url(), str)
768 # We have a temporary subscription file we can use
769 assert os.listdir(str(tmpdir1)) == ["00088ad3"]
770 # We will have a dud configuration file, but at least it's something
771 # to help the user with
772 assert obj.send("test") is False
773 # Second instance fails as well
774 assert obj.send("test") is False
775
776 # AutoGen Temporary directory
777 tmpdir2 = tmpdir.mkdir("tmp02")
778 asset3 = asset.AppriseAsset(
779 storage_mode=PersistentStoreMode.FLUSH,
780 storage_path=str(tmpdir2),
781 # Auto-gen our private/public key pair
782 pem_autogen=True,
783 )
784
785 # Test invalid keyfile
786 assert os.path.exists(keyfile)
787 obj = NotifyVapid(
788 "[email protected]",
789 targets=[
790 "abc123",
791 ],
792 keyfile="invalid-file",
793 subfile=subfile,
794 asset=asset3,
795 )
796 assert isinstance(obj, NotifyVapid)
797 assert isinstance(obj.url(), str)
798 assert obj.send("test") is False
799 # A second message makes no difference; what is loaded into memory is used
800 assert obj.send("test") is False
801
802
803 @pytest.mark.skipif(
804 "cryptography" in sys.modules,
805 reason="Requires that cryptography NOT be installed",
806 )
807 def test_plugin_vapid_initializations_without_c(tmpdir):
808 """NotifyVapid() Initializations without cryptography."""
809 # Temporary directory
810 tmpdir0 = tmpdir.mkdir("tmp00")
811
812 # Write our subfile
813 smgr = WebPushSubscriptionManager()
814 sub = {
815 "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",
816 "keys": {
817 "p256dh": (
818 "BI2RNIK2PkeCVoEfgVQNjievBi4gWvZxMiuCpOx6K6qCO"
819 "5caru5QCPuc-nEaLplbbFkHxTrR9YzE8ZkTjie5Fq0"
820 ),
821 "auth": "k9Xzm43nBGo=",
822 },
823 }
824 subfile = os.path.join(str(tmpdir0), "subscriptions.json")
825 assert smgr.add(sub) is False
826 asset_ = asset.AppriseAsset(
827 storage_mode=PersistentStoreMode.FLUSH,
828 storage_path=str(tmpdir0),
829 # Auto-gen our private/public key pair
830 pem_autogen=True,
831 )
832
833 # Auto-Key Generation
834 obj = NotifyVapid(
835 "[email protected]",
836 targets=[
837 "abc123",
838 ],
839 subfile=subfile,
840 asset=asset_,
841 )
842 assert isinstance(obj, NotifyVapid)