Skip to content

Automate browser based workflows with AI

9.6 KB
Raw Blame
from types import SimpleNamespace
from unittest.mock import AsyncMock

import pytest

from skyvern.forge.sdk.schemas.credentials import (
    CredentialVaultType,
    CreditCardBillingAddress,
    CreditCardCredential,
    PasswordCredential,
)
from skyvern.forge.sdk.workflow import context_manager as cm
from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext
from skyvern.forge.sdk.workflow.models.block import TaskV2Block
from skyvern.forge.sdk.workflow.models.parameter import CredentialParameter


@pytest.mark.asyncio
async def test_register_credential_parameter_uses_db_totp_identifier(monkeypatch: pytest.MonkeyPatch) -> None:
    db_credential = SimpleNamespace(
        credential_id="cred-1",
        organization_id="org-1",
        vault_type=CredentialVaultType.BITWARDEN,
        totp_identifier="[email protected]",
        run_sequentially=False,
        tested_url="https://login.example.com/account",
    )

    class FakeCredential:
        def __init__(self) -> None:
            self.totp_identifier = None
            self.totp = None

        def model_dump(self, exclude_none: bool = False) -> dict:
            return {}

    class FakeCredentialItem:
        def __init__(self) -> None:
            self.credential = FakeCredential()

    class FakeCredentialService:
        async def get_credential_item(self, _db_credential: object) -> FakeCredentialItem:
            return FakeCredentialItem()

    class FakeCredentialRepo:
        async def get_credential(self, credential_id: str, organization_id: str) -> object:
            assert credential_id == "cred-1"
            assert organization_id == "org-1"
            return db_credential

    class FakeDatabase:
        def __init__(self) -> None:
            self.credentials = FakeCredentialRepo()

    process_item = AsyncMock(side_effect=lambda **kwargs: kwargs["credential_item"])
    fake_app = SimpleNamespace(
        DATABASE=FakeDatabase(),
        CREDENTIAL_VAULT_SERVICES={CredentialVaultType.BITWARDEN: FakeCredentialService()},
        AGENT_FUNCTION=SimpleNamespace(
            enforce_credential_use=AsyncMock(),
            parse_enterprise_totp_secret=AsyncMock(return_value=None),
            process_registered_credential_item=process_item,
        ),
    )
    monkeypatch.setattr(cm, "app", fake_app)

    context = WorkflowRunContext(
        workflow_title="title",
        workflow_id="wf-1",
        workflow_permanent_id="wfp-1",
        workflow_run_id="wr-1",
        aws_client=SimpleNamespace(),
    )

    parameter = CredentialParameter.model_construct(
        key="credential_param",
        credential_parameter_id="cp-1",
        workflow_id="wf-1",
        credential_id="cred-1",
    )
    organization = SimpleNamespace(organization_id="org-1")

    await context._register_credential_parameter_value("cred-1", parameter, organization)

    assert context.get_credential_totp_identifier("credential_param") == "[email protected]"
    assert context.get_resolved_credential_parameter_id("credential_param") == "cred-1"
    # The credential's tested login site is what the code-block release check is armed from;
    # without it here the guard is silently inert for every block bound to this credential.
    assert context.credential_tested_urls["credential_param"] == "https://login.example.com/account"


async def _register_with_credential(
    monkeypatch: pytest.MonkeyPatch,
    credential: object,
) -> WorkflowRunContext:
    db_credential = SimpleNamespace(
        credential_id="cred-1",
        organization_id="org-1",
        vault_type=CredentialVaultType.BITWARDEN,
        totp_identifier=None,
        run_sequentially=False,
        tested_url=None,
    )

    class FakeCredentialItem:
        def __init__(self, cred: PasswordCredential) -> None:
            self.credential = cred

    class FakeCredentialService:
        async def get_credential_item(self, _db_credential: object) -> FakeCredentialItem:
            return FakeCredentialItem(credential)

    class FakeCredentialRepo:
        async def get_credential(self, credential_id: str, organization_id: str) -> object:
            return db_credential

    class FakeDatabase:
        def __init__(self) -> None:
            self.credentials = FakeCredentialRepo()

    process_item = AsyncMock(side_effect=lambda **kwargs: kwargs["credential_item"])
    fake_app = SimpleNamespace(
        DATABASE=FakeDatabase(),
        CREDENTIAL_VAULT_SERVICES={CredentialVaultType.BITWARDEN: FakeCredentialService()},
        AGENT_FUNCTION=SimpleNamespace(
            enforce_credential_use=AsyncMock(),
            parse_enterprise_totp_secret=AsyncMock(return_value=None),
            process_registered_credential_item=process_item,
        ),
    )
    monkeypatch.setattr(cm, "app", fake_app)

    context = WorkflowRunContext(
        workflow_title="title",
        workflow_id="wf-1",
        workflow_permanent_id="wfp-1",
        workflow_run_id="wr-1",
        aws_client=SimpleNamespace(),
    )
    parameter = CredentialParameter.model_construct(
        key="credential_param",
        credential_parameter_id="cp-1",
        workflow_id="wf-1",
        credential_id="cred-1",
    )
    organization = SimpleNamespace(organization_id="org-1")
    await context._register_credential_parameter_value("cred-1", parameter, organization)
    return context


