Files
vuln-redhat/tests/test_sync.py
T
2026-11-10 21:28:10 +01:00

33 lines
1.3 KiB
Python

import sys
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import sync
class RedHatSyncTests(unittest.TestCase):
def test_index_year_filter(self):
data = b'2016/rhsa-2016_001.json\n2017/rhsa-2017_001.json\n2026/rhsa-2026_012.json\n'
self.assertEqual(sync.valid_paths(data, 2017), ['2017/rhsa-2017_001.json', '2026/rhsa-2026_012.json'])
def test_changes_csv(self):
items = {'2025/rhsa-2025_012.json'}
self.assertEqual(sync.extract_changed(b'url,info\nhttps://example.test/2025/rhsa-2025_012.json,updated\n', items), items)
def test_first_sync_and_second_no_change(self):
with tempfile.TemporaryDirectory() as d:
idx = b'2026/rhsa-2026_001.json\n'
doc = b'{"document":{"title":"RHSA"}}'
def fetch(url):
if url.endswith('index.txt'): return idx
if url.endswith('.csv'): return b''
return doc
with patch.object(sync, 'get', side_effect=fetch):
sync.sync(d, 2017, rotate=1)
sync.sync(d, 2017, rotate=1)
self.assertEqual((Path(d)/'data/csaf/2026/rhsa-2026_001.json').read_bytes(), doc)
if __name__ == '__main__':
unittest.main()