Skip to content

Commit a1a483e

Browse files
committed
add tests
1 parent 6b5b54a commit a1a483e

9 files changed

+320
-1
lines changed

Diff for: .coveragerc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[run]
2+
omit =
3+
*tests*
4+
*scripts*

Diff for: tests/test_monitor.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import unittest
2+
from uptime_kuma_test_case import UptimeKumaTestCase
3+
from uptime_kuma_api import UptimeKumaException
4+
5+
6+
class TestMonitor(UptimeKumaTestCase):
7+
def test_monitor(self):
8+
expected_monitor = {
9+
"type_": "http",
10+
"name": "monitor 1",
11+
"url": "http://192.168.20.135"
12+
}
13+
14+
# add monitor
15+
r = self.api.add_monitor(
16+
type_=expected_monitor["type_"],
17+
name=expected_monitor["name"],
18+
url=expected_monitor["url"]
19+
)
20+
self.assertEqual(r["msg"], "Added Successfully.")
21+
monitor_id = r["monitor_id"]
22+
23+
# get monitor
24+
monitor = self.api.get_monitor(monitor_id)
25+
self.compare(monitor, expected_monitor)
26+
27+
# get monitors
28+
monitors = self.api.get_monitors()
29+
monitor = self.find_by_id(monitors, monitor_id)
30+
self.assertIsNotNone(monitor)
31+
self.compare(monitor, expected_monitor)
32+
33+
# edit monitor
34+
expected_monitor["type_"] = "ping"
35+
expected_monitor["name"] = "monitor 1 new"
36+
expected_monitor["hostname"] = "127.0.0.1"
37+
del expected_monitor["url"]
38+
r = self.api.edit_monitor(monitor_id, **expected_monitor)
39+
self.assertEqual(r["msg"], "Saved.")
40+
monitor = self.api.get_monitor(monitor_id)
41+
self.compare(monitor, expected_monitor)
42+
43+
# pause monitor
44+
r = self.api.pause_monitor(monitor_id)
45+
self.assertEqual(r["msg"], "Paused Successfully.")
46+
47+
# resume monitor
48+
r = self.api.resume_monitor(monitor_id)
49+
self.assertEqual(r["msg"], "Resumed Successfully.")
50+
51+
# delete monitor
52+
r = self.api.delete_monitor(monitor_id)
53+
self.assertEqual(r["msg"], "Deleted Successfully.")
54+
with self.assertRaises(UptimeKumaException):
55+
self.api.get_monitor(monitor_id)
56+
57+
58+
if __name__ == '__main__':
59+
unittest.main()

Diff for: tests/test_monitor_tag.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from uptime_kuma_test_case import UptimeKumaTestCase
3+
4+
5+
class TestMonitorTag(UptimeKumaTestCase):
6+
def test_monitor_tag(self):
7+
r = self.api.add_tag(name="tag 1", color="#ffffff")
8+
tag_id = r["id"]
9+
r = self.api.add_monitor(type_="http", name="monitor 1", url="http://127.0.0.1")
10+
monitor_id = r["monitor_id"]
11+
12+
expected_monitor_tag = {
13+
"tag_id": tag_id,
14+
"monitor_id": monitor_id,
15+
"value": "value 1"
16+
}
17+
18+
# add monitor tag
19+
r = self.api.add_monitor_tag(**expected_monitor_tag)
20+
self.assertEqual(r["msg"], "Added Successfully.")
21+
22+
# delete monitor tag
23+
r = self.api.delete_monitor_tag(**expected_monitor_tag)
24+
self.assertEqual(r["msg"], "Deleted Successfully.")
25+
26+
27+
if __name__ == '__main__':
28+
unittest.main()

