Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

30.6 KB
Raw Blame
import time
from collections.abc import Mapping
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass, field, replace
from datetime import datetime, timedelta
from threading import Barrier, local
from typing import Never, cast, override

import pytest
from sqlalchemy.orm import Session, sessionmaker

import services.data_source.credential_adapters as credential_adapters_module
from core.datasource.entities.datasource_entities import (
    DatasourceProviderEntityWithPlugin,
    DatasourceProviderIdentity,
    DatasourceProviderType,
)
from core.entities.provider_entities import ProviderConfig, ProviderConfigType
from core.plugin.entities.oauth import OAuthSchema
from core.plugin.entities.plugin_daemon import PluginDatasourceProviderEntity, PluginOAuthCredentialsResponse
from core.plugin.impl.datasource import PluginDatasourceManager
from core.plugin.impl.oauth import OAuthHandler
from models.oauth import DatasourceOauthParamConfig, DatasourceOauthTenantParamConfig
from models.provider_ids import DatasourceProviderID
from repositories.data_source.credential_repository import SQLAlchemyDatasourceCredentialRepository
from services.credentials.refresh import CredentialRefreshUnavailableError
from services.data_source.credential_adapters import (
    OAuthDatasourceCredentialRefresher,
    PluginDatasourceCredentialCodec,
    PluginDatasourceOAuthClientResolver,
)
from services.data_source.credential_gateway import (
    ActorAwareDatasourceCredentialGateway,
    DatasourceCredentialConcurrentUpdateError,
    DatasourceCredentialError,
    DatasourceCredentialNotFoundError,
    DatasourceCredentialRefreshError,
    RefreshedDatasourceCredential,
    TrustedStoredDatasourceCredentialGateway,
)
from services.data_source.entities.credential import DatasourceCredentialRecord
from services.data_source.provider_service import DatasourceProviderService
from services.plugin.dispatch_identity import PluginOAuthClientConfig
from tests.helpers.unit.credential_refresh import ThreadCredentialRefreshLock


def _record(
    *,
    auth_type: str = "oauth2",
    expires_at: int = -1,
    updated_at: datetime | None = None,
) -> DatasourceCredentialRecord:
    return DatasourceCredentialRecord(
        id="credential-1",
        workspace_id="workspace-1",
        owner_id="credential-owner-1",
        name="Notion",
        provider="notion_datasource",
        plugin_id="langgenius/notion_datasource",
        auth_type=auth_type,
        encrypted_credentials={"integration_secret": "encrypted"},
        expires_at=expires_at,
        updated_at=updated_at or datetime(2026, 1, 1),
    )


@dataclass
class ScriptedCredentialStore:
    records: list[DatasourceCredentialRecord | None]
    update_result: bool = True
    get_calls: list[tuple[str, str, str, str, str]] = field(default_factory=list)
    updates: list[tuple[DatasourceCredentialRecord, Mapping[str, object], int]] = field(default_factory=list)
    events: list[str] = field(default_factory=list)

    def get_visible(
        self,
        *,
        workspace_id: str,
        actor_id: str,
        credential_id: str,
        provider: str,
        plugin_id: str,
    ) -> DatasourceCredentialRecord | None:
        self.events.append("read")
        self.get_calls.append((workspace_id, actor_id, credential_id, provider, plugin_id))
        return self.records.pop(0) if len(self.records) > 1 else self.records[0]

    def update_if_unchanged(
        self,
        *,
        record: DatasourceCredentialRecord,
        encrypted_credentials: Mapping[str, object],
        expires_at: int,
    ) -> bool:
        self.events.append("update")
        self.updates.append((record, encrypted_credentials, expires_at))
        return self.update_result


@dataclass
class ReversibleCredentialCodec:
    def decrypt(self, record: DatasourceCredentialRecord) -> dict[str, object]:
        return {"integration_secret": f"plain:{record.encrypted_credentials['integration_secret']}"}

    def encrypt(self, record: DatasourceCredentialRecord, credentials: Mapping[str, object]) -> dict[str, object]:
        del record
        return {"integration_secret": f"cipher:{credentials['integration_secret']}"}


