|
| 1 | +# Copyright (C) 2026 Chris Caron <lead2gold@gmail.com> |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This code is licensed under the MIT License. |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files(the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions : |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +# THE SOFTWARE. |
| 23 | +import json |
| 24 | +from unittest import mock |
| 25 | + |
| 26 | +import apprise |
| 27 | +from django.test import SimpleTestCase, override_settings |
| 28 | + |
| 29 | +from ..forms import NotifyForm |
| 30 | + |
| 31 | +# Grant access to our Notification Manager Singleton |
| 32 | +N_MGR = apprise.manager_plugins.NotificationManager() |
| 33 | + |
| 34 | + |
| 35 | +class NotifyWithRedirectTests(SimpleTestCase): |
| 36 | + """ |
| 37 | + Test that APPRISE_HTTP_REDIRECTS is wired correctly to AppriseAsset. |
| 38 | +
|
| 39 | + The apprise package installed in the test environment may pre-date the |
| 40 | + http_redirects attribute, so these tests mock AppriseAsset directly to |
| 41 | + verify that views.py passes the correct value rather than running the |
| 42 | + full notification stack. |
| 43 | + """ |
| 44 | + |
| 45 | + def _make_asset_spy(self): |
| 46 | + """Return a mock that patches AppriseAsset and captures its kwargs.""" |
| 47 | + real_asset_cls = apprise.AppriseAsset |
| 48 | + |
| 49 | + captured = {} |
| 50 | + |
| 51 | + class SpyAsset(real_asset_cls): |
| 52 | + def __init__(self, **kwargs): |
| 53 | + # Capture what was passed before forwarding |
| 54 | + captured.update(kwargs) |
| 55 | + # Remove our new kwarg if the installed apprise doesn't |
| 56 | + # know about it yet so the real __init__ doesn't raise |
| 57 | + kwargs.pop("http_redirects", None) |
| 58 | + super().__init__(**kwargs) |
| 59 | + |
| 60 | + return SpyAsset, captured |
| 61 | + |
| 62 | + def test_stateful_notify_passes_redirects_to_asset(self): |
| 63 | + """ |
| 64 | + Stateful /notify/{key} passes APPRISE_HTTP_REDIRECTS to AppriseAsset. |
| 65 | + """ |
| 66 | + spy_cls, captured = self._make_asset_spy() |
| 67 | + |
| 68 | + key = "test_redirect_stateful" |
| 69 | + |
| 70 | + # Seed stateful config |
| 71 | + with mock.patch("apprise.AppriseAsset", spy_cls): |
| 72 | + self.client.post("/add/{}".format(key), {"urls": "json://user:pass@localhost"}) |
| 73 | + |
| 74 | + form_data = {"title": "Test", "body": "Body"} |
| 75 | + form = NotifyForm(data=form_data) |
| 76 | + assert form.is_valid() |
| 77 | + del form.cleaned_data["attachment"] |
| 78 | + |
| 79 | + # Redirects disabled (default) |
| 80 | + spy_cls, captured = self._make_asset_spy() |
| 81 | + with mock.patch("apprise.AppriseAsset", spy_cls), override_settings(APPRISE_HTTP_REDIRECTS=False): |
| 82 | + self.client.post("/notify/{}".format(key), form.cleaned_data) |
| 83 | + assert captured.get("http_redirects") is False |
| 84 | + |
| 85 | + # Redirects enabled |
| 86 | + spy_cls, captured = self._make_asset_spy() |
| 87 | + with mock.patch("apprise.AppriseAsset", spy_cls), override_settings(APPRISE_HTTP_REDIRECTS=True): |
| 88 | + self.client.post("/notify/{}".format(key), form.cleaned_data) |
| 89 | + assert captured.get("http_redirects") is True |
| 90 | + |
| 91 | + def test_stateless_notify_passes_redirects_to_asset(self): |
| 92 | + """ |
| 93 | + Stateless /notify passes APPRISE_HTTP_REDIRECTS to AppriseAsset. |
| 94 | + """ |
| 95 | + json_data = { |
| 96 | + "urls": "json://user:pass@localhost", |
| 97 | + "title": "Test", |
| 98 | + "body": "Body", |
| 99 | + } |
| 100 | + |
| 101 | + # Redirects disabled (default) |
| 102 | + spy_cls, captured = self._make_asset_spy() |
| 103 | + with mock.patch("apprise.AppriseAsset", spy_cls), override_settings(APPRISE_HTTP_REDIRECTS=False): |
| 104 | + self.client.post( |
| 105 | + "/notify", |
| 106 | + data=json.dumps(json_data), |
| 107 | + content_type="application/json", |
| 108 | + ) |
| 109 | + assert captured.get("http_redirects") is False |
| 110 | + |
| 111 | + # Redirects enabled |
| 112 | + spy_cls, captured = self._make_asset_spy() |
| 113 | + with mock.patch("apprise.AppriseAsset", spy_cls), override_settings(APPRISE_HTTP_REDIRECTS=True): |
| 114 | + self.client.post( |
| 115 | + "/notify", |
| 116 | + data=json.dumps(json_data), |
| 117 | + content_type="application/json", |
| 118 | + ) |
| 119 | + assert captured.get("http_redirects") is True |
0 commit comments