test_auth_me_filament_policies_3522.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """/auth/me tells the browser the caller's filament rules (#3522)."""
  2. from __future__ import annotations
  3. import pytest
  4. from httpx import AsyncClient
  5. pytestmark = [pytest.mark.asyncio, pytest.mark.integration]
  6. def _auth(jwt: str) -> dict[str, str]:
  7. return {"Authorization": f"Bearer {jwt}"}
  8. async def _admin_token(async_client: AsyncClient) -> str:
  9. await async_client.post(
  10. "/api/v1/auth/setup",
  11. json={"auth_enabled": True, "admin_username": "meadmin", "admin_password": "AdminPass1!"},
  12. )
  13. login = await async_client.post("/api/v1/auth/login", json={"username": "meadmin", "password": "AdminPass1!"})
  14. return login.json()["access_token"]
  15. async def _login(async_client: AsyncClient, admin: str, username: str, group_ids: list[int]) -> str:
  16. created = await async_client.post(
  17. "/api/v1/users/",
  18. headers=_auth(admin),
  19. json={"username": username, "password": "UserPass1!", "group_ids": group_ids},
  20. )
  21. assert created.status_code in (200, 201), created.text
  22. login = await async_client.post("/api/v1/auth/login", json={"username": username, "password": "UserPass1!"})
  23. return login.json()["access_token"]
  24. async def _friends_group(async_client: AsyncClient, admin: str) -> int:
  25. friends = await async_client.post(
  26. "/api/v1/groups/",
  27. headers=_auth(admin),
  28. json={
  29. "name": "friends",
  30. "permissions": ["api_keys:create", "users:read"],
  31. "restrict_filaments": True,
  32. "filament_rule_mode": "block",
  33. "filament_rules": [{"match": "filament_id", "value": "GFG03"}],
  34. },
  35. )
  36. assert friends.status_code == 201, friends.text
  37. return friends.json()["id"]
  38. FRIENDS_POLICY = [{"group": "friends", "mode": "block", "rules": [{"match": "filament_id", "value": "GFG03"}]}]
  39. async def _my_policies(async_client: AsyncClient, headers: dict[str, str]) -> list[dict] | None:
  40. me = await async_client.get("/api/v1/auth/me", headers=headers)
  41. assert me.status_code == 200, me.text
  42. return me.json()["filament_policies"]
  43. class TestMeFilamentPolicies:
  44. async def test_admin_is_unrestricted(self, async_client):
  45. admin = await _admin_token(async_client)
  46. assert await _my_policies(async_client, _auth(admin)) is None
  47. async def test_user_in_no_restricted_group_is_unrestricted(self, async_client):
  48. admin = await _admin_token(async_client)
  49. plain = await async_client.post("/api/v1/groups/", headers=_auth(admin), json={"name": "plain"})
  50. jwt = await _login(async_client, admin, "plainuser", [plain.json()["id"]])
  51. assert await _my_policies(async_client, _auth(jwt)) is None
  52. async def test_restricted_member_gets_their_groups_rules(self, async_client):
  53. admin = await _admin_token(async_client)
  54. jwt = await _login(async_client, admin, "friend", [await _friends_group(async_client, admin)])
  55. assert await _my_policies(async_client, _auth(jwt)) == FRIENDS_POLICY
  56. class TestOtherUserResponses:
  57. """null means unrestricted, so every builder of the response must fill it (#3522)."""
  58. async def test_an_api_key_reports_its_owners_rules(self, async_client):
  59. admin = await _admin_token(async_client)
  60. jwt = await _login(async_client, admin, "friend", [await _friends_group(async_client, admin)])
  61. created = await async_client.post("/api/v1/api-keys/", headers=_auth(jwt), json={"name": "native"})
  62. assert created.status_code in (200, 201), created.text
  63. assert await _my_policies(async_client, {"X-API-Key": created.json()["key"]}) == FRIENDS_POLICY
  64. async def test_the_user_list_reports_each_users_rules(self, async_client):
  65. admin = await _admin_token(async_client)
  66. await _login(async_client, admin, "friend", [await _friends_group(async_client, admin)])
  67. users = (await async_client.get("/api/v1/users/", headers=_auth(admin))).json()
  68. by_name = {u["username"]: u for u in users}
  69. assert by_name["friend"]["filament_policies"] == FRIENDS_POLICY
  70. assert by_name["meadmin"]["filament_policies"] is None