@dataclass
class StoredCredentialCatalog:
    record: DatasourceCredentialRecord | None
    calls: list[dict[str, str | None]] = field(default_factory=list)
    updates: list[tuple[DatasourceCredentialRecord, Mapping[str, object], int]] = field(default_factory=list)

    def get_for_stored_document(
        self,
        *,
        workspace_id: str,
        dataset_id: str,
        document_id: str,
        credential_id: str | None,
        provider: str,
        plugin_id: str,
    ) -> DatasourceCredentialRecord | None:
        self.calls.append(
            {
                "workspace_id": workspace_id,
                "dataset_id": dataset_id,
                "document_id": document_id,
                "credential_id": credential_id,
                "provider": provider,
                "plugin_id": plugin_id,
            }
        )
        return self.record

    def update_if_unchanged(
        self,
        *,
        record: DatasourceCredentialRecord,
        encrypted_credentials: Mapping[str, object],
        expires_at: int,
    ) -> bool:
        self.updates.append((record, encrypted_credentials, expires_at))
        return True


@dataclass
class RecordingCredentialRefresher:
    calls: list[tuple[str, str, DatasourceCredentialRecord, Mapping[str, object]]] = field(default_factory=list)
    events: list[str] = field(default_factory=list)

    def refresh(
        self,
        *,
        workspace_id: str,
        actor_id: str,
        record: DatasourceCredentialRecord,
        credentials: Mapping[str, object],
    ) -> RefreshedDatasourceCredential:
        self.events.append("refresh")
        self.calls.append((workspace_id, actor_id, record, credentials))
        return RefreshedDatasourceCredential({"integration_secret": "refreshed"}, 500)


@dataclass
class StaticProviderManager:
    provider: PluginDatasourceProviderEntity
    calls: list[tuple[str, str]] = field(default_factory=list)

    def fetch_datasource_provider(self, *, tenant_id: str, provider_id: str) -> PluginDatasourceProviderEntity:
        self.calls.append((tenant_id, provider_id))
        return self.provider


@dataclass
class RecordingOAuthHandler:
    response: PluginOAuthCredentialsResponse | None = None
    error: Exception | None = None
    calls: list[dict[str, object]] = field(default_factory=list)

    def refresh_credentials(
        self,
        tenant_id: str,
        user_id: str,
        plugin_id: str,
        provider: str,
        redirect_uri: str,
        system_credentials: Mapping[str, object],
        credentials: Mapping[str, object],
        *,
        plugin_unique_identifier: str | None = None,
    ) -> PluginOAuthCredentialsResponse:
        self.calls.append(
            {
                "tenant_id": tenant_id,
                "user_id": user_id,
                "plugin_id": plugin_id,
                "plugin_unique_identifier": plugin_unique_identifier,
                "provider": provider,
                "redirect_uri": redirect_uri,
                "system_credentials": system_credentials,
                "credentials": credentials,
            }
        )
        if self.error is not None:
            raise self.error
        assert self.response is not None
        return self.response


@dataclass
class RecordingCredentialEncrypter:
    decrypted: Mapping[str, object]
    calls: list[Mapping[str, object]] = field(default_factory=list)

    def decrypt(self, credentials: Mapping[str, object]) -> Mapping[str, object]:
        self.calls.append(credentials)
        return self.decrypted


class StaticOAuthClientResolver:
    def resolve(self, *, workspace_id: str, provider_id: DatasourceProviderID) -> PluginOAuthClientConfig:
        del workspace_id
        return PluginOAuthClientConfig(f"{provider_id.plugin_id}:1.0.0@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", {})


def _oauth_refresher(
    session_factory: sessionmaker[Session],
    *,
    manager: StaticProviderManager,
    handler: RecordingOAuthHandler,
) -> OAuthDatasourceCredentialRefresher:
    repository = SQLAlchemyDatasourceCredentialRepository(session_factory=session_factory)
    oauth_clients = PluginDatasourceOAuthClientResolver(
        configs=repository,
        provider_manager=cast(PluginDatasourceManager, manager),
    )
    return OAuthDatasourceCredentialRefresher(
        oauth_clients=oauth_clients,
        oauth_handler=cast(OAuthHandler, handler),
    )


