Files
kaoyan/tests/test_auth_session.py
T

243 lines
9.1 KiB
Python

"""CHSI exposes page one publicly; page two proves the catalogue session."""
import threading
import pytest
from studio.auth import AuthService, CookieVault
from studio.connectors import CatalogClient, LoginRequired
from studio.database import Database
from studio.jobs import JobManager
from studio.network import RateLimited
from tests.test_network import Remote, Response, setup_network
class CatalogSession(Remote):
def __init__(self, accepts_sso=True):
super().__init__()
self.accepts_sso = accepts_sso
self.authorized = False
self.calls = []
self.blocked = None
def request(self, method, url, **kwargs):
data = kwargs.get('data', {})
self.calls.append((method, url, data.copy()))
if method == 'GET':
if url.endswith('/a/dw.do'):
self.authorized = self.accepts_sso
return Response({}, text='2027年硕士专业目录查询')
if self.blocked:
return Response({'flag': False, 'msg': self.blocked})
start = int(data.get('start', 0))
if start > 0 and not self.authorized:
return Response({'flag': False, 'msg': '请登录'})
# The server caps the requested 50 rows at ten and supplies its next offset.
rows = [{'dwdm': str(10000 + i)} for i in range(start, min(start + 10, 23))]
return Response({'flag': True, 'msg': {'list': rows, 'size': 10,
'startOfNextPage': start + 10, 'totalCount': 23,
'nextPageAvailable': start + 10 < 23}})
def service(tmp_path, remote):
settings, router, transport = setup_network(tmp_path, remote=remote)
auth = AuthService(transport, CookieVault(tmp_path))
auth.cookie('CASTGC=fixture-sso-cookie')
return auth, settings, router, transport
def test_public_first_page_is_not_valid_login_evidence(tmp_path):
remote = CatalogSession(accepts_sso=False)
auth, _, _, _ = service(tmp_path, remote)
result = auth.check()
assert result['valid'] is False
assert result['saved'] is True
assert any(int(data.get('start', 0)) > 0 for method, _, data in remote.calls if method == 'POST')
def test_existing_sso_cookie_is_bound_before_testing_page_two(tmp_path):
remote = CatalogSession()
auth, _, _, _ = service(tmp_path, remote)
assert auth.check()['valid'] is True
assert remote.calls[0][0] == 'GET'
assert remote.calls[0][1].endswith('/a/dw.do')
assert int(remote.calls[-1][2]['start']) == 10
def test_server_pagination_offsets_keep_every_row_when_page_size_is_capped(tmp_path):
remote = CatalogSession()
remote.authorized = True
_, _, transport = setup_network(tmp_path, remote=remote)
rows = list(CatalogClient(transport).pages('dws', {}, page_size=50))
assert [r['dwdm'] for r in rows] == [str(10000 + i) for i in range(23)]
assert [data['start'] for _, _, data in remote.calls] == [0, 10, 20]
def test_sso_recovery_retries_the_same_failed_page_without_skipping_rows(tmp_path):
remote = CatalogSession()
auth, _, _, transport = service(tmp_path, remote)
rows = list(CatalogClient(transport, auth=auth).pages('dws', {}))
assert [r['dwdm'] for r in rows] == [str(10000 + i) for i in range(23)]
assert auth.status()['valid'] is True
assert sum(url.endswith('/a/dw.do') for _, url, _ in remote.calls) == 1
def test_unrecoverable_login_stops_after_one_sso_attempt(tmp_path):
remote = CatalogSession(accepts_sso=False)
auth, _, _, transport = service(tmp_path, remote)
with pytest.raises(LoginRequired):
list(CatalogClient(transport, auth=auth).pages('dws', {}))
assert sum(url.endswith('/a/dw.do') for _, url, _ in remote.calls) == 1
assert auth.status()['valid'] is False
def test_job_login_failure_invalidates_verified_badge_and_preserves_checkpoint(tmp_path):
auth, settings, _, transport = service(tmp_path, CatalogSession())
assert auth.check()['valid'] is True
auth.browser_state = 'completed'
auth.browser_message = '已获取登录 Cookie,目录接口验证成功'
db = Database(tmp_path / 'studio.db')
def runner(jid, params, manager):
manager.complete_unit(jid, 'already-saved')
db.job_update(jid, records=107)
raise LoginRequired('研招网要求登录,请重新验证后恢复任务')
jobs = JobManager(db, settings, transport, runner=runner, auth=auth)
try:
job = jobs.start({'mode': 'catalog', 'year': 2027})
jobs.controls[job['id']]['thread'].join(timeout=3)
result = jobs.get(job['id'])
assert result['status'] == 'waiting_login'
assert result['records'] == 107 and result['completed_units'] == 1
status = auth.status()
assert status['valid'] is False
assert status['state'] == 'login_required'
assert status['saved'] is True
assert status['browser_message'] == ''
finally:
jobs.close()
def test_rate_limiting_during_validation_is_not_reported_as_bad_cookie(tmp_path):
remote = CatalogSession()
auth, _, _, _ = service(tmp_path, remote)
assert auth.check()['valid'] is True
remote.blocked = '访问太频繁,请稍后重试'
with pytest.raises(RateLimited):
auth.check()
assert auth.status()['valid'] is False
assert auth.status()['state'] == 'unverified'
assert auth.status()['saved'] is True
def test_captcha_requires_human_verification_without_sso_retry(tmp_path):
remote = CatalogSession()
auth, _, _, transport = service(tmp_path, remote)
remote.blocked = '需要验证码'
with pytest.raises(LoginRequired):
list(CatalogClient(transport, auth=auth).pages('dws', {}))
assert not any(url.endswith('/a/dw.do') for _, url, _ in remote.calls)
def test_changing_route_invalidates_previous_verification_without_deleting_cookie(tmp_path):
auth, _, router, _ = service(tmp_path, CatalogSession())
assert auth.check()['valid'] is True
router.rotate()
status = auth.status()
assert status['valid'] is False
assert status['state'] == 'unverified'
assert status['saved'] is True
def test_route_changed_after_proof_is_not_marked_as_verified(tmp_path):
auth, _, router, _ = service(tmp_path, CatalogSession())
original_save = auth.vault.save
def switch_before_saving(cookies):
router.rotate()
original_save(cookies)
auth.vault.save = switch_before_saving
status = auth.check()
assert status['valid'] is False
assert status['state'] == 'unverified'
assert status['saved'] is True
def test_later_captcha_invalidation_is_not_overwritten_by_pending_verification(tmp_path):
auth, _, _, _ = service(tmp_path, CatalogSession())
before_save, finish_save = threading.Event(), threading.Event()
original_save = auth.vault.save
def delayed_save(cookies):
before_save.set()
assert finish_save.wait(3)
original_save(cookies)
auth.vault.save = delayed_save
result = []
checking = threading.Thread(target=lambda: result.append(auth.check()))
checking.start()
try:
assert before_save.wait(3)
auth.invalidate('采集中发现验证码,请在浏览器完成人工验证', state='captcha_required')
finally:
finish_save.set()
checking.join(timeout=3)
assert not checking.is_alive()
assert result[0]['valid'] is False
assert result[0]['state'] == 'captcha_required'
assert '验证码' in result[0]['message']
assert result[0]['saved'] is True
@pytest.mark.parametrize('action', ['clear', 'replace'])
def test_pending_check_cannot_override_a_later_credential_change(tmp_path, action):
auth, _, _, _ = service(tmp_path, CatalogSession())
before_save, finish_save, change_started = threading.Event(), threading.Event(), threading.Event()
original_save = auth.vault.save
def delayed_save(cookies):
# Only delay the verifier, not the user's later replacement.
if threading.current_thread().name == 'verification':
before_save.set()
assert finish_save.wait(3)
original_save(cookies)
auth.vault.save = delayed_save
errors = []
def check():
try:
auth.check()
except Exception as error:
errors.append(error)
def change():
change_started.set()
if action == 'clear':
auth.clear()
else:
auth.cookie('CASTGC=new-fixture-cookie')
checking = threading.Thread(target=check, name='verification')
changing = threading.Thread(target=change, name='credential-change')
checking.start()
try:
assert before_save.wait(3)
changing.start()
assert change_started.wait(3)
changing.join(timeout=.1)
finally:
finish_save.set()
checking.join(timeout=3)
if changing.ident:
changing.join(timeout=3)
assert not checking.is_alive() and not changing.is_alive()
assert not errors, errors
status = auth.status()
assert status['valid'] is False
assert status['state'] == 'unverified'
assert status['saved'] is (action == 'replace')
if action == 'replace':
assert auth.vault.load()['CASTGC'] == 'new-fixture-cookie'