diff --git a/tests/test_api_key.py b/tests/test_api_key.py index 48ea269..7d26361 100644 --- a/tests/test_api_key.py +++ b/tests/test_api_key.py @@ -50,6 +50,10 @@ class TestApiKey(UptimeKumaTestCase): with self.assertRaises(UptimeKumaException): self.api.get_api_key(api_key_id) + def test_delete_not_existing_api_key(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_api_key(42) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_docker_host.py b/tests/test_docker_host.py index 2b264ab..b9dc8b7 100644 --- a/tests/test_docker_host.py +++ b/tests/test_docker_host.py @@ -47,6 +47,10 @@ class TestDockerHost(UptimeKumaTestCase): with self.assertRaises(UptimeKumaException): self.api.get_docker_host(docker_host_id) + def test_delete_not_existing_docker_host(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_docker_host(42) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_maintenance.py b/tests/test_maintenance.py index 122026e..a33d67a 100644 --- a/tests/test_maintenance.py +++ b/tests/test_maintenance.py @@ -260,6 +260,10 @@ class TestMaintenance(UptimeKumaTestCase): with self.assertRaises(UptimeKumaException): self.api.get_maintenance(maintenance_id) + def test_delete_not_existing_maintenance(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_maintenance(42) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_monitor.py b/tests/test_monitor.py index 516256f..1b3e9b7 100644 --- a/tests/test_monitor.py +++ b/tests/test_monitor.py @@ -298,6 +298,10 @@ class TestMonitor(UptimeKumaTestCase): } self.do_test_monitor_type(expected_monitor) + def test_delete_not_existing_monitor(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_monitor(42) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_monitor_tag.py b/tests/test_monitor_tag.py index 9585893..90c4955 100644 --- a/tests/test_monitor_tag.py +++ b/tests/test_monitor_tag.py @@ -1,5 +1,6 @@ import unittest +from uptime_kuma_api import UptimeKumaException from uptime_kuma_test_case import UptimeKumaTestCase @@ -32,6 +33,10 @@ class TestMonitorTag(UptimeKumaTestCase): monitor = self.find_by_id(monitors, monitor_id) self.assertEqual(monitor["tags"], []) + def test_delete_not_existing_monitor_tag(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_monitor_tag(42, 42, 42) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_notification.py b/tests/test_notification.py index 1c20915..ed8b364 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -55,6 +55,10 @@ class TestNotification(UptimeKumaTestCase): with self.assertRaises(UptimeKumaException): self.api.delete_notification(notification_id) + def test_delete_not_existing_notification(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_notification(42) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 1e3a587..5ddeebb 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -50,6 +50,10 @@ class TestProxy(UptimeKumaTestCase): with self.assertRaises(UptimeKumaException): self.api.get_proxy(proxy_id) + def test_delete_not_existing_proxy(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_proxy(42) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_status_page.py b/tests/test_status_page.py index 1464942..41e0e00 100644 --- a/tests/test_status_page.py +++ b/tests/test_status_page.py @@ -89,6 +89,10 @@ class TestStatusPage(UptimeKumaTestCase): status_page = self.find_by_id(status_pages, slug, "slug") self.assertIsNone(status_page) + def test_delete_not_existing_status_page(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_status_page("slug42") + if __name__ == '__main__': unittest.main() diff --git a/tests/test_tag.py b/tests/test_tag.py index 5b6b8bf..d40521c 100644 --- a/tests/test_tag.py +++ b/tests/test_tag.py @@ -40,6 +40,10 @@ class TestTag(UptimeKumaTestCase): with self.assertRaises(UptimeKumaException): self.api.get_tag(tag_id) + def test_delete_not_existing_tag(self): + with self.assertRaises(UptimeKumaException): + self.api.delete_tag(42) + if __name__ == '__main__': unittest.main() diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index af01bfa..a8c11ce 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -1111,6 +1111,8 @@ class UptimeKumaApi(object): } """ with self.wait_for_event(Event.MONITOR_LIST): + if id_ not in [i["id"] for i in self.get_monitors()]: + raise UptimeKumaException("monitor does not exist") return self._call('deleteMonitor', id_) def get_monitor_beats(self, id_: int, hours: int) -> list[dict]: @@ -1304,10 +1306,22 @@ class UptimeKumaApi(object): 'msg': 'Deleted Successfully.' } """ - r = self._call('deleteMonitorTag', (tag_id, monitor_id, value)) - # the monitor list event does not send the updated tags - self._event_data[Event.MONITOR_LIST][str(monitor_id)] = self.get_monitor(monitor_id) - return r + with self.wait_for_event(Event.MONITOR_LIST): + tags = [ + { + "monitor_id": y["monitor_id"], + "tag_id": y["tag_id"], + "value": y["value"] + } for x in [ + i.get("tags") for i in self.get_monitors() + ] for y in x + ] + if {"monitor_id": monitor_id, "tag_id": tag_id, "value": value} not in tags: + raise UptimeKumaException("monitor tag does not exist") + r = self._call('deleteMonitorTag', (tag_id, monitor_id, value)) + # the monitor list event does not send the updated tags + self._event_data[Event.MONITOR_LIST][str(monitor_id)] = self.get_monitor(monitor_id) + return r # notification @@ -1487,6 +1501,8 @@ class UptimeKumaApi(object): } """ with self.wait_for_event(Event.NOTIFICATION_LIST): + if id_ not in [i["id"] for i in self.get_notifications()]: + raise UptimeKumaException("notification does not exist") return self._call('deleteNotification', id_) def check_apprise(self) -> bool: @@ -1646,6 +1662,8 @@ class UptimeKumaApi(object): } """ with self.wait_for_event(Event.PROXY_LIST): + if id_ not in [i["id"] for i in self.get_proxies()]: + raise UptimeKumaException("proxy does not exist") return self._call('deleteProxy', id_) # status page @@ -1783,16 +1801,19 @@ class UptimeKumaApi(object): >>> api.delete_status_page("slug1") {} """ - r = self._call('deleteStatusPage', slug) + with self.wait_for_event(Event.STATUS_PAGE_LIST): + if slug not in [i["slug"] for i in self.get_status_pages()]: + raise UptimeKumaException("status page does not exist") + r = self._call('deleteStatusPage', slug) - # uptime kuma does not send the status page list event when a status page is deleted - for status_page in self._event_data[Event.STATUS_PAGE_LIST].values(): - if status_page["slug"] == slug: - status_page_id = status_page["id"] - del self._event_data[Event.STATUS_PAGE_LIST][str(status_page_id)] - break + # uptime kuma does not send the status page list event when a status page is deleted + for status_page in self._event_data[Event.STATUS_PAGE_LIST].values(): + if status_page["slug"] == slug: + status_page_id = status_page["id"] + del self._event_data[Event.STATUS_PAGE_LIST][str(status_page_id)] + break - return r + return r def save_status_page(self, slug: str, **kwargs) -> dict: """ @@ -2424,6 +2445,8 @@ class UptimeKumaApi(object): 'msg': 'Deleted Successfully.' } """ + if id_ not in [i["id"] for i in self.get_tags()]: + raise UptimeKumaException("tag does not exist") return self._call('deleteTag', id_) # settings @@ -3000,6 +3023,8 @@ class UptimeKumaApi(object): } """ with self.wait_for_event(Event.DOCKER_HOST_LIST): + if id_ not in [i["id"] for i in self.get_docker_hosts()]: + raise UptimeKumaException("docker host does not exist") return self._call('deleteDockerHost', id_) # maintenance @@ -3355,7 +3380,10 @@ class UptimeKumaApi(object): "msg": "Deleted Successfully." } """ - return self._call('deleteMaintenance', id_) + with self.wait_for_event(Event.MAINTENANCE_LIST): + if id_ not in [i["id"] for i in self.get_maintenances()]: + raise UptimeKumaException("maintenance does not exist") + return self._call('deleteMaintenance', id_) def pause_maintenance(self, id_: int) -> dict: """ @@ -3672,6 +3700,8 @@ class UptimeKumaApi(object): } """ with self.wait_for_event(Event.API_KEY_LIST): + if id_ not in [i["id"] for i in self.get_api_keys()]: + raise UptimeKumaException("api key does not exist") return self._call('deleteAPIKey', id_) # helper methods