test_ntfy.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_auth_hits_hosted_ntfy_on_fail():
  44. hook_config = {
  45. 'topic': topic,
  46. 'username': 'testuser',
  47. 'password': 'fakepassword',
  48. }
  49. flexmock(module.requests).should_receive('post').with_args(
  50. f'{default_base_url}/{topic}',
  51. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  52. auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
  53. ).and_return(flexmock(ok=True)).once()
  54. module.ping_monitor(
  55. hook_config,
  56. {},
  57. 'config.yaml',
  58. borgmatic.hooks.monitor.State.FAIL,
  59. monitoring_log_level=1,
  60. dry_run=False,
  61. )
  62. def test_ping_monitor_auth_with_no_username_warning():
  63. hook_config = {'topic': topic, 'password': 'fakepassword'}
  64. flexmock(module.requests).should_receive('post').with_args(
  65. f'{default_base_url}/{topic}',
  66. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  67. auth=None,
  68. ).and_return(flexmock(ok=True)).once()
  69. flexmock(module.logger).should_receive('warning').once()
  70. module.ping_monitor(
  71. hook_config,
  72. {},
  73. 'config.yaml',
  74. borgmatic.hooks.monitor.State.FAIL,
  75. monitoring_log_level=1,
  76. dry_run=False,
  77. )
  78. def test_ping_monitor_auth_with_no_password_warning():
  79. hook_config = {'topic': topic, 'username': 'testuser'}
  80. flexmock(module.requests).should_receive('post').with_args(
  81. f'{default_base_url}/{topic}',
  82. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  83. auth=None,
  84. ).and_return(flexmock(ok=True)).once()
  85. flexmock(module.logger).should_receive('warning').once()
  86. module.ping_monitor(
  87. hook_config,
  88. {},
  89. 'config.yaml',
  90. borgmatic.hooks.monitor.State.FAIL,
  91. monitoring_log_level=1,
  92. dry_run=False,
  93. )
  94. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
  95. hook_config = {'topic': topic}
  96. flexmock(module.requests).should_receive('post').never()
  97. module.ping_monitor(
  98. hook_config,
  99. {},
  100. 'config.yaml',
  101. borgmatic.hooks.monitor.State.START,
  102. monitoring_log_level=1,
  103. dry_run=False,
  104. )
  105. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_finish():
  106. hook_config = {'topic': topic}
  107. flexmock(module.requests).should_receive('post').never()
  108. module.ping_monitor(
  109. hook_config,
  110. {},
  111. 'config.yaml',
  112. borgmatic.hooks.monitor.State.FINISH,
  113. monitoring_log_level=1,
  114. dry_run=False,
  115. )
  116. def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
  117. hook_config = {'topic': topic, 'server': custom_base_url}
  118. flexmock(module.requests).should_receive('post').with_args(
  119. f'{custom_base_url}/{topic}',
  120. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  121. auth=None,
  122. ).and_return(flexmock(ok=True)).once()
  123. module.ping_monitor(
  124. hook_config,
  125. {},
  126. 'config.yaml',
  127. borgmatic.hooks.monitor.State.FAIL,
  128. monitoring_log_level=1,
  129. dry_run=False,
  130. )
  131. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
  132. hook_config = {'topic': topic}
  133. flexmock(module.requests).should_receive('post').never()
  134. module.ping_monitor(
  135. hook_config,
  136. {},
  137. 'config.yaml',
  138. borgmatic.hooks.monitor.State.FAIL,
  139. monitoring_log_level=1,
  140. dry_run=True,
  141. )
  142. def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
  143. hook_config = {'topic': topic, 'fail': custom_message_config}
  144. flexmock(module.requests).should_receive('post').with_args(
  145. f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
  146. ).and_return(flexmock(ok=True)).once()
  147. module.ping_monitor(
  148. hook_config,
  149. {},
  150. 'config.yaml',
  151. borgmatic.hooks.monitor.State.FAIL,
  152. monitoring_log_level=1,
  153. dry_run=False,
  154. )
  155. def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
  156. hook_config = {'topic': topic, 'states': ['start', 'fail']}
  157. flexmock(module.requests).should_receive('post').with_args(
  158. f'{default_base_url}/{topic}',
  159. headers=return_default_message_headers(borgmatic.hooks.monitor.State.START),
  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.START,
  167. monitoring_log_level=1,
  168. dry_run=False,
  169. )
  170. def test_ping_monitor_with_connection_error_logs_warning():
  171. hook_config = {'topic': topic}
  172. flexmock(module.requests).should_receive('post').with_args(
  173. f'{default_base_url}/{topic}',
  174. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  175. auth=None,
  176. ).and_raise(module.requests.exceptions.ConnectionError)
  177. flexmock(module.logger).should_receive('warning').once()
  178. module.ping_monitor(
  179. hook_config,
  180. {},
  181. 'config.yaml',
  182. borgmatic.hooks.monitor.State.FAIL,
  183. monitoring_log_level=1,
  184. dry_run=False,
  185. )
  186. def test_ping_monitor_with_other_error_logs_warning():
  187. hook_config = {'topic': topic}
  188. response = flexmock(ok=False)
  189. response.should_receive('raise_for_status').and_raise(
  190. module.requests.exceptions.RequestException
  191. )
  192. flexmock(module.requests).should_receive('post').with_args(
  193. f'{default_base_url}/{topic}',
  194. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  195. auth=None,
  196. ).and_return(response)
  197. flexmock(module.logger).should_receive('warning').once()
  198. module.ping_monitor(
  199. hook_config,
  200. {},
  201. 'config.yaml',
  202. borgmatic.hooks.monitor.State.FAIL,
  203. monitoring_log_level=1,
  204. dry_run=False,
  205. )