From 943652eca70bd730d870123315c4a72c180ec328 Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Wed, 22 Oct 2025 11:39:27 +0530 Subject: [PATCH 1/3] Revert to old version when creds return none --- backend/app/crud/credentials.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/backend/app/crud/credentials.py b/backend/app/crud/credentials.py index f7601cc5..18f9b3bb 100644 --- a/backend/app/crud/credentials.py +++ b/backend/app/crud/credentials.py @@ -115,16 +115,6 @@ def get_creds_by_org( Credential.project_id == project_id, ) creds = session.exec(statement).all() - - if not creds: - logger.error( - f"[get_creds_by_org] No credentials found | organization_id {org_id}, project_id {project_id}" - ) - raise HTTPException( - status_code=404, - detail="Credentials not found for this organization and project", - ) - return creds @@ -166,12 +156,7 @@ def get_provider_credential( if creds and creds.credential: return creds if full else decrypt_credentials(creds.credential) - logger.error( - f"[get_provider_credential] Credentials not found | organization_id {org_id}, provider {provider}, project_id {project_id}" - ) - raise HTTPException( - status_code=404, detail=f"Credentials not found for provider '{provider}'" - ) + return None def get_providers(*, session: Session, org_id: int, project_id: int) -> list[str]: @@ -234,12 +219,16 @@ def remove_provider_credential( validate_provider(provider) # Verify credentials exist before attempting delete - get_provider_credential( + creds = get_provider_credential( session=session, org_id=org_id, project_id=project_id, provider=provider, ) + if creds is None: + raise HTTPException( + status_code=404, detail="Credentials not found for this provider" + ) # Build delete statement statement = delete(Credential).where( @@ -279,6 +268,10 @@ def remove_creds_for_org(*, session: Session, org_id: int, project_id: int) -> N org_id=org_id, project_id=project_id, ) + if existing_creds is None or len(existing_creds) == 0: + raise HTTPException( + status_code=404, detail="No credentials found for this organization" + ) expected_count = len(existing_creds) statement = delete(Credential).where( Credential.organization_id == org_id, From 779e99ae35890ab83372e46bc0b4b877d74c0c56 Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Wed, 22 Oct 2025 11:46:11 +0530 Subject: [PATCH 2/3] fix return type --- backend/app/crud/credentials.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/crud/credentials.py b/backend/app/crud/credentials.py index 18f9b3bb..f430ed71 100644 --- a/backend/app/crud/credentials.py +++ b/backend/app/crud/credentials.py @@ -125,7 +125,7 @@ def get_provider_credential( project_id: int, provider: str, full: bool = False, -) -> dict[str, Any] | Credential: +) -> dict[str, Any] | Credential | None: """ Fetch credentials for a specific provider within a project. From f9ab58a89d0f22a81bd660666cbdd6133c560e91 Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Wed, 22 Oct 2025 12:20:40 +0530 Subject: [PATCH 3/3] fix test --- backend/app/api/routes/credentials.py | 6 +++++ backend/app/tests/api/routes/test_creds.py | 8 ------- backend/app/tests/crud/test_credentials.py | 26 +++++++++------------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/backend/app/api/routes/credentials.py b/backend/app/api/routes/credentials.py index 012d05ea..e2f43cd8 100644 --- a/backend/app/api/routes/credentials.py +++ b/backend/app/api/routes/credentials.py @@ -66,6 +66,9 @@ def read_credential( org_id=_current_user.organization_id, project_id=_current_user.project_id, ) + if not creds: + raise HTTPException(status_code=404, detail="Credentials not found") + return APIResponse.success_response([cred.to_public() for cred in creds]) @@ -88,6 +91,9 @@ def read_provider_credential( provider=provider_enum, project_id=_current_user.project_id, ) + if credential is None: + raise HTTPException(status_code=404, detail="Provider credentials not found") + return APIResponse.success_response(credential) diff --git a/backend/app/tests/api/routes/test_creds.py b/backend/app/tests/api/routes/test_creds.py index 34a897a5..83c62b7f 100644 --- a/backend/app/tests/api/routes/test_creds.py +++ b/backend/app/tests/api/routes/test_creds.py @@ -126,7 +126,6 @@ def test_read_credentials_not_found(client: TestClient, user_api_key: TestAuthCo headers={"X-API-KEY": user_api_key.key}, ) assert response.status_code == 404 - assert "Credentials not found" in response.json()["error"] def test_read_provider_credential( @@ -159,7 +158,6 @@ def test_read_provider_credential_not_found( ) assert response.status_code == 404 - assert "Credentials not found for provider" in response.json()["error"] def test_update_credentials( @@ -258,7 +256,6 @@ def test_delete_provider_credential_not_found( ) assert response.status_code == 404 - assert "Credentials not found for provider" in response.json()["error"] def test_delete_all_credentials( @@ -285,7 +282,6 @@ def test_delete_all_credentials( headers={"X-API-KEY": user_api_key.key}, ) assert response.status_code == 404 # Expect 404 as credentials are deleted - assert "Credentials not found" in response.json()["error"] def test_delete_all_credentials_not_found( @@ -301,10 +297,6 @@ def test_delete_all_credentials_not_found( ) assert response.status_code == 404 - assert ( - "Credentials not found for this organization and project" - in response.json()["error"] - ) def test_duplicate_credential_creation( diff --git a/backend/app/tests/crud/test_credentials.py b/backend/app/tests/crud/test_credentials.py index 1116bc35..99dac0a3 100644 --- a/backend/app/tests/crud/test_credentials.py +++ b/backend/app/tests/crud/test_credentials.py @@ -168,15 +168,13 @@ def test_remove_provider_credential(db: Session) -> None: project_id=project.id, ) - # Verify the credentials are no longer retrievable - with pytest.raises(HTTPException) as exc_info: - get_provider_credential( - session=db, - org_id=credential.organization_id, - provider="openai", - project_id=project.id, - ) - assert exc_info.value.status_code == 404 + creds = get_provider_credential( + session=db, + org_id=credential.organization_id, + provider="openai", + project_id=project.id, + ) + assert creds is None def test_remove_creds_for_org(db: Session) -> None: @@ -209,12 +207,10 @@ def test_remove_creds_for_org(db: Session) -> None: session=db, org_id=project.organization_id, project_id=project.id ) - # Verify no credentials are retrievable - with pytest.raises(HTTPException) as exc_info: - get_creds_by_org( - session=db, org_id=project.organization_id, project_id=project.id - ) - assert exc_info.value.status_code == 404 + creds = get_creds_by_org( + session=db, org_id=project.organization_id, project_id=project.id + ) + assert creds == [] def test_invalid_provider(db: Session) -> None: