Skip to content

Commit f597e3e

Browse files
authored
chore: Fix broken storage permission integration tests (#543)
### Description - Update integration tests to create storage of the second user on the fly - Added token for test user with id: `RbWKSZSutvaNWffdJ` under this secret `APIFY_TEST_USER_2_API_TOKEN`. This test user has restricted permissions. ### Issues - Closes: #542 - Requires: apify/workflows#233
1 parent 6d75c56 commit f597e3e

File tree

2 files changed

+75
-23
lines changed

2 files changed

+75
-23
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ We have integration tests which build and run Actors using the Python SDK on the
7878
you need to set the `APIFY_TEST_USER_API_TOKEN` environment variable to the API token of the Apify user you want to
7979
use for the tests, and then start them with `make integration-tests`.
8080

81+
For subset of integration tests another token is needed `APIFY_TEST_USER_2_API_TOKEN`. Such tests are testing
82+
the storage restricted access and thus need two user accounts.
83+
8184
If you want to run the integration tests on a different environment than the main Apify Platform, you need to set
8285
the `APIFY_INTEGRATION_TESTS_API_URL` environment variable to the right URL to the Apify API you want to use.
8386

tests/integration/conftest.py

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
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
711

812
TOKEN_ENV_VAR = 'APIFY_TEST_USER_API_TOKEN'
13+
TOKEN_ENV_VAR_2 = 'APIFY_TEST_USER_2_API_TOKEN'
914
API_URL_ENV_VAR = 'APIFY_INTEGRATION_TESTS_API_URL'
1015

1116

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

1930

31+
@pytest.fixture(scope='session')
32+
def api_token_2() -> str:
33+
"""API token for the second test user for storage permission tests."""
34+
token = os.getenv(TOKEN_ENV_VAR_2)
35+
if not token:
36+
raise RuntimeError(f'{TOKEN_ENV_VAR_2} 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)