Diff for: tests/test_notification.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import unittest
2+
from uptime_kuma_test_case import UptimeKumaTestCase
3+
from uptime_kuma_api import UptimeKumaException
4+
5+
6+
class TestNotification(UptimeKumaTestCase):
7+
def test_notification(self):
8+
expected_notification = {
9+
"name": "notification 1",
10+
"default": True,
11+
"apply_existing": True,
12+
"type_": "push_by_techulus",
13+
"push_by_techulus_apikey": "123456789"
14+
}
15+
16+
# test notification
17+
with self.assertRaisesRegex(UptimeKumaException, r'Invalid API key'):
18+
self.api.test_notification(**expected_notification)
19+
20+
# add notification
21+
r = self.api.add_notification(**expected_notification)
22+
self.assertEqual(r["msg"], "Saved")
23+
notification_id = r["id"]
24+
25+
# get notification
26+
notification = self.api.get_notification(notification_id)
27+
self.compare(notification, expected_notification)
28+
29+
# get notifications
30+
notifications = self.api.get_notifications()
31+
notification = self.find_by_id(notifications, notification_id)
32+
self.assertIsNotNone(notification)
33+
self.compare(notification, expected_notification)
34+
35+
# edit notification
36+
expected_notification["name"] = "notification 1 new"
37+
expected_notification["default"] = False
38+
expected_notification["apply_existing"] = False
39+
expected_notification["type_"] = "push_deer"
40+
expected_notification["push_deer_deer_key"] = "987654321"
41+
del expected_notification["push_by_techulus_apikey"]
42+
r = self.api.edit_notification(notification_id, **expected_notification)
43+
self.assertEqual(r["msg"], "Saved")
44+
notification = self.api.get_notification(notification_id)
45+
self.compare(notification, expected_notification)
46+
47+
# delete notification
48+
r = self.api.delete_notification(notification_id)
49+
self.assertEqual(r["msg"], "Deleted")
50+
with self.assertRaises(UptimeKumaException):
51+
self.api.delete_notification(notification_id)
52+
53+
54+
if __name__ == '__main__':
55+
unittest.main()

Diff for: tests/test_proxy.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
from uptime_kuma_test_case import UptimeKumaTestCase
3+
from uptime_kuma_api import UptimeKumaException
4+
5+
6+
class TestProxy(UptimeKumaTestCase):
7+
def test_proxy(self):
8+
expected_proxy = {
9+
"protocol": "http",
10+
"host": "127.0.0.1",
11+
"port": 8080,
12+
"active": True
13+
}
14+
15+
# add proxy
16+
r = self.api.add_proxy(**expected_proxy)
17+
self.assertEqual(r["msg"], "Saved")
18+
proxy_id = r["id"]
19+
20+
# get proxy
21+
proxy = self.api.get_proxy(proxy_id)
22+
self.compare(proxy, expected_proxy)
23+
24+
# edit proxy
25+
expected_proxy["protocol"] = "https"
26+
expected_proxy["host"] = "127.0.0.2"
27+
expected_proxy["port"] = 8888
28+
expected_proxy["active"] = False
29+
r = self.api.edit_proxy(proxy_id, **expected_proxy)
30+
self.assertEqual(r["msg"], "Saved")
31+
proxy = self.api.get_proxy(proxy_id)
32+
self.compare(proxy, expected_proxy)
33+
34+
# delete proxy
35+
r = self.api.delete_proxy(proxy_id)
36+
self.assertEqual(r["msg"], "Deleted")
37+
print(r)
38+
with self.assertRaises(UptimeKumaException):
39+
self.api.get_proxy(proxy_id)
40+
41+
42+
if __name__ == '__main__':
43+
unittest.main()

