"""首次登录自动化作业与邮箱双通道回退测试。""" from __future__ import annotations from web.backend.models import Account, AutomationJob, LoginTask, User from web.backend.services.automation.jobs import account_onboarding as job from web.backend.services.login_automation import LoginBatchOutcome def _account(name: str, **overrides) -> Account: values = { "username": name, "password": "p", "email": f"{name}@example.com", "email_password": "ep", "status": "imported", "auto_managed": True, "cookie": "", "ck_check_status": "", } values.update(overrides) return Account(**values) def _job() -> AutomationJob: return AutomationJob( key="account_onboarding", title="账号首次登录(CK 获取)", enabled=True, run_interval_seconds=300, batch_size=20, concurrency=3, ) def _admin(db) -> User: user = User( username="onboarding-admin", password_hash="h", role="super_admin", is_active=True, ) db.add(user) db.commit() return user def test_selector_only_picks_unlogged_automatable_accounts(db): admin = _admin(db) selectable = _account("onboarding-selectable") has_cookie = _account("onboarding-has-cookie", cookie="acf_uid=1") opted_out = _account("onboarding-opted-out", auto_managed=False) manual = _account("onboarding-manual", status="manual") in_flight = _account("onboarding-in-flight") db.add_all([selectable, has_cookie, opted_out, manual, in_flight]) db.flush() db.add( LoginTask( batch_id="inflight", account_id=in_flight.id, created_by=admin.id, attempt_status="pending", ) ) db.commit() rows = job.build_select_query(db, _job()).all() assert [row.id for row in rows] == [selectable.id] assert job.count_pending_onboarding(db) == 1 def test_run_account_onboarding_returns_batch_summary(db, monkeypatch): _admin(db) first = _account("onboarding-first") second = _account("onboarding-second") db.add_all([first, second]) db.commit() outcome = LoginBatchOutcome( account_ids=[first.id, second.id], attempted_ids=[first.id, second.id], success_ids=[first.id], failed_ids=[second.id], failure_kinds={second.id: "email_code"}, batch_ids=["batch-wgapi"], fallback_batch_ids=["batch-iframe"], fallback_used=True, fallback_success_ids=[], ) monkeypatch.setattr( job, "run_login_batch_with_fallback", lambda *args, **kwargs: outcome, ) result = job.run_account_onboarding(db, _job()) assert result["scanned"] == 2 assert result["valid"] == 1 assert result["invalid"] == 1 assert "邮箱回退" in result["message"] assert result["detail"]["fallbackUsed"] is True assert result["detail"]["fallbackBatchIds"] == ["batch-iframe"] def test_email_fallback_switches_to_iframe_only_for_email_code(db, monkeypatch): from web.backend.services import login_automation as automation calls: list[tuple[str, list[int]]] = [] def fake_run(_db, *, account_ids, api_strategy, **_kwargs): calls.append((api_strategy, list(account_ids))) return "batch-wgapi" if api_strategy == "wgapi" else "batch-iframe" def fake_collect(batch_id): if batch_id == "batch-wgapi": return { 11: ("failed", "email_code"), 12: ("failed", "credential"), 13: ("success", ""), } return {11: ("success", "")} monkeypatch.setattr(automation, "run_login_batch", fake_run) monkeypatch.setattr(automation, "collect_batch_results", fake_collect) outcome = automation.run_login_batch_with_fallback( db, account_ids=[11, 12, 13], mode="login", created_by=1, ) assert calls == [("wgapi", [11, 12, 13]), ("iframe", [11])] assert outcome.success_ids == [11, 13] assert outcome.failed_ids == [12] assert outcome.failure_kinds == {12: "credential"} assert outcome.fallback_success_ids == [11] def test_onboarding_job_is_registered_but_default_off(): from web.backend.services.automation import ( register_default_jobs, registered_jobs, reset_registry, ) reset_registry() register_default_jobs() spec = registered_jobs()["account_onboarding"] assert spec.default_enabled is False assert "首次登录" in spec.title assert spec.batch_size == 20