"""Tests for the /region-warnings page and its API.""" from __future__ import annotations import configparser from datetime import datetime from unittest.mock import patch import pytest from modules import region_warning @pytest.fixture def viewer(tmp_path): """BotDataViewer against a temp config + empty DB (migrations create tables).""" from modules.web_viewer.app import BotDataViewer config = configparser.ConfigParser() config.add_section("Bot") config.set("Bot", "db_path", str(tmp_path / "meshcore_bot.db")) config.set("Bot", "bot_name", "TestBot") config.add_section("Web_Viewer") for key, value in [ ("host", "127.0.0.1"), ("port", "8080"), ("enabled", "false"), ("auto_start", "false"), ("debug", "false"), ("cors_allowed_origins", "*"), ("web_viewer_password", ""), ]: config.set("Web_Viewer", key, value) config_path = str(tmp_path / "config.ini") with open(config_path, "w") as handle: config.write(handle) with patch.object(BotDataViewer, "_start_database_polling"), \ patch.object(BotDataViewer, "_start_log_tailing"), \ patch.object(BotDataViewer, "_start_cleanup_scheduler"), \ patch.object(BotDataViewer, "_start_dashboard_refresher"), \ patch.object(BotDataViewer, "_setup_socketio_handlers"), \ patch("modules.web_viewer.app.RepeaterManager"): instance = BotDataViewer( db_path=str(tmp_path / "meshcore_bot.db"), config_path=config_path) instance.app.testing = True return instance def _seed_tally(viewer, channel="#general", scoped=30, globally=10, unknown=5): today = datetime.now().date().isoformat() with viewer.db_manager.connection() as conn: conn.execute( "INSERT INTO region_scope_daily " "(date, channel, scoped_count, global_count, unknown_count) " "VALUES (?, ?, ?, ?, ?)", (today, channel, scoped, globally, unknown), ) conn.commit() class TestRegionWarningsPage: def test_page_renders(self, viewer): resp = viewer.app.test_client().get("/region-warnings") assert resp.status_code == 200 assert b"Region Warnings" in resp.data def test_page_script_carries_a_csp_nonce(self, viewer): """The endpoint is in the nonce-hardened set, so its script must be nonced.""" resp = viewer.app.test_client().get("/region-warnings") body = resp.data.decode() assert "