forked from apify/apify-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_actor_create_proxy_configuration.py
More file actions
152 lines (117 loc) · 4.64 KB
/
test_actor_create_proxy_configuration.py
File metadata and controls
152 lines (117 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import Mock
import pytest
from apify_client import ApifyClientAsync
from apify_shared.consts import ApifyEnvVars
from apify import Actor
if TYPE_CHECKING:
from pytest_httpserver import HTTPServer
from werkzeug import Request, Response
from ..conftest import ApifyClientAsyncPatcher
DUMMY_PASSWORD = 'DUMMY_PASSWORD'
@pytest.fixture
def patched_apify_client(apify_client_async_patcher: ApifyClientAsyncPatcher) -> ApifyClientAsync:
apify_client_async_patcher.patch('user', 'get', return_value={'proxy': {'password': DUMMY_PASSWORD}})
return ApifyClientAsync()
@pytest.mark.usefixtures('patched_impit_client')
async def test_basic_proxy_configuration_creation(
monkeypatch: pytest.MonkeyPatch,
httpserver: HTTPServer,
patched_apify_client: ApifyClientAsync,
) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
monkeypatch.setenv(ApifyEnvVars.TOKEN.value, 'DUMMY_TOKEN')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
call_mock = Mock()
def request_handler(request: Request, response: Response) -> Response:
call_mock(request.url)
return response
httpserver.expect_oneshot_request('/').with_post_hook(request_handler).respond_with_json(
{
'connected': True,
'connectionError': None,
'isManInTheMiddle': True,
},
status=200,
)
groups = ['GROUP1', 'GROUP2']
country_code = 'US'
await Actor.init()
proxy_configuration = await Actor.create_proxy_configuration(groups=groups, country_code=country_code)
assert proxy_configuration is not None
assert proxy_configuration._groups == groups
assert proxy_configuration._password == DUMMY_PASSWORD
assert proxy_configuration._country_code == country_code
assert len(patched_apify_client.calls['user']['get']) == 1 # type: ignore[attr-defined]
assert call_mock.call_count == 1
await Actor.exit()
@pytest.mark.usefixtures('patched_impit_client')
async def test_proxy_configuration_with_actor_proxy_input(
monkeypatch: pytest.MonkeyPatch,
httpserver: HTTPServer,
patched_apify_client: ApifyClientAsync,
) -> None:
dummy_proxy_status_url = str(httpserver.url_for('/')).removesuffix('/')
dummy_proxy_url = 'http://dummy-proxy.com:8000'
monkeypatch.setenv(ApifyEnvVars.TOKEN.value, 'DUMMY_TOKEN')
monkeypatch.setenv(ApifyEnvVars.PROXY_STATUS_URL.value, dummy_proxy_status_url)
call_mock = Mock()
def request_handler(request: Request, response: Response) -> Response:
call_mock(request.url)
return response
httpserver.expect_request('/').with_post_hook(request_handler).respond_with_json(
{
'connected': True,
'connectionError': None,
'isManInTheMiddle': True,
},
status=200,
)
await Actor.init()
proxy_configuration = await Actor.create_proxy_configuration(actor_proxy_input={})
assert proxy_configuration is None
proxy_configuration = await Actor.create_proxy_configuration(
actor_proxy_input={
'useApifyProxy': False,
}
)
assert proxy_configuration is None
proxy_configuration = await Actor.create_proxy_configuration(
actor_proxy_input={
'proxyUrls': [],
}
)
assert proxy_configuration is None
proxy_configuration = await Actor.create_proxy_configuration(
actor_proxy_input={
'useApifyProxy': False,
'proxyUrls': [dummy_proxy_url],
}
)
assert proxy_configuration is not None
assert await proxy_configuration.new_url() == dummy_proxy_url
proxy_configuration = await Actor.create_proxy_configuration(
actor_proxy_input={
'useApifyProxy': True,
}
)
assert proxy_configuration is not None
assert await proxy_configuration.new_url() == f'http://auto:{DUMMY_PASSWORD}@proxy.apify.com:8000'
groups = ['GROUP1', 'GROUP2']
country_code = 'US'
proxy_configuration = await Actor.create_proxy_configuration(
actor_proxy_input={
'useApifyProxy': True,
'apifyProxyGroups': groups,
'apifyProxyCountry': country_code,
}
)
assert proxy_configuration is not None
assert (
await proxy_configuration.new_url()
== f'http://groups-{"+".join(groups)},country-{country_code}:{DUMMY_PASSWORD}@proxy.apify.com:8000'
)
assert len(patched_apify_client.calls['user']['get']) == 2 # type: ignore[attr-defined]
assert call_mock.call_count == 2
await Actor.exit()