From 1519e98cda154cf56e7f64f8c6037b0b904d8f99 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Thu, 30 Jun 2022 17:47:06 -0400 Subject: [PATCH] Verify that
is properly escapped in telegram calls (#608) There is no code changes here; but an extra set of unit tests can't hurt. --- test/test_plugin_telegram.py | 174 +++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/test/test_plugin_telegram.py b/test/test_plugin_telegram.py index d2fa3c0c..147ad4a7 100644 --- a/test/test_plugin_telegram.py +++ b/test/test_plugin_telegram.py @@ -792,6 +792,93 @@ def test_plugin_telegram_formating_py3(mock_post): '# A Great Title\r\n_[Apprise Body Title](http://localhost)_ had ' \ '[a change](http://127.0.0.2)' + # Reset our values + mock_post.reset_mock() + + # + # Now test that
is correctly escaped + # + title = 'Test Message Title' + body = 'Test Message Body
ok
' + + aobj = Apprise() + aobj.add('tgram://1234:aaaaaaaaa/-1123456245134') + assert len(aobj) == 1 + + assert aobj.notify( + title=title, body=body, body_format=NotifyFormat.MARKDOWN) + + # Test our calls + assert mock_post.call_count == 1 + + assert mock_post.call_args_list[0][0][0] == \ + 'https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage' + + payload = loads(mock_post.call_args_list[0][1]['data']) + + # Test that everything is escaped properly in a HTML mode + assert payload['text'] == \ + 'Test Message Title\r\n' \ + '\r\n' \ + 'Test Message Body\r\n' \ + 'ok\r\n' + + # Reset our values + mock_post.reset_mock() + + # + # Now test that
is correctly escaped as it would have been via the + # CLI mode where the body_format is TEXT + # + + aobj = Apprise() + aobj.add('tgram://1234:aaaaaaaaa/-1123456245134') + assert len(aobj) == 1 + + assert aobj.notify( + title=title, body=body, body_format=NotifyFormat.TEXT) + + # Test our calls + assert mock_post.call_count == 1 + + assert mock_post.call_args_list[0][0][0] == \ + 'https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage' + + payload = loads(mock_post.call_args_list[0][1]['data']) + + # Test that everything is escaped properly in a HTML mode + assert payload['text'] == \ + 'Test Message Title\r\n' \ + 'Test Message Body <br/> ok</br>' + + # Reset our values + mock_post.reset_mock() + + # + # Now test that
is correctly escaped if fed as HTML + # + + aobj = Apprise() + aobj.add('tgram://1234:aaaaaaaaa/-1123456245134') + assert len(aobj) == 1 + + assert aobj.notify( + title=title, body=body, body_format=NotifyFormat.HTML) + + # Test our calls + assert mock_post.call_count == 1 + + assert mock_post.call_args_list[0][0][0] == \ + 'https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage' + + payload = loads(mock_post.call_args_list[0][1]['data']) + + # Test that everything is escaped properly in a HTML mode + assert payload['text'] == \ + 'Test Message Title\r\n' \ + 'Test Message Body\r\n' \ + 'ok\r\n' + @pytest.mark.skipif(sys.version_info.major >= 3, reason="Requires Python 2.x+") @mock.patch('requests.post') @@ -1094,6 +1181,93 @@ def test_plugin_telegram_formating_py2(mock_post): '# A Great Title\r\n_[Apprise Body Title](http://localhost)_ had ' \ '[a change](http://127.0.0.2)' + # Reset our values + mock_post.reset_mock() + + # + # Now test that
is correctly escaped + # + title = 'Test Message Title' + body = 'Test Message Body
ok
' + + aobj = Apprise() + aobj.add('tgram://1234:aaaaaaaaa/-1123456245134') + assert len(aobj) == 1 + + assert aobj.notify( + title=title, body=body, body_format=NotifyFormat.MARKDOWN) + + # Test our calls + assert mock_post.call_count == 1 + + assert mock_post.call_args_list[0][0][0] == \ + 'https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage' + + payload = loads(mock_post.call_args_list[0][1]['data']) + + # Test that everything is escaped properly in a HTML mode + assert payload['text'] == \ + 'Test Message Title\r\n' \ + '\r\n' \ + 'Test Message Body\r\n' \ + 'ok\r\n' + + # Reset our values + mock_post.reset_mock() + + # + # Now test that
is correctly escaped as it would have been via the + # CLI mode where the body_format is TEXT + # + + aobj = Apprise() + aobj.add('tgram://1234:aaaaaaaaa/-1123456245134') + assert len(aobj) == 1 + + assert aobj.notify( + title=title, body=body, body_format=NotifyFormat.TEXT) + + # Test our calls + assert mock_post.call_count == 1 + + assert mock_post.call_args_list[0][0][0] == \ + 'https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage' + + payload = loads(mock_post.call_args_list[0][1]['data']) + + # Test that everything is escaped properly in a HTML mode + assert payload['text'] == \ + 'Test Message Title\r\n' \ + 'Test Message Body <br/> ok</br>' + + # Reset our values + mock_post.reset_mock() + + # + # Now test that
is correctly escaped if fed as HTML + # + + aobj = Apprise() + aobj.add('tgram://1234:aaaaaaaaa/-1123456245134') + assert len(aobj) == 1 + + assert aobj.notify( + title=title, body=body, body_format=NotifyFormat.HTML) + + # Test our calls + assert mock_post.call_count == 1 + + assert mock_post.call_args_list[0][0][0] == \ + 'https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage' + + payload = loads(mock_post.call_args_list[0][1]['data']) + + # Test that everything is escaped properly in a HTML mode + assert payload['text'] == \ + 'Test Message Title\r\n' \ + 'Test Message Body\r\n' \ + 'ok\r\n' + @mock.patch('requests.post') def test_plugin_telegram_html_formatting(mock_post):