def _provider(*, with_oauth_schema: bool = True) -> PluginDatasourceProviderEntity:
    oauth_schema = (
        OAuthSchema(
            client_schema=[ProviderConfig(type=ProviderConfigType.SECRET_INPUT, name="client_secret")],
            credentials_schema=[
                ProviderConfig(type=ProviderConfigType.SECRET_INPUT, name="integration_secret"),
                ProviderConfig(type=ProviderConfigType.TEXT_INPUT, name="workspace_name"),
            ],
        )
        if with_oauth_schema
        else None
    )
    return PluginDatasourceProviderEntity(
        provider="notion_datasource",
        plugin_unique_identifier="langgenius/notion_datasource:1.0.0@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        plugin_id="langgenius/notion_datasource",
        declaration=DatasourceProviderEntityWithPlugin(
            identity=DatasourceProviderIdentity(
                author="langgenius",
                name="notion_datasource",
                description={"en_US": "Notion", "zh_Hans": "Notion"},
                icon="notion.svg",
                label={"en_US": "Notion", "zh_Hans": "Notion"},
            ),
            credentials_schema=[
                ProviderConfig(type=ProviderConfigType.SECRET_INPUT, name="api_secret"),
                ProviderConfig(type=ProviderConfigType.TEXT_INPUT, name="label"),
            ],
            oauth_schema=oauth_schema,
            provider_type=DatasourceProviderType.ONLINE_DOCUMENT,
        ),
    )


def _gateway(
    store: ScriptedCredentialStore, refresher: RecordingCredentialRefresher | None = None
) -> tuple[ActorAwareDatasourceCredentialGateway, RecordingCredentialRefresher]:
    refresh = refresher or RecordingCredentialRefresher()
    return (
        ActorAwareDatasourceCredentialGateway(
            credentials=store,
            codec=ReversibleCredentialCodec(),
            refresher=refresh,
            refresh_lock=ThreadCredentialRefreshLock(),
            now=lambda: 100,
        ),
        refresh,
    )


def _resolve(gateway: ActorAwareDatasourceCredentialGateway) -> dict[str, object]:
    return gateway.resolve(
        workspace_id="workspace-1",
        actor_id="actor-1",
        credential_id="credential-1",
        provider="notion_datasource",
        plugin_id="langgenius/notion_datasource",
    )


def test_resolve_fails_closed_when_credential_is_not_visible() -> None:
    gateway, _ = _gateway(ScriptedCredentialStore(records=[None]))

    with pytest.raises(DatasourceCredentialNotFoundError):
        _resolve(gateway)


def test_trusted_stored_source_resolver_uses_distinct_owner_chain_port() -> None:
    catalog = StoredCredentialCatalog(_record())
    gateway = TrustedStoredDatasourceCredentialGateway(
        credentials=catalog,
        codec=ReversibleCredentialCodec(),
        refresher=RecordingCredentialRefresher(),
        refresh_lock=ThreadCredentialRefreshLock(),
        now=lambda: 100,
    )

    result = gateway.resolve_for_document(
        workspace_id="workspace-1",
        dataset_id="dataset-1",
        document_id="document-1",
        credential_id="credential-1",
        provider="notion_datasource",
        plugin_id="langgenius/notion_datasource",
    )

    assert result == {"integration_secret": "plain:encrypted"}
    assert catalog.calls == [
        {
            "workspace_id": "workspace-1",
            "dataset_id": "dataset-1",
            "document_id": "document-1",
            "credential_id": "credential-1",
            "provider": "notion_datasource",
            "plugin_id": "langgenius/notion_datasource",
        }
    ]


def test_trusted_stored_source_resolver_fails_closed_for_missing_credential() -> None:
    gateway = TrustedStoredDatasourceCredentialGateway(
        credentials=StoredCredentialCatalog(None),
        codec=ReversibleCredentialCodec(),
        refresher=RecordingCredentialRefresher(),
        refresh_lock=ThreadCredentialRefreshLock(),
        now=lambda: 100,
    )

    with pytest.raises(DatasourceCredentialNotFoundError):
        gateway.resolve_for_document(
            workspace_id="workspace-1",
            dataset_id="dataset-1",
            document_id="document-1",
            credential_id="credential-1",
            provider="notion_datasource",
            plugin_id="langgenius/notion_datasource",
        )


