@@ -203,7 +203,8 @@ def test_get_or_create_instance_with_service_token(self, mock_httpx):
203203
204204 @patch ("replicated.http_client.httpx.Client" )
205205 def test_ensure_instance_replaces_dynamic_token_from_api (self , mock_httpx ):
206- """Test that _ensure_instance replaces dynamic_token with service_token from API."""
206+ """Test that _ensure_instance replaces dynamic_token with
207+ service_token from API."""
207208 from replicated .resources import Instance
208209
209210 with tempfile .TemporaryDirectory () as tmpdir :
@@ -436,33 +437,12 @@ async def test_instance_uses_machine_id_in_headers(self):
436437 assert "X-Replicated-ClusterID" in headers
437438 assert headers ["X-Replicated-ClusterID" ] == client ._machine_id
438439
439- @pytest .mark .asyncio
440- async def test_instance_token_storage_and_retrieval (self ):
441- """Test that instance tokens can be stored and retrieved in async client."""
442- with tempfile .TemporaryDirectory () as tmpdir :
443- client = AsyncReplicatedClient (
444- publishable_key = "pk_test_123" ,
445- app_slug = "my-app" ,
446- state_directory = tmpdir ,
447- )
448-
449- # Store an instance token
450- client .state_manager .set_instance_token (
451- "instance_123" , "instance_token_abc"
452- )
453-
454- # Retrieve it
455- token = client .state_manager .get_instance_token ("instance_123" )
456- assert token == "instance_token_abc"
457-
458440 @pytest .mark .asyncio
459441 async def test_instance_with_service_account_token (self ):
460442 """Test that async instances can be created with a service account token."""
461443 from replicated .resources import AsyncInstance
462444
463- client = AsyncReplicatedClient (
464- publishable_key = "pk_test_123" , app_slug = "my-app"
465- )
445+ client = AsyncReplicatedClient (publishable_key = "pk_test_123" , app_slug = "my-app" )
466446 instance = AsyncInstance (
467447 client ,
468448 "customer_123" ,
@@ -503,71 +483,3 @@ async def test_get_or_create_instance_with_service_token(self):
503483 )
504484
505485 assert instance ._service_account_token == "instance_token_abc"
506-
507- @pytest .mark .asyncio
508- async def test_ensure_instance_stores_service_token_from_api (self ):
509- """Test that async _ensure_instance stores service_token from API response."""
510- from unittest .mock import AsyncMock
511-
512- from replicated .resources import AsyncInstance
513-
514- with tempfile .TemporaryDirectory () as tmpdir :
515- with patch ("replicated.http_client.httpx.AsyncClient" ) as mock_httpx :
516- mock_response = Mock ()
517- mock_response .is_success = True
518- mock_response .json .return_value = {
519- "instance_id" : "instance_789" ,
520- "service_token" : "api_returned_token_xyz" ,
521- }
522-
523- mock_client = Mock ()
524- mock_client .request = AsyncMock (return_value = mock_response )
525- mock_httpx .return_value = mock_client
526-
527- client = AsyncReplicatedClient (
528- publishable_key = "pk_test_123" ,
529- app_slug = "my-app" ,
530- state_directory = tmpdir ,
531- )
532- instance = AsyncInstance (client , "customer_123" )
533-
534- # Trigger instance creation
535- await instance ._ensure_instance ()
536-
537- # Verify token was stored
538- stored_token = client .state_manager .get_instance_token ("instance_789" )
539- assert stored_token == "api_returned_token_xyz"
540-
541- @pytest .mark .asyncio
542- async def test_auth_headers_prefer_instance_token (self ):
543- """Test that async _get_auth_headers prefers instance token over customer token."""
544- with tempfile .TemporaryDirectory () as tmpdir :
545- with patch ("replicated.http_client.httpx.AsyncClient" ) as mock_httpx :
546- mock_client = Mock ()
547- mock_httpx .return_value = mock_client
548-
549- client = AsyncReplicatedClient (
550- publishable_key = "pk_test_123" ,
551- app_slug = "my-app" ,
552- state_directory = tmpdir ,
553- )
554-
555- # Set customer-level token
556- client .state_manager .set_dynamic_token ("customer_token_abc" )
557-
558- # Set instance-level token
559- client .state_manager .set_instance_token (
560- "instance_123" , "instance_token_xyz"
561- )
562-
563- # Without instance_id, should use customer token
564- headers = client ._get_auth_headers ()
565- assert headers ["Authorization" ] == "customer_token_abc"
566-
567- # With instance_id, should prefer instance token
568- headers = client ._get_auth_headers (instance_id = "instance_123" )
569- assert headers ["Authorization" ] == "instance_token_xyz"
570-
571- # With non-existent instance_id, should fall back to customer token
572- headers = client ._get_auth_headers (instance_id = "instance_999" )
573- assert headers ["Authorization" ] == "customer_token_abc"
0 commit comments