test_group_filament_rules_3522.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """Group filament rules over the API (#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": "filadmin", "admin_password": "AdminPass1!"},
  12. )
  13. login = await async_client.post("/api/v1/auth/login", json={"username": "filadmin", "password": "AdminPass1!"})
  14. assert login.status_code == 200, login.text
  15. return login.json()["access_token"]
  16. class TestGroupFilamentRules:
  17. async def test_defaults_on_a_new_group(self, async_client):
  18. admin = await _admin_token(async_client)
  19. created = await async_client.post("/api/v1/groups/", headers=_auth(admin), json={"name": "Plain"})
  20. assert created.status_code == 201, created.text
  21. body = created.json()
  22. assert body["restrict_filaments"] is False
  23. assert body["filament_rule_mode"] == "block"
  24. assert body["filament_rules"] == []
  25. async def test_round_trip(self, async_client):
  26. admin = await _admin_token(async_client)
  27. created = await async_client.post(
  28. "/api/v1/groups/",
  29. headers=_auth(admin),
  30. json={
  31. "name": "Friends",
  32. "restrict_filaments": True,
  33. "filament_rule_mode": "block",
  34. "filament_rules": [
  35. {"match": "material", "value": " paht-cf "},
  36. {"match": "filament_id", "value": "GFG03"},
  37. {"match": "material", "value": "PAHT-CF"},
  38. ],
  39. },
  40. )
  41. assert created.status_code == 201, created.text
  42. group_id = created.json()["id"]
  43. assert created.json()["filament_rules"] == [
  44. {"match": "material", "value": "PAHT-CF"},
  45. {"match": "filament_id", "value": "GFG03"},
  46. ]
  47. updated = await async_client.patch(
  48. f"/api/v1/groups/{group_id}",
  49. headers=_auth(admin),
  50. json={"filament_rule_mode": "allow", "filament_rules": [{"match": "material", "value": "PLA"}]},
  51. )
  52. assert updated.status_code == 200, updated.text
  53. detail = (await async_client.get(f"/api/v1/groups/{group_id}", headers=_auth(admin))).json()
  54. assert detail["restrict_filaments"] is True
  55. assert detail["filament_rule_mode"] == "allow"
  56. assert detail["filament_rules"] == [{"match": "material", "value": "PLA"}]
  57. async def test_turning_off_keeps_the_rules(self, async_client):
  58. admin = await _admin_token(async_client)
  59. created = await async_client.post(
  60. "/api/v1/groups/",
  61. headers=_auth(admin),
  62. json={
  63. "name": "Keep",
  64. "restrict_filaments": True,
  65. "filament_rules": [{"match": "material", "value": "PETG"}],
  66. },
  67. )
  68. group_id = created.json()["id"]
  69. assert created.status_code == 201, created.text
  70. off = await async_client.patch(
  71. f"/api/v1/groups/{group_id}", headers=_auth(admin), json={"restrict_filaments": False}
  72. )
  73. assert off.status_code == 200, off.text
  74. assert off.json()["restrict_filaments"] is False
  75. assert off.json()["filament_rules"] == [{"match": "material", "value": "PETG"}]
  76. async def test_bad_rule_kind_is_rejected(self, async_client):
  77. admin = await _admin_token(async_client)
  78. response = await async_client.post(
  79. "/api/v1/groups/",
  80. headers=_auth(admin),
  81. json={"name": "Bad", "filament_rules": [{"match": "colour", "value": "red"}]},
  82. )
  83. assert response.status_code == 422
  84. async def test_whitespace_only_rule_is_rejected(self, async_client):
  85. """Not stored as nothing: a blank allow-list rule would block every filament."""
  86. admin = await _admin_token(async_client)
  87. response = await async_client.post(
  88. "/api/v1/groups/",
  89. headers=_auth(admin),
  90. json={"name": "Blank", "filament_rules": [{"match": "material", "value": " "}]},
  91. )
  92. assert response.status_code == 422, response.text
  93. async def test_administrators_cannot_be_restricted(self, async_client):
  94. admin = await _admin_token(async_client)
  95. groups = (await async_client.get("/api/v1/groups/", headers=_auth(admin))).json()
  96. admins = next(g for g in groups if g["name"] == "Administrators")
  97. response = await async_client.patch(
  98. f"/api/v1/groups/{admins['id']}", headers=_auth(admin), json={"restrict_filaments": True}
  99. )
  100. assert response.status_code == 400