def test_trusted_stored_source_refreshes_with_persisted_credential_owner() -> None:
    record = _record(expires_at=120)
    catalog = StoredCredentialCatalog(record)
    refresher = RecordingCredentialRefresher()
    gateway = TrustedStoredDatasourceCredentialGateway(
        credentials=catalog,
        codec=ReversibleCredentialCodec(),
        refresher=refresher,
        refresh_lock=ThreadCredentialRefreshLock(),
        now=lambda: 100,
    )

    result = gateway.resolve_for_document(
        workspace_id="workspace-1",
        dataset_id="dataset-1",
        document_id="document-1",
        credential_id="credential-1",
        provider="notion_datasource",
        plugin_id="langgenius/notion_datasource",
    )

    assert result == {"integration_secret": "refreshed"}
    assert refresher.calls == [("workspace-1", "credential-owner-1", record, {"integration_secret": "plain:encrypted"})]
    assert catalog.updates == [(record, {"integration_secret": "cipher:refreshed"}, 500)]


def test_resolve_decrypts_without_refreshing_non_expiring_credential() -> None:
    store = ScriptedCredentialStore(records=[_record()])
    gateway, refresher = _gateway(store)

    assert _resolve(gateway) == {"integration_secret": "plain:encrypted"}
    assert refresher.calls == []
    assert store.updates == []


def test_refresh_runs_between_read_and_conditional_update() -> None:
    record = _record(expires_at=120)
    events: list[str] = []
    store = ScriptedCredentialStore(records=[record], events=events)
    gateway, refresher = _gateway(store, RecordingCredentialRefresher(events=events))

    assert _resolve(gateway) == {"integration_secret": "refreshed"}
    assert refresher.calls == [("workspace-1", "actor-1", record, {"integration_secret": "plain:encrypted"})]
    assert store.updates == [(record, {"integration_secret": "cipher:refreshed"}, 500)]
    assert events == ["read", "read", "refresh", "update"]


def test_concurrent_refresh_uses_newer_non_expiring_snapshot() -> None:
    stale = _record(expires_at=120)
    latest = replace(
        stale,
        encrypted_credentials={"integration_secret": "latest"},
        expires_at=500,
        updated_at=stale.updated_at + timedelta(seconds=1),
    )
    store = ScriptedCredentialStore(records=[stale, latest], update_result=False)
    gateway, _ = _gateway(store)

    assert _resolve(gateway) == {"integration_secret": "plain:latest"}
    assert store.get_calls == [
        ("workspace-1", "actor-1", "credential-1", "notion_datasource", "langgenius/notion_datasource"),
        ("workspace-1", "actor-1", "credential-1", "notion_datasource", "langgenius/notion_datasource"),
    ]


def test_concurrent_refresh_fails_closed_if_latest_snapshot_is_still_expired() -> None:
    stale = _record(expires_at=120)
    latest = replace(stale, updated_at=stale.updated_at + timedelta(seconds=1))
    gateway, _ = _gateway(ScriptedCredentialStore(records=[stale, latest], update_result=False))

    with pytest.raises(DatasourceCredentialConcurrentUpdateError):
        _resolve(gateway)


