mirror of
https://github.com/Flowseal/tg-ws-proxy.git
synced 2026-10-21 02:58:45 +00:00
feat: автообновление на Linux без ручной установки пакета (#1223)
This commit is contained in:
1 parent
5f8820d41f
commit
a6177fec87
11 files changed
+771
-21
No files matched your search
@@ -1,5 +1,11 @@
|
||||
import hashlib
|
||||
import io
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from utils import update_check
|
||||
from utils.update_check import _extract_assets, _parse_version_tuple, _version_gt
|
||||
|
||||
|
||||
@@ -66,5 +72,92 @@ class ExtractAssetsTest(unittest.TestCase):
|
||||
self.assertEqual(_extract_assets({}), [])
|
||||
|
||||
|
||||
class _Response(io.BytesIO):
|
||||
def __init__(self, body, content_length='auto'):
|
||||
super().__init__(body)
|
||||
if content_length == 'auto':
|
||||
content_length = len(body)
|
||||
self.headers = {} if content_length is None else {
|
||||
'Content-Length': str(content_length),
|
||||
}
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc):
|
||||
self.close()
|
||||
return False
|
||||
|
||||
|
||||
def _opener(response):
|
||||
opener = mock.Mock()
|
||||
opener.open.return_value = response
|
||||
return mock.Mock(return_value=opener)
|
||||
|
||||
|
||||
class DownloadAssetTest(unittest.TestCase):
|
||||
def _download(self, response, digest=''):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
with mock.patch.object(update_check, 'build_github_opener', _opener(response)):
|
||||
path = update_check.download_asset(
|
||||
'https://example.invalid/a', Path(tmp), suffix='.bin', digest=digest,
|
||||
)
|
||||
try:
|
||||
return path.read_bytes(), sorted(p.name for p in Path(tmp).iterdir())
|
||||
finally:
|
||||
path.unlink(missing_ok=True)
|
||||
|
||||
def test_complete_download_is_kept(self):
|
||||
body, files = self._download(_Response(b'payload'))
|
||||
self.assertEqual(body, b'payload')
|
||||
self.assertEqual(len(files), 1)
|
||||
|
||||
def test_matching_digest_is_accepted(self):
|
||||
digest = 'sha256:' + hashlib.sha256(b'payload').hexdigest()
|
||||
body, _ = self._download(_Response(b'payload'), digest=digest)
|
||||
self.assertEqual(body, b'payload')
|
||||
|
||||
def test_unknown_digest_format_is_ignored(self):
|
||||
body, _ = self._download(_Response(b'payload'), digest='md5:whatever')
|
||||
self.assertEqual(body, b'payload')
|
||||
|
||||
def test_truncated_download_raises_and_leaves_no_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
resp = _Response(b'half', content_length=99)
|
||||
with mock.patch.object(update_check, 'build_github_opener', _opener(resp)):
|
||||
with self.assertRaises(OSError):
|
||||
update_check.download_asset(
|
||||
'https://example.invalid/a', Path(tmp), suffix='.bin',
|
||||
)
|
||||
self.assertEqual(list(Path(tmp).iterdir()), [])
|
||||
|
||||
def test_digest_mismatch_raises_and_leaves_no_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
resp = _Response(b'payload')
|
||||
with mock.patch.object(update_check, 'build_github_opener', _opener(resp)):
|
||||
with self.assertRaises(OSError):
|
||||
update_check.download_asset(
|
||||
'https://example.invalid/a', Path(tmp),
|
||||
suffix='.bin', digest='sha256:' + '0' * 64,
|
||||
)
|
||||
self.assertEqual(list(Path(tmp).iterdir()), [])
|
||||
|
||||
def test_missing_content_length_is_tolerated(self):
|
||||
body, _ = self._download(_Response(b'payload', content_length=None))
|
||||
self.assertEqual(body, b'payload')
|
||||
|
||||
|
||||
class FindAssetTest(unittest.TestCase):
|
||||
def test_returns_asset_with_digest(self):
|
||||
assets = [{'name': 'a.deb', 'url': 'https://example.invalid/a.deb', 'digest': 'sha256:ab'}]
|
||||
with mock.patch.dict(update_check._state, {'assets': assets}):
|
||||
self.assertEqual(update_check.find_asset('a.deb'), assets[0])
|
||||
self.assertIsNone(update_check.find_asset('b.deb'))
|
||||
|
||||
def test_ignores_asset_without_url(self):
|
||||
with mock.patch.dict(update_check._state, {'assets': [{'name': 'a.deb', 'url': ''}]}):
|
||||
self.assertIsNone(update_check.find_asset('a.deb'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user