Raven-1/tests/test_provider_auth_method.py
knqiufan d90b6deb7e
fix: allow agents to inherit OAuth-backed host models (#841)
## Summary

Allow all five shipped agents to inherit an OAuth-backed host model,
including OpenAI Codex, by validating the inherited model binding with
the runtime credential check after parent session overrides.

- Reuse the launcher's inheritance decision in setup and update its
English/Chinese message.
- Preserve raw provider settings, routing, reasoning effort and product
limits. OAuth tokens remain in the host credential store.
- Cover OAuth aliases, access/refresh tokens, missing or malformed
credentials, custom token paths, parent bindings and independent agent
keys. Remove the old API-key-only gate exemptions from the auth contract
test.

## Type

- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Selected regression tests: **321 passed, 48 deselected** on Windows with
Python 3.12. The selection focuses on inheritance and excludes untouched
POSIX permission/pruning scenarios.

```powershell
uv run --no-sync pytest tests/test_config_product_render.py tests/test_subagent_vendored_agents.py tests/test_cli_subagent_setup.py tests/test_provider_auth_method.py tests/test_providers_factory.py tests/test_openai_codex_provider.py tests/test_providers_oauth_login.py tests/test_i18n_prompts.py tests/test_i18n_boundary.py -q -o addopts='' -p no:cacheprovider -k '(not test_config_product_render or inherit_llm or launchers_inherit_oauth or launchers_own_keys or secret_slots or env_wins or dig_and_put) and not prune and not backup and not locks_it_down and not test_a_page_sign_in_runs_the_real_driver_and_leaves_the_token_owner_only' --cov=raven.config.product_render --cov=raven.agent.subagent.vendored_agents --cov=raven.cli.subagent_setup --cov=raven.i18n --cov-branch --cov-report=json:C:/Users/Administrator/AppData/Local/Temp/raven_issue809_coverage_final.json --cov-report=term-missing:skip-covered
```

The following checks passed: Ruff across the repository, formatting
(2149 files), relevant type checks, all 10 import contracts, commit
messages, source language and file/asset checks.

```powershell
uv run --no-sync ruff check raven evolver agents plugins-dist tests scripts docs-site
uv run --no-sync ruff format --check raven evolver agents plugins-dist tests scripts docs-site
uv run --no-sync ty check raven/config/product_render.py raven/agent/subagent/vendored_agents.py raven/cli/subagent_setup.py
uv run --no-sync lint-imports
uv run --no-sync python scripts/check_commit_messages.py main..HEAD
uv run --no-sync python scripts/check_source_language.py main..HEAD
uv run --no-sync python scripts/check_large_files.py main..HEAD
```

Changed-line coverage: **100% (22/22 executable lines)** after
normalizing the Windows report's path separators.

```powershell
uv run --no-sync python -c "import json,pathlib; p=pathlib.Path('C:/Users/Administrator/AppData/Local/Temp/raven_issue809_coverage_final.json'); d=json.loads(p.read_text(encoding='utf-8')); d['files']={k.replace(chr(92), '/'):v for k,v in d['files'].items()}; p.with_name('raven_issue809_coverage_posix.json').write_text(json.dumps(d), encoding='utf-8')"
uv run --no-sync python scripts/coverage_gate.py --coverage-json C:/Users/Administrator/AppData/Local/Temp/raven_issue809_coverage_posix.json diff --base-ref main --head HEAD --threshold 90
```

Full local CI remains unverified: `make ci` could not run because make
is unavailable. Existing POSIX file-permission tests fail on Windows.
`uv run --no-sync deptry raven` reports 12 missing optional channel
dependencies in this environment.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

Inheritance now refuses an unavailable selected model even when an
unrelated provider has an API key. API-key and independent-agent-key
paths remain covered. Credential validation only reads stored OAuth
credentials; it does not sign in or serialize tokens. Revert this change
to restore the previous inheritance gate.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

Fixes #809

---------

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: KT <74288668+0xKT@users.noreply.github.com>
2026-10-03 23:43:03 +08:00

592 lines
26 KiB
Python

"""Whether a provider is usable, asked of every module that answers it.
Decided in one place, ``providers.auth``. Three modules used to decide it
independently and disagreed:
* ``config.schema.section_has_credentials`` gates routing -- a section it rejects is
skipped when matching a model id to a provider.
* ``config.update_providers.list_providers`` gates display -- it is what
``raven provider list`` and the pickers show.
* ``providers.factory.check_provider_credentials`` gates startup -- it decides
whether ``raven agent`` runs at all.
A provider the second accepted and the first rejected was configured according
to the CLI and invisible to the router -- Gemini holding only ``api_key_list``
read as ready in ``provider list`` and refused to start.
(``registry.auth_shape`` is deliberately absent: it answers what shape a
provider's credentials take, not whether they are present. It is a fourth
implementation of a different question.)
These assert the one answer, and that all three ask it. The per-implementation
records exist so that a change to any single answer is visible rather than
silently rebalancing them back into disagreement.
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
import pytest
from raven.config.schema import Config
from raven.providers.auth import key_refusal
#: Provider sections paired with the model id that selects them. Each case is a
#: shape of credential material, not a vendor: "the key is in the plural field",
#: "the address is set but no key", "nothing is set at all".
SCENARIOS: dict[str, dict[str, Any]] = {
"gemini_key_list_only": {
"provider": "gemini",
"model": "gemini/gemini-2.5-flash",
"section": {"apiKeyList": ["AIzaTEST"]},
},
"gemini_key_only": {
"provider": "gemini",
"model": "gemini/gemini-2.5-flash",
"section": {"apiKey": "AIzaTEST"},
},
"gemini_empty": {
"provider": "gemini",
"model": "gemini/gemini-2.5-flash",
"section": {},
},
"anthropic_key": {
"provider": "anthropic",
"model": "anthropic/claude-sonnet-5",
"section": {"apiKey": "sk-ant-TEST"},
},
"anthropic_empty": {
"provider": "anthropic",
"model": "anthropic/claude-sonnet-5",
"section": {},
},
"azure_key_without_base": {
"provider": "azure_openai",
"model": "azure_openai/my-deployment",
"section": {"apiKey": "az-TEST"},
},
"azure_key_and_base": {
"provider": "azure_openai",
"model": "azure_openai/my-deployment",
"section": {"apiKey": "az-TEST", "apiBase": "https://x.openai.azure.com"},
},
"ollama_base_only": {
"provider": "ollama_chat",
"model": "ollama_chat/llama3.2",
"section": {"apiBase": "http://localhost:11434"},
},
"ollama_empty": {
"provider": "ollama_chat",
"model": "ollama_chat/llama3.2",
"section": {},
},
"anthropic_endpoints_only": {
"provider": "anthropic",
"model": "anthropic/claude-sonnet-5",
"section": {"endpoints": [{"label": "primary", "apiKey": "sk-ant-TEST"}]},
},
"anthropic_endpoints_all_keys_empty": {
"provider": "anthropic",
"model": "anthropic/claude-sonnet-5",
"section": {"endpoints": [{"label": "primary", "apiKey": ""}, {"label": "backup", "apiKey": ""}]},
},
"anthropic_flat_key_and_keyless_endpoint": {
"provider": "anthropic",
"model": "anthropic/claude-sonnet-5",
# A healthy flat key alongside an endpoints list whose only entry has
# none. `provider_endpoints` ignores the flat field outright once
# `endpoints` is set, so this section serves an empty key on every
# request -- the gate must say the same, not fall back to the flat key.
"section": {"apiKey": "sk-ant-TEST", "endpoints": [{"label": "primary", "apiKey": ""}]},
},
"hosted_vllm_endpoints_inherit_flat_base": {
"provider": "hosted_vllm",
"model": "hosted_vllm/some-model",
# `endpoint add` without `--api-base` is the common case: the address
# lives on the section, not repeated on every entry. The gate must read
# the same inherited address `provider_endpoints` resolves, not each
# endpoint's own (empty) field.
"section": {
"apiBase": "http://10.0.0.5:8000/v1",
"endpoints": [{"label": "a", "apiKey": "k1"}, {"label": "b", "apiKey": "k2"}],
},
},
}
def _config_file(tmp_path: Path, case: dict[str, Any]) -> Path:
path = tmp_path / "config.json"
path.write_text(
json.dumps(
{
"providers": {case["provider"]: case["section"]},
"agents": {"defaults": {"model": case["model"]}},
}
),
encoding="utf-8",
)
return path
def _routing_says(case: dict[str, Any], path: Path) -> bool:
"""Would the router match this model to this provider's section?"""
config = Config.model_validate(json.loads(path.read_text(encoding="utf-8")))
return config.get_provider(case["model"]) is not None
def _display_says(case: dict[str, Any], path: Path) -> bool:
"""Would `raven provider list` show this provider as configured?"""
from raven.config.update_providers import list_providers
rows = list_providers(config_path=path)
row = next((r for r in rows if r["name"] == case["provider"]), None)
return bool(row and row["configured"])
def _startup_says(case: dict[str, Any], path: Path) -> bool:
"""Would `raven agent` start?"""
from raven.providers.auth import MissingCredentialsError
from raven.providers.factory import check_provider_credentials
config = Config.model_validate(json.loads(path.read_text(encoding="utf-8")))
try:
check_provider_credentials(config)
except MissingCredentialsError:
return False
return True
def _status_says(case: dict[str, Any], path: Path) -> bool:
"""Would `raven status` print this provider as set up?
Driven through the command rather than the helper it calls: this gate is a
line of formatting logic, and testing the helper would have missed it for
the same reason the agreement test missed the gate itself.
"""
from typer.testing import CliRunner
from raven.cli.commands import app
from raven.config.loader import set_config_path
from raven.providers.registry import find_by_name
set_config_path(path)
try:
result = CliRunner().invoke(app, ["status"])
finally:
set_config_path(None) # type: ignore[arg-type]
spec = find_by_name(case["provider"])
label = (spec.label if spec else case["provider"]).lower()
for line in result.stdout.splitlines():
if line.strip().lower().startswith(label):
return "not set" not in line
return False
#: Every surface that decides whether a provider is usable. A gate absent from
#: this map is a gate the agreement test cannot see -- which is how `raven
#: status` and the router's final fallback kept their own rules through a change
#: that claimed to unify them. Adding a gate means adding it here.
ANSWERS = {
"routing": _routing_says,
"display": _display_says,
"startup": _startup_says,
"status": _status_says,
}
@pytest.mark.parametrize("name", sorted(SCENARIOS), ids=lambda n: n)
def test_every_gate_gives_the_same_verdict(name: str, tmp_path: Path) -> None:
"""One set of credentials, one verdict, whoever is asking.
Disagreement here is always user-visible: the CLI reports a provider as
ready that the agent then refuses to start on, or the reverse.
"""
case = SCENARIOS[name]
path = _config_file(tmp_path, case)
verdicts = {who: ask(case, path) for who, ask in ANSWERS.items()}
assert len(set(verdicts.values())) == 1, f"{name}: {verdicts}"
def test_a_key_in_the_plural_field_is_a_configured_provider(tmp_path: Path) -> None:
"""Gemini accepts a list of keys, and a list with a key in it is credentials.
Called out separately because it is the case that shipped broken: display
said yes, routing and startup said no, so the provider appeared configured
and the agent would not run on it.
"""
case = SCENARIOS["gemini_key_list_only"]
path = _config_file(tmp_path, case)
assert _display_says(case, path)
assert _routing_says(case, path)
assert _startup_says(case, path)
def test_a_key_in_an_endpoints_entry_is_a_configured_provider(tmp_path: Path) -> None:
"""An endpoints-only section is exactly as usable as a flat key -- routing and
startup both read the resolved list (``provider_endpoints``), not the flat
field, so a gate that only looked at the flat field would reject a section
its own request path can serve.
"""
case = SCENARIOS["anthropic_endpoints_only"]
path = _config_file(tmp_path, case)
assert _display_says(case, path)
assert _routing_says(case, path)
assert _startup_says(case, path)
def test_an_endpoints_list_with_every_key_empty_is_not_configured(tmp_path: Path) -> None:
case = SCENARIOS["anthropic_endpoints_all_keys_empty"]
path = _config_file(tmp_path, case)
assert not _display_says(case, path)
assert not _routing_says(case, path)
assert not _startup_says(case, path)
def test_flat_key_does_not_paper_over_a_keyless_endpoint(tmp_path: Path) -> None:
"""The reviewer's repro: a good flat key next to an endpoints list whose
entry has none.
`provider_endpoints` ignores the flat field once `endpoints` is set, so a
request from this section carries an empty key -- every gate that reads
the flat field first before falling through to endpoints would say the
section is configured while the request 401s. The gate must ask the
endpoints list first, exactly as `provider_endpoints` does.
"""
case = SCENARIOS["anthropic_flat_key_and_keyless_endpoint"]
path = _config_file(tmp_path, case)
assert not _display_says(case, path)
assert not _routing_says(case, path)
assert not _startup_says(case, path)
def test_endpoints_without_their_own_base_inherit_the_flat_one_at_the_gate() -> None:
"""`endpoint add` without `--api-base` must not be refused at startup --
the gate reads the same inherited address the reader resolves, not each
endpoint's own (empty) field."""
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
from raven.providers.endpoints import provider_endpoints
section = ProviderConfig.model_validate(
{
"apiBase": "http://10.0.0.5:8000/v1",
"endpoints": [{"label": "a", "apiKey": "k1"}, {"label": "b", "apiKey": "k2"}],
}
)
assert credential_status("hosted_vllm", section).ok
assert [e.api_base for e in provider_endpoints(section)] == [
"http://10.0.0.5:8000/v1",
"http://10.0.0.5:8000/v1",
]
def test_a_spec_shipped_default_address_satisfies_the_address_requirement() -> None:
"""`custom` ships a `default_api_base`, so a bare key is a runnable config
and the gate must say so; `azure_openai` ships none, so its address stays
mandatory. `is_local` specs keep the plain requirement either way: their
default (Ollama's standard port) must not make an untouched section look
configured."""
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
assert credential_status("custom", ProviderConfig(api_key="sk-local")).ok
assert not credential_status("azure_openai", ProviderConfig(api_key="sk-azure")).ok
assert not credential_status("ollama_chat", ProviderConfig()).ok
def test_the_key_hint_names_endpoint_add_when_endpoints_exist() -> None:
"""The generic hint says `provider set --api-key`, which writes the flat
field -- ignored the moment endpoints exist, so a user following it loops.
With endpoints in the section, the hint must name the command that works."""
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
section = ProviderConfig.model_validate({"endpoints": [{"label": "a", "apiKey": ""}]})
status = credential_status("openrouter", section)
assert not status.ok
assert "endpoint add" in status.summary
flat = credential_status("openrouter", ProviderConfig())
assert "endpoint add" not in flat.summary
def test_the_gate_never_accepts_a_default_address_the_reader_will_not_serve() -> None:
"""Closed loop over every spec: whenever the gate passes a bare-key config
because of a shipped default, `Config.get_api_base` must serve that same
default -- both read `usable_default_api_base`, and this pins that they
keep doing so."""
from raven.config.schema import Config
from raven.providers.auth import credential_status
from raven.providers.registry import PROVIDERS
for spec in PROVIDERS:
if not spec.requires_api_base or spec.is_oauth:
continue
cfg = Config.model_validate({"providers": {spec.name: {"apiKey": "sk-x"}}})
if credential_status(spec.name, cfg.providers.get(spec.name)).ok:
assert cfg.get_api_base(f"{spec.name}/some-model") == spec.usable_default_api_base != "", spec.name
def test_the_hint_ignores_a_duck_typed_endpoints_attribute_like_the_gate() -> None:
"""A section whose ``endpoints`` is truthy but not a real list (test
doubles reach the gate this way) is judged by its flat fields; the hint
must not send its owner to ``endpoint add`` while the gate reads flat."""
from types import SimpleNamespace
from raven.providers.auth import credential_status
section = SimpleNamespace(endpoints=object(), api_key="", api_base=None, api_key_list=None)
status = credential_status("openrouter", section)
assert not status.ok
assert "endpoint add" not in status.summary
assert "provider set" in status.summary
def test_a_default_the_reader_never_serves_does_not_satisfy_the_gate() -> None:
"""The every-spec invariant above is vacuous while the registry happens to
contain no spec whose default_api_base differs from its usable one; this
synthetic spec is exactly that shape, so reverting the gate to read the
raw default turns it red."""
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
from raven.providers.registry import ProviderSpec
spec = ProviderSpec(
name="synthetic_vendor",
keywords=(),
env_key="SYNTHETIC_API_KEY",
requires_api_base=True,
default_api_base="https://display-only.example/v1",
)
assert not credential_status("synthetic_vendor", ProviderConfig(api_key="sk-x"), spec=spec).ok
def test_credential_status_false_for_flat_key_and_keyless_endpoint() -> None:
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
section = ProviderConfig.model_validate({"apiKey": "sk-ant-TEST", "endpoints": [{"label": "a", "apiKey": ""}]})
assert not credential_status("anthropic", section).ok
def test_credential_status_true_when_the_endpoint_itself_has_a_key() -> None:
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
section = ProviderConfig.model_validate({"endpoints": [{"label": "a", "apiKey": "sk-1"}]})
assert credential_status("anthropic", section).ok
def test_credential_status_ok_for_an_endpoints_only_section() -> None:
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
section = ProviderConfig.model_validate({"endpoints": [{"label": "a", "apiKey": "sk-1"}]})
assert credential_status("anthropic", section).ok
def test_credential_status_not_ok_when_every_endpoint_key_is_empty() -> None:
from raven.config.schema import ProviderConfig
from raven.providers.auth import credential_status
section = ProviderConfig.model_validate({"endpoints": [{"label": "a", "apiKey": ""}, {"label": "b", "apiKey": ""}]})
assert not credential_status("anthropic", section).ok
def test_a_provider_whose_key_lives_in_a_list_sends_a_key(tmp_path: Path) -> None:
"""Passing the gate is not enough; the request has to carry a credential.
Gemini accepts several keys under one section. Reading ``api_key`` directly
at the call site sent an empty string for a section holding only the list --
a provider that every check called configured, failing at the API instead of
at startup, which is the worst of both.
"""
case = SCENARIOS["gemini_key_list_only"]
path = _config_file(tmp_path, case)
config = Config.model_validate(json.loads(path.read_text(encoding="utf-8")))
provider = config.get_provider(case["model"])
assert provider is not None
assert provider.effective_api_key == "AIzaTEST"
assert config.get_api_key(case["model"]) == "AIzaTEST"
def test_only_the_auth_module_decides_configuredness_from_a_key() -> None:
"""No surface may read a key off a provider section to decide if it is set up.
Six surfaces did, with six rules, and the divergence was invisible because
each looked reasonable alone.
Matched on the syntax tree rather than on a line pattern, and on three
spellings of the read -- see ``key_reads`` for why the net is this wide.
"""
import ast
root = Path(__file__).resolve().parents[1] / "raven"
# Every entry is argued, because an unargued allowlist is the line-pattern
# guard again with extra steps.
allowed = {
# Not an LLM provider section: a tool's own key (media generation,
# web search), the router's, or EverOS's.
"raven/agent/loop/wiring.py",
# The sub-agent loop asks the same question the main loop does, about the
# same tool: whether web_search resolved a Serper key, so an unusable
# search is withheld rather than offered and failed. It asks the built
# tool rather than the config because the tool resolves from either the
# constructor value or SERPER_API_KEY.
"raven/agent/subagent/backends/raven_loop.py",
"raven/agent/tools/media_gen.py",
"raven/agent/tools/web.py",
# The assembly door carries the tool-key reads the three entrances used
# to make (web search, jina): still a tool's own key, no provider
# verdict -- the entrances themselves no longer read any key.
"raven/core/runtime.py",
# The RPC surface that renders that EverOS section: the same key,
# reduced to a set/unset flag for the settings page. No verdict about
# a Raven provider is being made from it.
"raven/rpc/methods/console.py",
"raven/config/update_tools.py",
"raven/providers/transcription.py",
# Reads a key in order to *use* it -- put it on the request, redact it
# for display, rotate it -- rather than to rule on whether a provider is
# set up.
"raven/config/schema.py",
# Reports which source supplied a tool's key, and whether one is there
# to reuse, so a deployer is told "reusing the OpenRouter key you
# already have" rather than "needs a key" and does not go and create an
# account twice -- or, when nothing is there, is not told to reuse a
# credential that does not exist. It rules on nothing: `is_configured`
# and `has_credential` both ask the tools, which is where each family's
# rule already lives, so this file cannot become a second opinion.
"raven/agent/tools/capabilities.py",
"raven/config/update_providers.py",
"raven/providers/litellm_provider.py",
"raven/providers/factory.py",
# The router reads the OpenRouter key in order to call OpenRouter with it;
# the read moved here from the assembly root with the vendor knowledge,
# and an empty answer disables routing rather than ruling on a provider.
"raven/routing/classifier.py",
"raven/cli/onboard_commands.py",
# The same EverOS section again, read by the knowledge embedder: the
# three strings it needs to reach an OpenAI-compatible endpoint, and
# their absence read as "no embedding is configured, so there are no
# knowledge bases". That is a fact about EverOS's own section, not a
# verdict on a Raven provider -- see the module docstring for why the
# endpoint is inherited rather than picked from a provider catalogue.
"raven/knowledge/_embedding.py",
# The connection-material reading layer itself: resolves flat fields,
# api_key_list and endpoints into one list for whoever sends requests.
# Configuredness still rules through auth, which consults this shape
# via its own _present.
"raven/providers/endpoints.py",
"raven/cli/provider_commands.py",
"raven/cli/status_commands.py",
"raven/rpc/methods/model.py",
"raven/rpc/methods/setup.py",
"raven/providers/azure_openai_provider.py",
# The two native transports read their key only to put it on the
# Authorization header of the request they send, the way the Azure
# provider above does; whether the provider is set up is still auth's.
"raven/providers/openai_responses_provider.py",
"raven/providers/anthropic_messages_provider.py",
"raven/contracts/llm_provider.py",
"raven/providers/minimax_oauth_provider.py",
"raven/providers/per_model_provider.py",
# Other subsystems' credentials entirely: the skill hub, an embedding
# script.
"raven/config/update.py",
"raven/context_engine/factory.py",
"raven/routing/generate_embeddings.py",
# A third-party sub-agent's own credential, not a provider section: the
# key goes on that agent's own Authorization header against its own
# base_url, so `providers.auth` has no verdict to give about it. The
# roster surfaces do gate on presence -- an openai entry with no key
# cannot answer a dispatch -- but that is a fact about one sub-agent
# entry, not about whether a Raven provider is set up.
"raven/agent/subagent/backends/__init__.py",
"raven/agent/subagent/backends/openai_api.py",
"raven/agent/subagent/probe.py",
"raven/rpc/methods/subagents.py",
# Copies this raven's OpenRouter key into a sub-agent's own `.env`, so
# a user who configured one in step 1 is not asked for a second copy; the
# verdict that the provider is usable comes from `_configured_providers`
# (which rules through auth) before that value is touched at all.
"raven/cli/subagent_setup.py",
# The skill hub's endpoint credential, read to store or forward it.
"raven/config/update_skills.py",
}
names = {"api_key", "api_key_list", "apiKey", "apiKeyList"}
def key_reads(tree: ast.AST) -> list[int]:
"""Every read of a credential field, in any of its three spellings.
Deliberately not narrowed to "reads in a truthiness context": every
recognizer of that context misses a shape -- an attribute read on a
passthrough section, `v.get("apiKey")` on a raw payload, the same call
inside a comprehension.
So it flags the read and the allowlist carries the argument. A file that
legitimately touches a key says why, once, here -- which is a claim a
reviewer can check, unlike a pattern's silence.
"""
found: list[int] = []
for node in ast.walk(tree):
if isinstance(node, ast.Attribute) and node.attr in names:
found.append(node.lineno)
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == "get":
arg = node.args[0] if node.args else None
if isinstance(arg, ast.Constant) and arg.value in names:
found.append(node.lineno)
elif isinstance(node, ast.Subscript) and isinstance(node.slice, ast.Constant):
if node.slice.value in names:
found.append(node.lineno)
return found
offenders = sorted(
f"{path.relative_to(root.parent).as_posix()}:{line}"
for path in root.rglob("*.py")
if path.relative_to(root.parent).as_posix() not in allowed
for line in key_reads(ast.parse(path.read_text(encoding="utf-8")))
)
assert not offenders, "decide configuredness through providers.auth.credential_status: " + ", ".join(offenders)
#: The six vendors the refusal table marks unconfigurable by a bare key --
#: each needs credential material the onboarding wizard's generic single-key
#: prompt has no field for.
_KEY_REFUSED_VENDORS = ("chatgpt", "bedrock", "sagemaker", "vertex_ai", "azure", "cloudflare")
@pytest.mark.parametrize("vendor", _KEY_REFUSED_VENDORS)
def test_key_refusal_names_a_reason_for_vendors_a_key_cannot_configure(vendor: str) -> None:
reason = key_refusal(vendor)
assert reason is not None
assert reason.strip()
def test_key_refusal_chatgpt_points_at_ravens_own_oauth_path() -> None:
"""chatgpt is the one vendor where a *different* Raven path already exists."""
reason = key_refusal("chatgpt")
assert reason is not None
assert "openai-codex" in reason or "openai_codex" in reason
@pytest.mark.parametrize("vendor", ["gigachat", "openai", "anthropic", "custom", "deepseek"])
def test_key_refusal_is_none_for_vendors_a_key_configures(vendor: str) -> None:
"""Everyone else -- including gigachat, whose key merely has an odd shape."""
assert key_refusal(vendor) is None
def test_key_refusal_normalizes_hyphen_and_case() -> None:
"""Matched the same way every other provider-name comparison is made."""
assert key_refusal("Vertex-AI") == key_refusal("vertex_ai")
assert key_refusal("BEDROCK") == key_refusal("bedrock")