test_ntfy.py 12 KB

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