124 lines
4.1 KiB
Python
124 lines
4.1 KiB
Python
from pathlib import Path
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from apple_team_cli.api import AppleAPIError, AppleClient
|
|
from apple_team_cli.config import Settings
|
|
|
|
|
|
@pytest.fixture()
|
|
def settings(tmp_path: Path) -> Settings:
|
|
key = tmp_path / "key.p8"
|
|
key.write_text("-----BEGIN PRIVATE KEY-----\nfake\n", encoding="utf-8")
|
|
return Settings(
|
|
"11111111-2222-4333-8444-555555555555",
|
|
"ABCDE12345",
|
|
key,
|
|
"https://api.example.test",
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def fake_token(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr("apple_team_cli.api.create_token", lambda settings: "short-lived-token")
|
|
|
|
|
|
def user_resource(**overrides: object) -> dict[str, object]:
|
|
attrs: dict[str, object] = {
|
|
"username": "dev@example.com",
|
|
"firstName": "Dev",
|
|
"lastName": "Example",
|
|
"roles": ["ADMIN"],
|
|
"allAppsVisible": True,
|
|
"provisioningAllowed": False,
|
|
}
|
|
attrs.update(overrides)
|
|
return {"type": "users", "id": "user-1", "attributes": attrs}
|
|
|
|
|
|
def test_list_users_and_find_by_email(settings: Settings) -> None:
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
assert request.headers["authorization"] == "Bearer short-lived-token"
|
|
assert request.url.params["fields[users]"]
|
|
return httpx.Response(200, json={"data": [user_resource()], "links": {}})
|
|
|
|
with AppleClient(settings, transport=httpx.MockTransport(handler)) as client:
|
|
users = client.list_users()
|
|
match = client.get_user("DEV@example.com")
|
|
|
|
assert len(users) == 1
|
|
assert match.id == "user-1"
|
|
assert match.provisioning_allowed is False
|
|
|
|
|
|
def test_update_user_uses_json_api_shape(settings: Settings) -> None:
|
|
requests: list[httpx.Request] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
requests.append(request)
|
|
if request.method == "GET":
|
|
return httpx.Response(200, json={"data": [user_resource()], "links": {}})
|
|
body = __import__("json").loads(request.content)
|
|
assert body == {
|
|
"data": {
|
|
"type": "users",
|
|
"id": "user-1",
|
|
"attributes": {"provisioningAllowed": True},
|
|
}
|
|
}
|
|
return httpx.Response(
|
|
200, json={"data": user_resource(provisioningAllowed=True)}
|
|
)
|
|
|
|
with AppleClient(settings, transport=httpx.MockTransport(handler)) as client:
|
|
user = client.get_user("dev@example.com")
|
|
updated = client.update_user(user, provisioning_allowed=True)
|
|
|
|
assert requests[-1].method == "PATCH"
|
|
assert updated.provisioning_allowed
|
|
|
|
|
|
def test_invitation_payload(settings: Settings) -> None:
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
body = __import__("json").loads(request.content)
|
|
attrs = body["data"]["attributes"]
|
|
assert attrs["roles"] == ["DEVELOPER"]
|
|
assert attrs["provisioningAllowed"] is True
|
|
resource = {
|
|
"type": "userInvitations",
|
|
"id": "invite-1",
|
|
"attributes": {**attrs, "expirationDate": "2026-08-29T12:00:00Z"},
|
|
}
|
|
return httpx.Response(201, json={"data": resource})
|
|
|
|
with AppleClient(settings, transport=httpx.MockTransport(handler)) as client:
|
|
invitation = client.invite_user(
|
|
email="new@example.com",
|
|
first_name="New",
|
|
last_name="User",
|
|
roles=["DEVELOPER"],
|
|
provisioning_allowed=True,
|
|
all_apps_visible=True,
|
|
)
|
|
|
|
assert invitation.email == "new@example.com"
|
|
|
|
|
|
def test_apple_errors_are_sanitized(settings: Settings) -> None:
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(
|
|
403,
|
|
json={"errors": [{"title": "Forbidden", "detail": "Role is insufficient"}]},
|
|
)
|
|
|
|
with (
|
|
AppleClient(settings, transport=httpx.MockTransport(handler)) as client,
|
|
pytest.raises(AppleAPIError) as raised,
|
|
):
|
|
client.list_users()
|
|
|
|
assert raised.value.status_code == 403
|
|
assert raised.value.details == ("Forbidden: Role is insufficient",)
|
|
assert "short-lived-token" not in str(raised.value)
|