2
0

test_ntfy.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. from enum import Enum
  2. from flexmock import flexmock
  3. import borgmatic.hooks.monitor
  4. from borgmatic.hooks import ntfy as module
  5. default_base_url = 'https://ntfy.sh'
  6. custom_base_url = 'https://ntfy.example.com'
  7. topic = 'borgmatic-unit-testing'
  8. custom_message_config = {
  9. 'title': 'borgmatic unit testing',
  10. 'message': 'borgmatic unit testing',
  11. 'priority': 'min',
  12. 'tags': '+1',
  13. }
  14. custom_message_headers = {
  15. 'X-Title': custom_message_config['title'],
  16. 'X-Message': custom_message_config['message'],
  17. 'X-Priority': custom_message_config['priority'],
  18. 'X-Tags': custom_message_config['tags'],
  19. }
  20. def return_default_message_headers(state=Enum):
  21. headers = {
  22. 'X-Title': f'A borgmatic {state.name} event happened',
  23. 'X-Message': f'A borgmatic {state.name} event happened',
  24. 'X-Priority': 'default',
  25. 'X-Tags': 'borgmatic',
  26. }
  27. return headers
  28. def test_ping_monitor_minimal_config_hits_hosted_ntfy_on_fail():
  29. hook_config = {'topic': topic}
  30. flexmock(module.requests).should_receive('post').with_args(
  31. f'{default_base_url}/{topic}',
  32. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  33. auth=None,
  34. ).and_return(flexmock(ok=True)).once()
  35. module.ping_monitor(
  36. hook_config,
  37. {},
  38. 'config.yaml',
  39. borgmatic.hooks.monitor.State.FAIL,
  40. monitoring_log_level=1,
  41. dry_run=False,
  42. )
  43. def test_ping_monitor_with_access_token_hits_hosted_ntfy_on_fail():
  44. hook_config = {
  45. 'topic': topic,
  46. 'access_token': 'abc123',
  47. }
  48. flexmock(module.requests).should_receive('post').with_args(
  49. f'{default_base_url}/{topic}',
  50. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  51. auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
  52. ).and_return(flexmock(ok=True)).once()
  53. module.ping_monitor(
  54. hook_config,
  55. {},
  56. 'config.yaml',
  57. borgmatic.hooks.monitor.State.FAIL,
  58. monitoring_log_level=1,
  59. dry_run=False,
  60. )
  61. def test_ping_monitor_with_username_password_and_access_token_ignores_username_password():
  62. hook_config = {
  63. 'topic': topic,
  64. 'username': 'testuser',
  65. 'password': 'fakepassword',
  66. 'access_token': 'abc123',
  67. }
  68. flexmock(module.requests).should_receive('post').with_args(
  69. f'{default_base_url}/{topic}',
  70. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  71. auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
  72. ).and_return(flexmock(ok=True)).once()
  73. flexmock(module.logger).should_receive('warning').once()
  74. module.ping_monitor(
  75. hook_config,
  76. {},
  77. 'config.yaml',
  78. borgmatic.hooks.monitor.State.FAIL,
  79. monitoring_log_level=1,
  80. dry_run=False,
  81. )
  82. def test_ping_monitor_with_username_password_hits_hosted_ntfy_on_fail():
  83. hook_config = {
  84. 'topic': topic,
  85. 'username': 'testuser',
  86. 'password': 'fakepassword',
  87. }
  88. flexmock(module.requests).should_receive('post').with_args(
  89. f'{default_base_url}/{topic}',
  90. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  91. auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
  92. ).and_return(flexmock(ok=True)).once()
  93. module.ping_monitor(
  94. hook_config,
  95. {},
  96. 'config.yaml',
  97. borgmatic.hooks.monitor.State.FAIL,
  98. monitoring_log_level=1,
  99. dry_run=False,
  100. )
  101. def test_ping_monitor_with_password_but_no_username_warns():
  102. hook_config = {'topic': topic, 'password': 'fakepassword'}
  103. flexmock(module.requests).should_receive('post').with_args(
  104. f'{default_base_url}/{topic}',
  105. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  106. auth=None,
  107. ).and_return(flexmock(ok=True)).once()
  108. flexmock(module.logger).should_receive('warning').once()
  109. module.ping_monitor(
  110. hook_config,
  111. {},
  112. 'config.yaml',
  113. borgmatic.hooks.monitor.State.FAIL,
  114. monitoring_log_level=1,
  115. dry_run=False,
  116. )
  117. def test_ping_monitor_with_username_but_no_password_warns():
  118. hook_config = {'topic': topic, 'username': 'testuser'}
  119. flexmock(module.requests).should_receive('post').with_args(
  120. f'{default_base_url}/{topic}',
  121. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  122. auth=None,
  123. ).and_return(flexmock(ok=True)).once()
  124. flexmock(module.logger).should_receive('warning').once()
  125. module.ping_monitor(
  126. hook_config,
  127. {},
  128. 'config.yaml',
  129. borgmatic.hooks.monitor.State.FAIL,
  130. monitoring_log_level=1,
  131. dry_run=False,
  132. )
  133. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
  134. hook_config = {'topic': topic}
  135. flexmock(module.requests).should_receive('post').never()
  136. module.ping_monitor(
  137. hook_config,
  138. {},
  139. 'config.yaml',
  140. borgmatic.hooks.monitor.State.START,
  141. monitoring_log_level=1,
  142. dry_run=False,
  143. )
  144. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_finish():
  145. hook_config = {'topic': topic}
  146. flexmock(module.requests).should_receive('post').never()
  147. module.ping_monitor(
  148. hook_config,
  149. {},
  150. 'config.yaml',
  151. borgmatic.hooks.monitor.State.FINISH,
  152. monitoring_log_level=1,
  153. dry_run=False,
  154. )
  155. def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
  156. hook_config = {'topic': topic, 'server': custom_base_url}
  157. flexmock(module.requests).should_receive('post').with_args(
  158. f'{custom_base_url}/{topic}',
  159. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  160. auth=None,
  161. ).and_return(flexmock(ok=True)).once()
  162. module.ping_monitor(
  163. hook_config,
  164. {},
  165. 'config.yaml',
  166. borgmatic.hooks.monitor.State.FAIL,
  167. monitoring_log_level=1,
  168. dry_run=False,
  169. )
  170. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
  171. hook_config = {'topic': topic}
  172. flexmock(module.requests).should_receive('post').never()
  173. module.ping_monitor(
  174. hook_config,
  175. {},
  176. 'config.yaml',
  177. borgmatic.hooks.monitor.State.FAIL,
  178. monitoring_log_level=1,
  179. dry_run=True,
  180. )
  181. def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
  182. hook_config = {'topic': topic, 'fail': custom_message_config}
  183. flexmock(module.requests).should_receive('post').with_args(
  184. f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
  185. ).and_return(flexmock(ok=True)).once()
  186. module.ping_monitor(
  187. hook_config,
  188. {},
  189. 'config.yaml',
  190. borgmatic.hooks.monitor.State.FAIL,
  191. monitoring_log_level=1,
  192. dry_run=False,
  193. )
  194. def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
  195. hook_config = {'topic': topic, 'states': ['start', 'fail']}
  196. flexmock(module.requests).should_receive('post').with_args(
  197. f'{default_base_url}/{topic}',
  198. headers=return_default_message_headers(borgmatic.hooks.monitor.State.START),
  199. auth=None,
  200. ).and_return(flexmock(ok=True)).once()
  201. module.ping_monitor(
  202. hook_config,
  203. {},
  204. 'config.yaml',
  205. borgmatic.hooks.monitor.State.START,
  206. monitoring_log_level=1,
  207. dry_run=False,
  208. )
  209. def test_ping_monitor_with_connection_error_logs_warning():
  210. hook_config = {'topic': topic}
  211. flexmock(module.requests).should_receive('post').with_args(
  212. f'{default_base_url}/{topic}',
  213. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  214. auth=None,
  215. ).and_raise(module.requests.exceptions.ConnectionError)
  216. flexmock(module.logger).should_receive('warning').once()
  217. module.ping_monitor(
  218. hook_config,
  219. {},
  220. 'config.yaml',
  221. borgmatic.hooks.monitor.State.FAIL,
  222. monitoring_log_level=1,
  223. dry_run=False,
  224. )
  225. def test_ping_monitor_with_other_error_logs_warning():
  226. hook_config = {'topic': topic}
  227. response = flexmock(ok=False)
  228. response.should_receive('raise_for_status').and_raise(
  229. module.requests.exceptions.RequestException
  230. )
  231. flexmock(module.requests).should_receive('post').with_args(
  232. f'{default_base_url}/{topic}',
  233. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  234. auth=None,
  235. ).and_return(response)
  236. flexmock(module.logger).should_receive('warning').once()
  237. module.ping_monitor(
  238. hook_config,
  239. {},
  240. 'config.yaml',
  241. borgmatic.hooks.monitor.State.FAIL,
  242. monitoring_log_level=1,
  243. dry_run=False,
  244. )