From 12cd8067e469d83912fd8417ffb768c3d7572887 Mon Sep 17 00:00:00 2001 From: lucasheld Date: Mon, 12 Sep 2022 20:47:45 +0200 Subject: [PATCH] fix: generate pushToken on push monitor save https://github.com/lucasheld/ansible-uptime-kuma/issues/5 --- tests/test_monitor.py | 6 +++++- uptime_kuma_api/api.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_monitor.py b/tests/test_monitor.py index edd1292..901d970 100644 --- a/tests/test_monitor.py +++ b/tests/test_monitor.py @@ -62,6 +62,7 @@ class TestMonitor(UptimeKumaTestCase): monitor = self.api.get_monitor(monitor_id) self.compare(monitor, expected_monitor) + return monitor def test_monitor_type_http(self): json_data = '{"key": "value"}' @@ -115,7 +116,10 @@ class TestMonitor(UptimeKumaTestCase): "type": MonitorType.PUSH, "name": "monitor 1" } - self.do_test_monitor_type(expected_monitor) + monitor = self.do_test_monitor_type(expected_monitor) + + # https://github.com/lucasheld/ansible-uptime-kuma/issues/5 + self.assertIsNotNone(monitor["pushToken"]) def test_monitor_type_steam(self): expected_monitor = { diff --git a/uptime_kuma_api/api.py b/uptime_kuma_api/api.py index 2aa711a..c238f84 100644 --- a/uptime_kuma_api/api.py +++ b/uptime_kuma_api/api.py @@ -1,5 +1,7 @@ import json import time +import string +import random import requests import socketio @@ -25,6 +27,11 @@ def int_to_bool(data, keys): data[key] = True if data[key] == 1 else False +def gen_secret(length: int) -> str: + chars = string.ascii_uppercase + string.ascii_lowercase + string.digits + return ''.join(random.choice(chars) for _ in range(length)) + + def _convert_monitor_data(kwargs): if not kwargs["accepted_statuscodes"]: kwargs["accepted_statuscodes"] = ["200-299"] @@ -40,6 +47,9 @@ def _convert_monitor_data(kwargs): kwargs["databaseConnectionString"] = "Server=,;Database=;User Id=;Password=;Encrypt=;TrustServerCertificate=;Connection Timeout=" elif kwargs["type"] == MonitorType.POSTGRES: kwargs["databaseConnectionString"] = "postgres://username:password@host:port/database" + + if kwargs["type"] == MonitorType.PUSH and not kwargs.get("pushToken"): + kwargs["pushToken"] = gen_secret(10) return kwargs