test_ntfy.py 12 KB

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