|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# (c) 2021 The mqttwarn developers |
| 3 | +import logging |
| 4 | +from unittest import mock |
| 5 | +from unittest.mock import call |
| 6 | + |
| 7 | +from mqttwarn.model import ProcessorItem as Item |
| 8 | +from mqttwarn.util import load_module_by_name |
| 9 | +from surrogate import surrogate |
| 10 | + |
| 11 | + |
| 12 | +@surrogate("apprise") |
| 13 | +@mock.patch("apprise.Apprise", create=True) |
| 14 | +@mock.patch("apprise.AppriseAsset", create=True) |
| 15 | +def test_apprise_multi_basic_success(apprise_asset, apprise_mock, srv, caplog): |
| 16 | + |
| 17 | + with caplog.at_level(logging.DEBUG): |
| 18 | + |
| 19 | + module = load_module_by_name("mqttwarn.services.apprise_multi") |
| 20 | + |
| 21 | + item = Item( |
| 22 | + addrs=[ |
| 23 | + {"baseuri": "json://localhost:1234/mqtthook"}, |
| 24 | + {"baseuri": "json://daq.example.org:5555/foobar"}, |
| 25 | + ], |
| 26 | + title="⚽ Message title ⚽", |
| 27 | + message="⚽ Notification message ⚽", |
| 28 | + ) |
| 29 | + |
| 30 | + outcome = module.plugin(srv, item) |
| 31 | + |
| 32 | + assert apprise_mock.mock_calls == [ |
| 33 | + call(asset=mock.ANY), |
| 34 | + call().add("json://localhost:1234/mqtthook"), |
| 35 | + call().add("json://daq.example.org:5555/foobar"), |
| 36 | + call().notify(body="⚽ Notification message ⚽", title="⚽ Message title ⚽"), |
| 37 | + call().notify().__bool__(), |
| 38 | + ] |
| 39 | + |
| 40 | + assert outcome is True |
| 41 | + assert ( |
| 42 | + "Sending notification to Apprise. target=None, addresses=[{'baseuri': 'json://localhost:1234/mqtthook'}, {'baseuri': 'json://daq.example.org:5555/foobar'}]" |
| 43 | + in caplog.messages |
| 44 | + ) |
| 45 | + assert "Successfully sent message using Apprise" in caplog.messages |
| 46 | + |
| 47 | + |
| 48 | +@surrogate("apprise") |
| 49 | +@mock.patch("apprise.Apprise", create=True) |
| 50 | +@mock.patch("apprise.AppriseAsset", create=True) |
| 51 | +def test_apprise_multi_mailto_success(apprise_asset, apprise_mock, srv, caplog): |
| 52 | + |
| 53 | + with caplog.at_level(logging.DEBUG): |
| 54 | + |
| 55 | + module = load_module_by_name("mqttwarn.services.apprise_multi") |
| 56 | + |
| 57 | + item = Item( |
| 58 | + addrs=[ |
| 59 | + { |
| 60 | + "baseuri": "mailtos://smtp_username:[email protected]", |
| 61 | + |
| 62 | + |
| 63 | + "sender_name": "Example Monitoring", |
| 64 | + } |
| 65 | + ], |
| 66 | + title="⚽ Message title ⚽", |
| 67 | + message="⚽ Notification message ⚽", |
| 68 | + ) |
| 69 | + |
| 70 | + outcome = module.plugin(srv, item) |
| 71 | + |
| 72 | + assert apprise_mock.mock_calls == [ |
| 73 | + call(asset=mock.ANY), |
| 74 | + call().add( |
| 75 | + "mailtos://smtp_username:[email protected]?to=foo%40example.org%2Cbar%40example.org&from=monitoring%40example.org&name=Example+Monitoring" |
| 76 | + ), |
| 77 | + call().notify(body="⚽ Notification message ⚽", title="⚽ Message title ⚽"), |
| 78 | + call().notify().__bool__(), |
| 79 | + ] |
| 80 | + |
| 81 | + assert outcome is True |
| 82 | + assert ( |
| 83 | + "Sending notification to Apprise. target=None, addresses=[{'baseuri': 'mailtos://smtp_username:[email protected]', 'recipients': ['[email protected]', '[email protected]'], 'sender': '[email protected]', 'sender_name': 'Example Monitoring'}]" |
| 84 | + in caplog.messages |
| 85 | + ) |
| 86 | + assert "Successfully sent message using Apprise" in caplog.messages |
| 87 | + |
| 88 | + |
| 89 | +@surrogate("apprise") |
| 90 | +def test_apprise_multi_failure_notify(srv, caplog): |
| 91 | + |
| 92 | + with caplog.at_level(logging.DEBUG): |
| 93 | + |
| 94 | + mock_connection = mock.MagicMock() |
| 95 | + |
| 96 | + # Make the call to `notify` signal failure. |
| 97 | + def error(*args, **kwargs): |
| 98 | + return False |
| 99 | + |
| 100 | + mock_connection.notify = error |
| 101 | + |
| 102 | + with mock.patch( |
| 103 | + "apprise.Apprise", side_effect=[mock_connection], create=True |
| 104 | + ) as mock_client: |
| 105 | + with mock.patch("apprise.AppriseAsset", create=True) as mock_asset: |
| 106 | + module = load_module_by_name("mqttwarn.services.apprise_multi") |
| 107 | + |
| 108 | + item = Item( |
| 109 | + addrs=[{"baseuri": "json://localhost:1234/mqtthook"}], |
| 110 | + title="⚽ Message title ⚽", |
| 111 | + message="⚽ Notification message ⚽", |
| 112 | + ) |
| 113 | + |
| 114 | + outcome = module.plugin(srv, item) |
| 115 | + |
| 116 | + assert mock_client.mock_calls == [ |
| 117 | + mock.call(asset=mock.ANY), |
| 118 | + ] |
| 119 | + assert mock_connection.mock_calls == [ |
| 120 | + call.add("json://localhost:1234/mqtthook"), |
| 121 | + ] |
| 122 | + |
| 123 | + assert outcome is False |
| 124 | + assert ( |
| 125 | + "Sending notification to Apprise. target=None, addresses=[{'baseuri': 'json://localhost:1234/mqtthook'}]" |
| 126 | + in caplog.messages |
| 127 | + ) |
| 128 | + assert "Sending message using Apprise failed" in caplog.messages |
| 129 | + |
| 130 | + |
| 131 | +@surrogate("apprise") |
| 132 | +def test_apprise_multi_error(srv, caplog): |
| 133 | + |
| 134 | + with caplog.at_level(logging.DEBUG): |
| 135 | + |
| 136 | + mock_connection = mock.MagicMock() |
| 137 | + |
| 138 | + # Make the call to `notify` raise an exception. |
| 139 | + def error(*args, **kwargs): |
| 140 | + raise Exception("something failed") |
| 141 | + |
| 142 | + mock_connection.notify = error |
| 143 | + |
| 144 | + with mock.patch( |
| 145 | + "apprise.Apprise", side_effect=[mock_connection], create=True |
| 146 | + ) as mock_client: |
| 147 | + with mock.patch("apprise.AppriseAsset", create=True) as mock_asset: |
| 148 | + module = load_module_by_name("mqttwarn.services.apprise_multi") |
| 149 | + |
| 150 | + item = Item( |
| 151 | + addrs=[{"baseuri": "json://localhost:1234/mqtthook"}], |
| 152 | + title="⚽ Message title ⚽", |
| 153 | + message="⚽ Notification message ⚽", |
| 154 | + ) |
| 155 | + |
| 156 | + outcome = module.plugin(srv, item) |
| 157 | + |
| 158 | + assert mock_client.mock_calls == [ |
| 159 | + mock.call(asset=mock.ANY), |
| 160 | + ] |
| 161 | + assert mock_connection.mock_calls == [ |
| 162 | + call.add("json://localhost:1234/mqtthook"), |
| 163 | + ] |
| 164 | + |
| 165 | + assert outcome is False |
| 166 | + assert ( |
| 167 | + "Sending notification to Apprise. target=None, addresses=[{'baseuri': 'json://localhost:1234/mqtthook'}]" |
| 168 | + in caplog.messages |
| 169 | + ) |
| 170 | + assert ( |
| 171 | + "Sending message using Apprise failed. target=None, error=something failed" |
| 172 | + in caplog.messages |
| 173 | + ) |
0 commit comments