|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from types import SimpleNamespace |
| 4 | + |
3 | 5 | import pytest |
4 | 6 |
|
5 | | -from flexmeasures.auth.policy import user_matches_principals, can_modify_role |
| 7 | +from flexmeasures.auth.policy import ( |
| 8 | + CONSULTANT_ROLE, |
| 9 | + can_modify_role, |
| 10 | + user_matches_principals, |
| 11 | +) |
6 | 12 |
|
7 | 13 |
|
8 | 14 | class MockAccount: |
@@ -194,3 +200,41 @@ def test_can_modify_role( |
194 | 200 | assert ( |
195 | 201 | can_modify_role(mock_user, roles_to_modify, modified_user) == can_modify_roles |
196 | 202 | ) |
| 203 | + |
| 204 | + |
| 205 | +@pytest.mark.parametrize( |
| 206 | + "mock_user, modified_user, roles_to_modify", |
| 207 | + [ |
| 208 | + # Empty role changes are not explicitly allowed. |
| 209 | + ( |
| 210 | + make_mock_user(19, ["consultant"], 1, []), |
| 211 | + make_mock_user(20, ["account-admin"], 2, [], 1), |
| 212 | + [], |
| 213 | + ), |
| 214 | + # None is not an explicitly supported role. |
| 215 | + ( |
| 216 | + make_mock_user(19, ["consultant"], 1, []), |
| 217 | + make_mock_user(21, ["account-admin"], 2, [], 1), |
| 218 | + [None], |
| 219 | + ), |
| 220 | + # Unsupported roles must not be allowed by falling through supported checks. |
| 221 | + ( |
| 222 | + make_mock_user(19, ["consultant"], 1, []), |
| 223 | + make_mock_user(22, [], 2, [], 1), |
| 224 | + [SimpleNamespace(name="unsupported-role")], |
| 225 | + ), |
| 226 | + # Every requested role must be explicitly allowed, so one unsupported role denies the whole change. |
| 227 | + ( |
| 228 | + make_mock_user(19, ["admin"], 1, []), |
| 229 | + make_mock_user(23, ["consultant"], 1, []), |
| 230 | + [ |
| 231 | + SimpleNamespace(name="unsupported-role"), |
| 232 | + SimpleNamespace(name=CONSULTANT_ROLE), |
| 233 | + ], |
| 234 | + ), |
| 235 | + ], |
| 236 | +) |
| 237 | +def test_can_modify_role_denies_unexplicit_role_changes( |
| 238 | + db, setup_roles_users, mock_user, modified_user, roles_to_modify |
| 239 | +): |
| 240 | + assert can_modify_role(mock_user, roles_to_modify, modified_user) is False |
0 commit comments