feat: support autoLogin for enabled disableAuth

Call the login method without parameters to log in when disableAuth is enabled.
This commit is contained in:
lucasheld 2022-09-23 18:24:00 +02:00
parent ebadfb73e6
commit 661c06b15f
3 changed files with 48 additions and 2 deletions

31
tests/test_login.py Normal file
View file

@ -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()

View file

@ -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,

View file

@ -16,3 +16,4 @@ class Event(str, Enum):
INFO = "info"
CERT_INFO = "certInfo"
DOCKER_HOST_LIST = "dockerHostList"
AUTO_LOGIN = "autoLogin"