From b2df23f7184710718d0d18d90393c421513c9155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Volf?= Date: Tue, 10 Nov 2026 15:42:29 +0100 Subject: [PATCH 01/13] fix: use run token in metamorph test, as metamorphing from outside is not allowed anymore --- tests/_utils.py | 32 +++++++++- tests/integration/conftest.py | 21 +------ tests/integration/test_run.py | 107 ++++++++++++++++++++++------------ 3 files changed, 104 insertions(+), 56 deletions(-) diff --git a/tests/_utils.py b/tests/_utils.py index 52cd8031..2e50801d 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -2,15 +2,20 @@ import asyncio import inspect +import os import secrets import string import time -from collections.abc import AsyncIterator, Iterator +from collections.abc import AsyncGenerator, AsyncIterator, Iterator +from contextlib import asynccontextmanager from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, overload import pytest +from apify_client import ApifyClient, ApifyClientAsync +from apify_client._consts import DEFAULT_API_URL + if TYPE_CHECKING: from collections.abc import Awaitable, Callable @@ -65,6 +70,31 @@ class HttpClientClasses: async_: type[HttpClientAsync] +@asynccontextmanager +async def make_client( + token: str, client_type: str, http_client_classes: HttpClientClasses +) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: + """Build a sync or async client with the given token and HTTP client implementation, closing it on exit.""" + api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + if client_type == 'sync': + http_client = http_client_classes.sync() + yield ApifyClient.with_custom_http_client( + token, + api_url=api_url, + http_client=http_client, + ) + http_client.close() + return + + http_client_async = http_client_classes.async_() + yield ApifyClientAsync.with_custom_http_client( + token, + api_url=api_url, + http_client=http_client_async, + ) + await http_client_async.aclose() + + def get_crypto_random_object_id(length: int = 17) -> str: """Generate a cryptographically secure random object ID.""" chars = 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789' diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index b57a8647..29a6f5d4 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -14,6 +14,7 @@ HttpClientClasses, KvsFixture, get_crypto_random_object_id, + make_client, ) from apify_client import ApifyClient, ApifyClientAsync from apify_client._consts import DEFAULT_API_URL @@ -132,24 +133,8 @@ async def client( http_client_classes: HttpClientClasses, ) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: """Return each sync/async and HTTP client implementation combination.""" - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL - if client_type == 'sync': - http_client = http_client_classes.sync() - yield ApifyClient.with_custom_http_client( - api_token, - api_url=api_url, - http_client=http_client, - ) - http_client.close() - return - - http_client_async = http_client_classes.async_() - yield ApifyClientAsync.with_custom_http_client( - api_token, - api_url=api_url, - http_client=http_client_async, - ) - await http_client_async.aclose() + async with make_client(api_token, client_type, http_client_classes) as client: + yield client @pytest.fixture diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index c5e0a46d..e0697a95 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -6,7 +6,7 @@ from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING -from .._utils import get_random_resource_name, maybe_await, poll_until_condition +from .._utils import HttpClientClasses, get_random_resource_name, make_client, maybe_await, poll_until_condition from apify_client._models import ( ActorResource, Build, @@ -316,48 +316,81 @@ async def test_run_runs_client(client: ApifyClient | ApifyClientAsync) -> None: assert first_run.act_id is not None -async def test_run_metamorph(client: ApifyClient | ApifyClientAsync) -> None: - """Test metamorphing a run into another Actor.""" - # Start an actor that will run long enough to metamorph. We use hello-world and try to metamorph it into itself - actor = client.actor(HELLO_WORLD_ACTOR) - run = await maybe_await(actor.start()) - assert isinstance(run, Run) - assert run.id is not None - - run_client = client.run(run.id) - - try: - # Wait until the run starts (leaves READY), with exponential backoff: container startup time varies widely - async def get_run() -> Run | None: - return await maybe_await(run_client.get()) - - await poll_until_condition( - get_run, lambda run: isinstance(run, Run) and run.status != 'READY', backoff_factor=2 +async def test_run_metamorph(client_type: str, http_client_classes: HttpClientClasses, api_token: str) -> None: + """Test metamorphing a run into another Actor. + + We have to cheat a bit - metamorphing from outside is disallowed, so we need the Actor to leak its run token into + the key-value store, which we then use to construct a new client and do the test we want. + """ + async with make_client(api_token, client_type, http_client_classes) as default_client: + created_actor = await maybe_await( + default_client.actors().create( + name=get_random_resource_name('actor'), + versions=[ + { + 'versionNumber': '0.0', + 'sourceType': 'SOURCE_FILES', + 'buildTag': 'latest', + # The Actor stores its own run token in its key-value store, so the test can metamorph with it. + 'sourceFiles': [ + { + 'name': 'Dockerfile', + 'format': 'TEXT', + 'content': 'FROM apify/actor-node:22\nCOPY . ./\nCMD ["node", "main.js"]\n', + }, + { + 'name': 'main.js', + 'format': 'TEXT', + 'content': """ + const { Actor } = require('apify'); + Actor.main(async () => { + await Actor.setValue( + 'TOKEN', process.env.APIFY_TOKEN, { contentType: 'text/plain' } + ); + await new Promise((resolve) => setTimeout(resolve, 60000)); + }); + """, + }, + ], + } + ], + ) ) + assert isinstance(created_actor, ActorResource) + actor_client = default_client.actor(created_actor.id) + run_client = None - # Metamorph the run into the same actor (allowed) with new input - # Note: hello-world may finish before we can metamorph, so we handle that case try: - metamorphed_run = await maybe_await( - run_client.metamorph( - target_actor_id=HELLO_WORLD_ACTOR, - run_input={'message': 'Hello from metamorph!'}, + # build and run the Actor + started_build = await maybe_await(actor_client.build(version_number='0.0')) + assert isinstance(started_build, Build) + await maybe_await(default_client.build(started_build.id).wait_for_finish()) + run = await maybe_await(actor_client.start()) + assert isinstance(run, Run) + run_client = default_client.run(run.id) + + # get the run token + async def get_token_record() -> dict | None: + return await maybe_await( + default_client.key_value_store(run.default_key_value_store_id).get_record('TOKEN') ) - ) - assert isinstance(metamorphed_run, Run) - assert metamorphed_run.id == run.id # Same run ID - # Wait for the metamorphed run to finish - final_run = await maybe_await(run_client.wait_for_finish()) - assert isinstance(final_run, Run) - except ApifyApiError as exc: - # If the actor finished before we could metamorph, that's OK - the test still verified the API call - if 'already finished' not in str(exc): - raise + record = await poll_until_condition(get_token_record, lambda rec: rec is not None, backoff_factor=2) + assert record is not None - finally: - await maybe_await(run_client.wait_for_finish()) - await maybe_await(run_client.delete()) + # the actual test + async with make_client(record['value'], client_type, http_client_classes) as run_token_client: + metamorphed_run = await maybe_await( + run_token_client.run(run.id).metamorph(target_actor_id=HELLO_WORLD_ACTOR) + ) + assert isinstance(metamorphed_run, Run) + assert metamorphed_run.id == run.id + finally: + if run_client is not None: + await maybe_await(run_client.abort()) + await maybe_await(run_client.wait_for_finish()) + await maybe_await(run_client.delete()) + await maybe_await(actor_client.delete()) async def test_run_reboot(client: ApifyClient | ApifyClientAsync) -> None: From 2a05cc88987921b3b78d002e26c5a2c8ea422235 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 09:42:09 +0100 Subject: [PATCH 02/13] test: share the metamorph Actor and address review comments --- tests/_utils.py | 28 +++--- tests/integration/test_run.py | 168 +++++++++++++++++++--------------- 2 files changed, 111 insertions(+), 85 deletions(-) diff --git a/tests/_utils.py b/tests/_utils.py index 2e50801d..0ab4be67 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -78,21 +78,25 @@ async def make_client( api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL if client_type == 'sync': http_client = http_client_classes.sync() - yield ApifyClient.with_custom_http_client( - token, - api_url=api_url, - http_client=http_client, - ) - http_client.close() + try: + yield ApifyClient.with_custom_http_client( + token, + api_url=api_url, + http_client=http_client, + ) + finally: + http_client.close() return http_client_async = http_client_classes.async_() - yield ApifyClientAsync.with_custom_http_client( - token, - api_url=api_url, - http_client=http_client_async, - ) - await http_client_async.aclose() + try: + yield ApifyClientAsync.with_custom_http_client( + token, + api_url=api_url, + http_client=http_client_async, + ) + finally: + await http_client_async.aclose() def get_crypto_random_object_id(length: int = 17) -> str: diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index e0697a95..bd3036bf 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -2,11 +2,23 @@ from __future__ import annotations -from collections.abc import AsyncIterator, Iterator +import os +from collections.abc import AsyncIterator, Generator, Iterator from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING -from .._utils import HttpClientClasses, get_random_resource_name, make_client, maybe_await, poll_until_condition +import pytest + +from .._utils import ( + API_URL_ENV_VAR, + HttpClientClasses, + get_random_resource_name, + make_client, + maybe_await, + poll_until_condition, +) +from apify_client import ApifyClient +from apify_client._consts import DEFAULT_API_URL from apify_client._models import ( ActorResource, Build, @@ -20,7 +32,7 @@ from apify_client.errors import ApifyApiError if TYPE_CHECKING: - from apify_client import ApifyClient, ApifyClientAsync + from apify_client import ApifyClientAsync HELLO_WORLD_ACTOR = 'apify/hello-world' @@ -53,6 +65,27 @@ """Source of an Actor that pushes `LIVE_ITEM_COUNT` items tagged with its run ID to its dataset, one per second.""" +METAMORPH_SOURCE_FILES = [ + { + 'name': 'Dockerfile', + 'format': 'TEXT', + 'content': 'FROM apify/actor-node:22\nCOPY . ./\nCMD ["node", "main.js"]\n', + }, + { + 'name': 'main.js', + 'format': 'TEXT', + 'content': """ +const { Actor } = require('apify'); +Actor.main(async () => { + await Actor.setValue('TOKEN', process.env.APIFY_TOKEN, { contentType: 'text/plain' }); + await new Promise((resolve) => setTimeout(resolve, 60000)); +}); +""", + }, +] +"""Source of an Actor that stores its run token under `TOKEN` in its key-value store, then waits for 60 seconds.""" + + async def test_run_collection_list_multiple_statuses(client: ApifyClient | ApifyClientAsync) -> None: """Test listing runs with multiple statuses.""" created_runs = list[Run]() @@ -316,81 +349,70 @@ async def test_run_runs_client(client: ApifyClient | ApifyClientAsync) -> None: assert first_run.act_id is not None -async def test_run_metamorph(client_type: str, http_client_classes: HttpClientClasses, api_token: str) -> None: +@pytest.fixture(scope='module') +def metamorph_actor_id(api_token: str) -> Generator[str]: + """Create and build an Actor that stores its run token, and delete it after the module's tests.""" + client = ApifyClient(api_token, api_url=os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL) + actor = client.actors().create( + name=get_random_resource_name('actor'), + versions=[ + { + 'versionNumber': '0.0', + 'sourceType': 'SOURCE_FILES', + 'buildTag': 'latest', + 'sourceFiles': METAMORPH_SOURCE_FILES, + } + ], + ) + actor_client = client.actor(actor.id) + + try: + build = actor_client.build(version_number='0.0') + finished_build = client.build(build.id).wait_for_finish() + assert finished_build is not None + assert finished_build.status == 'SUCCEEDED' + yield actor.id + finally: + actor_client.delete() + + +async def test_run_metamorph( + client: ApifyClient | ApifyClientAsync, + client_type: str, + http_client_classes: HttpClientClasses, + metamorph_actor_id: str, +) -> None: """Test metamorphing a run into another Actor. - We have to cheat a bit - metamorphing from outside is disallowed, so we need the Actor to leak its run token into - the key-value store, which we then use to construct a new client and do the test we want. + A metamorph must use the run's own token, so the Actor writes `APIFY_TOKEN` to its key-value store and the test + reads it from there. """ - async with make_client(api_token, client_type, http_client_classes) as default_client: - created_actor = await maybe_await( - default_client.actors().create( - name=get_random_resource_name('actor'), - versions=[ - { - 'versionNumber': '0.0', - 'sourceType': 'SOURCE_FILES', - 'buildTag': 'latest', - # The Actor stores its own run token in its key-value store, so the test can metamorph with it. - 'sourceFiles': [ - { - 'name': 'Dockerfile', - 'format': 'TEXT', - 'content': 'FROM apify/actor-node:22\nCOPY . ./\nCMD ["node", "main.js"]\n', - }, - { - 'name': 'main.js', - 'format': 'TEXT', - 'content': """ - const { Actor } = require('apify'); - Actor.main(async () => { - await Actor.setValue( - 'TOKEN', process.env.APIFY_TOKEN, { contentType: 'text/plain' } - ); - await new Promise((resolve) => setTimeout(resolve, 60000)); - }); - """, - }, - ], - } - ], - ) + run = await maybe_await( + client.actor(metamorph_actor_id).start(memory_mbytes=256, run_timeout=timedelta(seconds=120)) + ) + assert isinstance(run, Run) + run_client = client.run(run.id) + + try: + + async def get_token_record() -> dict | None: + return await maybe_await(client.key_value_store(run.default_key_value_store_id).get_record('TOKEN')) + + record = await poll_until_condition( + get_token_record, lambda rec: rec is not None, timeout=120, backoff_factor=2 ) - assert isinstance(created_actor, ActorResource) - actor_client = default_client.actor(created_actor.id) - run_client = None + assert record is not None - try: - # build and run the Actor - started_build = await maybe_await(actor_client.build(version_number='0.0')) - assert isinstance(started_build, Build) - await maybe_await(default_client.build(started_build.id).wait_for_finish()) - run = await maybe_await(actor_client.start()) - assert isinstance(run, Run) - run_client = default_client.run(run.id) - - # get the run token - async def get_token_record() -> dict | None: - return await maybe_await( - default_client.key_value_store(run.default_key_value_store_id).get_record('TOKEN') - ) - - record = await poll_until_condition(get_token_record, lambda rec: rec is not None, backoff_factor=2) - assert record is not None - - # the actual test - async with make_client(record['value'], client_type, http_client_classes) as run_token_client: - metamorphed_run = await maybe_await( - run_token_client.run(run.id).metamorph(target_actor_id=HELLO_WORLD_ACTOR) - ) - assert isinstance(metamorphed_run, Run) - assert metamorphed_run.id == run.id - finally: - if run_client is not None: - await maybe_await(run_client.abort()) - await maybe_await(run_client.wait_for_finish()) - await maybe_await(run_client.delete()) - await maybe_await(actor_client.delete()) + async with make_client(record['value'], client_type, http_client_classes) as run_token_client: + metamorphed_run = await maybe_await( + run_token_client.run(run.id).metamorph(target_actor_id=HELLO_WORLD_ACTOR) + ) + assert isinstance(metamorphed_run, Run) + assert metamorphed_run.id == run.id + finally: + await maybe_await(run_client.abort()) + await maybe_await(run_client.wait_for_finish()) + await maybe_await(run_client.delete()) async def test_run_reboot(client: ApifyClient | ApifyClientAsync) -> None: From f504502a8d5832c52a91019f692ea58c165181c2 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 09:48:26 +0100 Subject: [PATCH 03/13] test: pass run input in the metamorph integration test --- tests/integration/test_run.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index bd3036bf..e969407d 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -405,7 +405,10 @@ async def get_token_record() -> dict | None: async with make_client(record['value'], client_type, http_client_classes) as run_token_client: metamorphed_run = await maybe_await( - run_token_client.run(run.id).metamorph(target_actor_id=HELLO_WORLD_ACTOR) + run_token_client.run(run.id).metamorph( + target_actor_id=HELLO_WORLD_ACTOR, + run_input={'message': 'Hello from metamorph!'}, + ) ) assert isinstance(metamorphed_run, Run) assert metamorphed_run.id == run.id From ea02daa3b582396d39f4f2285f201c2666fa2a12 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 09:48:40 +0100 Subject: [PATCH 04/13] test: rename `make_client` to `open_apify_client` --- tests/_utils.py | 2 +- tests/integration/conftest.py | 4 ++-- tests/integration/test_run.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/_utils.py b/tests/_utils.py index 0ab4be67..f812e5b0 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -71,7 +71,7 @@ class HttpClientClasses: @asynccontextmanager -async def make_client( +async def open_apify_client( token: str, client_type: str, http_client_classes: HttpClientClasses ) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: """Build a sync or async client with the given token and HTTP client implementation, closing it on exit.""" diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 29a6f5d4..9a4348d8 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -14,7 +14,7 @@ HttpClientClasses, KvsFixture, get_crypto_random_object_id, - make_client, + open_apify_client, ) from apify_client import ApifyClient, ApifyClientAsync from apify_client._consts import DEFAULT_API_URL @@ -133,7 +133,7 @@ async def client( http_client_classes: HttpClientClasses, ) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: """Return each sync/async and HTTP client implementation combination.""" - async with make_client(api_token, client_type, http_client_classes) as client: + async with open_apify_client(api_token, client_type, http_client_classes) as client: yield client diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index e969407d..c005db74 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -13,8 +13,8 @@ API_URL_ENV_VAR, HttpClientClasses, get_random_resource_name, - make_client, maybe_await, + open_apify_client, poll_until_condition, ) from apify_client import ApifyClient @@ -403,7 +403,7 @@ async def get_token_record() -> dict | None: ) assert record is not None - async with make_client(record['value'], client_type, http_client_classes) as run_token_client: + async with open_apify_client(record['value'], client_type, http_client_classes) as run_token_client: metamorphed_run = await maybe_await( run_token_client.run(run.id).metamorph( target_actor_id=HELLO_WORLD_ACTOR, From 3a0094203f475e2d5b361a3234f31fcbed6941f2 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 09:48:47 +0100 Subject: [PATCH 05/13] test: drop the redundant poll condition in the metamorph test --- tests/integration/test_run.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index c005db74..f7287704 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -398,9 +398,7 @@ async def test_run_metamorph( async def get_token_record() -> dict | None: return await maybe_await(client.key_value_store(run.default_key_value_store_id).get_record('TOKEN')) - record = await poll_until_condition( - get_token_record, lambda rec: rec is not None, timeout=120, backoff_factor=2 - ) + record = await poll_until_condition(get_token_record, timeout=120, backoff_factor=2) assert record is not None async with open_apify_client(record['value'], client_type, http_client_classes) as run_token_client: From 1e083aec20c1aace04064b483da55ed6e17103aa Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 09:55:45 +0100 Subject: [PATCH 06/13] test: share the API URL lookup in a `get_api_url` helper --- tests/_utils.py | 7 ++++++- tests/integration/conftest.py | 7 +++---- tests/integration/test_actor_server.py | 6 ++---- tests/integration/test_apify_client.py | 7 ++----- tests/integration/test_run.py | 6 ++---- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/tests/_utils.py b/tests/_utils.py index f812e5b0..adf47dd7 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -70,12 +70,17 @@ class HttpClientClasses: async_: type[HttpClientAsync] +def get_api_url() -> str: + """Return the API URL for integration tests, from `API_URL_ENV_VAR` or the default.""" + return os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + + @asynccontextmanager async def open_apify_client( token: str, client_type: str, http_client_classes: HttpClientClasses ) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: """Build a sync or async client with the given token and HTTP client implementation, closing it on exit.""" - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + api_url = get_api_url() if client_type == 'sync': http_client = http_client_classes.sync() try: diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 9a4348d8..34886dbf 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -7,17 +7,16 @@ import pytest from .._utils import ( - API_URL_ENV_VAR, TOKEN_ENV_VAR, TOKEN_ENV_VAR_2, DatasetFixture, HttpClientClasses, KvsFixture, + get_api_url, get_crypto_random_object_id, open_apify_client, ) from apify_client import ApifyClient, ApifyClientAsync -from apify_client._consts import DEFAULT_API_URL from apify_client._utils.crypto import create_hmac_signature, create_storage_content_signature from apify_client.http_clients import ( Httpx2HttpClient, @@ -51,7 +50,7 @@ def api_token_2() -> str: @pytest.fixture(scope='session') def test_dataset_of_another_user(api_token_2: str) -> Generator[DatasetFixture]: """Dataset owned by secondary user for testing cross-user access restrictions.""" - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + api_url = get_api_url() client = ApifyClient(api_token_2, api_url=api_url) # Create dataset with test data @@ -80,7 +79,7 @@ def test_dataset_of_another_user(api_token_2: str) -> Generator[DatasetFixture]: @pytest.fixture(scope='session') def test_kvs_of_another_user(api_token_2: str) -> Generator[KvsFixture]: """Key-value store owned by secondary user for testing cross-user access restrictions.""" - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + api_url = get_api_url() client = ApifyClient(api_token_2, api_url=api_url) # Create key-value store with test data diff --git a/tests/integration/test_actor_server.py b/tests/integration/test_actor_server.py index 895eac0d..cb150374 100644 --- a/tests/integration/test_actor_server.py +++ b/tests/integration/test_actor_server.py @@ -3,15 +3,13 @@ from __future__ import annotations import json -import os from datetime import timedelta from typing import TYPE_CHECKING import pytest -from .._utils import API_URL_ENV_VAR, get_random_resource_name, maybe_await +from .._utils import get_api_url, get_random_resource_name, maybe_await from apify_client import ApifyClient -from apify_client._consts import DEFAULT_API_URL if TYPE_CHECKING: from collections.abc import Generator @@ -50,7 +48,7 @@ @pytest.fixture(scope='module') def server_actor_id(api_token: str) -> Generator[str]: """Create and build a Server Actor running an echo server, and delete it after the module's tests.""" - client = ApifyClient(api_token, api_url=os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL) + client = ApifyClient(api_token, api_url=get_api_url()) actor_name = get_random_resource_name('server') actor_json = { 'actorSpecification': 1, diff --git a/tests/integration/test_apify_client.py b/tests/integration/test_apify_client.py index 6935f88c..77cfb276 100644 --- a/tests/integration/test_apify_client.py +++ b/tests/integration/test_apify_client.py @@ -2,11 +2,8 @@ from __future__ import annotations -import os - -from .._utils import API_URL_ENV_VAR, maybe_await +from .._utils import get_api_url, maybe_await from apify_client import ApifyClient, ApifyClientAsync -from apify_client._consts import DEFAULT_API_URL from apify_client._models import UserPrivateInfo, UserPublicInfo from apify_client.http_clients import ImpitHttpClient, ImpitHttpClientAsync @@ -25,7 +22,7 @@ async def test_plain_constructor_reaches_the_api(client_type: str, api_token: st The rest of the suite injects a transport through `with_custom_http_client` to run against every built-in implementation, which leaves the default construction path untested against the live API. """ - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + api_url = get_api_url() if client_type == 'sync': sync_client = ApifyClient(api_token, api_url=api_url) diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index f7287704..fa71628c 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -2,7 +2,6 @@ from __future__ import annotations -import os from collections.abc import AsyncIterator, Generator, Iterator from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING @@ -10,15 +9,14 @@ import pytest from .._utils import ( - API_URL_ENV_VAR, HttpClientClasses, + get_api_url, get_random_resource_name, maybe_await, open_apify_client, poll_until_condition, ) from apify_client import ApifyClient -from apify_client._consts import DEFAULT_API_URL from apify_client._models import ( ActorResource, Build, @@ -352,7 +350,7 @@ async def test_run_runs_client(client: ApifyClient | ApifyClientAsync) -> None: @pytest.fixture(scope='module') def metamorph_actor_id(api_token: str) -> Generator[str]: """Create and build an Actor that stores its run token, and delete it after the module's tests.""" - client = ApifyClient(api_token, api_url=os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL) + client = ApifyClient(api_token, api_url=get_api_url()) actor = client.actors().create( name=get_random_resource_name('actor'), versions=[ From ae514d88ab6eb13135559c44374bf199c6f89469 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 09:55:58 +0100 Subject: [PATCH 07/13] test: type the client type as a `sync`/`async` literal --- tests/_utils.py | 6 ++++-- tests/integration/conftest.py | 5 +++-- tests/integration/test_run.py | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/_utils.py b/tests/_utils.py index adf47dd7..ff7af576 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -9,7 +9,7 @@ from collections.abc import AsyncGenerator, AsyncIterator, Iterator from contextlib import asynccontextmanager from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, overload +from typing import TYPE_CHECKING, Any, Literal, Protocol, TypeVar, cast, overload import pytest @@ -28,6 +28,8 @@ T = TypeVar('T') +ClientType = Literal['sync', 'async'] + class _HasId(Protocol): """Items returned by collection `iterate()` endpoints all expose `.id`.""" @@ -77,7 +79,7 @@ def get_api_url() -> str: @asynccontextmanager async def open_apify_client( - token: str, client_type: str, http_client_classes: HttpClientClasses + token: str, client_type: ClientType, http_client_classes: HttpClientClasses ) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: """Build a sync or async client with the given token and HTTP client implementation, closing it on exit.""" api_url = get_api_url() diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 34886dbf..0115ea25 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -9,6 +9,7 @@ from .._utils import ( TOKEN_ENV_VAR, TOKEN_ENV_VAR_2, + ClientType, DatasetFixture, HttpClientClasses, KvsFixture, @@ -120,14 +121,14 @@ def http_client_classes(request: pytest.FixtureRequest) -> HttpClientClasses: @pytest.fixture(params=['sync', 'async']) -def client_type(request: pytest.FixtureRequest) -> str: +def client_type(request: pytest.FixtureRequest) -> ClientType: """Parametrize tests to run with both sync and async clients.""" return request.param @pytest.fixture async def client( - client_type: str, + client_type: ClientType, api_token: str, http_client_classes: HttpClientClasses, ) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index fa71628c..0cb242ba 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -9,6 +9,7 @@ import pytest from .._utils import ( + ClientType, HttpClientClasses, get_api_url, get_random_resource_name, @@ -376,7 +377,7 @@ def metamorph_actor_id(api_token: str) -> Generator[str]: async def test_run_metamorph( client: ApifyClient | ApifyClientAsync, - client_type: str, + client_type: ClientType, http_client_classes: HttpClientClasses, metamorph_actor_id: str, ) -> None: From d304ab5efc7651f9a1c15a1685e023877623636e Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 09:56:19 +0100 Subject: [PATCH 08/13] test: verify the metamorph target ran and guard the cleanup abort --- tests/integration/test_run.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index 0cb242ba..ded5d0c0 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import AsyncIterator, Generator, Iterator +from contextlib import suppress from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING @@ -381,11 +382,7 @@ async def test_run_metamorph( http_client_classes: HttpClientClasses, metamorph_actor_id: str, ) -> None: - """Test metamorphing a run into another Actor. - - A metamorph must use the run's own token, so the Actor writes `APIFY_TOKEN` to its key-value store and the test - reads it from there. - """ + """Test metamorphing a run into another Actor with the run's own token.""" run = await maybe_await( client.actor(metamorph_actor_id).start(memory_mbytes=256, run_timeout=timedelta(seconds=120)) ) @@ -393,14 +390,17 @@ async def test_run_metamorph( run_client = client.run(run.id) try: - + # A metamorph needs the run's own token, which the Actor stores in its key-value store. async def get_token_record() -> dict | None: return await maybe_await(client.key_value_store(run.default_key_value_store_id).get_record('TOKEN')) record = await poll_until_condition(get_token_record, timeout=120, backoff_factor=2) assert record is not None + run_token = record['value'] + assert isinstance(run_token, str) + assert run_token - async with open_apify_client(record['value'], client_type, http_client_classes) as run_token_client: + async with open_apify_client(run_token, client_type, http_client_classes) as run_token_client: metamorphed_run = await maybe_await( run_token_client.run(run.id).metamorph( target_actor_id=HELLO_WORLD_ACTOR, @@ -409,8 +409,16 @@ async def get_token_record() -> dict | None: ) assert isinstance(metamorphed_run, Run) assert metamorphed_run.id == run.id + + # Hello-world replaces the waiting Actor, so the run finishes on its own. + final_run = await maybe_await(run_client.wait_for_finish()) + assert isinstance(final_run, Run) + assert final_run.status == 'SUCCEEDED' + assert final_run.metamorphs finally: - await maybe_await(run_client.abort()) + # The run may have finished already, and then the abort can fail. + with suppress(ApifyApiError): + await maybe_await(run_client.abort()) await maybe_await(run_client.wait_for_finish()) await maybe_await(run_client.delete()) From dc027e867ab20c0d238b115cde0e8d9e76829ad6 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 10:10:54 +0100 Subject: [PATCH 09/13] test: annotate the remaining `client_type` parameters as `ClientType` --- tests/integration/conftest.py | 2 +- tests/integration/test_apify_client.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 0115ea25..df1fd48c 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -138,6 +138,6 @@ async def client( @pytest.fixture -def is_async(client_type: str) -> bool: +def is_async(client_type: ClientType) -> bool: """True if current test is using async client.""" return client_type == 'async' diff --git a/tests/integration/test_apify_client.py b/tests/integration/test_apify_client.py index 77cfb276..d6478c08 100644 --- a/tests/integration/test_apify_client.py +++ b/tests/integration/test_apify_client.py @@ -2,7 +2,7 @@ from __future__ import annotations -from .._utils import get_api_url, maybe_await +from .._utils import ClientType, get_api_url, maybe_await from apify_client import ApifyClient, ApifyClientAsync from apify_client._models import UserPrivateInfo, UserPublicInfo from apify_client.http_clients import ImpitHttpClient, ImpitHttpClientAsync @@ -16,7 +16,7 @@ async def test_apify_client(client: ApifyClient | ApifyClientAsync) -> None: assert me.username is not None -async def test_plain_constructor_reaches_the_api(client_type: str, api_token: str) -> None: +async def test_plain_constructor_reaches_the_api(client_type: ClientType, api_token: str) -> None: """The plain constructor - the form users write - reaches the API with the HTTP client it builds itself. The rest of the suite injects a transport through `with_custom_http_client` to run against every built-in From 55a063a82fc5225b1e24764e65b520d3e78d19ef Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 10:11:26 +0100 Subject: [PATCH 10/13] test: share the Actor create-and-build fixture code in a `create_built_actor` helper --- tests/_utils.py | 34 +++++++++++++++++++++-- tests/integration/test_actor_server.py | 38 ++++++++------------------ tests/integration/test_run.py | 24 ++-------------- 3 files changed, 47 insertions(+), 49 deletions(-) diff --git a/tests/_utils.py b/tests/_utils.py index ff7af576..81411595 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -7,8 +7,9 @@ import string import time from collections.abc import AsyncGenerator, AsyncIterator, Iterator -from contextlib import asynccontextmanager +from contextlib import asynccontextmanager, contextmanager from dataclasses import dataclass +from datetime import timedelta from typing import TYPE_CHECKING, Any, Literal, Protocol, TypeVar, cast, overload import pytest @@ -17,7 +18,7 @@ from apify_client._consts import DEFAULT_API_URL if TYPE_CHECKING: - from collections.abc import Awaitable, Callable + from collections.abc import Awaitable, Callable, Generator from apify_client.http_clients import HttpClient, HttpClientAsync @@ -106,6 +107,35 @@ async def open_apify_client( await http_client_async.aclose() +@contextmanager +def create_built_actor( + client: ApifyClient, name: str, source_files: list[dict[str, Any]], **create_kwargs: Any +) -> Generator[str]: + """Create an Actor from source files and build it, yield its ID, and delete it on exit.""" + actor = client.actors().create( + name=name, + versions=[ + { + 'versionNumber': '0.0', + 'sourceType': 'SOURCE_FILES', + 'buildTag': 'latest', + 'sourceFiles': source_files, + } + ], + **create_kwargs, + ) + actor_client = client.actor(actor.id) + + try: + build = actor_client.build(version_number='0.0') + finished_build = client.build(build.id).wait_for_finish(wait_duration=timedelta(minutes=5)) + assert finished_build is not None + assert finished_build.status == 'SUCCEEDED' + yield actor.id + finally: + actor_client.delete() + + def get_crypto_random_object_id(length: int = 17) -> str: """Generate a cryptographically secure random object ID.""" chars = 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789' diff --git a/tests/integration/test_actor_server.py b/tests/integration/test_actor_server.py index cb150374..ff577ebb 100644 --- a/tests/integration/test_actor_server.py +++ b/tests/integration/test_actor_server.py @@ -8,7 +8,7 @@ import pytest -from .._utils import get_api_url, get_random_resource_name, maybe_await +from .._utils import create_built_actor, get_api_url, get_random_resource_name, maybe_await from apify_client import ApifyClient if TYPE_CHECKING: @@ -57,34 +57,20 @@ def server_actor_id(api_token: str) -> Generator[str]: 'usesStandbyMode': True, 'webServerSchema': OPENAPI_SPEC, } - actor = client.actors().create( - name=actor_name, - versions=[ - { - 'versionNumber': '0.0', - 'sourceType': 'SOURCE_FILES', - 'buildTag': 'latest', - 'sourceFiles': [ - {'name': 'Dockerfile', 'format': 'TEXT', 'content': DOCKERFILE}, - {'name': 'main.js', 'format': 'TEXT', 'content': ECHO_SERVER_SOURCE}, - {'name': '.actor/actor.json', 'format': 'TEXT', 'content': json.dumps(actor_json)}, - ], - } - ], + source_files = [ + {'name': 'Dockerfile', 'format': 'TEXT', 'content': DOCKERFILE}, + {'name': 'main.js', 'format': 'TEXT', 'content': ECHO_SERVER_SOURCE}, + {'name': '.actor/actor.json', 'format': 'TEXT', 'content': json.dumps(actor_json)}, + ] + with create_built_actor( + client, + actor_name, + source_files, server_is_enabled=True, server_memory_mbytes=256, server_idle_timeout=timedelta(minutes=1), - ) - actor_client = client.actor(actor.id) - - try: - build = actor_client.build(version_number='0.0') - finished_build = client.build(build.id).wait_for_finish(wait_duration=timedelta(minutes=5)) - assert finished_build is not None - assert finished_build.status == 'SUCCEEDED' - yield actor.id - finally: - actor_client.delete() + ) as actor_id: + yield actor_id async def test_actor_server(client: ApifyClient | ApifyClientAsync, server_actor_id: str) -> None: diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index ded5d0c0..5b80acae 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -12,6 +12,7 @@ from .._utils import ( ClientType, HttpClientClasses, + create_built_actor, get_api_url, get_random_resource_name, maybe_await, @@ -353,27 +354,8 @@ async def test_run_runs_client(client: ApifyClient | ApifyClientAsync) -> None: def metamorph_actor_id(api_token: str) -> Generator[str]: """Create and build an Actor that stores its run token, and delete it after the module's tests.""" client = ApifyClient(api_token, api_url=get_api_url()) - actor = client.actors().create( - name=get_random_resource_name('actor'), - versions=[ - { - 'versionNumber': '0.0', - 'sourceType': 'SOURCE_FILES', - 'buildTag': 'latest', - 'sourceFiles': METAMORPH_SOURCE_FILES, - } - ], - ) - actor_client = client.actor(actor.id) - - try: - build = actor_client.build(version_number='0.0') - finished_build = client.build(build.id).wait_for_finish() - assert finished_build is not None - assert finished_build.status == 'SUCCEEDED' - yield actor.id - finally: - actor_client.delete() + with create_built_actor(client, get_random_resource_name('actor'), METAMORPH_SOURCE_FILES) as actor_id: + yield actor_id async def test_run_metamorph( From 32450cde37c4b5b3a4d1b60e185bbe096990523c Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 10:11:38 +0100 Subject: [PATCH 11/13] test: poll for the run token at a fixed interval and fail fast when the run ends --- tests/integration/test_run.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index 5b80acae..66450f74 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -366,19 +366,24 @@ async def test_run_metamorph( ) -> None: """Test metamorphing a run into another Actor with the run's own token.""" run = await maybe_await( - client.actor(metamorph_actor_id).start(memory_mbytes=256, run_timeout=timedelta(seconds=120)) + client.actor(metamorph_actor_id).start(memory_mbytes=256, run_timeout=timedelta(seconds=300)) ) assert isinstance(run, Run) run_client = client.run(run.id) try: # A metamorph needs the run's own token, which the Actor stores in its key-value store. - async def get_token_record() -> dict | None: - return await maybe_await(client.key_value_store(run.default_key_value_store_id).get_record('TOKEN')) - - record = await poll_until_condition(get_token_record, timeout=120, backoff_factor=2) - assert record is not None - run_token = record['value'] + async def get_run_token() -> str | None: + record = await maybe_await(client.key_value_store(run.default_key_value_store_id).get_record('TOKEN')) + if record is not None: + return record['value'] + current_run = await maybe_await(run_client.get()) + assert isinstance(current_run, Run) + assert current_run.status in ('READY', 'RUNNING'), f'The run ended as {current_run.status} without a token.' + return None + + # A fixed short interval, so the token is read well within the 60 seconds the Actor waits after storing it. + run_token = await poll_until_condition(get_run_token, timeout=120, poll_interval=2) assert isinstance(run_token, str) assert run_token From bb80b3cfef4ceecf32b733dec4470807c3dd32df Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 10:11:48 +0100 Subject: [PATCH 12/13] test: abort the metamorph run only while it is still running --- tests/integration/test_run.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index 66450f74..962f7f92 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -3,7 +3,6 @@ from __future__ import annotations from collections.abc import AsyncIterator, Generator, Iterator -from contextlib import suppress from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING @@ -403,8 +402,8 @@ async def get_run_token() -> str | None: assert final_run.status == 'SUCCEEDED' assert final_run.metamorphs finally: - # The run may have finished already, and then the abort can fail. - with suppress(ApifyApiError): + current_run = await maybe_await(run_client.get()) + if isinstance(current_run, Run) and current_run.status in ('READY', 'RUNNING'): await maybe_await(run_client.abort()) await maybe_await(run_client.wait_for_finish()) await maybe_await(run_client.delete()) From 62c23cca3a469a139bb0277175dd189f6ea446ff Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Mon, 16 Nov 2026 10:15:37 +0100 Subject: [PATCH 13/13] test: build the integration test Actors on Node 26 --- tests/integration/test_actor_server.py | 2 +- tests/integration/test_run.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration/test_actor_server.py b/tests/integration/test_actor_server.py index ff577ebb..5ee765b8 100644 --- a/tests/integration/test_actor_server.py +++ b/tests/integration/test_actor_server.py @@ -39,7 +39,7 @@ """ DOCKERFILE = """ -FROM node:22-alpine +FROM node:26-alpine COPY main.js ./ CMD ["node", "main.js"] """ diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index 962f7f92..fee1ed77 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -42,7 +42,7 @@ { 'name': 'Dockerfile', 'format': 'TEXT', - 'content': 'FROM apify/actor-node:22\nCOPY . ./\nCMD ["node", "main.mjs"]\n', + 'content': 'FROM apify/actor-node:26\nCOPY . ./\nCMD ["node", "main.mjs"]\n', }, { 'name': 'main.mjs', @@ -69,7 +69,7 @@ { 'name': 'Dockerfile', 'format': 'TEXT', - 'content': 'FROM apify/actor-node:22\nCOPY . ./\nCMD ["node", "main.js"]\n', + 'content': 'FROM apify/actor-node:26\nCOPY . ./\nCMD ["node", "main.js"]\n', }, { 'name': 'main.js',