From c6b3d592217cddc5603a023dae49fc8fafee2b8a Mon Sep 17 00:00:00 2001 From: lucasheld Date: Fri, 26 Aug 2022 14:04:43 +0200 Subject: [PATCH] fix monitors in status pages --- tests/test_status_page.py | 19 +++++++++++++++++-- tests/uptime_kuma_test_case.py | 20 ++++++++++++++++++-- uptime_kuma_api/api.py | 29 ++++++++++++++++------------- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/tests/test_status_page.py b/tests/test_status_page.py index cf56c32..f85d854 100644 --- a/tests/test_status_page.py +++ b/tests/test_status_page.py @@ -6,12 +6,25 @@ from uptime_kuma_test_case import UptimeKumaTestCase class TestStatusPage(UptimeKumaTestCase): def test_status_page(self): + monitor_id = self.add_monitor() + slug = "slug1" expected_status_page = { "slug": slug, "title": "status page 1", "description": "description 1", - "showPoweredBy": False + "showPoweredBy": False, + "publicGroupList": [ + { + 'name': 'Services', + 'weight': 1, + 'monitorList': [ + { + "id": monitor_id + } + ] + } + ] } # slug must be unique @@ -35,7 +48,9 @@ class TestStatusPage(UptimeKumaTestCase): status_pages = self.api.get_status_pages() status_page = self.find_by_id(status_pages, slug, "slug") self.assertIsNotNone(status_page) - self.compare(status_page, expected_status_page) + # publicGroupList and incident is not available in status pages + expected_status_page_config = {i: expected_status_page[i] for i in expected_status_page if i != "publicGroupList"} + self.compare(status_page, expected_status_page_config) # edit status page expected_status_page["title"] = "status page 1 new" diff --git a/tests/uptime_kuma_test_case.py b/tests/uptime_kuma_test_case.py index 2c63df5..967933b 100644 --- a/tests/uptime_kuma_test_case.py +++ b/tests/uptime_kuma_test_case.py @@ -6,6 +6,22 @@ from uptime_kuma_api import UptimeKumaApi, Event, MonitorType token = None +def compare(subset, superset): + for key, value in subset.items(): + value2 = superset.get(key) + if type(value) == list: + for i in range(len(value)): + if not value2 or not compare(value[i], value2[i]): + return False + elif type(value) == dict: + if not compare(value, value2): + return False + else: + if value != value2: + return False + return True + + class UptimeKumaTestCase(unittest.TestCase): api = None url = "http://127.0.0.1:3001" @@ -40,7 +56,7 @@ class UptimeKumaTestCase(unittest.TestCase): self.api.disconnect() def compare(self, superset, subset): - self.assertTrue(subset.items() <= superset.items()) + self.assertTrue(compare(subset, superset)) def find_by_id(self, objects, value, key="id"): for obj in objects: @@ -48,7 +64,7 @@ class UptimeKumaTestCase(unittest.TestCase): return obj def add_monitor(self): - r = self.api.add_monitor(type="http", name="monitor 1", url="http://127.0.0.1") + r = self.api.add_monitor(type=MonitorType.HTTP, name="monitor 1", url="http://127.0.0.1") monitor_id = r["monitorID"] return monitor_id diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index 60e4274..fe221e1 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -1,6 +1,7 @@ import json import time +import requests import socketio from . import AuthMethod @@ -226,18 +227,14 @@ def _build_status_page_data( showPoweredBy: bool = True, icon: str = "/icon.svg", - monitors: list = None + publicGroupList: list = None ): if theme not in ["light", "dark"]: raise ValueError if not domainNameList: domainNameList = [] - public_group_list = [] - if monitors: - public_group_list.append({ - "name": "Services", - "monitorList": monitors - }) + if not publicGroupList: + publicGroupList = [] config = { "id": id, "slug": slug, @@ -252,7 +249,7 @@ def _build_status_page_data( "footerText": footerText, "showPoweredBy": showPoweredBy } - return slug, config, icon, public_group_list + return slug, config, icon, publicGroupList def _check_missing_arguments(required_params, kwargs): @@ -641,11 +638,16 @@ class UptimeKumaApi(object): return r def get_status_page(self, slug: str): - r = self._call('getStatusPage', slug) - config = r["config"] - del r["config"] - r.update(config) - return r + r1 = self._call('getStatusPage', slug) + r2 = requests.get(f"{self.url}/api/status-page/{slug}").json() + + config = r1["config"] + config.update(r2["config"]) + return { + **config, + "incident": r2["incident"], + "publicGroupList": r2["publicGroupList"] + } def add_status_page(self, slug: str, title: str): return self._call('addStatusPage', (title, slug)) @@ -655,6 +657,7 @@ class UptimeKumaApi(object): def save_status_page(self, slug: str, **kwargs): status_page = self.get_status_page(slug) + status_page.pop("incident") status_page.update(kwargs) data = _build_status_page_data(**status_page) return self._call('saveStatusPage', data)