1111 CursorAgentIntegration ,
1212 CursorAgentIntegrationProvider ,
1313)
14- from sentry .shared_integrations .exceptions import IntegrationConfigurationError
14+ from sentry .shared_integrations .exceptions import ApiError , IntegrationConfigurationError
1515from sentry .testutils .cases import IntegrationTestCase
1616from sentry .testutils .silo import assume_test_silo_mode_of
1717
@@ -38,6 +38,63 @@ def test_build_integration_stores_metadata(provider):
3838 assert metadata ["webhook_secret" ] == "hook-secret"
3939
4040
41+ def test_build_integration_fetches_and_stores_api_key_metadata (provider ):
42+ """Test that build_integration fetches metadata from /v0/me and stores it"""
43+ from sentry .integrations .cursor .models import CursorApiKeyMetadata
44+
45+ fake_uuid = UUID ("22222222-3333-4444-5555-666666666666" )
46+ mock_metadata = CursorApiKeyMetadata (
47+ apiKeyName = "Production API Key" ,
48+ createdAt = "2024-01-15T10:30:00Z" ,
49+ 50+ )
51+
52+ with (
53+ patch ("sentry.integrations.cursor.integration.uuid.uuid4" , return_value = fake_uuid ),
54+ patch ("sentry.integrations.cursor.integration.generate_token" , return_value = "hook-secret" ),
55+ patch (
56+ "sentry.integrations.cursor.client.CursorAgentClient.get_api_key_metadata"
57+ ) as mock_get_metadata ,
58+ ):
59+ mock_get_metadata .return_value = mock_metadata
60+ integration_data = provider .build_integration (state = {"config" : {"api_key" : "cursor-api" }})
61+
62+ # Verify metadata was fetched
63+ mock_get_metadata .assert_called_once ()
64+
65+ # Verify metadata is stored
66+ metadata = integration_data ["metadata" ]
67+ assert metadata ["api_key_name" ] == "Production API Key"
68+ assert metadata [
"user_email" ]
== "[email protected] " 69+
70+ # Verify integration name includes API key name
71+ assert (
72+ integration_data [
"name" ]
== "Cursor Cloud Agent - [email protected] /Production API Key" 73+ )
74+
75+
76+ def test_build_integration_fallback_on_metadata_fetch_failure (provider ):
77+ """Test that build_integration falls back gracefully if metadata fetch fails"""
78+ fake_uuid = UUID ("33333333-4444-5555-6666-777777777777" )
79+
80+ with (
81+ patch ("sentry.integrations.cursor.integration.uuid.uuid4" , return_value = fake_uuid ),
82+ patch ("sentry.integrations.cursor.integration.generate_token" , return_value = "hook-secret" ),
83+ patch (
84+ "sentry.integrations.cursor.client.CursorAgentClient.get_api_key_metadata"
85+ ) as mock_get_metadata ,
86+ ):
87+ # Simulate API call failure
88+ mock_get_metadata .side_effect = ApiError ("API Error" , 500 )
89+ integration_data = provider .build_integration (state = {"config" : {"api_key" : "cursor-api" }})
90+
91+ # Verify integration was still created with fallback name
92+ assert integration_data ["name" ] == "Cursor Cloud Agent"
93+ metadata = integration_data ["metadata" ]
94+ assert metadata ["api_key_name" ] is None
95+ assert metadata ["user_email" ] is None
96+
97+
4198def test_build_integration_stores_api_key_and_webhook_secret (provider ):
4299 """Test that build_integration stores both API key and webhook secret"""
43100 integration_data = provider .build_integration (state = {"config" : {"api_key" : "new-api" }})
@@ -266,7 +323,7 @@ def test_build_integration_creates_unique_installations(self):
266323
267324 # All should have the same basic structure
268325 for integration_dict in [integration_dict_1 , integration_dict_2 , integration_dict_3 ]:
269- assert integration_dict ["name" ] == "Cursor Agent"
326+ assert integration_dict ["name" ] == "Cursor Cloud Agent"
270327 assert "external_id" in integration_dict
271328 assert "metadata" in integration_dict
272329 assert integration_dict ["metadata" ]["domain_name" ] == "cursor.sh"
@@ -280,3 +337,53 @@ def test_build_integration_creates_unique_installations(self):
280337
281338 webhook_secrets = {webhook_secret_1 , webhook_secret_2 , webhook_secret_3 }
282339 assert len (webhook_secrets ) == 3 , "Each integration should have a unique webhook secret"
340+
341+ def test_get_dynamic_display_information (self ):
342+ """Test that get_dynamic_display_information returns metadata"""
343+ integration = self .create_integration (
344+ organization = self .organization ,
345+ provider = "cursor" ,
346+ name = "Cursor Agent - Production Key" ,
347+ external_id = "cursor" ,
348+ metadata = {
349+ "api_key" : "test_api_key" ,
350+ "webhook_secret" : "test_secret" ,
351+ "domain_name" : "cursor.sh" ,
352+ "api_key_name" : "Production Key" ,
353+ "user_email" :
"[email protected] " ,
354+ },
355+ )
356+
357+ installation = cast (
358+ CursorAgentIntegration ,
359+ integration .get_installation (organization_id = self .organization .id ),
360+ )
361+
362+ display_info = installation .get_dynamic_display_information ()
363+
364+ assert display_info is not None
365+ assert display_info ["api_key_name" ] == "Production Key"
366+ assert display_info [
"user_email" ]
== "[email protected] " 367+
368+ def test_get_dynamic_display_information_returns_none_when_no_metadata (self ):
369+ """Test that get_dynamic_display_information returns None when metadata is missing"""
370+ integration = self .create_integration (
371+ organization = self .organization ,
372+ provider = "cursor" ,
373+ name = "Cursor Agent" ,
374+ external_id = "cursor" ,
375+ metadata = {
376+ "api_key" : "test_api_key" ,
377+ "webhook_secret" : "test_secret" ,
378+ "domain_name" : "cursor.sh" ,
379+ },
380+ )
381+
382+ installation = cast (
383+ CursorAgentIntegration ,
384+ integration .get_installation (organization_id = self .organization .id ),
385+ )
386+
387+ display_info = installation .get_dynamic_display_information ()
388+
389+ assert display_info is None
0 commit comments