Simplify browser session bootstrap
This commit is contained in:
@@ -5,12 +5,11 @@ from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import browser_session
|
||||
import retailer_sessions
|
||||
import scrape_costco
|
||||
|
||||
|
||||
class BrowserSessionTests(unittest.TestCase):
|
||||
def test_read_firefox_ls_entries_reads_storage_from_copied_sqlite(self):
|
||||
def test_read_firefox_local_storage_reads_copied_sqlite(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
profile_dir = Path(tmpdir) / "abcd.default-release"
|
||||
ls_dir = profile_dir / "storage" / "default" / "https+++www.costco.com" / "ls"
|
||||
@@ -24,38 +23,35 @@ class BrowserSessionTests(unittest.TestCase):
|
||||
("costco-x-wcs-clientId", "4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf"),
|
||||
)
|
||||
|
||||
entries = browser_session.read_firefox_storage_entries(
|
||||
values = browser_session.read_firefox_local_storage(
|
||||
profile_dir,
|
||||
origin_filters=["costco.com"],
|
||||
origin_filter="costco.com",
|
||||
)
|
||||
|
||||
self.assertEqual(1, len(entries))
|
||||
self.assertEqual("https://www.costco.com", entries[0].origin)
|
||||
self.assertEqual("costco-x-wcs-clientId", entries[0].key)
|
||||
self.assertEqual(
|
||||
"4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
values["costco-x-wcs-clientId"],
|
||||
)
|
||||
|
||||
def test_extract_costco_headers_uses_exact_keys(self):
|
||||
entries = [
|
||||
browser_session.StorageEntry(
|
||||
origin="https://www.costco.com",
|
||||
key="costco-x-authorization",
|
||||
value="Bearer header.payload.signature",
|
||||
source="memory",
|
||||
),
|
||||
browser_session.StorageEntry(
|
||||
origin="https://www.costco.com",
|
||||
key="costco-x-wcs-clientId",
|
||||
value="4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
source="memory",
|
||||
),
|
||||
browser_session.StorageEntry(
|
||||
origin="https://www.costco.com",
|
||||
key="client-identifier",
|
||||
value="481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
source="memory",
|
||||
),
|
||||
]
|
||||
def test_load_costco_browser_headers_reads_exact_auth_key(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
profile_dir = Path(tmpdir)
|
||||
storage_dir = profile_dir / "storage" / "default" / "https+++www.costco.com" / "ls"
|
||||
storage_dir.mkdir(parents=True)
|
||||
db_path = storage_dir / "data.sqlite"
|
||||
|
||||
headers = retailer_sessions.extract_costco_headers(entries)
|
||||
with sqlite3.connect(db_path) as connection:
|
||||
connection.execute("CREATE TABLE data (key TEXT, value TEXT)")
|
||||
connection.execute(
|
||||
"INSERT INTO data (key, value) VALUES (?, ?)",
|
||||
("costco-x-authorization", "Bearer header.payload.signature"),
|
||||
)
|
||||
|
||||
headers = scrape_costco.load_costco_browser_headers(
|
||||
profile_dir,
|
||||
client_id="4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
client_identifier="481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
)
|
||||
|
||||
self.assertEqual("Bearer header.payload.signature", headers["costco-x-authorization"])
|
||||
self.assertEqual(
|
||||
@@ -67,42 +63,60 @@ class BrowserSessionTests(unittest.TestCase):
|
||||
headers["client-identifier"],
|
||||
)
|
||||
|
||||
def test_extract_costco_headers_uses_exact_json_header_blob(self):
|
||||
entries = [
|
||||
browser_session.StorageEntry(
|
||||
origin="https://www.costco.com",
|
||||
key="headers",
|
||||
value=(
|
||||
'{"costco-x-authorization":"Bearer header.payload.signature",'
|
||||
'"costco-x-wcs-clientId":"4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",'
|
||||
'"client-identifier":"481b1aec-aa3b-454b-b81b-48187e28f205"}'
|
||||
),
|
||||
source="memory",
|
||||
)
|
||||
]
|
||||
def test_load_costco_browser_headers_falls_back_to_exact_header_blob(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
profile_dir = Path(tmpdir)
|
||||
storage_dir = profile_dir / "storage" / "default" / "https+++www.costco.com" / "ls"
|
||||
storage_dir.mkdir(parents=True)
|
||||
db_path = storage_dir / "data.sqlite"
|
||||
|
||||
headers = retailer_sessions.extract_costco_headers(entries)
|
||||
with sqlite3.connect(db_path) as connection:
|
||||
connection.execute("CREATE TABLE data (key TEXT, value TEXT)")
|
||||
connection.execute(
|
||||
"INSERT INTO data (key, value) VALUES (?, ?)",
|
||||
(
|
||||
"headers",
|
||||
'{"costco-x-authorization":"Bearer header.payload.signature"}',
|
||||
),
|
||||
)
|
||||
|
||||
headers = scrape_costco.load_costco_browser_headers(
|
||||
profile_dir,
|
||||
client_id="4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
client_identifier="481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
)
|
||||
|
||||
self.assertEqual("Bearer header.payload.signature", headers["costco-x-authorization"])
|
||||
self.assertEqual(
|
||||
"4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
headers["costco-x-wcs-clientId"],
|
||||
)
|
||||
self.assertEqual(
|
||||
"481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
headers["client-identifier"],
|
||||
)
|
||||
|
||||
def test_scrape_costco_prompts_for_profile_dir_when_autodiscovery_fails(self):
|
||||
with mock.patch.object(
|
||||
scrape_costco,
|
||||
"build_session",
|
||||
side_effect=[FileNotFoundError("no default profile"), object()],
|
||||
"find_firefox_profile_dir",
|
||||
side_effect=FileNotFoundError("no default profile"),
|
||||
), mock.patch.object(
|
||||
scrape_costco.click,
|
||||
"prompt",
|
||||
return_value=Path("/tmp/profile"),
|
||||
) as mocked_prompt, mock.patch.object(
|
||||
scrape_costco,
|
||||
"load_config",
|
||||
return_value={
|
||||
"client_id": "4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
"client_identifier": "481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
},
|
||||
), mock.patch.object(
|
||||
scrape_costco,
|
||||
"load_costco_browser_headers",
|
||||
return_value={
|
||||
"costco-x-authorization": "Bearer header.payload.signature",
|
||||
"costco-x-wcs-clientId": "4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
"client-identifier": "481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
},
|
||||
), mock.patch.object(
|
||||
scrape_costco,
|
||||
"build_session",
|
||||
return_value=object(),
|
||||
), mock.patch.object(
|
||||
scrape_costco,
|
||||
"fetch_summary_windows",
|
||||
return_value=(
|
||||
|
||||
@@ -411,6 +411,25 @@ class CostcoPipelineTests(unittest.TestCase):
|
||||
]
|
||||
|
||||
with mock.patch.object(
|
||||
scrape_costco,
|
||||
"load_config",
|
||||
return_value={
|
||||
"client_id": "4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
"client_identifier": "481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
},
|
||||
), mock.patch.object(
|
||||
scrape_costco,
|
||||
"find_firefox_profile_dir",
|
||||
return_value=Path("/tmp/profile"),
|
||||
), mock.patch.object(
|
||||
scrape_costco,
|
||||
"load_costco_browser_headers",
|
||||
return_value={
|
||||
"costco-x-authorization": "Bearer header.payload.signature",
|
||||
"costco-x-wcs-clientId": "4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf",
|
||||
"client-identifier": "481b1aec-aa3b-454b-b81b-48187e28f205",
|
||||
},
|
||||
), mock.patch.object(
|
||||
scrape_costco, "build_session", return_value=object()
|
||||
), mock.patch.object(
|
||||
scrape_costco,
|
||||
|
||||
Reference in New Issue
Block a user