first commit

This commit is contained in:
Mateusz Gruszczyński
2026-04-12 21:26:12 +02:00
commit ff7dbcb4e4
123 changed files with 27749 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
from fastapi.testclient import TestClient
from app.main import app
def test_login_accepts_form_and_json(monkeypatch, tmp_path):
monkeypatch.setenv("DATABASE_URL", f"sqlite:///{tmp_path / 'auth.db'}")
monkeypatch.setenv("DATA_DIR", str(tmp_path / 'data'))
monkeypatch.setenv("SECRET_KEY", "test-secret")
monkeypatch.setenv("DEFAULT_ADMIN_USERNAME", "admin")
monkeypatch.setenv("DEFAULT_ADMIN_PASSWORD", "admin")
with TestClient(app) as client:
form_response = client.post("/api/auth/login", data={"username": "admin", "password": "admin"})
assert form_response.status_code == 200
assert "access_token" in form_response.json()
json_response = client.post("/api/auth/login", json={"username": "admin", "password": "admin"})
assert json_response.status_code == 200
assert "access_token" in json_response.json()
def test_auth_me(monkeypatch, tmp_path):
monkeypatch.setenv("DATABASE_URL", f"sqlite:///{tmp_path / 'me.db'}")
monkeypatch.setenv("DATA_DIR", str(tmp_path / 'data'))
monkeypatch.setenv("SECRET_KEY", "test-secret")
monkeypatch.setenv("DEFAULT_ADMIN_USERNAME", "admin")
monkeypatch.setenv("DEFAULT_ADMIN_PASSWORD", "admin")
with TestClient(app) as client:
login_response = client.post("/api/auth/login", data={"username": "admin", "password": "admin"})
token = login_response.json()["access_token"]
me_response = client.get("/api/auth/me", headers={"Authorization": f"Bearer {token}"})
assert me_response.status_code == 200
assert me_response.json()["username"] == "admin"

View File

@@ -0,0 +1,10 @@
from fastapi.testclient import TestClient
from app.main import app
def test_health_endpoint():
client = TestClient(app)
response = client.get("/api/health")
assert response.status_code == 200
assert response.json()["status"] in {"ok", "error"}

View File

@@ -0,0 +1,24 @@
from app.core.cron_utils import CronValidationError, describe_cron_expression, preview_next_runs, validate_cron_expression
def test_validate_cron_expression_accepts_daily_schedule():
validate_cron_expression('15 2 * * *', 'Europe/Warsaw')
def test_validate_cron_expression_rejects_invalid_schedule():
try:
validate_cron_expression('bad cron', 'Europe/Warsaw')
except CronValidationError:
assert True
return
assert False, 'invalid cron should raise'
def test_preview_next_runs_returns_future_datetimes():
runs = preview_next_runs('0 3 * * 1', 'Europe/Warsaw', count=2)
assert len(runs) == 2
assert runs[0] < runs[1]
def test_describe_cron_expression_humanizes_common_patterns():
assert describe_cron_expression('0 2 * * *') == 'Every day at 02:00'

View File

@@ -0,0 +1,74 @@
from fastapi.testclient import TestClient
from app.main import app
from app.schemas.swos_beta import SwosBetaProbeResponse
def _login(client: TestClient) -> str:
response = client.post('/api/auth/login', data={'username': 'admin', 'password': 'admin'})
return response.json()['access_token']
def test_swos_probe_endpoint(monkeypatch, tmp_path):
monkeypatch.setenv('DATABASE_URL', f'sqlite:///{tmp_path / "swos_probe.db"}')
monkeypatch.setenv('DATA_DIR', str(tmp_path / 'data'))
monkeypatch.setenv('SECRET_KEY', 'test-secret')
monkeypatch.setenv('DEFAULT_ADMIN_USERNAME', 'admin')
monkeypatch.setenv('DEFAULT_ADMIN_PASSWORD', 'admin')
from app.api.routes import swos_beta
monkeypatch.setattr(
swos_beta.swos_beta_service,
'probe',
lambda payload: SwosBetaProbeResponse(
success=True,
base_url='http://192.168.88.1',
status_code=200,
auth_mode='digest',
page_title='SwOS',
content_type='text/html',
server='MikroTik',
save_backup_visible=True,
backup_endpoint_ok=True,
note='beta',
),
)
with TestClient(app) as client:
token = _login(client)
response = client.post(
'/api/swos-beta/probe',
json={'host': '192.168.88.1', 'port': 80, 'username': 'admin', 'password': ''},
headers={'Authorization': f'Bearer {token}'},
)
assert response.status_code == 200
assert response.json()['backup_endpoint_ok'] is True
def test_swos_download_endpoint(monkeypatch, tmp_path):
monkeypatch.setenv('DATABASE_URL', f'sqlite:///{tmp_path / "swos_download.db"}')
monkeypatch.setenv('DATA_DIR', str(tmp_path / 'data'))
monkeypatch.setenv('SECRET_KEY', 'test-secret')
monkeypatch.setenv('DEFAULT_ADMIN_USERNAME', 'admin')
monkeypatch.setenv('DEFAULT_ADMIN_PASSWORD', 'admin')
from app.api.routes import swos_beta
class FakeBackup:
filename = 'switch.swb'
content = b'binary-data'
content_type = 'application/octet-stream'
monkeypatch.setattr(swos_beta.swos_beta_service, 'download_backup', lambda payload: FakeBackup())
with TestClient(app) as client:
token = _login(client)
response = client.post(
'/api/swos-beta/download',
json={'host': '192.168.88.1', 'port': 80, 'username': 'admin', 'password': ''},
headers={'Authorization': f'Bearer {token}'},
)
assert response.status_code == 200
assert response.content == b'binary-data'
assert 'attachment; filename="switch.swb"' == response.headers['content-disposition']