From 41b95da6e61e14b56f2dc33775e894b63194ce58 Mon Sep 17 00:00:00 2001 From: Chandrapal Badshah <12944530+Chan9390@users.noreply.github.com> Date: Wed, 1 Oct 2025 15:13:52 +0530 Subject: [PATCH] chore: add few more lighthouse provider config tests --- api/src/backend/api/tests/test_views.py | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/api/src/backend/api/tests/test_views.py b/api/src/backend/api/tests/test_views.py index aad7b41ef8..fbb6f0f891 100644 --- a/api/src/backend/api/tests/test_views.py +++ b/api/src/backend/api/tests/test_views.py @@ -8799,3 +8799,76 @@ class TestLighthouseProviderConfigViewSet: updated = patch_resp.json()["data"]["attributes"] assert updated["base_url"] == "https://api.example.com/v1" assert updated["is_active"] is False + + def test_openai_patch_invalid_credentials(self, authenticated_client): + """PATCH with invalid credentials.api_key should error (400)""" + valid_key = "sk-ok123T3BlbkFJok456" + create_payload = { + "data": { + "type": "lighthouse-providers", + "attributes": { + "provider_type": "openai", + "credentials": {"api_key": valid_key}, + }, + } + } + create_resp = authenticated_client.post( + reverse("lighthouse-providers-list"), + data=create_payload, + content_type=API_JSON_CONTENT_TYPE, + ) + assert create_resp.status_code == status.HTTP_201_CREATED + provider_id = create_resp.json()["data"]["id"] + + # Try patch with invalid api_key format + patch_payload = { + "data": { + "type": "lighthouse-providers", + "id": provider_id, + "attributes": { + "credentials": {"api_key": "sk-invalid"}, + }, + } + } + patch_resp = authenticated_client.patch( + reverse("lighthouse-providers-detail", kwargs={"pk": provider_id}), + data=patch_payload, + content_type=API_JSON_CONTENT_TYPE, + ) + assert patch_resp.status_code == status.HTTP_400_BAD_REQUEST + + def test_openai_get_masking_and_fields_filter(self, authenticated_client): + valid_key = "sk-get123T3BlbkFJget456" + create_payload = { + "data": { + "type": "lighthouse-providers", + "attributes": { + "provider_type": "openai", + "credentials": {"api_key": valid_key}, + }, + } + } + create_resp = authenticated_client.post( + reverse("lighthouse-providers-list"), + data=create_payload, + content_type=API_JSON_CONTENT_TYPE, + ) + assert create_resp.status_code == status.HTTP_201_CREATED + provider_id = create_resp.json()["data"]["id"] + + # Default GET should return masked credentials + get_resp = authenticated_client.get( + reverse("lighthouse-providers-detail", kwargs={"pk": provider_id}) + ) + assert get_resp.status_code == status.HTTP_200_OK + masked = get_resp.json()["data"]["attributes"]["credentials"]["api_key"] + assert masked == ("*" * len(valid_key)) + + # Fields filter should return decrypted credentials structure + get_full = authenticated_client.get( + reverse("lighthouse-providers-detail", kwargs={"pk": provider_id}) + + "?fields[lighthouse-providers]=credentials" + ) + assert get_full.status_code == status.HTTP_200_OK + creds = get_full.json()["data"]["attributes"]["credentials"] + assert creds["api_key"] == valid_key