import platform import random import os import pytest from studio.auth import CookieVault from tests.test_network import setup_network def test_requests_sample_a_fresh_delay_inside_configured_bounds(tmp_path, monkeypatch): settings, _, transport = setup_network(tmp_path) settings.update({'request_interval': .3, 'request_interval_max': .5}) samples = iter([.31, .47, .39]) monkeypatch.setattr(random, 'uniform', lambda low, high: next(samples)) delays = [] transport.sleep = delays.append for _ in range(3): transport.request('GET', 'https://yz.chsi.com.cn/zsml/') assert delays == [.31, .47, .39] @pytest.mark.parametrize('maximum', [.2, 61, float('nan')]) def test_invalid_interval_range_is_rejected(tmp_path, maximum): settings, _, _ = setup_network(tmp_path) with pytest.raises(ValueError): settings.update({'request_interval': .3, 'request_interval_max': maximum}) def test_successful_requests_rotate_without_quarantining_old_proxy(tmp_path): settings, router, transport = setup_network(tmp_path) settings.update({'proxy_rotate_every': 2}) routes = [transport.request('GET', 'https://yz.chsi.com.cn/zsml/').json()['routed_through'] for _ in range(3)] assert routes == ['http://1.2.3.4:8080', 'http://1.2.3.4:8080', 'http://5.6.7.8:9090'] assert router.failed == set() assert router.status()['last_request_route'] == 'http://5.6.7.8:9090' assert router.status()['route_requests'] == 1 def test_route_pin_defers_rotation_until_login_proof_finishes(tmp_path): settings, router, transport = setup_network(tmp_path) settings.update({'proxy_rotate_every': 1}) with router.hold_route(): routes = [transport.request('GET', 'https://yz.chsi.com.cn/zsml/').json()['routed_through'] for _ in range(2)] assert routes == ['http://1.2.3.4:8080'] * 2 assert transport.request('GET', 'https://yz.chsi.com.cn/zsml/').json()['routed_through'] == 'http://5.6.7.8:9090' def test_linux_cookie_vault_encrypts_and_preserves_domain_cookies(tmp_path, monkeypatch): monkeypatch.setattr(platform, 'system', lambda: 'Linux') cookies = [{'name': 'JSESSIONID', 'value': 'private-account-cookie', 'domain': 'account.chsi.com.cn'}, {'name': 'JSESSIONID', 'value': 'private-catalog-cookie', 'domain': 'yz.chsi.com.cn'}] vault = CookieVault(tmp_path) vault.save(cookies) assert (tmp_path / 'cookie.key').exists() assert b'private-account-cookie' not in vault.path.read_bytes() assert CookieVault(tmp_path).load() == cookies if os.name != 'nt': assert (tmp_path / 'cookie.key').stat().st_mode & 0o777 == 0o600 assert vault.path.stat().st_mode & 0o777 == 0o600 def test_configured_server_host_is_allowed_but_other_hosts_and_origins_are_blocked(tmp_path, monkeypatch): from fastapi.testclient import TestClient from studio.api import create_app monkeypatch.setenv('STUDIO_ALLOWED_HOSTS', 'kaoyan.fjayson.com') with TestClient(create_app(tmp_path), base_url='https://kaoyan.fjayson.com') as client: assert client.get('/api/health').status_code == 200 assert client.get('/api/health', headers={'Host': 'evil.example'}).status_code == 403 assert client.put('/api/settings', json={'network_mode': 'direct'}, headers={'Origin': 'https://evil.example'}).status_code == 403 assert client.put('/api/settings', json={'network_mode': 'direct'}, headers={'Origin': 'https://kaoyan.fjayson.com'}).status_code == 200 def test_only_interrupted_running_tasks_are_selected_for_restart(tmp_path): from studio.database import Database db = Database(tmp_path / 'resume.db') for status in ('running', 'queued', 'paused', 'waiting_login', 'cancelled'): db.job_create(status, {'year': 2027}) db.job_update(status, status=status) restarted = Database(tmp_path / 'resume.db') assert set(restarted.recovered_job_ids) == {'running', 'queued'} assert restarted.job_get('paused')['status'] == 'paused' assert restarted.job_get('waiting_login')['status'] == 'waiting_login' @pytest.mark.parametrize('manually_paused', [False, True]) def test_graceful_restart_remembers_running_but_not_manual_pause(tmp_path, manually_paused): import threading from studio.database import Database from studio.jobs import JobManager from studio.settings import SettingsStore ready = threading.Event() stop = threading.Event() def runner(jid, params, manager): ready.set() while not stop.wait(.01): manager.checkpoint(jid) db = Database(tmp_path / 'shutdown.db') manager = JobManager(db, SettingsStore(tmp_path), runner=runner) job = manager.start({'mode': 'catalog', 'year': 2027, 'provinces': ['11']}) assert ready.wait(3) try: if manually_paused: manager.pause(job['id']) manager.close() restarted = Database(tmp_path / 'shutdown.db') assert (job['id'] in restarted.recovered_job_ids) is (not manually_paused) finally: stop.set() manager.close()