test_ntfy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. from enum import Enum
  2. from flexmock import flexmock
  3. import borgmatic.hooks.monitoring.monitor
  4. from borgmatic.hooks.monitoring 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.borgmatic.hooks.credential.parse).should_receive(
  31. 'resolve_credential'
  32. ).replace_with(lambda value, config: value)
  33. flexmock(module.requests).should_receive('post').with_args(
  34. f'{default_base_url}/{topic}',
  35. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  36. auth=None,
  37. ).and_return(flexmock(ok=True)).once()
  38. module.ping_monitor(
  39. hook_config,
  40. {},
  41. 'config.yaml',
  42. borgmatic.hooks.monitoring.monitor.State.FAIL,
  43. monitoring_log_level=1,
  44. dry_run=False,
  45. )
  46. def test_ping_monitor_with_access_token_hits_hosted_ntfy_on_fail():
  47. hook_config = {
  48. 'topic': topic,
  49. 'access_token': 'abc123',
  50. }
  51. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  52. 'resolve_credential'
  53. ).replace_with(lambda value, config: value)
  54. flexmock(module.requests).should_receive('post').with_args(
  55. f'{default_base_url}/{topic}',
  56. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  57. auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
  58. ).and_return(flexmock(ok=True)).once()
  59. module.ping_monitor(
  60. hook_config,
  61. {},
  62. 'config.yaml',
  63. borgmatic.hooks.monitoring.monitor.State.FAIL,
  64. monitoring_log_level=1,
  65. dry_run=False,
  66. )
  67. def test_ping_monitor_with_username_password_and_access_token_ignores_username_password():
  68. hook_config = {
  69. 'topic': topic,
  70. 'username': 'testuser',
  71. 'password': 'fakepassword',
  72. 'access_token': 'abc123',
  73. }
  74. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  75. 'resolve_credential'
  76. ).replace_with(lambda value, config: value)
  77. flexmock(module.requests).should_receive('post').with_args(
  78. f'{default_base_url}/{topic}',
  79. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  80. auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
  81. ).and_return(flexmock(ok=True)).once()
  82. flexmock(module.logger).should_receive('warning').once()
  83. module.ping_monitor(
  84. hook_config,
  85. {},
  86. 'config.yaml',
  87. borgmatic.hooks.monitoring.monitor.State.FAIL,
  88. monitoring_log_level=1,
  89. dry_run=False,
  90. )
  91. def test_ping_monitor_with_username_password_hits_hosted_ntfy_on_fail():
  92. hook_config = {
  93. 'topic': topic,
  94. 'username': 'testuser',
  95. 'password': 'fakepassword',
  96. }
  97. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  98. 'resolve_credential'
  99. ).replace_with(lambda value, config: value)
  100. flexmock(module.requests).should_receive('post').with_args(
  101. f'{default_base_url}/{topic}',
  102. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  103. auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
  104. ).and_return(flexmock(ok=True)).once()
  105. module.ping_monitor(
  106. hook_config,
  107. {},
  108. 'config.yaml',
  109. borgmatic.hooks.monitoring.monitor.State.FAIL,
  110. monitoring_log_level=1,
  111. dry_run=False,
  112. )
  113. def test_ping_monitor_with_password_but_no_username_warns():
  114. hook_config = {'topic': topic, 'password': 'fakepassword'}
  115. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  116. 'resolve_credential'
  117. ).replace_with(lambda value, config: value)
  118. flexmock(module.requests).should_receive('post').with_args(
  119. f'{default_base_url}/{topic}',
  120. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  121. auth=None,
  122. ).and_return(flexmock(ok=True)).once()
  123. flexmock(module.logger).should_receive('warning').once()
  124. module.ping_monitor(
  125. hook_config,
  126. {},
  127. 'config.yaml',
  128. borgmatic.hooks.monitoring.monitor.State.FAIL,
  129. monitoring_log_level=1,
  130. dry_run=False,
  131. )
  132. def test_ping_monitor_with_username_but_no_password_warns():
  133. hook_config = {'topic': topic, 'username': 'testuser'}
  134. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  135. 'resolve_credential'
  136. ).replace_with(lambda value, config: value)
  137. flexmock(module.requests).should_receive('post').with_args(
  138. f'{default_base_url}/{topic}',
  139. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  140. auth=None,
  141. ).and_return(flexmock(ok=True)).once()
  142. flexmock(module.logger).should_receive('warning').once()
  143. module.ping_monitor(
  144. hook_config,
  145. {},
  146. 'config.yaml',
  147. borgmatic.hooks.monitoring.monitor.State.FAIL,
  148. monitoring_log_level=1,
  149. dry_run=False,
  150. )
  151. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
  152. hook_config = {'topic': topic}
  153. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  154. 'resolve_credential'
  155. ).replace_with(lambda value, config: value)
  156. flexmock(module.requests).should_receive('post').never()
  157. module.ping_monitor(
  158. hook_config,
  159. {},
  160. 'config.yaml',
  161. borgmatic.hooks.monitoring.monitor.State.START,
  162. monitoring_log_level=1,
  163. dry_run=False,
  164. )
  165. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_finish():
  166. hook_config = {'topic': topic}
  167. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  168. 'resolve_credential'
  169. ).replace_with(lambda value, config: value)
  170. flexmock(module.requests).should_receive('post').never()
  171. module.ping_monitor(
  172. hook_config,
  173. {},
  174. 'config.yaml',
  175. borgmatic.hooks.monitoring.monitor.State.FINISH,
  176. monitoring_log_level=1,
  177. dry_run=False,
  178. )
  179. def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
  180. hook_config = {'topic': topic, 'server': custom_base_url}
  181. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  182. 'resolve_credential'
  183. ).replace_with(lambda value, config: value)
  184. flexmock(module.requests).should_receive('post').with_args(
  185. f'{custom_base_url}/{topic}',
  186. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  187. auth=None,
  188. ).and_return(flexmock(ok=True)).once()
  189. module.ping_monitor(
  190. hook_config,
  191. {},
  192. 'config.yaml',
  193. borgmatic.hooks.monitoring.monitor.State.FAIL,
  194. monitoring_log_level=1,
  195. dry_run=False,
  196. )
  197. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
  198. hook_config = {'topic': topic}
  199. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  200. 'resolve_credential'
  201. ).replace_with(lambda value, config: value)
  202. flexmock(module.requests).should_receive('post').never()
  203. module.ping_monitor(
  204. hook_config,
  205. {},
  206. 'config.yaml',
  207. borgmatic.hooks.monitoring.monitor.State.FAIL,
  208. monitoring_log_level=1,
  209. dry_run=True,
  210. )
  211. def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
  212. hook_config = {'topic': topic, 'fail': custom_message_config}
  213. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  214. 'resolve_credential'
  215. ).replace_with(lambda value, config: value)
  216. flexmock(module.requests).should_receive('post').with_args(
  217. f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
  218. ).and_return(flexmock(ok=True)).once()
  219. module.ping_monitor(
  220. hook_config,
  221. {},
  222. 'config.yaml',
  223. borgmatic.hooks.monitoring.monitor.State.FAIL,
  224. monitoring_log_level=1,
  225. dry_run=False,
  226. )
  227. def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
  228. hook_config = {'topic': topic, 'states': ['start', 'fail']}
  229. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  230. 'resolve_credential'
  231. ).replace_with(lambda value, config: value)
  232. flexmock(module.requests).should_receive('post').with_args(
  233. f'{default_base_url}/{topic}',
  234. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.START),
  235. auth=None,
  236. ).and_return(flexmock(ok=True)).once()
  237. module.ping_monitor(
  238. hook_config,
  239. {},
  240. 'config.yaml',
  241. borgmatic.hooks.monitoring.monitor.State.START,
  242. monitoring_log_level=1,
  243. dry_run=False,
  244. )
  245. def test_ping_monitor_with_connection_error_logs_warning():
  246. hook_config = {'topic': topic}
  247. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  248. 'resolve_credential'
  249. ).replace_with(lambda value, config: value)
  250. flexmock(module.requests).should_receive('post').with_args(
  251. f'{default_base_url}/{topic}',
  252. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  253. auth=None,
  254. ).and_raise(module.requests.exceptions.ConnectionError)
  255. flexmock(module.logger).should_receive('warning').once()
  256. module.ping_monitor(
  257. hook_config,
  258. {},
  259. 'config.yaml',
  260. borgmatic.hooks.monitoring.monitor.State.FAIL,
  261. monitoring_log_level=1,
  262. dry_run=False,
  263. )
  264. def test_ping_monitor_with_credential_error_logs_warning():
  265. hook_config = {'topic': topic}
  266. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  267. 'resolve_credential'
  268. ).and_raise(ValueError)
  269. flexmock(module.requests).should_receive('post').never()
  270. flexmock(module.logger).should_receive('warning').once()
  271. module.ping_monitor(
  272. hook_config,
  273. {},
  274. 'config.yaml',
  275. borgmatic.hooks.monitoring.monitor.State.FAIL,
  276. monitoring_log_level=1,
  277. dry_run=False,
  278. )
  279. def test_ping_monitor_with_other_error_logs_warning():
  280. hook_config = {'topic': topic}
  281. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  282. 'resolve_credential'
  283. ).replace_with(lambda value, config: value)
  284. response = flexmock(ok=False)
  285. response.should_receive('raise_for_status').and_raise(
  286. module.requests.exceptions.RequestException
  287. )
  288. flexmock(module.requests).should_receive('post').with_args(
  289. f'{default_base_url}/{topic}',
  290. headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
  291. auth=None,
  292. ).and_return(response)
  293. flexmock(module.logger).should_receive('warning').once()
  294. module.ping_monitor(
  295. hook_config,
  296. {},
  297. 'config.yaml',
  298. borgmatic.hooks.monitoring.monitor.State.FAIL,
  299. monitoring_log_level=1,
  300. dry_run=False,
  301. )