153 lines
7.0 KiB
Python
153 lines
7.0 KiB
Python
import platform
|
|
import random
|
|
import os
|
|
import pytest
|
|
|
|
from studio.auth import CookieVault, AuthService, LOGIN_URL, CATALOG_ENTRY
|
|
from studio.connectors import CatalogClient
|
|
from studio.network import RemoteUnavailable
|
|
from tests.test_network import setup_network, Remote, Response
|
|
|
|
|
|
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'
|
|
|
|
|
|
class RouteBoundSession(Remote):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.bound = set()
|
|
self.form_route = None
|
|
|
|
def request(self, method, url, **kwargs):
|
|
route = kwargs.get('proxies', {}).get('https')
|
|
if url == LOGIN_URL and method == 'GET':
|
|
self.form_route = route
|
|
return Response({}, text='<input type="hidden" name="lt" value="test"><input type="hidden" name="execution" value="test">')
|
|
if url == LOGIN_URL and method == 'POST':
|
|
if route != self.form_route:
|
|
raise RemoteUnavailable('login form and credentials used different routes')
|
|
return Response({})
|
|
if url == CATALOG_ENTRY:
|
|
self.bound = {route}
|
|
return Response({})
|
|
if url.endswith('/yjfxs.do'):
|
|
return Response({'flag': True, 'msg': {'list': [{'direction': 'confirmed'}]}}) if route in self.bound else Response({'flag': False, 'msg': '请登录'})
|
|
return super().request(method, url, **kwargs)
|
|
|
|
|
|
@pytest.mark.parametrize('threshold', [1, 2])
|
|
def test_auth_recovery_preserves_proven_route_through_catalog_retry(tmp_path, threshold):
|
|
settings, _, transport = setup_network(tmp_path, remote=RouteBoundSession())
|
|
settings.update({'proxy_rotate_every': threshold})
|
|
auth = AuthService(transport, CookieVault(tmp_path))
|
|
assert auth.check()['valid']
|
|
payload = CatalogClient(transport, auth).fetch_page('yjfxs', {'curPage': 2, 'start': 10, 'pageSize': 10})
|
|
assert payload['list'][0]['direction'] == 'confirmed'
|
|
|
|
|
|
def test_credential_login_pins_form_submission_and_verification(tmp_path):
|
|
settings, _, transport = setup_network(tmp_path, remote=RouteBoundSession())
|
|
settings.update({'proxy_rotate_every': 2})
|
|
transport.request('GET', 'https://yz.chsi.com.cn/zsml/')
|
|
auth = AuthService(transport, CookieVault(tmp_path))
|
|
assert auth.login('test-user', 'test-password')['valid']
|
|
|
|
|
|
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()
|