From 661c06b15fc54f32184fbf65c5f6ce78b937b359 Mon Sep 17 00:00:00 2001 From: lucasheld Date: Fri, 23 Sep 2022 18:24:00 +0200 Subject: [PATCH] feat: support autoLogin for enabled disableAuth Call the login method without parameters to log in when disableAuth is enabled. --- tests/test_login.py | 31 +++++++++++++++++++++++++++++++ uptime_kuma_api/api.py | 18 ++++++++++++++++-- uptime_kuma_api/event.py | 1 + 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/test_login.py diff --git a/tests/test_login.py b/tests/test_login.py new file mode 100644 index 0000000..55f15a5 --- /dev/null +++ b/tests/test_login.py @@ -0,0 +1,31 @@ +import unittest + +from uptime_kuma_api import UptimeKumaApi +from uptime_kuma_test_case import UptimeKumaTestCase + + +class TestLogin(UptimeKumaTestCase): + def test_auto_login(self): + # disable auth + r = self.api.set_settings(self.password, disableAuth=True) + self.assertEqual(r["msg"], "Saved") + + # login again without username and password + self.api.logout() + self.api.disconnect() + self.api = UptimeKumaApi(self.url) + self.api.login() + + r = self.api.get_settings() + self.assertTrue(r["disableAuth"]) + + # enable auth again + r = self.api.set_settings(disableAuth=False) + self.assertEqual(r["msg"], "Saved") + + r = self.api.get_settings() + self.assertFalse(r["disableAuth"]) + + +if __name__ == '__main__': + unittest.main() diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index a8177df..7ff0703 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -283,7 +283,8 @@ class UptimeKumaApi(object): Event.HEARTBEAT: None, Event.INFO: None, Event.CERT_INFO: None, - Event.DOCKER_HOST_LIST: None + Event.DOCKER_HOST_LIST: None, + Event.AUTO_LOGIN: None } self.sio.on(Event.CONNECT, self._event_connect) @@ -300,6 +301,7 @@ class UptimeKumaApi(object): self.sio.on(Event.INFO, self._event_info) self.sio.on(Event.CERT_INFO, self._event_cert_info) self.sio.on(Event.DOCKER_HOST_LIST, self._event_docker_host_list) + self.sio.on(Event.AUTO_LOGIN, self._event_auto_login) self.connect() @@ -395,6 +397,9 @@ class UptimeKumaApi(object): def _event_docker_host_list(self, data): self._event_data[Event.DOCKER_HOST_LIST] = data + def _event_auto_login(self): + self._event_data[Event.AUTO_LOGIN] = True + # connection def connect(self): @@ -927,7 +932,16 @@ class UptimeKumaApi(object): # login - def login(self, username: str, password: str, token: str = ""): + def _wait_for_auto_login(self): + while self._event_data[Event.AUTO_LOGIN] is None: + time.sleep(0.01) + + def login(self, username: str = None, password: str = None, token: str = ""): + # autologin + if username is None and password is None: + self._wait_for_auto_login() + return + return self._call('login', { "username": username, "password": password, diff --git a/uptime_kuma_api/event.py b/uptime_kuma_api/event.py index 795fb48..0fed929 100644 --- a/uptime_kuma_api/event.py +++ b/uptime_kuma_api/event.py @@ -16,3 +16,4 @@ class Event(str, Enum): INFO = "info" CERT_INFO = "certInfo" DOCKER_HOST_LIST = "dockerHostList" + AUTO_LOGIN = "autoLogin"