Skip to content

Automate browser based workflows with AI

2.5 KB
Raw Blame
"""Tests for _convert_to_response in credentials routes."""

from datetime import datetime

from skyvern.forge.sdk.routes.credentials import _convert_to_response
from skyvern.forge.sdk.schemas.credentials import (
    Credential,
    CredentialType,
    CredentialVaultType,
    PasswordCredential,
    TotpType,
)


def _make_credential(**overrides: object) -> Credential:
    defaults = {
        "credential_id": "cred_test",
        "organization_id": "org_test",
        "name": "Test Credential",
        "vault_type": CredentialVaultType.BITWARDEN,
        "item_id": "item_test",
        "credential_type": CredentialType.PASSWORD,
        "username": "[email protected]",
        "totp_type": TotpType.AUTHENTICATOR,
        "totp_identifier": None,
        "card_last4": None,
        "card_brand": None,
        "secret_label": None,
        "browser_profile_id": None,
        "auto_profile_disabled": None,
        "tested_url": None,
        "user_context": None,
        "save_browser_session_intent": False,
        "created_at": datetime(2026, 1, 1),
        "modified_at": datetime(2026, 1, 1),
        "deleted_at": None,
    }
    defaults.update(overrides)
    return Credential(**defaults)


def test_convert_to_response_includes_totp_identifier() -> None:
    credential = _make_credential(totp_identifier="login_otp")
    response = _convert_to_response(credential)
    assert response.credential.totp_identifier == "login_otp"


def test_convert_to_response_includes_auto_profile_disabled() -> None:
    assert _convert_to_response(_make_credential(auto_profile_disabled=True)).auto_profile_disabled is True
    assert _convert_to_response(_make_credential(auto_profile_disabled=False)).auto_profile_disabled is False
    assert _convert_to_response(_make_credential(auto_profile_disabled=None)).auto_profile_disabled is None


def test_convert_to_response_totp_identifier_none_when_not_set() -> None:
    credential = _make_credential(totp_identifier=None)
    response = _convert_to_response(credential)
    assert response.credential.totp_identifier is None


def test_convert_to_response_includes_totp_type() -> None:
    credential = _make_credential(totp_type=TotpType.EMAIL)
    response = _convert_to_response(credential)
    assert response.credential.totp_type == TotpType.EMAIL


def test_password_credential_normalizes_empty_metadata() -> None:
    credential = PasswordCredential(username="[email protected]", password="pw", metadata={})

    assert credential.metadata is None