Skip to content

Commit 1bce291

Browse files
committed
Update integration tests to create storages of second user on the fly
1 parent 5bae0cf commit 1bce291

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

tests/integration/conftest.py

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import json
12
import os
3+
import secrets
4+
from collections.abc import Generator
25

36
import pytest
7+
from apify_shared.utils import create_hmac_signature, create_storage_content_signature
48

59
from .integration_test_utils import TestDataset, TestKvs
610
from apify_client import ApifyClient, ApifyClientAsync
@@ -9,18 +13,33 @@
913
API_URL_ENV_VAR = 'APIFY_INTEGRATION_TESTS_API_URL'
1014

1115

12-
@pytest.fixture
16+
def crypto_random_object_id(length: int = 17) -> str:
17+
"""Generate a random object ID."""
18+
chars = 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789'
19+
return ''.join(secrets.choice(chars) for _ in range(length))
20+
21+
22+
@pytest.fixture(scope='session')
1323
def api_token() -> str:
1424
token = os.getenv(TOKEN_ENV_VAR)
1525
if not token:
1626
raise RuntimeError(f'{TOKEN_ENV_VAR} environment variable is missing, cannot run tests!')
1727
return token
1828

1929

30+
@pytest.fixture(scope='session')
31+
def api_token_2() -> str:
32+
"""API token for the second test user for storage permission tests."""
33+
second_user_env_var = 'APIFY_TEST_USER_PYTHON_SDK_API_TOKEN'
34+
token = os.getenv(second_user_env_var)
35+
if not token:
36+
raise RuntimeError(f'{second_user_env_var} environment variable is missing, cannot run permission tests!')
37+
return token
38+
39+
2040
@pytest.fixture
2141
def apify_client(api_token: str) -> ApifyClient:
22-
api_url = os.getenv(API_URL_ENV_VAR)
23-
return ApifyClient(api_token, api_url=api_url)
42+
return ApifyClient(api_token, api_url=os.getenv(API_URL_ENV_VAR))
2443

2544

2645
# This fixture can't be session-scoped,
@@ -30,30 +49,60 @@ def apify_client(api_token: str) -> ApifyClient:
3049
# and uses a new one for the next test.
3150
@pytest.fixture
3251
def apify_client_async(api_token: str) -> ApifyClientAsync:
33-
api_url = os.getenv(API_URL_ENV_VAR)
34-
return ApifyClientAsync(api_token, api_url=api_url)
52+
return ApifyClientAsync(api_token, api_url=os.getenv(API_URL_ENV_VAR))
3553

3654

37-
@pytest.fixture
38-
def test_dataset_of_another_user() -> TestDataset:
39-
"""Pre-existing dataset of another test user with restricted access."""
40-
return TestDataset(
41-
id='InrsNvJNGwJMFAR2l',
42-
signature='MC4wLjFGbVN3UjB5T0xvMU1hU0lFQjZCMQ',
55+
@pytest.fixture(scope='session')
56+
def test_dataset_of_another_user(api_token_2: str) -> Generator[TestDataset]:
57+
"""Pre-existing named dataset of another test user with restricted access."""
58+
client = ApifyClient(api_token_2, api_url=os.getenv(API_URL_ENV_VAR))
59+
60+
dataset_name = f'API-test-permissions-{crypto_random_object_id()}'
61+
dataset = client.datasets().get_or_create(name=dataset_name)
62+
dataset_client = client.dataset(dataset_id=dataset['id'])
63+
expected_content = [{'item1': 1, 'item2': 2, 'item3': 3}, {'item1': 4, 'item2': 5, 'item3': 6}]
64+
65+
# Push data to dataset
66+
dataset_client.push_items(json.dumps(expected_content))
67+
68+
# Generate signature for the test
69+
signature = create_storage_content_signature(
70+
resource_id=dataset['id'], url_signing_secret_key=dataset['urlSigningSecretKey']
71+
)
72+
73+
yield TestDataset(
74+
id=dataset['id'],
75+
signature=signature,
4376
expected_content=[{'item1': 1, 'item2': 2, 'item3': 3}, {'item1': 4, 'item2': 5, 'item3': 6}],
4477
)
4578

79+
dataset_client.delete()
4680

47-
@pytest.fixture
48-
def test_kvs_of_another_user() -> TestKvs:
49-
"""Pre-existing key value store of another test user with restricted access."""
50-
return TestKvs(
51-
id='0SWREKM4yzKnpQRGA',
52-
signature='MC4wLjVKVmlMSVpDNEhaazg1Z1VXTnBP',
53-
expected_content={'key1': 1, 'key2': 2, 'key3': 3},
54-
keys_signature={
55-
'key1': 'qrQL9pHpiok99v9kWhKx',
56-
'key2': '1BhGTfsLvpsF8aPiIgoBt',
57-
'key3': 'rPPqxmTNcxvvpvO0Bx5s',
58-
},
81+
82+
@pytest.fixture(scope='session')
83+
def test_kvs_of_another_user(api_token_2: str) -> Generator[TestKvs]:
84+
"""Pre-existing named key value store of another test user with restricted access."""
85+
client = ApifyClient(api_token_2, api_url=os.getenv(API_URL_ENV_VAR))
86+
87+
kvs_name = f'API-test-permissions-{crypto_random_object_id()}'
88+
kvs = client.key_value_stores().get_or_create(name=kvs_name)
89+
kvs_client = client.key_value_store(key_value_store_id=kvs['id'])
90+
expected_content = {'key1': 1, 'key2': 2, 'key3': 3}
91+
92+
# Push data to kvs
93+
for key, value in expected_content.items():
94+
kvs_client.set_record(key, value)
95+
96+
# Generate signature for the test
97+
signature = create_storage_content_signature(
98+
resource_id=kvs['id'], url_signing_secret_key=kvs['urlSigningSecretKey']
5999
)
100+
101+
yield TestKvs(
102+
id=kvs['id'],
103+
signature=signature,
104+
expected_content=expected_content,
105+
keys_signature={key: create_hmac_signature(kvs['urlSigningSecretKey'], key) for key in expected_content},
106+
)
107+
108+
kvs_client.delete()

0 commit comments

Comments
 (0)