From 3e92af063ddfb67f9bb7e81aad4c8f1680ed4d94 Mon Sep 17 00:00:00 2001 From: Chandrapal Badshah <12944530+Chan9390@users.noreply.github.com> Date: Thu, 2 Oct 2025 13:38:08 +0530 Subject: [PATCH] fix: update test case to mock llm provider config and models --- api/src/backend/api/tests/test_views.py | 46 ++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/api/src/backend/api/tests/test_views.py b/api/src/backend/api/tests/test_views.py index fbb6f0f891..65bcb7027c 100644 --- a/api/src/backend/api/tests/test_views.py +++ b/api/src/backend/api/tests/test_views.py @@ -8528,13 +8528,49 @@ class TestLighthouseTenantConfigViewSet: assert response.status_code == status.HTTP_400_BAD_REQUEST assert "already exists" in str(response.json()).lower() + @patch("openai.OpenAI") def test_lighthouse_tenant_config_retrieve( - self, authenticated_client, tenants_fixture + self, mock_openai_client, authenticated_client, tenants_fixture ): - """Test retrieving the singleton tenant config""" - from api.models import LighthouseTenantConfiguration + """Test retrieving the singleton tenant config with proper provider and model validation""" + from api.models import ( + LighthouseProviderConfiguration, + LighthouseProviderModels, + LighthouseTenantConfiguration, + ) - # Create config first + # Mock OpenAI client and models response + mock_models_response = Mock() + mock_models_response.data = [ + Mock(id="gpt-4o"), + Mock(id="gpt-4o-mini"), + Mock(id="gpt-5"), + ] + mock_openai_client.return_value.models.list.return_value = mock_models_response + + # Create OpenAI provider configuration + provider_config = LighthouseProviderConfiguration.objects.create( + tenant_id=tenants_fixture[0].id, + provider_type="openai", + credentials=b'{"api_key": "sk-test1234567890T3BlbkFJtest1234567890"}', + is_active=True, + ) + + # Create provider models (simulating refresh) + LighthouseProviderModels.objects.create( + tenant_id=tenants_fixture[0].id, + provider_configuration=provider_config, + model_id="gpt-4o", + default_parameters={}, + ) + LighthouseProviderModels.objects.create( + tenant_id=tenants_fixture[0].id, + provider_configuration=provider_config, + model_id="gpt-4o-mini", + default_parameters={}, + ) + + # Create tenant configuration with valid provider and model config = LighthouseTenantConfiguration.objects.create( tenant_id=tenants_fixture[0].id, business_context="Test context", @@ -8542,7 +8578,7 @@ class TestLighthouseTenantConfigViewSet: default_models={"openai": "gpt-4o"}, ) - # Retrieve it + # Retrieve and verify the configuration response = authenticated_client.get(reverse("lighthouse-config")) assert response.status_code == status.HTTP_200_OK data = response.json()["data"]