From e981e90f898989e54f490c9bd0802428a3ebe075 Mon Sep 17 00:00:00 2001 From: Chandrapal Badshah <12944530+Chan9390@users.noreply.github.com> Date: Mon, 20 Oct 2025 20:17:50 +0530 Subject: [PATCH] test: add test for tenant defaults update on provider delete --- api/src/backend/api/tests/test_views.py | 47 ++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/api/src/backend/api/tests/test_views.py b/api/src/backend/api/tests/test_views.py index d1ac8142db..c07dcce71b 100644 --- a/api/src/backend/api/tests/test_views.py +++ b/api/src/backend/api/tests/test_views.py @@ -35,6 +35,9 @@ from api.db_router import MainRouter from api.models import ( Integration, Invitation, + LighthouseProviderConfiguration, + LighthouseProviderModels, + LighthouseTenantConfiguration, Membership, Processor, Provider, @@ -8559,11 +8562,6 @@ class TestLighthouseTenantConfigViewSet: self, mock_openai_client, authenticated_client, tenants_fixture ): """Test retrieving the singleton tenant config with proper provider and model validation""" - from api.models import ( - LighthouseProviderConfiguration, - LighthouseProviderModels, - LighthouseTenantConfiguration, - ) # Mock OpenAI client and models response mock_models_response = Mock() @@ -8934,3 +8932,42 @@ class TestLighthouseProviderConfigViewSet: assert get_full.status_code == status.HTTP_200_OK creds = get_full.json()["data"]["attributes"]["credentials"] assert creds["api_key"] == valid_key + + def test_delete_provider_updates_tenant_defaults( + self, authenticated_client, tenants_fixture + ): + """Deleting a provider config should clear tenant default_provider and its default_model entry.""" + + tenant = tenants_fixture[0] + + # Create provider configuration to delete + provider = LighthouseProviderConfiguration.objects.create( + tenant_id=tenant.id, + provider_type="openai", + credentials=b'{"api_key":"sk-test123T3BlbkFJ"}', + is_active=True, + ) + + # Seed tenant defaults referencing the provider we will delete + cfg = LighthouseTenantConfiguration.objects.create( + tenant_id=tenant.id, + business_context="Test", + default_provider="openai", + default_models={"openai": "gpt-4o", "other": "model-x"}, + ) + + # Delete via API and validate response + url = reverse("lighthouse-providers-detail", kwargs={"pk": str(provider.id)}) + resp = authenticated_client.delete(url) + assert resp.status_code in ( + status.HTTP_204_NO_CONTENT, + status.HTTP_200_OK, + ) + + # Tenant defaults should be updated + cfg.refresh_from_db() + assert cfg.default_provider == "" + assert "openai" not in cfg.default_models + + # Unrelated entries should remain untouched + assert cfg.default_models.get("other") == "model-x"