79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
from app import app
|
|
import json
|
|
import pytest
|
|
import requests
|
|
|
|
|
|
# Patch requests.Session used by clients before importing app to avoid network calls
|
|
class SilentDummySession:
|
|
def __init__(self):
|
|
self.headers = {}
|
|
self.auth = None
|
|
|
|
def post(self, *args, **kwargs):
|
|
class R:
|
|
def raise_for_status(self):
|
|
return None
|
|
|
|
def json(self):
|
|
return {'data': {'ticket': 'TICKET', 'CSRFPreventionToken': 'CSRF'}}
|
|
|
|
return R()
|
|
|
|
def get(self, *args, **kwargs):
|
|
class R:
|
|
def raise_for_status(self):
|
|
return None
|
|
|
|
def json(self):
|
|
return {'data': []}
|
|
|
|
return R()
|
|
|
|
|
|
def _patch_sessions(monkeypatch):
|
|
monkeypatch.setattr('utils.proxmox_client.requests.Session',
|
|
lambda: SilentDummySession())
|
|
monkeypatch.setattr(
|
|
'utils.check_mk_client.requests.Session', lambda: SilentDummySession())
|
|
|
|
|
|
# Replace requests.Session globally immediately so importing app won't trigger real requests
|
|
requests.Session = lambda: SilentDummySession()
|
|
|
|
|
|
@pytest.fixture
|
|
def client(monkeypatch):
|
|
_patch_sessions(monkeypatch)
|
|
# mock proxmox and checkmk clients inside app
|
|
|
|
class DummyProx:
|
|
def get_cluster(self):
|
|
return {'nodes': [{'name': 'node1', 'status': 'online', 'maxmem': 1024, 'maxcpu': 2, 'qemu': []}]}
|
|
|
|
class DummyCheck:
|
|
def get_host_status(self, name):
|
|
return {'name': name, 'state': 'ok'}
|
|
|
|
def get_host_services(self, name):
|
|
return [{'service_description': 'ping', 'state': 'ok', 'output': 'OK - PING'}]
|
|
|
|
monkeypatch.setattr('app.proxmox', DummyProx())
|
|
monkeypatch.setattr('app.checkmk', DummyCheck())
|
|
|
|
app.testing = True
|
|
with app.test_client() as c:
|
|
yield c
|
|
|
|
|
|
def test_index(client):
|
|
r = client.get('/')
|
|
assert r.status_code == 200
|
|
assert b'node1' in r.data
|
|
|
|
|
|
def test_host_detail(client):
|
|
r = client.get('/host/node1')
|
|
assert r.status_code == 200
|
|
assert b'ping' in r.data
|