Initial
This commit is contained in:
22 files changed
+1578
No files matched your search
@@ -0,0 +1,123 @@
|
||||
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)
|
||||
@@ -0,0 +1,74 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from apple_team_cli.config import ConfigError, Settings, write_config
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def fake_key(tmp_path: Path) -> Path:
|
||||
key = tmp_path / "AuthKey_ABCDE12345.p8"
|
||||
key.write_text("-----BEGIN PRIVATE KEY-----\nnot-a-real-key\n", encoding="utf-8")
|
||||
key.chmod(0o600)
|
||||
return key
|
||||
|
||||
|
||||
def test_write_and_load_config(
|
||||
fake_key: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
config_path = tmp_path / ".env"
|
||||
write_config(
|
||||
"11111111-2222-4333-8444-555555555555",
|
||||
"ABCDE12345",
|
||||
fake_key,
|
||||
config_path,
|
||||
)
|
||||
for name in ("ASC_ISSUER_ID", "ASC_KEY_ID", "ASC_PRIVATE_KEY_PATH", "ASC_BASE_URL"):
|
||||
monkeypatch.delenv(name, raising=False)
|
||||
|
||||
monkeypatch.chdir(tmp_path)
|
||||
settings = Settings.load(tmp_path / "missing.toml")
|
||||
|
||||
assert settings.key_id == "ABCDE12345"
|
||||
assert settings.private_key_path == fake_key
|
||||
assert settings.key_permissions_are_private
|
||||
assert config_path.stat().st_mode & 0o777 == 0o600
|
||||
contents = config_path.read_text(encoding="utf-8")
|
||||
assert "not-a-real-key" not in contents
|
||||
assert 'ASC_ISSUER_ID="11111111-2222-4333-8444-555555555555"' in contents
|
||||
assert 'ASC_KEY_ID="ABCDE12345"' in contents
|
||||
assert f'ASC_PRIVATE_KEY_PATH="{fake_key}"' in contents
|
||||
|
||||
|
||||
def test_missing_configuration_is_actionable(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.chdir(tmp_path)
|
||||
for name in ("ASC_ISSUER_ID", "ASC_KEY_ID", "ASC_PRIVATE_KEY_PATH"):
|
||||
monkeypatch.delenv(name, raising=False)
|
||||
|
||||
with pytest.raises(ConfigError, match="apple-team configure"):
|
||||
Settings.load(tmp_path / "missing.toml")
|
||||
|
||||
|
||||
def test_rejects_non_p8_file(tmp_path: Path) -> None:
|
||||
key = tmp_path / "bad.p8"
|
||||
key.write_text("secret", encoding="utf-8")
|
||||
settings = Settings(
|
||||
"11111111-2222-4333-8444-555555555555", "ABCDE12345", key
|
||||
)
|
||||
|
||||
with pytest.raises(ConfigError, match="not an App Store Connect"):
|
||||
settings.validate()
|
||||
|
||||
|
||||
def test_secure_private_key_removes_group_and_world_access(fake_key: Path) -> None:
|
||||
fake_key.chmod(0o644)
|
||||
settings = Settings(
|
||||
"11111111-2222-4333-8444-555555555555", "ABCDE12345", fake_key
|
||||
)
|
||||
|
||||
settings.secure_private_key()
|
||||
|
||||
assert fake_key.stat().st_mode & 0o777 == 0o600
|
||||
assert settings.key_permissions_are_private
|
||||
@@ -0,0 +1,42 @@
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from apple_team_cli.models import Invitation, TeamUser
|
||||
|
||||
|
||||
def test_user_maps_effective_access() -> None:
|
||||
user = TeamUser.from_resource(
|
||||
{
|
||||
"id": "user-1",
|
||||
"attributes": {
|
||||
"username": "dev@example.com",
|
||||
"firstName": "Dev",
|
||||
"lastName": "Example",
|
||||
"roles": ["ADMIN"],
|
||||
"allAppsVisible": True,
|
||||
"provisioningAllowed": False,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
assert user.name == "Dev Example"
|
||||
assert not user.provisioning_allowed
|
||||
assert user.as_dict()["roles"] == ["ADMIN"]
|
||||
|
||||
|
||||
def test_invitation_expiration() -> None:
|
||||
expired_at = datetime.now(UTC) - timedelta(minutes=1)
|
||||
invite = Invitation.from_resource(
|
||||
{
|
||||
"id": "invite-1",
|
||||
"attributes": {
|
||||
"email": "dev@example.com",
|
||||
"firstName": "Dev",
|
||||
"lastName": "Example",
|
||||
"expirationDate": expired_at.isoformat(),
|
||||
"roles": ["DEVELOPER"],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
assert invite.expired
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
from apple_team_cli.models import TeamUser
|
||||
from apple_team_cli.ui import access_diagnostic
|
||||
|
||||
|
||||
def make_user(*, holder: bool, provisioning: bool) -> TeamUser:
|
||||
return TeamUser(
|
||||
id="1" if holder else "2",
|
||||
username="holder@example.com" if holder else "admin@example.com",
|
||||
first_name="Test",
|
||||
last_name="User",
|
||||
roles=("ACCOUNT_HOLDER", "ADMIN") if holder else ("ADMIN",),
|
||||
all_apps_visible=True,
|
||||
provisioning_allowed=provisioning,
|
||||
)
|
||||
|
||||
|
||||
def test_diagnoses_likely_individual_membership() -> None:
|
||||
panel = access_diagnostic(
|
||||
[make_user(holder=True, provisioning=True), make_user(holder=False, provisioning=False)]
|
||||
)
|
||||
|
||||
assert panel is not None
|
||||
|
||||
|
||||
def test_no_diagnostic_when_delegated_admin_has_provisioning() -> None:
|
||||
panel = access_diagnostic(
|
||||
[make_user(holder=True, provisioning=True), make_user(holder=False, provisioning=True)]
|
||||
)
|
||||
|
||||
assert panel is None
|
||||
|
||||
Reference in new issue
Block a user