返回 last30days-skill
test_scrapecreators_base_url.py
根目录 / tests / test_scrapecreators_base_url.py
1 """Tests for configurable SCRAPECREATORS_BASE_URL (#1367).
2
3 Verifies that ScrapeCreators API calls across all modules respect the
4 SCRAPECREATORS_BASE_URL override (env var or config) while defaulting
5 to https://api.scrapecreators.com and supporting legacy aliases and mocks.
6 """
7
8 from __future__ import annotations
9
10 import os
11 from unittest.mock import MagicMock, patch
12
13 import pytest
14
15 from lib import (
16 env,
17 http,
18 instagram,
19 linkedin,
20 meta_ads,
21 permission_preflight,
22 pinterest,
23 reddit,
24 setup_wizard,
25 telegram,
26 threads,
27 tiktok,
28 usage,
29 youtube_yt,
30 )
31
32
33 class TestScrapeCreatorsBaseUrlResolution:
34 def test_default_base_url(self, monkeypatch):
35 monkeypatch.delenv("SCRAPECREATORS_BASE_URL", raising=False)
36 monkeypatch.delenv("SCRAPE_CREATORS_BASE_URL", raising=False)
37 assert env.get_scrapecreators_base_url() == "https://api.scrapecreators.com"
38 assert env.get_scrapecreators_base_url({}) == "https://api.scrapecreators.com"
39
40 def test_env_override(self, monkeypatch):
41 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", "https://api.lurkapi.com")
42 assert env.get_scrapecreators_base_url() == "https://api.lurkapi.com"
43
44 def test_trailing_slash_and_whitespace_stripped(self, monkeypatch):
45 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", " https://api.lurkapi.com/ ")
46 assert env.get_scrapecreators_base_url() == "https://api.lurkapi.com"
47
48 def test_empty_env_falls_back_to_default(self, monkeypatch):
49 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", " ")
50 assert env.get_scrapecreators_base_url() == "https://api.scrapecreators.com"
51
52 def test_config_dict_override_takes_precedence(self, monkeypatch):
53 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", "https://env.example.com")
54 config = {"SCRAPECREATORS_BASE_URL": "http://127.0.0.1:8080"}
55 assert env.get_scrapecreators_base_url(config) == "http://127.0.0.1:8080"
56
57 def test_legacy_alias_fallback(self, monkeypatch):
58 monkeypatch.delenv("SCRAPECREATORS_BASE_URL", raising=False)
59 monkeypatch.setenv("SCRAPE_CREATORS_BASE_URL", "https://legacy.example.com/")
60 assert env.get_scrapecreators_base_url() == "https://legacy.example.com"
61
62 def test_remote_http_override_is_refused_with_warning(self, monkeypatch, capsys):
63 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", "http://proxy.example.com/v1")
64 assert env.get_scrapecreators_base_url() == "https://api.scrapecreators.com"
65 assert "SCRAPECREATORS_BASE_URL" in capsys.readouterr().err
66
67 def test_remote_http_legacy_alias_is_refused_with_warning(self, monkeypatch, capsys):
68 monkeypatch.delenv("SCRAPECREATORS_BASE_URL", raising=False)
69 monkeypatch.setenv("SCRAPE_CREATORS_BASE_URL", "http://proxy.example.com")
70 assert env.get_scrapecreators_base_url() == "https://api.scrapecreators.com"
71 assert "SCRAPECREATORS_BASE_URL" in capsys.readouterr().err
72
73 @pytest.mark.parametrize("loopback", [
74 "http://localhost:8080",
75 "http://127.0.0.1:9000",
76 "http://[::1]:8080",
77 ])
78 def test_loopback_http_override_is_allowed(self, loopback, monkeypatch):
79 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", loopback)
80 assert env.get_scrapecreators_base_url() == loopback
81
82 def test_permission_preflight_endpoint_override_key(self):
83 assert "SCRAPECREATORS_BASE_URL" in permission_preflight.ENDPOINT_OVERRIDE_KEYS
84
85 def test_permission_preflight_rejects_remote_http_override(self):
86 preflight = permission_preflight.build(
87 {"SCRAPECREATORS_BASE_URL": "http://proxy.example.com"},
88 {"has_scrapecreators": True},
89 )
90 assert "SCRAPECREATORS_BASE_URL" in preflight["network"]["ignored_endpoint_overrides"]
91 assert "SCRAPECREATORS_BASE_URL" not in preflight["network"]["endpoint_overrides"]
92 assert preflight["status"] == "action_needed"
93
94
95 class TestModuleUrlConstructionWithOverride:
96 @pytest.fixture(autouse=True)
97 def set_custom_base(self, monkeypatch):
98 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", "https://mock.scrapecreators.local")
99
100 def test_tiktok_endpoints(self):
101 assert tiktok._sc_base() == "https://mock.scrapecreators.local/v1/tiktok"
102 assert tiktok._sc_root() == "https://mock.scrapecreators.local"
103
104 def test_instagram_endpoints(self):
105 assert instagram._sc_base() == "https://mock.scrapecreators.local"
106
107 def test_reddit_endpoints(self):
108 assert reddit._sc_base() == "https://mock.scrapecreators.local/v1/reddit"
109
110 def test_youtube_endpoints(self):
111 assert youtube_yt._sc_yt_base() == "https://mock.scrapecreators.local/v1/youtube"
112
113 def test_linkedin_endpoints(self):
114 assert linkedin._sc_base() == "https://mock.scrapecreators.local/v1/linkedin"
115
116 def test_telegram_endpoints(self):
117 assert telegram._sc_base() == "https://mock.scrapecreators.local/v1/telegram"
118
119 def test_threads_endpoints(self):
120 assert threads._sc_base() == "https://mock.scrapecreators.local/v1/threads"
121
122 def test_pinterest_endpoints(self):
123 assert pinterest._sc_base() == "https://mock.scrapecreators.local/v1/pinterest"
124
125 def test_meta_ads_endpoints(self):
126 assert meta_ads._sc_base() == "https://mock.scrapecreators.local/v1/facebook/adLibrary"
127 assert meta_ads._search_ads_url() == "https://mock.scrapecreators.local/v1/facebook/adLibrary/search/ads"
128 assert meta_ads._search_companies_url() == "https://mock.scrapecreators.local/v1/facebook/adLibrary/search/companies"
129 assert meta_ads._company_ads_url() == "https://mock.scrapecreators.local/v1/facebook/adLibrary/company/ads"
130 assert meta_ads._ad_transcript_url() == "https://mock.scrapecreators.local/v1/facebook/adLibrary/ad/transcript"
131
132 def test_setup_wizard_endpoints(self):
133 assert setup_wizard._device_base() == "https://mock.scrapecreators.local/v1/github/device"
134
135
136 class TestMonkeypatchBackwardCompatibility:
137 """Existing tests monkeypatch module-level constants; ensure that still works."""
138
139 def test_tiktok_base_override(self, monkeypatch):
140 monkeypatch.setattr(tiktok, "SCRAPECREATORS_BASE", "https://custom.test/tiktok")
141 assert tiktok._sc_base() == "https://custom.test/tiktok"
142
143 def test_instagram_base_override(self, monkeypatch):
144 monkeypatch.setattr(instagram, "SCRAPECREATORS_BASE", "https://custom.test/instagram")
145 assert instagram._sc_base() == "https://custom.test/instagram"
146
147 def test_reddit_base_override(self, monkeypatch):
148 monkeypatch.setattr(reddit, "SCRAPECREATORS_BASE", "https://custom.test/reddit")
149 assert reddit._sc_base() == "https://custom.test/reddit"
150
151 def test_youtube_base_override(self, monkeypatch):
152 monkeypatch.setattr(youtube_yt, "SCRAPECREATORS_YT_BASE", "https://custom.test/youtube")
153 assert youtube_yt._sc_yt_base() == "https://custom.test/youtube"
154
155 def test_linkedin_base_override(self, monkeypatch):
156 monkeypatch.setattr(linkedin, "SC_BASE", "https://custom.test/linkedin")
157 assert linkedin._sc_base() == "https://custom.test/linkedin"
158
159 def test_telegram_base_override(self, monkeypatch):
160 monkeypatch.setattr(telegram, "SCRAPECREATORS_BASE", "https://custom.test/telegram")
161 assert telegram._sc_base() == "https://custom.test/telegram"
162
163 def test_threads_base_override(self, monkeypatch):
164 monkeypatch.setattr(threads, "SCRAPECREATORS_BASE", "https://custom.test/threads")
165 assert threads._sc_base() == "https://custom.test/threads"
166
167 def test_pinterest_base_override(self, monkeypatch):
168 monkeypatch.setattr(pinterest, "SCRAPECREATORS_BASE", "https://custom.test/pinterest")
169 assert pinterest._sc_base() == "https://custom.test/pinterest"
170
171 def test_meta_ads_base_override(self, monkeypatch):
172 monkeypatch.setattr(meta_ads, "SC_BASE", "https://custom.test/meta")
173 assert meta_ads._sc_base() == "https://custom.test/meta"
174 assert meta_ads._search_ads_url() == "https://custom.test/meta/search/ads"
175
176 def test_meta_ads_direct_url_override(self, monkeypatch):
177 monkeypatch.setattr(meta_ads, "SEARCH_ADS_URL", "https://custom.test/direct/search/ads")
178 assert meta_ads._search_ads_url() == "https://custom.test/direct/search/ads"
179
180
181 class TestUsageJournalWithCustomBaseUrl:
182 def test_usage_journal_recognizes_custom_base_url(self, monkeypatch):
183 monkeypatch.setenv(usage.JOURNAL_ENV, "/dev/null")
184 monkeypatch.setenv("SCRAPECREATORS_BASE_URL", "https://api.lurkapi.com")
185 with patch.object(usage, "begin", return_value=("path", "attempt_1", "scrapecreators")) as mock_begin:
186 res = usage.begin_http("https://api.lurkapi.com/v1/reddit/search", "GET")
187 assert res is not None
188 mock_begin.assert_called_once_with("scrapecreators")
189
190
191 class TestPropagateConfigToEnviron:
192 def test_propagates_scrapecreators_base_url(self, monkeypatch):
193 import importlib
194 import sys
195
196 scripts_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "skills", "last30days", "scripts"))
197 if scripts_dir not in sys.path:
198 sys.path.insert(0, scripts_dir)
199 import last30days
200
201 monkeypatch.delenv("SCRAPECREATORS_BASE_URL", raising=False)
202 last30days._propagate_config_to_environ({"SCRAPECREATORS_BASE_URL": "https://api.lurkapi.com"})
203 assert os.environ.get("SCRAPECREATORS_BASE_URL") == "https://api.lurkapi.com"
204
204 lines PYTHON