137 lines
4.9 KiB
Python
137 lines
4.9 KiB
Python
"""Тесты фоновой проверки обновлений (без автоустановки)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import sqlite3
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from app.services.auto_update_db import upsert_auto_update_dict
|
|
from update import AutoUpdater
|
|
|
|
|
|
class AutoUpdateRuntimeTests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self._root = tempfile.mkdtemp(prefix="wesp-update-runtime-")
|
|
data = os.path.join(self._root, "data")
|
|
os.makedirs(data, exist_ok=True)
|
|
self._db = os.path.join(data, "recipes.db")
|
|
conn = sqlite3.connect(self._db)
|
|
conn.execute(
|
|
"""
|
|
CREATE TABLE auto_update_settings (
|
|
id INTEGER NOT NULL PRIMARY KEY,
|
|
enabled INTEGER NOT NULL DEFAULT 0,
|
|
auto_install INTEGER NOT NULL DEFAULT 0,
|
|
gitea_url VARCHAR(512) NOT NULL DEFAULT '',
|
|
gitea_owner VARCHAR(255) NOT NULL DEFAULT '',
|
|
gitea_repo VARCHAR(255) NOT NULL DEFAULT '',
|
|
repository_url VARCHAR(512) NOT NULL DEFAULT '',
|
|
check_interval_sec INTEGER NOT NULL DEFAULT 3600,
|
|
CHECK (id = 1)
|
|
)
|
|
"""
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
upsert_auto_update_dict(
|
|
self._root,
|
|
{
|
|
"enabled": True,
|
|
"gitea_url": "https://git.example.com",
|
|
"gitea_owner": "farm",
|
|
"gitea_repo": "wesp",
|
|
"check_interval_sec": 120,
|
|
},
|
|
)
|
|
cfg_path = os.path.join(data, "config.json")
|
|
with open(cfg_path, "w", encoding="utf-8") as f:
|
|
json.dump({"version": "1.0.0"}, f)
|
|
|
|
@patch.dict(os.environ, {"GITEA_TOKEN": "test-token"}, clear=False)
|
|
@patch("update.requests.get")
|
|
def test_check_caches_pending_release(self, mock_get: MagicMock) -> None:
|
|
mock_get.return_value = MagicMock(
|
|
status_code=200,
|
|
json=lambda: [
|
|
{
|
|
"tag_name": "v2.0.0",
|
|
"name": "Release 2",
|
|
"body": "Changelog",
|
|
"published_at": "2026-01-01T00:00:00Z",
|
|
"assets": [],
|
|
}
|
|
],
|
|
)
|
|
with patch.object(AutoUpdater, "__init__", lambda self: None):
|
|
updater = AutoUpdater()
|
|
updater.base_dir = self._root
|
|
updater.config_file = os.path.join(self._root, "data", "config.json")
|
|
updater.enabled = True
|
|
updater.gitea_url = "https://git.example.com"
|
|
updater.gitea_owner = "farm"
|
|
updater.gitea_repo = "wesp"
|
|
updater.gitea_token = "test-token"
|
|
updater.gitea_username = ""
|
|
updater.gitea_password = ""
|
|
updater.current_version = "1.0.0"
|
|
updater.pending_release = None
|
|
updater.last_check_at = None
|
|
updater._state_lock = __import__("threading").Lock()
|
|
updater._last_forced_check_mono = 0.0
|
|
updater.protected_files = []
|
|
updater.protected_folders = []
|
|
|
|
info = updater.check_for_updates()
|
|
self.assertIsNotNone(info)
|
|
assert info is not None
|
|
self.assertEqual(info["version"], "2.0.0")
|
|
pending = updater.get_pending_update()
|
|
self.assertIsNotNone(pending)
|
|
assert pending is not None
|
|
self.assertEqual(pending["version"], "2.0.0")
|
|
|
|
@patch.dict(os.environ, {"GITEA_TOKEN": "test-token"}, clear=False)
|
|
@patch("update.requests.get")
|
|
def test_update_loop_does_not_auto_install(self, mock_get: MagicMock) -> None:
|
|
mock_get.return_value = MagicMock(
|
|
status_code=200,
|
|
json=lambda: [
|
|
{
|
|
"tag_name": "v9.9.9",
|
|
"name": "Never auto",
|
|
"body": "",
|
|
"published_at": "",
|
|
"assets": [{"name": "wesp.zip", "browser_download_url": "http://x/wesp.zip"}],
|
|
}
|
|
],
|
|
)
|
|
with patch.object(AutoUpdater, "__init__", lambda self: None):
|
|
updater = AutoUpdater()
|
|
updater.base_dir = self._root
|
|
updater.enabled = True
|
|
updater.is_running = True
|
|
updater.check_interval = 1
|
|
updater.gitea_url = "https://git.example.com"
|
|
updater.gitea_owner = "farm"
|
|
updater.gitea_repo = "wesp"
|
|
updater.gitea_token = "t"
|
|
updater.current_version = "1.0.0"
|
|
updater.pending_release = None
|
|
updater.last_check_at = None
|
|
updater._state_lock = __import__("threading").Lock()
|
|
updater._last_forced_check_mono = 0.0
|
|
updater.protected_files = []
|
|
updater.protected_folders = []
|
|
|
|
with patch.object(updater, "update") as mock_update:
|
|
updater.check_for_updates()
|
|
mock_update.assert_not_called()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|