def test_actor_stored_and_provider_callers_share_one_refresh() -> None:
    class ReaderState(local):
        first = True

    class SharedCatalog:
        record = _record(expires_at=1)
        barrier = Barrier(3)
        reader = ReaderState()
        writes = 0

        def _read(
            self, workspace_id: str, credential_id: str | None, provider: str, plugin_id: str
        ) -> DatasourceCredentialRecord:
            record = self.record
            assert (workspace_id, credential_id, provider, plugin_id) == (
                record.workspace_id,
                record.id,
                record.provider,
                record.plugin_id,
            )
            if self.reader.first:
                self.reader.first = False
                self.barrier.wait(timeout=2)
            return record

        def get_visible(
            self, *, workspace_id: str, actor_id: str, credential_id: str, provider: str, plugin_id: str
        ) -> DatasourceCredentialRecord:
            assert actor_id == "actor-1"
            return self._read(workspace_id, credential_id, provider, plugin_id)

        def get_for_stored_document(
            self,
            *,
            workspace_id: str,
            dataset_id: str,
            document_id: str,
            credential_id: str | None,
            provider: str,
            plugin_id: str,
        ) -> DatasourceCredentialRecord:
            assert (dataset_id, document_id) == ("dataset-1", "document-1")
            return self._read(workspace_id, credential_id, provider, plugin_id)

        def get_provider_credential(
            self, *, workspace_id: str, credential_id: str | None, provider: str, plugin_id: str
        ) -> DatasourceCredentialRecord:
            return self._read(workspace_id, credential_id, provider, plugin_id)

        def list_provider_credentials(
            self, *, workspace_id: str, provider: str, plugin_id: str
        ) -> list[DatasourceCredentialRecord]:
            return [self._read(workspace_id, self.record.id, provider, plugin_id)]

        def get_oauth_client_config(self, *, workspace_id: str, provider: str, plugin_id: str) -> Never:
            raise AssertionError(f"Unexpected OAuth lookup for {workspace_id}:{plugin_id}/{provider}")

        def update_if_unchanged(
            self, *, record: DatasourceCredentialRecord, encrypted_credentials: Mapping[str, object], expires_at: int
        ) -> bool:
            assert self.record == record
            self.writes += 1
            self.record = replace(record, encrypted_credentials=encrypted_credentials, expires_at=expires_at)
            return True

    class RotatingRefresher:
        calls = 0

        def refresh(
            self,
            *,
            workspace_id: str,
            actor_id: str,
            record: DatasourceCredentialRecord,
            credentials: Mapping[str, object],
        ) -> RefreshedDatasourceCredential:
            assert (workspace_id, record.id) == ("workspace-1", "credential-1")
            assert actor_id in {"actor-1", "credential-owner-1"}
            assert credentials == {"integration_secret": "plain:encrypted"}
            self.calls += 1
            lock.wait_for_callers(3)
            return RefreshedDatasourceCredential({"integration_secret": "refreshed"}, int(time.time()) + 3600)

    catalog, codec, refresher, lock = (
        SharedCatalog(),
        ReversibleCredentialCodec(),
        RotatingRefresher(),
        ThreadCredentialRefreshLock(),
    )
    actor = ActorAwareDatasourceCredentialGateway(
        credentials=catalog, codec=codec, refresher=refresher, refresh_lock=lock
    )
    stored = TrustedStoredDatasourceCredentialGateway(
        credentials=catalog, codec=codec, refresher=refresher, refresh_lock=lock
    )
    provider = DatasourceProviderService(
        credentials=catalog,
        codec=codec,
        refresher=refresher,
        refresh_lock=lock,
        provider_manager=PluginDatasourceManager(),
    )
    with ThreadPoolExecutor(max_workers=3) as pool:
        futures = [
            pool.submit(_resolve, actor),
            pool.submit(
                stored.resolve_for_document,
                workspace_id="workspace-1",
                dataset_id="dataset-1",
                document_id="document-1",
                credential_id="credential-1",
                provider="notion_datasource",
                plugin_id="langgenius/notion_datasource",
            ),
            pool.submit(
                provider.get_datasource_credentials,
                "workspace-1",
                "notion_datasource",
                "langgenius/notion_datasource",
                "credential-1",
            ),
        ]
        results = [future.result(timeout=3) for future in futures]
    assert all(result["integration_secret"] in {"refreshed", "plain:cipher:refreshed"} for result in results)
    assert refresher.calls == catalog.writes == 1
    assert lock.keys == [("datasource", "workspace-1", "credential-1")] * 3


@pytest.mark.parametrize("failure", ["unavailable", "lost-before", "lost-after"])
def test_refresh_lease_failure_never_writes_or_returns_expired_data(failure: str) -> None:
    lock = ThreadCredentialRefreshLock()
    lock.unavailable = failure == "unavailable"
    lock.lost = failure == "lost-before"

    class LosingRefresher(RecordingCredentialRefresher):
        @override
        def refresh(
            self,
            *,
            workspace_id: str,
            actor_id: str,
            record: DatasourceCredentialRecord,
            credentials: Mapping[str, object],
        ) -> RefreshedDatasourceCredential:
            result = super().refresh(
                workspace_id=workspace_id, actor_id=actor_id, record=record, credentials=credentials
            )
            lock.lost = True
            return result

    store, refresher = ScriptedCredentialStore([_record(expires_at=1)]), LosingRefresher()
    gateway = ActorAwareDatasourceCredentialGateway(
        credentials=store, codec=ReversibleCredentialCodec(), refresher=refresher, refresh_lock=lock, now=lambda: 100
    )
    with pytest.raises(CredentialRefreshUnavailableError):
        _resolve(gateway)
    assert store.updates == []
    assert len(refresher.calls) == (1 if failure == "lost-after" else 0)


