Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Complete imported 1Password credential surface parity (#8951)
Aaron Perez34400ad65e23
diff --git a/skyvern/forge/sdk/routes/credentials.py b/skyvern/forge/sdk/routes/credentials.pyindex b050f8fdb..89d0496e4 100644--- a/skyvern/forge/sdk/routes/credentials.py+++ b/skyvern/forge/sdk/routes/credentials.py@@ -49,6 +49,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom skyvern.config import settingsfrom skyvern.exceptions import (BrowserProfileNotFound,+ CredentialSourceNotConfiguredError,)from skyvern.exceptions import HttpException as SkyvernHttpExceptionfrom skyvern.exceptions import (@@ -2567,6 +2568,15 @@ async def get_credential_totp_code(detail="Custom credential service is not configured for this organization",) from eexcept Exception as e:+ if vault_type == CredentialVaultType.ONEPASSWORD:+ http_error = _onepassword_error_response(e, fallback_detail="Unable to retrieve credential from vault")+ if http_error.status_code == 500:+ LOG.exception(+ "Failed to retrieve credential item for TOTP code preview",+ credential_id=credential_id,+ organization_id=current_org.organization_id,+ )+ raise http_error from eLOG.exception("Failed to retrieve credential item for TOTP code preview",credential_id=credential_id,@@ -3067,6 +3077,8 @@ async def get_onepassword_token_status(def _onepassword_error_response(error: Exception, *, fallback_detail: str) -> HTTPException:+ if isinstance(error, CredentialSourceNotConfiguredError):+ return HTTPException(status_code=400, detail=str(error))if isinstance(error, TimeoutError):return HTTPException(status_code=502,diff --git a/tests/unit/test_credential_totp_code.py b/tests/unit/test_credential_totp_code.pyindex 875c9732e..87340e76f 100644--- a/tests/unit/test_credential_totp_code.py+++ b/tests/unit/test_credential_totp_code.py@@ -1,15 +1,24 @@+from collections.abc import Callablefrom types import SimpleNamespacefrom unittest.mock import AsyncMock, Mockfrom urllib.parse import quoteimport pyotpimport pytest+import structlog.testingfrom fastapi import HTTPException, Response+from skyvern.exceptions import (+ CredentialSourceNotConfiguredError,+ OnePasswordGetItemError,+ OnePasswordRateLimitError,+ OnePasswordServiceUnavailableError,+)from skyvern.forge.sdk.routes import credentialsfrom skyvern.forge.sdk.schemas.credentials import (CredentialItem,CredentialType,+ CredentialVaultType,NonEmptyPasswordCredential,PasswordCredential,TotpType,@@ -28,12 +37,13 @@ def _mock_totp_preview_dependencies(secret: str | None,credential_id: str = "cred_test",organization_id: str = "org_test",+ vault_type: CredentialVaultType | None = None,) -> tuple[SimpleNamespace, SimpleNamespace, SimpleNamespace]:db_credential = SimpleNamespace(credential_id=credential_id,organization_id=organization_id,name="Example",- vault_type=None,+ vault_type=vault_type,item_id="item_test",credential_type=CredentialType.PASSWORD,totp_type=TotpType.AUTHENTICATOR,@@ -426,3 +436,87 @@ async def test_get_credential_totp_code_sets_no_store_headers(monkeypatch: pytesassert response.headers["Cache-Control"] == "no-store"assert response.headers["Pragma"] == "no-cache"+++ ("error_factory", "status_code", "detail"),+ [+ pytest.param(+ lambda: CredentialSourceNotConfiguredError(+ "1Password is not configured for this organization. Add a 1Password service account token in Settings."+ ),+ 400,+ "1Password is not configured for this organization. Add a 1Password service account token in Settings.",+ id="missing-configuration",+ ),+ pytest.param(+ lambda: OnePasswordRateLimitError("429"),+ 429,+ "1Password rate limit exceeded. Please retry in a few minutes.",+ id="rate-limited",+ ),+ pytest.param(+ lambda: OnePasswordServiceUnavailableError(status_code=503),+ 502,+ "1Password is temporarily unavailable. Please retry in a few minutes.",+ id="unavailable",+ ),+ pytest.param(+ lambda: OnePasswordGetItemError("service account token is invalid"),+ 400,+ "Your 1Password service account token appears to be invalid or expired. Please update it in Settings and try again.",+ id="invalid-token",+ ),+ ],+)+async def test_onepassword_totp_preview_maps_item_list_failures(+ monkeypatch: pytest.MonkeyPatch,+ error_factory: Callable[[], Exception],+ status_code: int,+ detail: str,+) -> None:+ _db_credential, vault_service, _mock_credentials = _mock_totp_preview_dependencies(+ monkeypatch,+ secret="JBSWY3DPEHPK3PXP",+ vault_type=CredentialVaultType.ONEPASSWORD,+ )+ vault_service.get_credential_item.side_effect = error_factory()++ with pytest.raises(HTTPException) as exc_info:+ await credentials.get_credential_totp_code(+ response=Response(),+ credential_id="cred_test",+ current_org=SimpleNamespace(organization_id="org_test"),+ )++ assert exc_info.value.status_code == status_code+ assert exc_info.value.detail == detail+++async def test_onepassword_totp_preview_logs_unmapped_failure_without_secret(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _db_credential, vault_service, _mock_credentials = _mock_totp_preview_dependencies(+ monkeypatch,+ secret="JBSWY3DPEHPK3PXP",+ vault_type=CredentialVaultType.ONEPASSWORD,+ )+ vault_service.get_credential_item.side_effect = RuntimeError("unexpected provider failure")++ with structlog.testing.capture_logs() as logs, pytest.raises(HTTPException) as exc_info:+ await credentials.get_credential_totp_code(+ response=Response(),+ credential_id="cred_test",+ current_org=SimpleNamespace(organization_id="org_test"),+ )++ assert exc_info.value.status_code == 500+ assert exc_info.value.detail == "Unable to retrieve credential from vault"+ error_logs = [record for record in logs if record.get("log_level") == "error"]+ assert len(error_logs) == 1+ assert error_logs[0]["event"] == "Failed to retrieve credential item for TOTP code preview"+ assert error_logs[0]["organization_id"] == "org_test"+ assert error_logs[0]["credential_id"] == "cred_test"+ assert "JBSWY3DPEHPK3PXP" not in repr(error_logs[0])diff --git a/tests/unit/test_mcp_browser_tools.py b/tests/unit/test_mcp_browser_tools.pyindex b6b8f8fc6..932abce90 100644--- a/tests/unit/test_mcp_browser_tools.py+++ b/tests/unit/test_mcp_browser_tools.py@@ -179,12 +179,13 @@ async def test_skyvern_login_success_returns_run_data(monkeypatch: pytest.Monkeyresult = await mcp_browser.skyvern_login(credential_type="skyvern",- credential_id="cred_123",+ credential_id="cred_onepassword_imported",)assert result["ok"] is Trueassert result["data"]["run_id"] == "wr_123"assert result["data"]["status"] == "completed"+ assert page.agent.login.await_args.kwargs["credential_id"] == "cred_onepassword_imported"@pytest.mark.asynciodiff --git a/tests/unit/test_mcp_credential_provider_tools.py b/tests/unit/test_mcp_credential_provider_tools.pyindex 0072c1a8c..daf099284 100644--- a/tests/unit/test_mcp_credential_provider_tools.py+++ b/tests/unit/test_mcp_credential_provider_tools.py@@ -1,5 +1,6 @@from __future__ import annotations+from types import SimpleNamespacefrom unittest.mock import AsyncMockimport pytest@@ -10,6 +11,34 @@ from skyvern.cli.mcp_tools import mcpfrom skyvern.client.errors import NotFoundError+async def test_credential_list_includes_imported_onepassword_row_without_secret(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ imported = SimpleNamespace(+ credential_id="cred_onepassword_imported",+ name="Imported login",+ credential_type="password",+ credential=SimpleNamespace(username="imported-user", password="runtime-only-secret", totp_type=None),+ )+ get_credentials = AsyncMock(return_value=[imported])+ monkeypatch.setattr(credential_tools, "get_skyvern", lambda: SimpleNamespace(get_credentials=get_credentials))++ result = await credential_tools.skyvern_credential_list()++ get_credentials.assert_awaited_once_with(page=1, page_size=10)+ assert result["data"]["credentials"] == [+ {+ "credential_id": "cred_onepassword_imported",+ "name": "Imported login",+ "credential_type": "password",+ "username": "imported-user",+ "totp_type": None,+ }+ ]+ assert "runtime-only-secret" not in str(result)++@pytest.mark.asyncioasync def test_onepassword_items_lists_metadata_with_filters(monkeypatch: pytest.MonkeyPatch) -> None:raw_http_get = AsyncMock(