Skip to content

Commit

Permalink
fix: generate pushToken on push monitor save
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasheld committed Sep 12, 2022
1 parent 9b0f0c1 commit 12cd806
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def do_test_monitor_type(self, expected_monitor):

monitor = self.api.get_monitor(monitor_id)
self.compare(monitor, expected_monitor)
return monitor

def test_monitor_type_http(self):
json_data = '{"key": "value"}'
Expand Down Expand Up @@ -115,7 +116,10 @@ def test_monitor_type_push(self):
"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 = {
Expand Down
10 changes: 10 additions & 0 deletions uptime_kuma_api/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import time
import string
import random

import requests
import socketio
Expand All @@ -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"]
Expand All @@ -40,6 +47,9 @@ def _convert_monitor_data(kwargs):
kwargs["databaseConnectionString"] = "Server=<hostname>,<port>;Database=<your database>;User Id=<your user id>;Password=<your password>;Encrypt=<true/false>;TrustServerCertificate=<Yes/No>;Connection Timeout=<int>"
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


Expand Down

0 comments on commit 12cd806

Please sign in to comment.