|
4 | 4 | Note: These will fail if the test user's permissions are too elevated. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import base64 |
| 8 | +import json |
7 | 9 | import os |
| 10 | +from unittest.mock import AsyncMock, MagicMock |
8 | 11 |
|
9 | 12 | from fastapi.security import HTTPAuthorizationCredentials |
10 | 13 | import pytest |
11 | 14 |
|
12 | 15 | from ..auth import User |
13 | | -from .fixtures import requires_token |
| 16 | +from .fixtures import requires_group_token, requires_token |
14 | 17 |
|
15 | 18 |
|
16 | 19 |
|
|
19 | 22 | ) |
20 | 23 |
|
21 | 24 |
|
| 25 | +def _make_token(payload: dict) -> HTTPAuthorizationCredentials: |
| 26 | + """Build a syntactically-valid JWT whose payload decodes to the given dict. |
| 27 | + Not signed — only suitable for tests that exercise _decode_jwt_payload.""" |
| 28 | + def b64(b: bytes) -> str: |
| 29 | + return base64.urlsafe_b64encode(b).rstrip(b"=").decode() |
| 30 | + header = b64(b'{"alg":"none","typ":"JWT"}') |
| 31 | + body = b64(json.dumps(payload).encode()) |
| 32 | + return HTTPAuthorizationCredentials( |
| 33 | + scheme="Bearer", credentials=f"{header}.{body}.sig" |
| 34 | + ) |
| 35 | + |
| 36 | + |
22 | 37 | @pytest.mark.asyncio |
23 | 38 | @requires_token |
24 | 39 | async def test_user__is_admin(): |
@@ -60,3 +75,132 @@ async def test_can_edit_collab(): |
60 | 75 | user = User(token, allow_anonymous=False) |
61 | 76 | can_edit = await user.can_edit_collab("model-validation") |
62 | 77 | assert not can_edit |
| 78 | + |
| 79 | + |
| 80 | +# --- Unit tests for group-based role resolution (no live IDM calls) --- |
| 81 | + |
| 82 | + |
| 83 | +def test_get_groups_from_jwt(): |
| 84 | + user = User( |
| 85 | + _make_token({"sub": "1", "preferred_username": "alice", |
| 86 | + "group": ["unit-x", "unit-y"]}), |
| 87 | + allow_anonymous=False, |
| 88 | + ) |
| 89 | + assert user.get_groups() == {"unit-x", "unit-y"} |
| 90 | + |
| 91 | + |
| 92 | +def test_get_groups_absent_claim_returns_empty_set(): |
| 93 | + user = User( |
| 94 | + _make_token({"sub": "1", "preferred_username": "alice"}), |
| 95 | + allow_anonymous=False, |
| 96 | + ) |
| 97 | + assert user.get_groups() == set() |
| 98 | + |
| 99 | + |
| 100 | +def _mock_client(*, json_data=None, status_code=200): |
| 101 | + resp = MagicMock() |
| 102 | + resp.status_code = status_code |
| 103 | + resp.json.return_value = json_data or [] |
| 104 | + resp.raise_for_status = MagicMock() |
| 105 | + client = MagicMock() |
| 106 | + client.get = AsyncMock(return_value=resp) |
| 107 | + return client |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.asyncio |
| 111 | +async def test_has_role_via_group_matches(): |
| 112 | + user = User( |
| 113 | + _make_token({"sub": "1", "preferred_username": "alice", |
| 114 | + "group": ["unit-x"]}), |
| 115 | + allow_anonymous=False, |
| 116 | + ) |
| 117 | + client = _mock_client(json_data=[{"name": "unit-x"}, {"name": "other"}]) |
| 118 | + assert await user._has_role_via_group("administrator", "foo", client) is True |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +async def test_has_role_via_group_no_intersection(): |
| 123 | + user = User( |
| 124 | + _make_token({"sub": "1", "preferred_username": "alice", |
| 125 | + "group": ["unit-z"]}), |
| 126 | + allow_anonymous=False, |
| 127 | + ) |
| 128 | + client = _mock_client(json_data=[{"name": "unit-x"}]) |
| 129 | + assert await user._has_role_via_group("administrator", "foo", client) is False |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.asyncio |
| 133 | +async def test_has_role_via_group_empty_groups_skips_call(): |
| 134 | + user = User( |
| 135 | + _make_token({"sub": "1", "preferred_username": "alice"}), |
| 136 | + allow_anonymous=False, |
| 137 | + ) |
| 138 | + client = _mock_client() |
| 139 | + assert await user._has_role_via_group("administrator", "foo", client) is False |
| 140 | + client.get.assert_not_called() |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.asyncio |
| 144 | +async def test_has_role_via_group_404_returns_false(): |
| 145 | + user = User( |
| 146 | + _make_token({"sub": "1", "preferred_username": "alice", |
| 147 | + "group": ["unit-x"]}), |
| 148 | + allow_anonymous=False, |
| 149 | + ) |
| 150 | + client = _mock_client(status_code=404) |
| 151 | + assert await user._has_role_via_group("administrator", "foo", client) is False |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.asyncio |
| 155 | +async def test_user_has_role_direct_only(): |
| 156 | + user = User( |
| 157 | + _make_token({"sub": "1", "preferred_username": "alice"}), |
| 158 | + allow_anonymous=False, |
| 159 | + ) |
| 160 | + user._has_role = AsyncMock(return_value=True) |
| 161 | + user._has_role_via_group = AsyncMock(return_value=False) |
| 162 | + assert await user._user_has_role("administrator", "foo", MagicMock()) is True |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.asyncio |
| 166 | +async def test_user_has_role_via_group_only(): |
| 167 | + user = User( |
| 168 | + _make_token({"sub": "1", "preferred_username": "alice", |
| 169 | + "group": ["unit-x"]}), |
| 170 | + allow_anonymous=False, |
| 171 | + ) |
| 172 | + user._has_role = AsyncMock(return_value=False) |
| 173 | + user._has_role_via_group = AsyncMock(return_value=True) |
| 174 | + assert await user._user_has_role("administrator", "foo", MagicMock()) is True |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.asyncio |
| 178 | +async def test_user_has_role_neither(): |
| 179 | + user = User( |
| 180 | + _make_token({"sub": "1", "preferred_username": "alice"}), |
| 181 | + allow_anonymous=False, |
| 182 | + ) |
| 183 | + user._has_role = AsyncMock(return_value=False) |
| 184 | + user._has_role_via_group = AsyncMock(return_value=False) |
| 185 | + assert await user._user_has_role("administrator", "foo", MagicMock()) is False |
| 186 | + |
| 187 | + |
| 188 | +# --- Integration test: requires a token whose holder gets admin via a group --- |
| 189 | + |
| 190 | + |
| 191 | +@pytest.mark.asyncio |
| 192 | +@requires_group_token |
| 193 | +async def test_user_teams_includes_group_conferred_admin(): |
| 194 | + """With a token from a user who holds administrator on a collab via group |
| 195 | + membership (e.g. closed-loop-motor-t3-4), the collab should be reported as |
| 196 | + administrator, not viewer.""" |
| 197 | + group_token = HTTPAuthorizationCredentials( |
| 198 | + scheme="Bearer", |
| 199 | + credentials=os.environ["VF_GROUP_TEST_TOKEN"], |
| 200 | + ) |
| 201 | + expected_collab = os.environ.get( |
| 202 | + "VF_GROUP_TEST_COLLAB", "closed-loop-motor-t3-4" |
| 203 | + ) |
| 204 | + user = User(group_token, allow_anonymous=False) |
| 205 | + teams = await user.get_teams() |
| 206 | + assert f"collab-{expected_collab}-administrator" in teams |
0 commit comments