From 5cdab3c3c7f8126470a5c16c4b0395cb21a41804 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Mon, 13 Nov 2023 16:29:50 -0500 Subject: [PATCH 1/3] reproduced error; group reference returns http code 424 --- apprise_api/api/tests/test_stateful_notify.py | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/apprise_api/api/tests/test_stateful_notify.py b/apprise_api/api/tests/test_stateful_notify.py index 57e0af0..263b1dd 100644 --- a/apprise_api/api/tests/test_stateful_notify.py +++ b/apprise_api/api/tests/test_stateful_notify.py @@ -31,6 +31,7 @@ import re import apprise import requests +import inspect class StatefulNotifyTests(SimpleTestCase): @@ -301,3 +302,97 @@ def test_stateful_configuration_io(self, mock_post): # Reset our count mock_post.reset_mock() + + @patch('requests.post') + def test_stateful_group_notify(self, mock_post): + """ + Test the writing, removal, writing of a group based configuration + """ + + # our key to use + key = 'test_stateful_group_notify' + + request = Mock() + request.content = b'ok' + request.status_code = requests.codes.ok + mock_post.return_value = request + + # Monkey Patch + apprise.plugins.NotifyEmail.NotifyEmail.enabled = True + + config = inspect.cleandoc(""" + version: 1 + groups: + mygroup: user1, user2 + + urls: + - json:///user:pass@localhost: + - to: user1@example.com + tag: user1 + - to: user2@example.com + tag: user2 + """) + + # Monkey Patch + apprise.plugins.NotifyJSON.NotifyJSON.enabled = True + + # Add our content + response = self.client.post( + '/add/{}'.format(key), + {'config': config}) + assert response.status_code == 200 + + # Now we should be able to see our content + response = self.client.post('/get/{}'.format(key)) + assert response.status_code == 200 + + for tag in ('user1', 'user2'): + form_data = { + 'body': '## test notifiction', + 'format': apprise.NotifyFormat.MARKDOWN, + 'tag': tag, + } + form = NotifyForm(data=form_data) + assert form.is_valid() + + # Required to prevent None from being passed into + # self.client.post() + del form.cleaned_data['attachment'] + + # We sent the notification successfully + response = self.client.post( + '/notify/{}'.format(key), form.cleaned_data) + assert response.status_code == 200 + assert mock_post.call_count == 1 + + mock_post.reset_mock() + + + # Now let's notify by our group + form_data = { + 'body': '## test notifiction', + 'format': apprise.NotifyFormat.MARKDOWN, + 'tag': 'mygroup', + } + + form = NotifyForm(data=form_data) + assert form.is_valid() + + # Required to prevent None from being passed into + # self.client.post() + del form.cleaned_data['attachment'] + + # We sent the notification successfully + response = self.client.post( + '/notify/{}'.format(key), form.cleaned_data) + assert response.status_code == 200 + assert mock_post.call_count == 1 + + mock_post.reset_mock() + + # Now empty our data + response = self.client.post('/del/{}'.format(key)) + assert response.status_code == 200 + + # Reset our count + mock_post.reset_mock() From dc5994822c0e0bc9c4787f488f1ed98114a31b2d Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Mon, 13 Nov 2023 19:45:33 -0500 Subject: [PATCH 2/3] updated testing --- apprise_api/api/tests/test_stateful_notify.py | 108 +++++++++++++++++- 1 file changed, 104 insertions(+), 4 deletions(-) diff --git a/apprise_api/api/tests/test_stateful_notify.py b/apprise_api/api/tests/test_stateful_notify.py index 263b1dd..e325ec3 100644 --- a/apprise_api/api/tests/test_stateful_notify.py +++ b/apprise_api/api/tests/test_stateful_notify.py @@ -304,13 +304,13 @@ def test_stateful_configuration_io(self, mock_post): mock_post.reset_mock() @patch('requests.post') - def test_stateful_group_notify(self, mock_post): + def test_stateful_group_dict_notify(self, mock_post): """ - Test the writing, removal, writing of a group based configuration + Test the handling of a group defined as a dictionary """ # our key to use - key = 'test_stateful_group_notify' + key = 'test_stateful_group_notify_dict' request = Mock() request.content = b'ok' @@ -363,10 +363,108 @@ def test_stateful_group_notify(self, mock_post): response = self.client.post( '/notify/{}'.format(key), form.cleaned_data) assert response.status_code == 200 + + # Our single endpoint is notified assert mock_post.call_count == 1 mock_post.reset_mock() + # Now let's notify by our group + form_data = { + 'body': '## test notifiction', + 'format': apprise.NotifyFormat.MARKDOWN, + 'tag': 'mygroup', + } + + form = NotifyForm(data=form_data) + assert form.is_valid() + + # Required to prevent None from being passed into + # self.client.post() + del form.cleaned_data['attachment'] + + # We sent the notification successfully + response = self.client.post( + '/notify/{}'.format(key), form.cleaned_data) + assert response.status_code == 200 + + # Our 2 endpoints are notified + assert mock_post.call_count == 2 + + mock_post.reset_mock() + + # Now empty our data + response = self.client.post('/del/{}'.format(key)) + assert response.status_code == 200 + + # Reset our count + mock_post.reset_mock() + + @patch('requests.post') + def test_stateful_group_dictlist_notify(self, mock_post): + """ + Test the handling of a group defined as a list of dictionaries + """ + + # our key to use + key = 'test_stateful_group_notify_list_dict' + + request = Mock() + request.content = b'ok' + request.status_code = requests.codes.ok + mock_post.return_value = request + + # Monkey Patch + apprise.plugins.NotifyEmail.NotifyEmail.enabled = True + + config = inspect.cleandoc(""" + version: 1 + groups: + - mygroup: user1, user2 + + urls: + - json:///user:pass@localhost: + - to: user1@example.com + tag: user1 + - to: user2@example.com + tag: user2 + """) + + # Monkey Patch + apprise.plugins.NotifyJSON.NotifyJSON.enabled = True + + # Add our content + response = self.client.post( + '/add/{}'.format(key), + {'config': config}) + assert response.status_code == 200 + + # Now we should be able to see our content + response = self.client.post('/get/{}'.format(key)) + assert response.status_code == 200 + + for tag in ('user1', 'user2'): + form_data = { + 'body': '## test notifiction', + 'format': apprise.NotifyFormat.MARKDOWN, + 'tag': tag, + } + form = NotifyForm(data=form_data) + assert form.is_valid() + + # Required to prevent None from being passed into + # self.client.post() + del form.cleaned_data['attachment'] + + # We sent the notification successfully + response = self.client.post( + '/notify/{}'.format(key), form.cleaned_data) + assert response.status_code == 200 + + # Our single endpoint is notified + assert mock_post.call_count == 1 + + mock_post.reset_mock() # Now let's notify by our group form_data = { @@ -386,7 +484,9 @@ def test_stateful_group_notify(self, mock_post): response = self.client.post( '/notify/{}'.format(key), form.cleaned_data) assert response.status_code == 200 - assert mock_post.call_count == 1 + + # Our 2 endpoints are notified + assert mock_post.call_count == 2 mock_post.reset_mock() From 6bb596633bae4c92fc00dfcba4fd5d789902e033 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Thu, 28 Dec 2023 15:18:45 -0500 Subject: [PATCH 3/3] cleanup --- apprise_api/api/tests/test_stateful_notify.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/apprise_api/api/tests/test_stateful_notify.py b/apprise_api/api/tests/test_stateful_notify.py index e325ec3..63983cc 100644 --- a/apprise_api/api/tests/test_stateful_notify.py +++ b/apprise_api/api/tests/test_stateful_notify.py @@ -33,6 +33,9 @@ import requests import inspect +# Grant access to our Notification Manager Singleton +N_MGR = apprise.NotificationManager.NotificationManager() + class StatefulNotifyTests(SimpleTestCase): """ @@ -69,19 +72,19 @@ def test_stateful_configuration_io(self, mock_post): mock_post.return_value = request # Monkey Patch - apprise.plugins.NotifyEmail.NotifyEmail.enabled = True + N_MGR['mailto'].enabled = True # Preare our list of URLs we want to save urls = [ 'devops=slack://TokenA/TokenB/TokenC', - 'pusbullet=pbul://tokendetails', + 'pushbullet=pbul://tokendetails', 'general,json=json://hostname', ] # Monkey Patch - apprise.plugins.NotifySlack.NotifySlack.enabled = True - apprise.plugins.NotifyPushBullet.NotifyPushBullet.enabled = True - apprise.plugins.NotifyJSON.NotifyJSON.enabled = True + N_MGR['slack'].enabled = True + N_MGR['pbul'].enabled = True + N_MGR['json'].enabled = True # For 10 iterations, repeat these tests to verify that don't change # and our saved content is not different on subsequent calls. @@ -318,7 +321,7 @@ def test_stateful_group_dict_notify(self, mock_post): mock_post.return_value = request # Monkey Patch - apprise.plugins.NotifyEmail.NotifyEmail.enabled = True + N_MGR['mailto'].enabled = True config = inspect.cleandoc(""" version: 1 @@ -334,7 +337,7 @@ def test_stateful_group_dict_notify(self, mock_post): """) # Monkey Patch - apprise.plugins.NotifyJSON.NotifyJSON.enabled = True + N_MGR['json'].enabled = True # Add our content response = self.client.post( @@ -415,7 +418,7 @@ def test_stateful_group_dictlist_notify(self, mock_post): mock_post.return_value = request # Monkey Patch - apprise.plugins.NotifyEmail.NotifyEmail.enabled = True + N_MGR['mailto'].enabled = True config = inspect.cleandoc(""" version: 1 @@ -431,7 +434,7 @@ def test_stateful_group_dictlist_notify(self, mock_post): """) # Monkey Patch - apprise.plugins.NotifyJSON.NotifyJSON.enabled = True + N_MGR['json'].enabled = True # Add our content response = self.client.post(