test_ntfy.py 12 KB

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