Files
gandalf/tests/test_kuma.py
T

91 lines
3.7 KiB
Python
Raw Normal View History

"""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