Lint / JS (eslint) (push) Successful in 10s
Lint / Python (flake8) (push) Successful in 58s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Security / Python Security (bandit) (push) Successful in 1m1s
Lint / Python (flake8) (pull_request) Successful in 57s
Lint / JS (eslint) (pull_request) Successful in 8s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Test / Python Tests (pytest) (push) Successful in 1m34s
Test / Python Tests (pytest) (pull_request) Successful in 1m45s
Security / Python Security (bandit) (pull_request) Successful in 59s
The Kuma account has two-factor login, so a password login always ends
in tokenRequired. Prod config now holds a Kuma session token instead
(uptime_kuma = {url, token, maintenance_id}).
- Sign in with loginByToken; a refusal says the token was rejected
(Kuma password changed?). username/password stay as a fallback for an
account without two-factor login.
- Wait for Kuma's first 'info' event before emitting: Kuma registers
its handlers only after sending it, so an earlier emit is dropped and
times out now and then.
- configured() needs url, maintenance_id and a token (or username and
password).
Tested from prod against Kuma 2.5.5: signed in and paused window 7,
which was already paused.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
3.7 KiB
Python
92 lines
3.7 KiB
Python
"""kuma.py: switching the Uptime Kuma maintenance window, against a fake Socket.IO client."""
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
import kuma # noqa: E402
|
|
|
|
CFG = {'url': 'http://kuma.test:3001', 'token': 'tok', 'maintenance_id': 7}
|
|
PASSWORD_CFG = {'url': 'http://kuma.test:3001', 'username': 'gandalf', 'password': 'pw', 'maintenance_id': 7}
|
|
|
|
|
|
class FakeSocket:
|
|
def __init__(self, answers=None, connect_error=None, sends_info=True):
|
|
self.answers = answers or {}
|
|
self.connect_error = connect_error
|
|
self.sends_info = sends_info
|
|
self.calls, self.disconnected, self.handlers = [], False, {}
|
|
|
|
def on(self, event, handler):
|
|
self.handlers[event] = handler
|
|
|
|
def connect(self, url, **kw):
|
|
if self.connect_error:
|
|
raise self.connect_error
|
|
self.url = url
|
|
if self.sends_info: # Kuma's first event; its handlers exist only after it
|
|
self.handlers['info']({'serverTimezone': 'America/New_York'})
|
|
|
|
def call(self, event, data=None, timeout=None):
|
|
self.calls.append((event, data))
|
|
return self.answers.get(event, {'ok': True})
|
|
|
|
def disconnect(self):
|
|
self.disconnected = True
|
|
|
|
|
|
def run(active=True, cfg=CFG, **kw):
|
|
sock = FakeSocket(**kw)
|
|
kuma.set_maintenance(dict(cfg, timeout=0.05), active, client_factory=lambda: sock)
|
|
return sock
|
|
|
|
|
|
class TestKuma:
|
|
def test_configured_needs_url_window_and_a_way_to_sign_in(self):
|
|
assert kuma.configured(CFG) and kuma.configured(PASSWORD_CFG)
|
|
assert not kuma.configured(dict(CFG, token=''))
|
|
assert not kuma.configured(dict(CFG, maintenance_id=None))
|
|
assert not kuma.configured(dict(PASSWORD_CFG, password=''))
|
|
assert not kuma.configured(None)
|
|
|
|
def test_token_sign_in_then_switches_the_window_on(self):
|
|
sock = run(True)
|
|
assert sock.calls == [('loginByToken', 'tok'), ('resumeMaintenance', 7)]
|
|
assert sock.disconnected and sock.url == CFG['url']
|
|
|
|
def test_rejected_token(self):
|
|
with pytest.raises(kuma.KumaError, match='token rejected'):
|
|
run(answers={'loginByToken': {'ok': False}})
|
|
|
|
def test_waits_for_kuma_to_be_ready(self):
|
|
sock = FakeSocket(sends_info=False)
|
|
with pytest.raises(kuma.KumaError, match='not ready'):
|
|
kuma.set_maintenance(dict(CFG, timeout=0.05), True, client_factory=lambda: sock)
|
|
assert sock.calls == [] and sock.disconnected # nothing sent before info
|
|
|
|
def test_password_sign_in(self):
|
|
sock = run(True, cfg=PASSWORD_CFG)
|
|
assert sock.calls[0] == ('login', {'username': 'gandalf', 'password': 'pw', 'token': ''})
|
|
|
|
def test_pause(self):
|
|
assert run(False).calls[-1] == ('pauseMaintenance', 7)
|
|
|
|
def test_refused_password(self):
|
|
with pytest.raises(kuma.KumaError, match='login refused: Incorrect username or password'):
|
|
run(cfg=PASSWORD_CFG, answers={'login': {'ok': False, 'msg': 'Incorrect username or password.'}})
|
|
|
|
def test_two_factor_account_needs_the_token(self):
|
|
with pytest.raises(kuma.KumaError, match='use uptime_kuma.token'):
|
|
run(cfg=PASSWORD_CFG, answers={'login': {'ok': False, 'tokenRequired': True}})
|
|
|
|
def test_refused_change(self):
|
|
with pytest.raises(kuma.KumaError, match='resumeMaintenance refused'):
|
|
run(answers={'resumeMaintenance': {'ok': False, 'msg': 'not found'}})
|
|
|
|
def test_unreachable(self):
|
|
sock = FakeSocket(connect_error=ConnectionError('refused'))
|
|
with pytest.raises(kuma.KumaError, match='ConnectionError'):
|
|
kuma.set_maintenance(dict(CFG, timeout=0.05), True, client_factory=lambda: sock)
|
|
assert sock.disconnected
|