test_principal_matching.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from __future__ import annotations
  2. import pytest
  3. from flexmeasures.auth.policy import user_matches_principals, can_modify_role
  4. class MockAccount:
  5. id: int
  6. account_roles: list[str]
  7. def __init__(self, id, roles):
  8. self.id = id
  9. self.account_roles = roles
  10. def has_role(self, role):
  11. return role in self.account_roles
  12. class MockUser:
  13. id: int
  14. roles: list[str]
  15. account: MockAccount
  16. def __init__(self, id, username, roles, account):
  17. self.id = id
  18. self.username = username
  19. self.roles = roles
  20. self.account = account
  21. def has_role(self, role):
  22. return role in self.roles
  23. def make_mock_user(
  24. user_id: int, user_roles: list[str], account_id: int, account_roles: list[str]
  25. ) -> MockUser:
  26. account = MockAccount(account_id, account_roles)
  27. return MockUser(id=user_id, username="Tester", roles=user_roles, account=account)
  28. @pytest.mark.parametrize(
  29. "mock_user,principals,should_match",
  30. [
  31. (make_mock_user(19, [], 1, []), "user:19", True),
  32. (make_mock_user(19, [], 1, []), "user:28", False),
  33. (make_mock_user(19, ["gardener"], 1, []), "role:gardener", True),
  34. (
  35. make_mock_user(19, ["gardener"], 1, ["castle"]),
  36. ("role:gardener", "account-role:castle"),
  37. True,
  38. ),
  39. (
  40. make_mock_user(19, ["gardener"], 1, ["castle"]),
  41. ("role:gardener", "account-role:villa"),
  42. False,
  43. ),
  44. (make_mock_user(19, [], 113, []), "account:114", False),
  45. (make_mock_user(19, [], 113, []), "account:113", True),
  46. (
  47. make_mock_user(19, ["waitress"], 113, ["restaurant"]),
  48. ("user:19", "account:113", "role:waitress", "account-role:restaurant"),
  49. True,
  50. ),
  51. (
  52. make_mock_user(19, ["waitress"], 113, ["hotel"]),
  53. ("user:13", "account:113", "role:waitress", "role:chef"),
  54. False,
  55. ),
  56. (
  57. make_mock_user(19, ["waitress", "chef"], 113, ["hotel", "cinema"]),
  58. (
  59. "user:19",
  60. "account:113",
  61. "role:waitress",
  62. "role:chef",
  63. "account-role:hotel",
  64. "account-role:cinema",
  65. ),
  66. True,
  67. ),
  68. (
  69. make_mock_user(19, ["waitress"], 113, ["hotel"]),
  70. ["user:13", ("account:113", "role:waitress", "role:chef")],
  71. False, # not user 13; well a waitress, but not also a chef of hotel 113
  72. ),
  73. (
  74. make_mock_user(19, ["waitress"], 113, ["hotel"]),
  75. ["user:13", ("account:113", "role:waitress"), "role:chef"],
  76. True, # not user 13; well a waitress of hotel 113 -
  77. ),
  78. ],
  79. )
  80. def test_principals_match(mock_user, principals, should_match):
  81. assert user_matches_principals(mock_user, principals) == should_match
  82. @pytest.mark.parametrize(
  83. "mock_user, roles_to_modify, can_modify_roles",
  84. [
  85. # Admin user should be able to modify (admin-reader & consultant) roles
  86. (make_mock_user(19, ["admin"], 1, []), [3, 4], True),
  87. # Consultant user should not be able to modify (admin-reader) role
  88. (make_mock_user(19, ["consultant"], 1, []), [3], False),
  89. # Admin-reader user should not be able to modify (admin-reader) role
  90. (
  91. make_mock_user(19, ["admin-reader"], 1, []),
  92. [3],
  93. False,
  94. ),
  95. # Account-admin user should not be able to modify (admin-reader) role
  96. (
  97. make_mock_user(19, ["account-admin"], 1, []),
  98. [3],
  99. False,
  100. ),
  101. # Account-admin user should be able to modify (consultant) role
  102. (
  103. make_mock_user(19, ["account-admin"], 1, []),
  104. [4],
  105. True,
  106. ),
  107. ],
  108. )
  109. def test_can_modify_role(
  110. db, setup_roles_users, mock_user, roles_to_modify, can_modify_roles
  111. ):
  112. assert can_modify_role(mock_user, roles_to_modify) == can_modify_roles