test_media_token_with_headers_scope.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """A media token decides both who the caller is and which printers they see."""
  2. from __future__ import annotations
  3. import pytest
  4. from httpx import AsyncClient
  5. from backend.tests.integration.test_printer_scope_1727 import _admin_token, _auth, _group, _user
  6. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  7. async def test_headers_next_to_a_media_token_do_not_widen_its_printers(
  8. async_client: AsyncClient, printer_factory, archive_factory
  9. ):
  10. a, b = await printer_factory(name="A"), await printer_factory(name="B")
  11. theirs = await archive_factory(b.id, print_name="Theirs")
  12. ours = await archive_factory(a.id, print_name="Ours")
  13. token = await _admin_token(async_client)
  14. perms = await _group(async_client, token, "perms", permissions=["printers:read", "archives:read_all"])
  15. team = await _group(async_client, token, "team", printer_ids=[a.id])
  16. member_jwt, _ = await _user(async_client, token, "member", [perms, team])
  17. media = await async_client.post("/api/v1/auth/media-token", headers=_auth(member_jwt))
  18. assert media.status_code == 200, media.text
  19. key = await async_client.post(
  20. "/api/v1/api-keys/", headers=_auth(token), json={"name": "k", "can_read_status": True}
  21. )
  22. assert key.status_code == 200, key.text
  23. headers = {"X-API-Key": key.json()["key"]}
  24. def qrcode(archive_id: int):
  25. return async_client.get(
  26. f"/api/v1/archives/{archive_id}/qrcode", params={"token": media.json()["token"]}, headers=headers
  27. )
  28. assert (await qrcode(ours.id)).status_code == 200
  29. assert (await qrcode(theirs.id)).status_code == 404
  30. async def test_a_media_token_next_to_headers_does_not_widen_their_printers(
  31. async_client: AsyncClient, printer_factory, archive_factory
  32. ):
  33. a, b = await printer_factory(name="A"), await printer_factory(name="B")
  34. theirs = await archive_factory(b.id, print_name="Theirs")
  35. ours = await archive_factory(a.id, print_name="Ours")
  36. token = await _admin_token(async_client)
  37. perms = await _group(async_client, token, "perms", permissions=["printers:read", "archives:read_all"])
  38. team = await _group(async_client, token, "team", printer_ids=[a.id])
  39. member_jwt, _ = await _user(async_client, token, "member", [perms, team])
  40. admin_media = (await async_client.post("/api/v1/auth/media-token", headers=_auth(token))).json()["token"]
  41. def get(archive_id: int):
  42. return async_client.get(
  43. f"/api/v1/archives/{archive_id}", params={"token": admin_media}, headers=_auth(member_jwt)
  44. )
  45. assert (await get(ours.id)).status_code == 200
  46. assert (await get(theirs.id)).status_code == 404