Files
agentic-mobile-control/apps/device-host-agent/tests/test_local_account.py
T
q792602257 efeb3eb926
Tests / Test passed: 581
Implement edge-host-self-enrollment
Host Agent:
- One-time local operator account bootstrap (PBKDF2-HMAC-SHA256, atomic
  0600-permission write) gating the daemon's first unattended start via a
  new `setup` CLI subcommand.
- Default control-plane URL now https://amcp.home.jerryyan.top (env var
  override unchanged).
- Enrollment no longer requires a pre-issued token; falls back to
  zero-token self-service enrollment when none is configured.

Cloud control plane:
- CLOUD_SELF_SERVICE_ENROLLMENT_ENABLED (default false) opt-in flag.
- SelfServiceEnrollmentAuthProvider + ChainedEnrollmentAuthProvider:
  configured tokens still take priority; self-service only applies when
  no token matches, preserving edge-host-enrollment's token-bound path.
- Fixed a latent bug in sql_repository.py::enroll_host: the token-conflict
  lookup used `== enrollment_token_digest`, which SQLAlchemy compiles to
  `IS NULL` when the value is None, so every self-service enrollment after
  the first would have falsely collided with an existing NULL-digest host.
  Skipped that lookup entirely when the digest is None.

Docs/deploy: .env.example, compose.yaml, compose.deploy.yaml,
CLOUD_DEPLOYMENT.md, MACOS_IPHONE_SETUP.md updated for the new flag,
URL default, and required `device-host-agent setup` step.

Verification: 494 non-integration tests pass; openspec validate --strict
passes. PostgreSQL-backed contract tests and full manual end-to-end
verification were not run (no Postgres/Docker or reachable cloud-api in
this environment); noted as unchecked in tasks.md 7.2/7.4.
2026-07-13 18:30:49 +08:00

63 lines
2.0 KiB
Python

from __future__ import annotations
import os
import pytest
from host_agent.local_account import LocalAccountStateError, LocalAccountStore
def test_local_account_store_creates_and_verifies_password(tmp_path) -> None:
path = tmp_path / "state" / "host_local_account.json"
store = LocalAccountStore(path)
assert store.load() is None
created = store.create("operator", "correct horse battery staple")
assert created.username == "operator"
assert store.load() == created
assert store.verify(created, "correct horse battery staple") is True
assert store.verify(created, "wrong password") is False
raw = path.read_text(encoding="utf-8")
assert "correct horse battery staple" not in raw
assert "password" not in raw.lower() or "password_hash" in raw
if os.name != "nt":
assert path.stat().st_mode & 0o777 == 0o600
def test_local_account_state_never_exposes_password_via_repr(tmp_path) -> None:
store = LocalAccountStore(tmp_path / "host_local_account.json")
created = store.create("operator", "hunter2")
assert "hunter2" not in repr(created)
assert "salt=" not in repr(created)
assert "password_hash=" not in repr(created)
def test_local_account_store_rejects_corrupted_file(tmp_path) -> None:
path = tmp_path / "host_local_account.json"
path.write_text('{"username": "operator"}', encoding="utf-8")
store = LocalAccountStore(path)
with pytest.raises(LocalAccountStateError):
store.load()
def test_local_account_store_rejects_invalid_json(tmp_path) -> None:
path = tmp_path / "host_local_account.json"
path.write_text("not json", encoding="utf-8")
store = LocalAccountStore(path)
with pytest.raises(LocalAccountStateError):
store.load()
def test_local_account_store_rejects_empty_credentials(tmp_path) -> None:
store = LocalAccountStore(tmp_path / "host_local_account.json")
with pytest.raises(ValueError):
store.create("", "password")
with pytest.raises(ValueError):
store.create("operator", "")