Files
FabledCurator/tests/test_credential_service.py
T
bvandeusen 8243740a04 fix(subscribestar): inject 18_plus_agreement_generic age cookie to bypass server gate
Operator-flagged 2026-05-27: subscribestar source check aborted with
`AbortExtraction: HTTP redirect to .../age_confirmation_warning`. The
captured `_personalization_id` cookie in the browser-stored file had
expired (annual rotation), and the user could not realistically refresh
it: SubscribeStar's frontend JS uses localStorage to suppress the
age-confirmation popup once dismissed, so a logged-in revisit doesn't
re-show the popup and the server-side cookie is never re-issued.

gallery-dl's own login flow (which FC doesn't exercise — cookies come
from the extension instead) sidesteps this by manually setting
`18_plus_agreement_generic=true` on `.subscribestar.adult`. The server
accepts that as the age-confirmation marker.

`credential_service._augment_cookies(platform, netscape)` mirrors that
behavior: when the materialized cookies file is for subscribestar and
the age cookie isn't already present, append a synthetic line for
`.subscribestar.adult` with name=`18_plus_agreement_generic` value=`true`
and a far-future expiry. No-op for other platforms; no-op if the cookie
is already present (idempotent for manual pastes / extension captures
that happen to include it).

Three new tests pin: (a) injection fires for subscribestar, preserves
existing cookies; (b) idempotent when already present (no double
injection); (c) does NOT fire for non-subscribestar platforms (Patreon
etc. don't get a foreign-domain cookie).

Not a curator handling bug per se — the extension faithfully captured
what the browser had. This is mirroring a documented gallery-dl
workaround so the cookies-via-extension auth path doesn't degrade as the
server-side cookie expires.
2026-05-27 18:18:04 -04:00

176 lines
6.5 KiB
Python

import pytest
from sqlalchemy import select
from backend.app.models import Credential
from backend.app.services.credential_crypto import CredentialCrypto
from backend.app.services.credential_service import (
CredentialService,
EmptyDataError,
UnknownPlatformError,
WrongAuthTypeError,
)
pytestmark = pytest.mark.integration
_NETSCAPE = (
"# Netscape HTTP Cookie File\n"
".patreon.com\tTRUE\t/\tTRUE\t1700000000\tsession_id\tabc\n"
)
@pytest.fixture
def crypto(tmp_path):
return CredentialCrypto(tmp_path / "k")
@pytest.mark.asyncio
async def test_upsert_creates_then_updates(db, crypto):
svc = CredentialService(db, crypto)
rec = await svc.upsert(
platform="patreon", credential_type="cookies", data=_NETSCAPE,
)
assert rec.platform == "patreon"
assert rec.credential_type == "cookies"
again = await svc.upsert(
platform="patreon", credential_type="cookies", data=_NETSCAPE + "x",
)
assert again.platform == "patreon"
rows = (await db.execute(
select(Credential).where(Credential.platform == "patreon")
)).scalars().all()
assert len(rows) == 1
@pytest.mark.asyncio
async def test_upsert_rejects_unknown_platform(db, crypto):
svc = CredentialService(db, crypto)
with pytest.raises(UnknownPlatformError):
await svc.upsert(platform="fanbox", credential_type="cookies", data="x")
@pytest.mark.asyncio
async def test_upsert_rejects_wrong_auth_type(db, crypto):
svc = CredentialService(db, crypto)
# discord is token-only
with pytest.raises(WrongAuthTypeError):
await svc.upsert(platform="discord", credential_type="cookies", data="x")
# patreon is cookies-only
with pytest.raises(WrongAuthTypeError):
await svc.upsert(platform="patreon", credential_type="token", data="x")
@pytest.mark.asyncio
async def test_upsert_rejects_empty_data(db, crypto):
svc = CredentialService(db, crypto)
with pytest.raises(EmptyDataError):
await svc.upsert(platform="patreon", credential_type="cookies", data=" ")
@pytest.mark.asyncio
async def test_list_excludes_blob(db, crypto):
svc = CredentialService(db, crypto)
await svc.upsert(platform="patreon", credential_type="cookies", data=_NETSCAPE)
records = await svc.list()
assert len(records) == 1
d = records[0].to_dict()
assert "data" not in d
assert "encrypted_blob" not in d
@pytest.mark.asyncio
async def test_delete(db, crypto):
svc = CredentialService(db, crypto)
await svc.upsert(platform="patreon", credential_type="cookies", data=_NETSCAPE)
await svc.delete("patreon")
assert await svc.get("patreon") is None
@pytest.mark.asyncio
async def test_get_cookies_path_writes_usable_file(db, crypto, tmp_path):
svc = CredentialService(db, crypto, cookies_dir=tmp_path / "cookies")
await svc.upsert(platform="patreon", credential_type="cookies", data=_NETSCAPE)
path = await svc.get_cookies_path("patreon")
assert path is not None and path.exists()
contents = path.read_text()
assert ".patreon.com" in contents
assert "session_id" in contents
@pytest.mark.asyncio
async def test_get_cookies_path_none_for_missing(db, crypto, tmp_path):
svc = CredentialService(db, crypto, cookies_dir=tmp_path)
assert await svc.get_cookies_path("patreon") is None
@pytest.mark.asyncio
async def test_get_cookies_path_none_for_token_kind(db, crypto, tmp_path):
svc = CredentialService(db, crypto, cookies_dir=tmp_path)
await svc.upsert(platform="discord", credential_type="token", data="tok")
assert await svc.get_cookies_path("discord") is None
@pytest.mark.asyncio
async def test_get_cookies_path_subscribestar_injects_age_cookie(db, crypto, tmp_path):
"""SubscribeStar's server gates artist pages behind a _personalization_id
cookie; the browser-stored cookie expires annually and can't be easily
refreshed (the JS age popup is suppressed by localStorage). Mirror
gallery-dl's own login-flow workaround by injecting
`18_plus_agreement_generic=true` on `.subscribestar.adult` whenever
cookies for subscribestar are materialized."""
netscape_in = (
"# Netscape HTTP Cookie File\n"
".subscribestar.adult\tTRUE\t/\tTRUE\t1700000000\tsession_id\txyz\n"
)
svc = CredentialService(db, crypto, cookies_dir=tmp_path / "cookies")
await svc.upsert(platform="subscribestar", credential_type="cookies", data=netscape_in)
path = await svc.get_cookies_path("subscribestar")
contents = path.read_text()
assert "18_plus_agreement_generic\ttrue" in contents
assert ".subscribestar.adult" in contents
# Original session_id cookie preserved.
assert "session_id\txyz" in contents
@pytest.mark.asyncio
async def test_get_cookies_path_subscribestar_idempotent_when_present(db, crypto, tmp_path):
"""If the operator's captured cookies ALREADY contain the age cookie
(e.g. a manual paste, or a re-login), don't double-inject."""
netscape_in = (
"# Netscape HTTP Cookie File\n"
".subscribestar.adult\tTRUE\t/\tTRUE\t1700000000\t18_plus_agreement_generic\ttrue\n"
".subscribestar.adult\tTRUE\t/\tTRUE\t1700000000\tsession_id\txyz\n"
)
svc = CredentialService(db, crypto, cookies_dir=tmp_path / "cookies")
await svc.upsert(platform="subscribestar", credential_type="cookies", data=netscape_in)
path = await svc.get_cookies_path("subscribestar")
contents = path.read_text()
assert contents.count("18_plus_agreement_generic") == 1
@pytest.mark.asyncio
async def test_get_cookies_path_non_subscribestar_unchanged(db, crypto, tmp_path):
"""The age-cookie injection MUST NOT fire for non-subscribestar
platforms — Patreon/HF/etc. don't need it and shouldn't carry a
foreign-domain cookie in their cookies.txt."""
svc = CredentialService(db, crypto, cookies_dir=tmp_path / "cookies")
await svc.upsert(platform="patreon", credential_type="cookies", data=_NETSCAPE)
path = await svc.get_cookies_path("patreon")
contents = path.read_text()
assert "18_plus_agreement_generic" not in contents
assert "subscribestar" not in contents
@pytest.mark.asyncio
async def test_get_token_decrypts(db, crypto):
svc = CredentialService(db, crypto)
await svc.upsert(platform="discord", credential_type="token", data="my-token")
assert await svc.get_token("discord") == "my-token"
@pytest.mark.asyncio
async def test_get_token_none_for_cookies_kind(db, crypto):
svc = CredentialService(db, crypto)
await svc.upsert(platform="patreon", credential_type="cookies", data=_NETSCAPE)
assert await svc.get_token("patreon") is None