def test_visibility_is_rechecked_after_waiting_for_the_refresh_lock() -> None:
    store = ScriptedCredentialStore([_record(expires_at=1), None])
    gateway, refresher = _gateway(store)
    with pytest.raises(DatasourceCredentialNotFoundError):
        _resolve(gateway)
    assert refresher.calls == []
    assert store.updates == []


@pytest.mark.parametrize(
    ("auth_type", "secret_name"),
    [("api-key", "api_secret"), ("oauth2", "integration_secret")],
)
def test_plugin_codec_encrypts_and_decrypts_only_declared_secret_fields(
    monkeypatch: pytest.MonkeyPatch,
    auth_type: str,
    secret_name: str,
) -> None:
    manager = StaticProviderManager(_provider())
    codec = PluginDatasourceCredentialCodec(provider_manager=cast(PluginDatasourceManager, manager))
    record = replace(
        _record(auth_type=auth_type),
        encrypted_credentials={secret_name: "cipher", "label": "plain"},
    )
    decrypt_calls: list[tuple[str, str]] = []
    encrypt_calls: list[tuple[str, str]] = []

    def decrypt_token(workspace_id: str, value: str) -> str:
        decrypt_calls.append((workspace_id, value))
        return f"decrypted:{value}"

    def encrypt_token(workspace_id: str, value: str) -> str:
        encrypt_calls.append((workspace_id, value))
        return f"encrypted:{value}"

    monkeypatch.setattr(credential_adapters_module.encrypter, "decrypt_token", decrypt_token)
    monkeypatch.setattr(credential_adapters_module.encrypter, "encrypt_token", encrypt_token)

    assert codec.decrypt(record) == {secret_name: "decrypted:cipher", "label": "plain"}
    assert codec.encrypt(record, {secret_name: "secret", "label": "plain"}) == {
        secret_name: "encrypted:secret",
        "label": "plain",
    }
    assert decrypt_calls == [("workspace-1", "cipher")]
    assert encrypt_calls == [("workspace-1", "secret")]

    non_string_record = replace(record, encrypted_credentials={secret_name: 42})
    assert codec.decrypt(non_string_record) == {secret_name: 42}
    assert codec.encrypt(record, {secret_name: None}) == {secret_name: None}
    assert decrypt_calls == [("workspace-1", "cipher")]
    assert encrypt_calls == [("workspace-1", "secret")]


def test_plugin_codec_rejects_unsupported_credential_type() -> None:
    codec = PluginDatasourceCredentialCodec(
        provider_manager=cast(PluginDatasourceManager, StaticProviderManager(_provider()))
    )

    with pytest.raises(DatasourceCredentialError, match="Unsupported credential type"):
        codec.decrypt(_record(auth_type="unauthorized"))


def test_oauth_refresher_prefers_enabled_tenant_client_and_returns_refreshed_snapshot(
    monkeypatch: pytest.MonkeyPatch,
    sqlite_session_factory: sessionmaker[Session],
) -> None:
    with sqlite_session_factory.begin() as session:
        session.add_all(
            [
                DatasourceOauthTenantParamConfig(
                    tenant_id="workspace-1",
                    provider="notion_datasource",
                    plugin_id="langgenius/notion_datasource",
                    client_params={"client_secret": "tenant-cipher"},
                    enabled=True,
                ),
                DatasourceOauthParamConfig(
                    provider="notion_datasource",
                    plugin_id="langgenius/notion_datasource",
                    system_credentials={"client_secret": "system-secret"},
                ),
            ]
        )
    manager = StaticProviderManager(_provider())
    handler = RecordingOAuthHandler(
        response=PluginOAuthCredentialsResponse(
            credentials={"integration_secret": "refreshed-secret"},
            expires_at=500,
        )
    )
    encrypter = RecordingCredentialEncrypter({"client_secret": "tenant-secret"})

    def create_encrypter(**kwargs: object) -> tuple[RecordingCredentialEncrypter, None]:
        assert kwargs["tenant_id"] == "workspace-1"
        return encrypter, None

    monkeypatch.setattr(credential_adapters_module, "create_provider_encrypter", create_encrypter)
    refresher = _oauth_refresher(sqlite_session_factory, manager=manager, handler=handler)

    result = refresher.refresh(
        workspace_id="workspace-1",
        actor_id="actor-1",
        record=_record(),
        credentials={"integration_secret": "old-secret"},
    )

    assert result == RefreshedDatasourceCredential({"integration_secret": "refreshed-secret"}, 500)
    assert encrypter.calls == [{"client_secret": "tenant-cipher"}]
    assert handler.calls[0]["system_credentials"] == {"client_secret": "tenant-secret"}
    assert handler.calls[0]["credentials"] == {"integration_secret": "old-secret"}
    assert handler.calls[0]["user_id"] == "actor-1"
    assert handler.calls[0]["plugin_unique_identifier"] == manager.provider.plugin_unique_identifier


