From 804bd85909b43cfdefa9fdd398d54a0fcc53ddc4 Mon Sep 17 00:00:00 2001 From: lucasheld Date: Fri, 26 Aug 2022 14:01:29 +0200 Subject: [PATCH] reset uptime kuma config after each test --- tests/uptime_kuma_test_case.py | 34 ++++++++++++++++++++++------------ uptime_kuma_api/api.py | 12 ++++++++---- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/tests/uptime_kuma_test_case.py b/tests/uptime_kuma_test_case.py index e6dacec..2c63df5 100644 --- a/tests/uptime_kuma_test_case.py +++ b/tests/uptime_kuma_test_case.py @@ -1,7 +1,7 @@ +import json import unittest -from uptime_kuma_api import UptimeKumaApi - +from uptime_kuma_api import UptimeKumaApi, Event, MonitorType token = None @@ -12,22 +12,32 @@ class UptimeKumaTestCase(unittest.TestCase): username = "admin" password = "secret123" - @classmethod - def setUpClass(cls): - cls.api = UptimeKumaApi(cls.url) + def setUp(self): + self.api = UptimeKumaApi(self.url) global token if not token: - if cls.api.need_setup(): - cls.api.setup(cls.username, cls.password) - r = cls.api.login(cls.username, cls.password) + if self.api.need_setup(): + self.api.setup(self.username, self.password) + r = self.api.login(self.username, self.password) token = r["token"] - cls.api.login_by_token(token) + self.api.login_by_token(token) - @classmethod - def tearDownClass(cls): - cls.api.disconnect() + data = { + "version": "1.17.1", + "notificationList": [], + "monitorList": [], + "proxyList": [] + } + data_str = json.dumps(data) + r = self.api.upload_backup(data_str, "overwrite") + self.assertEqual(r["msg"], "Backup successfully restored.") + + self.api._event_data[Event.MONITOR_LIST] = {} + + def tearDown(self): + self.api.disconnect() def compare(self, superset, subset): self.assertTrue(subset.items() <= superset.items()) diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index 2573d31..60e4274 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -366,6 +366,7 @@ def _check_arguments_proxy(kwargs): class UptimeKumaApi(object): def __init__(self, url): + self.url = url self.sio = socketio.Client() self._event_data: dict = { @@ -396,7 +397,7 @@ class UptimeKumaApi(object): self.sio.on(Event.INFO, self._event_info) self.sio.on(Event.CERT_INFO, self._event_cert_info) - self.connect(url) + self.connect() def _get_event_data(self, event): monitor_events = [Event.AVG_PING, Event.UPTIME, Event.HEARTBEAT_LIST, Event.IMPORTANT_HEARTBEAT_LIST, Event.CERT_INFO, Event.HEARTBEAT] @@ -489,9 +490,12 @@ class UptimeKumaApi(object): # connection - def connect(self, url: str): - url = url.rstrip("/") - self.sio.connect(f'{url}/socket.io/') + def connect(self): + url = self.url.rstrip("/") + try: + self.sio.connect(f'{url}/socket.io/') + except: + print("") def disconnect(self): self.sio.disconnect()