homelab_automation/tests/backend/test_terminal_connect_page.py
Bruno Charest 70c15c9b6f
Some checks failed
Tests / Backend Tests (Python) (3.10) (push) Has been cancelled
Tests / Backend Tests (Python) (3.11) (push) Has been cancelled
Tests / Backend Tests (Python) (3.12) (push) Has been cancelled
Tests / Frontend Tests (JS) (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / All Tests Passed (push) Has been cancelled
Add debug mode feature flag with environment variable parsing, UI badge indicator, secret redaction utility, and enhanced terminal session management with status checks and session limit error handling
2025-12-21 17:22:36 -05:00

71 lines
2.5 KiB
Python

import pytest
from datetime import datetime, timedelta, timezone
from unittest.mock import AsyncMock, patch
from app.models.terminal_session import TerminalSession, SESSION_STATUS_ACTIVE
class TestTerminalConnectPage:
@pytest.mark.asyncio
async def test_connect_page_renders_html(self, client, db_session):
now = datetime.now(timezone.utc)
sess = TerminalSession(
id="s" * 64,
host_id="host-1",
host_name="test-host",
host_ip="127.0.0.1",
user_id="user-1",
username="testuser",
token_hash="x" * 64,
ttyd_port=7680,
ttyd_pid=123,
mode="embedded",
status=SESSION_STATUS_ACTIVE,
created_at=now,
last_seen_at=now,
expires_at=now + timedelta(minutes=10),
)
db_session.add(sess)
await db_session.commit()
with patch("app.services.terminal_service.terminal_service.verify_token", return_value=True), \
patch("app.services.terminal_service.terminal_service.is_session_process_alive", new=AsyncMock(return_value=True)):
resp = await client.get(f"/api/terminal/connect/{sess.id}?token=dummy")
assert resp.status_code == 200
assert "<html" in resp.text.lower()
assert "terminal" in resp.text.lower()
@pytest.mark.asyncio
async def test_popout_page_allowed_when_debug_disabled(self, client, db_session, monkeypatch):
from app.core.config import settings
monkeypatch.setattr(settings, "debug_mode", False)
now = datetime.now(timezone.utc)
sess = TerminalSession(
id="p" * 64,
host_id="host-1",
host_name="test-host",
host_ip="127.0.0.1",
user_id="user-1",
username="testuser",
token_hash="x" * 64,
ttyd_port=7680,
ttyd_pid=123,
mode="popout",
status=SESSION_STATUS_ACTIVE,
created_at=now,
last_seen_at=now,
expires_at=now + timedelta(minutes=10),
)
db_session.add(sess)
await db_session.commit()
with patch("app.services.terminal_service.terminal_service.verify_token", return_value=True), \
patch("app.services.terminal_service.terminal_service.is_session_process_alive", new=AsyncMock(return_value=True)):
resp = await client.get(f"/api/terminal/popout/{sess.id}?token=dummy")
assert resp.status_code == 200
assert "<html" in resp.text.lower()