diff --git a/backend/tests/test_plex_routes.py b/backend/tests/test_plex_routes.py new file mode 100644 index 00000000..a6a51967 --- /dev/null +++ b/backend/tests/test_plex_routes.py @@ -0,0 +1,66 @@ +"""The Plex pages' local reads and the one local-only write, through HTTP. + +The service tests (`test_plex_service.py`) hold what each read and write does; these hold that the +route calls it, commits — the `db` fixture fails a test whose handler left a write uncommitted — +and what a client sees back. None of these reach Plex. Needs the DB. +""" + +from app.plex.models import PlexCollection, PlexItem, PlexLibrary + + +def _seed(db): + lib = PlexLibrary(plex_key="1", title="Movies", kind="movie", enabled=True) + db.add(lib) + db.flush() + db.add_all( + [ + PlexItem(rating_key="m1", library_id=lib.id, kind="movie", title="M"), + PlexCollection(rating_key="c1", library_id=lib.id, title="Mine"), + PlexCollection(rating_key="c2", library_id=lib.id, title="IMDb Top 250"), + ] + ) + db.commit() + + +def test_an_admin_marks_a_collection_editable_and_it_stays(db, make_user, as_user): + _seed(db) + admin = as_user(make_user(role="admin")) + r = admin.post("/api/plex/collections/c1/editable", json={"editable": True}) + assert r.status_code == 200, r.text + assert (r.json()["editable"], r.json()["can_edit"]) == (True, True) + db.expire_all() + listed = admin.get("/api/plex/collections").json()["collections"] + assert {c["id"]: c["editable"] for c in listed} == {"c1": True, "c2": False} + # An auto-list is never editable. + r = admin.post("/api/plex/collections/c2/editable", json={"editable": True}) + assert r.status_code == 400 + assert ( + admin.post("/api/plex/collections/c9/editable", json={"editable": True}).status_code == 404 + ) + + +def test_the_library_list_and_a_movie_page_read_from_the_mirror( + db, make_user, as_user, monkeypatch +): + from app.routes.plex import detail + + _seed(db) + client = as_user(make_user()) + libs = client.get("/api/plex/libraries").json()["libraries"] + assert [(lib["key"], lib["count"]) for lib in libs] == [("1", 1)] + + class _NoPlex: + def __init__(self, db): + from app.plex.client import PlexNotConfigured + + raise PlexNotConfigured() + + monkeypatch.setattr(detail, "PlexClient", _NoPlex) + body = client.get("/api/plex/item/m1").json() + assert (body["id"], body["library"], body["status"], body["collections"]) == ( + "m1", + "1", + "new", + [], + ) + assert client.get("/api/plex/item/nope").json()["detail"]["reason"] == "plex_item"