|
| 1 | +# Copyright (C) 2025 Chris Caron <[email protected]> |
| 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 | +from django.test import SimpleTestCase, override_settings |
| 27 | +import requests |
| 28 | + |
| 29 | +import apprise |
| 30 | + |
| 31 | +from ..forms import NotifyForm |
| 32 | + |
| 33 | +# Grant access to our Notification Manager Singleton |
| 34 | +N_MGR = apprise.manager_plugins.NotificationManager() |
| 35 | + |
| 36 | + |
| 37 | +class NotifyWithEmojiTests(SimpleTestCase): |
| 38 | + """ |
| 39 | + Test notifications with Emoji Settings |
| 40 | + """ |
| 41 | + |
| 42 | + @mock.patch("requests.post") |
| 43 | + def test_stateful_notify_with_emoji(self, mock_post): |
| 44 | + """ |
| 45 | + Test adding a simple stateful notification with emoji flags |
| 46 | + """ |
| 47 | + |
| 48 | + # Prepare our response |
| 49 | + response = requests.Request() |
| 50 | + response.status_code = requests.codes.ok |
| 51 | + |
| 52 | + # Prepare Mock |
| 53 | + mock_post.return_value = response |
| 54 | + |
| 55 | + # our key to use |
| 56 | + key = "test_notify_stateful_emoji" |
| 57 | + |
| 58 | + # Add some content |
| 59 | + response = self.client.post("/add/{}".format(key), {"urls": "json://user:pass@localhost"}) |
| 60 | + assert response.status_code == 200 |
| 61 | + |
| 62 | + # Preare our form data (with emoji's in it) |
| 63 | + form_data = { |
| 64 | + "title": ":grin:", |
| 65 | + "body": "test notifiction :smile:", |
| 66 | + } |
| 67 | + |
| 68 | + # At a minimum, just a body is required |
| 69 | + form = NotifyForm(data=form_data) |
| 70 | + assert form.is_valid() |
| 71 | + |
| 72 | + # Required to prevent None from being passed into self.client.post() |
| 73 | + del form.cleaned_data["attachment"] |
| 74 | + |
| 75 | + # we always set a type if one wasn't done so already |
| 76 | + assert form.cleaned_data["type"] == apprise.NotifyType.INFO.value |
| 77 | + |
| 78 | + # we always set a format if one wasn't done so already |
| 79 | + assert form.cleaned_data["format"] == apprise.NotifyFormat.TEXT.value |
| 80 | + |
| 81 | + # Send our notification |
| 82 | + with override_settings(APPRISE_INTERPRET_EMOJIS=True): |
| 83 | + response = self.client.post("/notify/{}".format(key), form.cleaned_data) |
| 84 | + assert response.status_code == 200 |
| 85 | + assert mock_post.call_count == 1 |
| 86 | + |
| 87 | + details = mock_post.call_args_list[0] |
| 88 | + assert details[0][0].startswith("http://localhost") |
| 89 | + |
| 90 | + payload = json.loads(details[1]["data"]) |
| 91 | + assert payload["title"] == "😃" |
| 92 | + assert payload["message"] == "test notifiction 😄" |
| 93 | + |
| 94 | + # Reset our mock object |
| 95 | + mock_post.reset_mock() |
| 96 | + |
| 97 | + # Send our notification |
| 98 | + with override_settings(APPRISE_INTERPRET_EMOJIS=False): |
| 99 | + response = self.client.post("/notify/{}".format(key), form.cleaned_data) |
| 100 | + assert response.status_code == 200 |
| 101 | + assert mock_post.call_count == 1 |
| 102 | + |
| 103 | + details = mock_post.call_args_list[0] |
| 104 | + assert details[0][0].startswith("http://localhost") |
| 105 | + |
| 106 | + payload = json.loads(details[1]["data"]) |
| 107 | + assert payload["title"] == ":grin:" |
| 108 | + assert payload["message"] == "test notifiction :smile:" |
| 109 | + |
| 110 | + # Reset our mock object |
| 111 | + mock_post.reset_mock() |
| 112 | + |
| 113 | + @mock.patch("requests.post") |
| 114 | + def test_stateless_notify_with_emoji(self, mock_post): |
| 115 | + """ |
| 116 | + Test adding a simple stateless notification with emoji flags |
| 117 | + """ |
| 118 | + |
| 119 | + # Prepare our response |
| 120 | + response = requests.Request() |
| 121 | + response.status_code = requests.codes.ok |
| 122 | + |
| 123 | + # Prepare Mock |
| 124 | + mock_post.return_value = response |
| 125 | + |
| 126 | + # Preare our payload (with emoji entries) |
| 127 | + json_data = { |
| 128 | + "urls": "json://user:pass@localhost", |
| 129 | + "title": ":grin:", |
| 130 | + "body": "test notifiction :smile:", |
| 131 | + } |
| 132 | + |
| 133 | + with override_settings(APPRISE_INTERPRET_EMOJIS=True): |
| 134 | + # Send our notification as a JSON object |
| 135 | + response = self.client.post( |
| 136 | + "/notify", |
| 137 | + data=json.dumps(json_data), |
| 138 | + content_type="application/json", |
| 139 | + ) |
| 140 | + assert response.status_code == 200 |
| 141 | + assert mock_post.call_count == 1 |
| 142 | + |
| 143 | + details = mock_post.call_args_list[0] |
| 144 | + assert details[0][0].startswith("http://localhost") |
| 145 | + |
| 146 | + payload = json.loads(details[1]["data"]) |
| 147 | + assert payload["title"] == "😃" |
| 148 | + assert payload["message"] == "test notifiction 😄" |
| 149 | + |
| 150 | + # Reset Mock |
| 151 | + mock_post.reset_mock() |
| 152 | + |
| 153 | + with override_settings(APPRISE_INTERPRET_EMOJIS=False): |
| 154 | + # Send our notification as a JSON object |
| 155 | + response = self.client.post( |
| 156 | + "/notify", |
| 157 | + data=json.dumps(json_data), |
| 158 | + content_type="application/json", |
| 159 | + ) |
| 160 | + assert response.status_code == 200 |
| 161 | + assert mock_post.call_count == 1 |
| 162 | + |
| 163 | + details = mock_post.call_args_list[0] |
| 164 | + assert details[0][0].startswith("http://localhost") |
| 165 | + |
| 166 | + payload = json.loads(details[1]["data"]) |
| 167 | + assert payload["title"] == ":grin:" |
| 168 | + assert payload["message"] == "test notifiction :smile:" |
| 169 | + |
| 170 | + # Reset Mock |
| 171 | + mock_post.reset_mock() |
0 commit comments