@pytest.mark.asyncio
async def test_register_credential_registers_totp_seed_when_present(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    credential = PasswordCredential(
        username="[email protected]",
        password="secret",
        totp="JBSWY3DPEHPK3PXP",
    )
    context = await _register_with_credential(monkeypatch, credential)
    assert "totp" in context.values["credential_param"]
    totp_secret_id = context.values["credential_param"]["totp"]
    totp_seed = context.secrets[context.totp_secret_value_key(totp_secret_id)]
    assert totp_seed == "JBSWY3DPEHPK3PXP"


@pytest.mark.asyncio
async def test_register_credential_skips_totp_when_seed_missing(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    credential = PasswordCredential(
        username="[email protected]",
        password="secret",
        totp=None,
    )
    context = await _register_with_credential(monkeypatch, credential)
    assert "totp" not in context.values["credential_param"]


@pytest.mark.asyncio
async def test_find_credential_parameter_key_for_secret_round_trip(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    credential = PasswordCredential(
        username="[email protected]",
        password="secret",
        totp="JBSWY3DPEHPK3PXP",
    )
    context = await _register_with_credential(monkeypatch, credential)
    username_secret_id = context.values["credential_param"]["username"]
    assert context.find_credential_parameter_key_for_secret(username_secret_id) == "credential_param"
    assert context.find_credential_parameter_key_for_secret("nonexistent") is None


@pytest.mark.asyncio
async def test_register_credit_card_flattens_billing_fields(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    credential = CreditCardCredential(
        card_number="4111111111111111",
        card_cvv="123",
        card_exp_month="12",
        card_exp_year="2030",
        card_brand="visa",
        card_holder_name="Jane Doe",
        billing_address=CreditCardBillingAddress(
            line1="123 Main St",
            state_code="CA",
            country_code="US",
        ),
        billing_email="[email protected]",
        metadata={"customer_id": "cus_123"},
    )
    context = await _register_with_credential(monkeypatch, credential)

    values = context.values["credential_param"]
    assert context.secrets[values["billing_address_line1"]] == "123 Main St"
    assert context.secrets[values["billing_address_state_code"]] == "CA"
    assert context.secrets[values["billing_address_country_code"]] == "US"
    assert context.secrets[values["billing_email"]] == "[email protected]"
    assert context.secrets[values["metadata_customer_id"]] == "cus_123"


@pytest.mark.asyncio
async def test_register_password_flattens_metadata_like_credit_card(monkeypatch: pytest.MonkeyPatch) -> None:
    credential = PasswordCredential(
        username="[email protected]",
        password="secret",
        metadata={"customer_id": "cus_123"},
    )
    context = await _register_with_credential(monkeypatch, credential)

    values = context.values["credential_param"]
    assert context.secrets[values["metadata_customer_id"]] == "cus_123"


@pytest.mark.asyncio
async def test_register_credit_card_preserves_colliding_metadata_keys(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    credential = CreditCardCredential(
        card_number="4111111111111111",
        card_cvv="123",
        card_exp_month="12",
        card_exp_year="2030",
        card_brand="visa",
        card_holder_name="Jane Doe",
        metadata={
            "Customer ID": "cus_upper",
            "customer-id": "cus_dash",
        },
    )
    context = await _register_with_credential(monkeypatch, credential)

    values = context.values["credential_param"]
    assert context.secrets[values["metadata_customer_id"]] == "cus_upper"
    assert context.secrets[values["metadata_customer_id_2"]] == "cus_dash"


def test_task_v2_block_resolves_totp_identifier_from_context() -> None:
    block = TaskV2Block.model_construct(totp_identifier=None)
    workflow_run_context = SimpleNamespace(credential_totp_identifiers={"credential_param": "[email protected]"})

    assert block._resolve_totp_identifier(workflow_run_context) == "[email protected]"

    block_with_explicit_totp = TaskV2Block.model_construct(totp_identifier="[email protected]")
    assert block_with_explicit_totp._resolve_totp_identifier(workflow_run_context) == "[email protected]"