133 lines
4.6 KiB
Python
133 lines
4.6 KiB
Python
import sqlite3
|
|
import tempfile
|
|
import unittest
|
|
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):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
profile_dir = Path(tmpdir) / "abcd.default-release"
|
|
ls_dir = profile_dir / "storage" / "default" / "https+++www.costco.com" / "ls"
|
|
ls_dir.mkdir(parents=True)
|
|
db_path = ls_dir / "data.sqlite"
|
|
|
|
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-wcs-clientId", "4900eb1f-0c10-4bd9-99c3-c59e6c1ecebf"),
|
|
)
|
|
|
|
entries = browser_session.read_firefox_storage_entries(
|
|
profile_dir,
|
|
origin_filters=["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)
|
|
|
|
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",
|
|
),
|
|
]
|
|
|
|
headers = retailer_sessions.extract_costco_headers(entries)
|
|
|
|
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_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",
|
|
)
|
|
]
|
|
|
|
headers = retailer_sessions.extract_costco_headers(entries)
|
|
|
|
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()],
|
|
), mock.patch.object(
|
|
scrape_costco.click,
|
|
"prompt",
|
|
return_value=Path("/tmp/profile"),
|
|
) as mocked_prompt, mock.patch.object(
|
|
scrape_costco,
|
|
"fetch_summary_windows",
|
|
return_value=(
|
|
{"data": {"receiptsWithCounts": {"receipts": []}}},
|
|
[],
|
|
),
|
|
), mock.patch.object(
|
|
scrape_costco,
|
|
"write_json",
|
|
), mock.patch.object(
|
|
scrape_costco,
|
|
"write_csv",
|
|
):
|
|
scrape_costco.main.callback(
|
|
outdir="/tmp/costco_output",
|
|
document_type="all",
|
|
document_sub_type="all",
|
|
window_days=92,
|
|
months_back=3,
|
|
firefox_profile_dir=None,
|
|
)
|
|
|
|
mocked_prompt.assert_called_once()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|