def test_oauth_refresher_rejects_tenant_client_when_provider_has_no_oauth_schema(
    sqlite_session_factory: sessionmaker[Session],
) -> None:
    with sqlite_session_factory.begin() as session:
        session.add(
            DatasourceOauthTenantParamConfig(
                tenant_id="workspace-1",
                provider="notion_datasource",
                plugin_id="langgenius/notion_datasource",
                client_params={"client_secret": "tenant-cipher"},
                enabled=True,
            )
        )
    provider = _provider(with_oauth_schema=False)
    handler = RecordingOAuthHandler()
    refresher = _oauth_refresher(
        sqlite_session_factory,
        manager=StaticProviderManager(provider),
        handler=handler,
    )

    with pytest.raises(DatasourceCredentialError, match="oauth schema not found"):
        refresher.refresh(
            workspace_id="workspace-1",
            actor_id="actor-1",
            record=_record(),
            credentials={"integration_secret": "old"},
        )

    assert handler.calls == []


@pytest.mark.parametrize("verified", [True, False])
def test_oauth_refresher_uses_system_client_only_for_verified_plugin(
    monkeypatch: pytest.MonkeyPatch,
    sqlite_session_factory: sessionmaker[Session],
    verified: bool,
) -> None:
    with sqlite_session_factory.begin() as session:
        session.add_all(
            [
                DatasourceOauthTenantParamConfig(
                    tenant_id="workspace-1",
                    provider="notion_datasource",
                    plugin_id="langgenius/notion_datasource",
                    client_params={"client_secret": "disabled-tenant-secret"},
                    enabled=False,
                ),
                DatasourceOauthParamConfig(
                    provider="notion_datasource",
                    plugin_id="langgenius/notion_datasource",
                    system_credentials={"client_secret": "system-secret"},
                ),
            ]
        )
    manager = StaticProviderManager(_provider())
    handler = RecordingOAuthHandler(
        response=PluginOAuthCredentialsResponse(credentials={"integration_secret": "refreshed"}, expires_at=500)
    )
    verification_calls: list[tuple[str, str]] = []

    def is_plugin_verified(workspace_id: str, plugin_unique_identifier: str) -> bool:
        verification_calls.append((workspace_id, plugin_unique_identifier))
        return verified

    monkeypatch.setattr(credential_adapters_module.PluginService, "is_plugin_verified", is_plugin_verified)
    refresher = _oauth_refresher(sqlite_session_factory, manager=manager, handler=handler)

    if not verified:
        with pytest.raises(DatasourceCredentialError, match="OAuth client is not configured"):
            refresher.refresh(
                workspace_id="workspace-1",
                actor_id="actor-1",
                record=_record(),
                credentials={"integration_secret": "old"},
            )
        assert handler.calls == []
    else:
        refresher.refresh(
            workspace_id="workspace-1",
            actor_id="actor-1",
            record=_record(),
            credentials={"integration_secret": "old"},
        )
        assert handler.calls[0]["system_credentials"] == {"client_secret": "system-secret"}
        assert handler.calls[0]["plugin_unique_identifier"] == manager.provider.plugin_unique_identifier

    assert verification_calls == [
        ("workspace-1", "langgenius/notion_datasource:1.0.0@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    ]


def test_oauth_refresher_translates_plugin_refresh_failure() -> None:
    handler = RecordingOAuthHandler(error=RuntimeError("plugin unavailable"))
    refresher = OAuthDatasourceCredentialRefresher(
        oauth_clients=StaticOAuthClientResolver(),
        oauth_handler=cast(OAuthHandler, handler),
    )

    with pytest.raises(DatasourceCredentialRefreshError, match="credential-1"):
        refresher.refresh(
            workspace_id="workspace-1",
            actor_id="actor-1",
            record=_record(),
            credentials={"integration_secret": "old"},
        )