test_ntfy.py 7.1 KB

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