Diff for: tests/test_status_page.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import unittest
2+
from uptime_kuma_test_case import UptimeKumaTestCase
3+
from uptime_kuma_api import UptimeKumaException, IncidentStyle
4+
5+
6+
class TestStatusPage(UptimeKumaTestCase):
7+
def test_status_page(self):
8+
slug = "slug1"
9+
expected_status_page = {
10+
"slug": slug,
11+
"title": "status page 1",
12+
"description": "description 1",
13+
"show_powered_by": False
14+
}
15+
16+
# slug must be unique
17+
try:
18+
self.api.delete_status_page(slug)
19+
except UptimeKumaException:
20+
pass
21+
22+
# add status page
23+
r = self.api.add_status_page(slug, expected_status_page["title"])
24+
self.assertEqual(r["msg"], "OK!")
25+
26+
# save status page
27+
self.api.save_status_page(**expected_status_page)
28+
29+
# get status page
30+
status_page = self.api.get_status_page(slug)
31+
self.compare(status_page, expected_status_page)
32+
33+
# get status pages
34+
status_pages = self.api.get_status_pages()
35+
status_page = self.find_by_id(status_pages, slug, "slug")
36+
self.assertIsNotNone(status_page)
37+
self.compare(status_page, expected_status_page)
38+
39+
# edit status page
40+
expected_status_page["title"] = "status page 1 new"
41+
expected_status_page["theme"] = "dark"
42+
self.api.save_status_page(**expected_status_page)
43+
status_page = self.api.get_status_page(slug)
44+
self.compare(status_page, expected_status_page)
45+
46+
# pin incident
47+
incident_expected = {
48+
"title": "title 1",
49+
"content": "content 1",
50+
"style": IncidentStyle.DANGER
51+
}
52+
incident = self.api.post_incident(slug, **incident_expected)
53+
self.compare(incident, incident_expected)
54+
55+
# unpin incident
56+
self.api.unpin_incident(slug)
57+
58+
# delete status page
59+
self.api.delete_status_page(slug)
60+
with self.assertRaises(UptimeKumaException):
61+
self.api.get_status_page(slug)
62+
63+
64+
if __name__ == '__main__':
65+
unittest.main()

Diff for: tests/test_tag.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
from uptime_kuma_test_case import UptimeKumaTestCase
3+
from uptime_kuma_api import UptimeKumaException
4+
5+
6+
class TestTag(UptimeKumaTestCase):
7+
def test_tag(self):
8+
expected_tag = {
9+
"name": "tag 1",
10+
"color": "#ffffff"
11+
}
12+
13+
# add tag
14+
tag = self.api.add_tag(**expected_tag)
15+
self.compare(tag, expected_tag)
16+
tag_id = tag["id"]
17+
18+
# get tag
19+
tag = self.api.get_tag(tag_id)
20+
self.compare(tag, expected_tag)
21+
22+
# get tags
23+
tags = self.api.get_tags()
24+
tag = self.find_by_id(tags, tag_id)
25+
self.assertIsNotNone(tag)
26+
self.compare(tag, expected_tag)
27+
28+
# delete tag
29+
r = self.api.delete_tag(tag_id)
30+
self.assertEqual(r["msg"], "Deleted Successfully.")
31+
print(r)
32+
with self.assertRaises(UptimeKumaException):
33+
self.api.get_proxy(tag_id)
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

Diff for: tests/uptime_kuma_test_case.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from uptime_kuma_api import UptimeKumaApi
3+
4+
5+
class UptimeKumaTestCase(unittest.TestCase):
6+
api = None
7+
8+
@classmethod
9+
def setUpClass(cls):
10+
cls.api = UptimeKumaApi("http://127.0.0.1:3001")
11+
username = "testuser"
12+
password = "zS7zhQSc"
13+
if cls.api.need_setup():
14+
cls.api.setup(username, password)
15+
cls.api.login(username, password)
16+
17+
@classmethod
18+
def tearDownClass(cls):
19+
cls.api.logout()
20+
cls.api.disconnect()
21+
22+
def compare(self, superset, subset):
23+
return subset.items() <= superset.items()
24+
25+
def find_by_id(self, objects, value, key="id"):
26+
for obj in objects:
27+
if obj[key] == value:
28+
return obj

Diff for: uptime_kuma_api/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def _check_arguments_notification(kwargs):
370370

371371
def _check_arguments_proxy(kwargs):
372372
required_args = ["protocol", "host", "port"]
373-
if "auth" in kwargs:
373+
if kwargs.get("auth"):
374374
required_args.extend(["username", "password"])
375375
_check_missing_arguments(required_args, kwargs, params_map_proxy)
376376

0 commit comments

Comments
 (0)