Files
siftlode/backend/tests/test_catalog_routes.py
T
peter 85fd9b6704 feat(channels): an Added column, and About cards that wait for a resting pointer
The channel manager lists when each follow reached the list (the
subscription import that first saw it, or a subscribe made here) as a
sortable Added date after Last upload. The About cell's card opens after the
pointer rests on it for half a second instead of at once, and a scroll puts
an open one away.
2026-11-01 01:30:30 +01:00

95 lines
3.8 KiB
Python

"""The channel manager's local writes, through HTTP: each one lands and is committed.
The service tests (`test_catalog_service.py`) hold what each write does; these hold that the route
calls it and commits — the `db` fixture fails a test whose handler left a write uncommitted — and
what a client sees back. None of these reach YouTube. Needs the DB.
"""
from sqlalchemy import select
from app.catalog.models import BlockedChannel, Channel, Subscription
from app.reader.models import ChannelTag, Tag
def _seed(db, user):
db.add(Channel(id="UCr", title="R"))
db.flush()
db.add(Subscription(user_id=user.id, channel_id="UCr"))
tag = Tag(user_id=user.id, name="Mine")
db.add(tag)
db.commit()
return tag
def test_a_subscriptions_settings_change_and_list_back(db, make_user, as_user):
user = make_user()
_seed(db, user)
client = as_user(user)
r = client.patch("/api/channels/UCr", json={"priority": 2, "hidden": True})
assert r.status_code == 200, r.text
assert (r.json()["priority"], r.json()["hidden"]) == (2, True)
listed = client.get("/api/channels").json()
assert [(c["id"], c["priority"], c["hidden"]) for c in listed] == [("UCr", 2, True)]
r = client.post("/api/sync/deep-all?on=false")
assert r.json() == {"ok": True, "updated": 1, "deep_requested": False}
def test_a_block_and_an_unblock(db, make_user, as_user):
user = make_user()
_seed(db, user)
client = as_user(user)
assert client.post("/api/channels/UCr/block").json() == {"ok": True, "blocked": "UCr"}
assert client.post("/api/channels/UCr/block").status_code == 200 # idempotent
assert [c["id"] for c in client.get("/api/channels/blocked/list").json()] == ["UCr"]
assert client.get("/api/channels/UCr").json()["blocked"] is True
assert client.delete("/api/channels/UCr/block").json() == {"ok": True, "unblocked": "UCr"}
db.expire_all()
assert db.execute(select(BlockedChannel)).first() is None
def test_a_tag_attached_to_a_channel_and_detached(db, make_user, as_user):
user, stranger = make_user(), make_user()
tag = _seed(db, user)
client = as_user(user)
r = client.post("/api/channels/UCr/tags", json={"tag_id": tag.id})
assert r.status_code == 200, r.text
assert client.post("/api/channels/UCr/tags", json={"tag_id": tag.id}).status_code == 200
assert client.get("/api/channels/UCr").json()["tag_ids"] == [tag.id]
db.add(Subscription(user_id=stranger.id, channel_id="UCr"))
db.commit()
# Another user's tag is not theirs to attach: the same 404 as a missing one.
r = as_user(stranger).post("/api/channels/UCr/tags", json={"tag_id": tag.id})
assert r.json()["detail"] == {"code": "not_found", "reason": "tag"}
assert client.delete(f"/api/channels/UCr/tags/{tag.id}").status_code == 200
db.expire_all()
assert db.execute(select(ChannelTag)).first() is None
def test_the_sync_status_counts_the_callers_subscriptions(db, make_user, as_user):
user = make_user()
_seed(db, user)
body = as_user(user).get("/api/sync/status").json()
assert (body["subscriptions"], body["channels_total"]) == (1, 1)
mine = as_user(user).get("/api/sync/my-status").json()
assert (mine["channels_total"], mine["channels_recent_pending"]) == (1, 1)
def test_a_channel_lists_when_it_entered_this_users_list_not_when_the_channel_did(
db, make_user, as_user
):
from datetime import datetime, timezone
user = make_user()
db.add(Channel(id="UCa", title="A", created_at=datetime(2026, 6, 10, tzinfo=timezone.utc)))
db.flush()
db.add(
Subscription(
user_id=user.id,
channel_id="UCa",
created_at=datetime(2026, 10, 12, 8, 30, tzinfo=timezone.utc),
)
)
db.commit()
listed = as_user(user).get("/api/channels").json()
assert [(c["id"], c["added_at"]) for c in listed] == [("UCa", "2026-10-12T08:30:00+00:00")]