test_ntfy.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from enum import Enum
  2. from flexmock import flexmock
  3. from borgmatic.hooks import ntfy as module
  4. default_base_url = 'https://ntfy.sh'
  5. custom_base_url = 'https://ntfy.example.com'
  6. topic = 'borgmatic-unit-testing'
  7. custom_message_config = {
  8. 'title': 'Borgmatic unit testing',
  9. 'message': 'Borgmatic unit testing',
  10. 'priority': 'min',
  11. 'tags': '+1',
  12. }
  13. custom_message_headers = {
  14. 'X-Title': custom_message_config['title'],
  15. 'X-Message': custom_message_config['message'],
  16. 'X-Priority': custom_message_config['priority'],
  17. 'X-Tags': custom_message_config['tags'],
  18. }
  19. def return_default_message_headers(state=Enum):
  20. headers = {
  21. 'X-Title': f'A Borgmatic {state.name} event happened',
  22. 'X-Message': f'A Borgmatic {state.name} event happened',
  23. 'X-Priority': 'default',
  24. 'X-Tags': 'borgmatic',
  25. }
  26. return headers
  27. def test_ping_monitor_minimal_config_hits_hosted_ntfy_on_fail():
  28. hook_config = {'topic': topic}
  29. flexmock(module.requests).should_receive('post').with_args(
  30. f'{default_base_url}/{topic}',
  31. headers=return_default_message_headers(module.monitor.State.FAIL),
  32. auth=None,
  33. ).and_return(flexmock(ok=True)).once()
  34. module.ping_monitor(
  35. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  36. )
  37. def test_ping_monitor_with_auth_hits_hosted_ntfy_on_fail():
  38. hook_config = {
  39. 'topic': topic,
  40. 'username': 'testuser',
  41. 'password': 'fakepassword',
  42. }
  43. flexmock(module.requests).should_receive('post').with_args(
  44. f'{default_base_url}/{topic}',
  45. headers=return_default_message_headers(module.monitor.State.FAIL),
  46. auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
  47. ).and_return(flexmock(ok=True)).once()
  48. module.ping_monitor(
  49. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  50. )
  51. def test_ping_monitor_auth_with_no_username_warning():
  52. hook_config = {'topic': topic, 'password': 'fakepassword'}
  53. flexmock(module.requests).should_receive('post').with_args(
  54. f'{default_base_url}/{topic}',
  55. headers=return_default_message_headers(module.monitor.State.FAIL),
  56. auth=None,
  57. ).and_return(flexmock(ok=True)).once()
  58. flexmock(module.logger).should_receive('warning').once()
  59. module.ping_monitor(
  60. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  61. )
  62. def test_ping_monitor_auth_with_no_password_warning():
  63. hook_config = {'topic': topic, 'username': 'testuser'}
  64. flexmock(module.requests).should_receive('post').with_args(
  65. f'{default_base_url}/{topic}',
  66. headers=return_default_message_headers(module.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, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  72. )
  73. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
  74. hook_config = {'topic': topic}
  75. flexmock(module.requests).should_receive('post').never()
  76. module.ping_monitor(
  77. hook_config,
  78. 'config.yaml',
  79. module.monitor.State.START,
  80. monitoring_log_level=1,
  81. dry_run=False,
  82. )
  83. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_finish():
  84. hook_config = {'topic': topic}
  85. flexmock(module.requests).should_receive('post').never()
  86. module.ping_monitor(
  87. hook_config,
  88. 'config.yaml',
  89. module.monitor.State.FINISH,
  90. monitoring_log_level=1,
  91. dry_run=False,
  92. )
  93. def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
  94. hook_config = {'topic': topic, 'server': custom_base_url}
  95. flexmock(module.requests).should_receive('post').with_args(
  96. f'{custom_base_url}/{topic}',
  97. headers=return_default_message_headers(module.monitor.State.FAIL),
  98. auth=None,
  99. ).and_return(flexmock(ok=True)).once()
  100. module.ping_monitor(
  101. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  102. )
  103. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
  104. hook_config = {'topic': topic}
  105. flexmock(module.requests).should_receive('post').never()
  106. module.ping_monitor(
  107. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=True
  108. )
  109. def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
  110. hook_config = {'topic': topic, 'fail': custom_message_config}
  111. flexmock(module.requests).should_receive('post').with_args(
  112. f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
  113. ).and_return(flexmock(ok=True)).once()
  114. module.ping_monitor(
  115. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  116. )
  117. def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
  118. hook_config = {'topic': topic, 'states': ['start', 'fail']}
  119. flexmock(module.requests).should_receive('post').with_args(
  120. f'{default_base_url}/{topic}',
  121. headers=return_default_message_headers(module.monitor.State.START),
  122. auth=None,
  123. ).and_return(flexmock(ok=True)).once()
  124. module.ping_monitor(
  125. hook_config,
  126. 'config.yaml',
  127. module.monitor.State.START,
  128. monitoring_log_level=1,
  129. dry_run=False,
  130. )
  131. def test_ping_monitor_with_connection_error_logs_warning():
  132. hook_config = {'topic': topic}
  133. flexmock(module.requests).should_receive('post').with_args(
  134. f'{default_base_url}/{topic}',
  135. headers=return_default_message_headers(module.monitor.State.FAIL),
  136. auth=None,
  137. ).and_raise(module.requests.exceptions.ConnectionError)
  138. flexmock(module.logger).should_receive('warning').once()
  139. module.ping_monitor(
  140. hook_config,
  141. 'config.yaml',
  142. module.monitor.State.FAIL,
  143. monitoring_log_level=1,
  144. dry_run=False,
  145. )
  146. def test_ping_monitor_with_other_error_logs_warning():
  147. hook_config = {'topic': topic}
  148. response = flexmock(ok=False)
  149. response.should_receive('raise_for_status').and_raise(
  150. module.requests.exceptions.RequestException
  151. )
  152. flexmock(module.requests).should_receive('post').with_args(
  153. f'{default_base_url}/{topic}',
  154. headers=return_default_message_headers(module.monitor.State.FAIL),
  155. auth=None,
  156. ).and_return(response)
  157. flexmock(module.logger).should_receive('warning').once()
  158. module.ping_monitor(
  159. hook_config,
  160. 'config.yaml',
  161. module.monitor.State.FAIL,
  162. monitoring_log_level=1,
  163. dry_run=False,
  164. )