forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_demo.py
More file actions
131 lines (107 loc) · 4.21 KB
/
Copy pathtest_cli_demo.py
File metadata and controls
131 lines (107 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""End-to-end CLI flow and the offline demo."""
from __future__ import annotations
import json
from collections.abc import Callable
from pathlib import Path
import pytest
from habitable.cli import main
from habitable.demo import run_demo
def test_demo_runs_offline() -> None:
assert run_demo() == 0
def _init_capture_export(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> Path:
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "bldg-12", "--unit", "4B"]) == 0
assert main(["issue", "--vault", str(vault), "--category", "mold", "--title", "Mold"]) == 0
out = capsys.readouterr().out
issue_id = next(token for token in out.split() if token.startswith("issue-"))
photo = make_jpeg(with_location=True)
capture_args = ["capture", str(photo), "--vault", str(vault), "--issue", issue_id, "--dev-tsa"]
assert main(capture_args) == 0
assert (
main(
[
"timeline",
"--vault",
str(vault),
"--issue",
issue_id,
"--kind",
"observed",
"--text",
"cold",
]
)
== 0
)
assert main(["status", "--vault", str(vault)]) == 0
packet = tmp_path / "packet"
assert main(["export", "--vault", str(vault), "--out", str(packet)]) == 0
return packet
def test_cli_full_flow_verifies(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
packet = _init_capture_export(tmp_path, make_jpeg, monkeypatch, capsys)
assert main(["verify", str(packet)]) == 0
assert "packet intact" in capsys.readouterr().out
def test_cli_verify_detects_tamper(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
packet = _init_capture_export(tmp_path, make_jpeg, monkeypatch, capsys)
bundle = json.loads((packet / "bundle.json").read_text())
bundle["unit"] = "TAMPERED"
(packet / "bundle.json").write_text(json.dumps(bundle))
assert main(["verify", str(packet)]) == 1
def test_cli_verify_json_is_structured(
tmp_path: Path,
make_jpeg: Callable[..., Path],
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
packet = _init_capture_export(tmp_path, make_jpeg, monkeypatch, capsys)
capsys.readouterr() # drop prior output
assert main(["verify", str(packet), "--json"]) == 0
report = json.loads(capsys.readouterr().out)
assert report["ok"] is True
assert report["signature_ok"] and report["custody_ok"]
assert report["item_count"] >= 1 and report["verified_items"] == report["item_count"]
item = report["items"][0]
for key in ("capture_id", "content_hash", "ok", "timestamp_verified", "notes"):
assert key in item
def test_no_command_prints_help() -> None:
assert main([]) == 2
def test_cli_id_prints_fingerprint(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "c"]) == 0
capsys.readouterr()
assert main(["id", "--vault", str(vault)]) == 0
out = capsys.readouterr().out
assert "fingerprint:" in out and "public-id:" in out
def test_cli_sync_requires_transport(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
from habitable.vault import Vault
monkeypatch.setenv("HABITABLE_PASSPHRASE", "pw")
vault = tmp_path / "vault"
assert main(["init", str(vault), "--case", "c"]) == 0
peer = Vault.open(vault, "pw").identity.public().encode()
capsys.readouterr()
# No --relay or --dir: the command reports a clear error and exits non-zero.
assert main(["sync", "--vault", str(vault), "--peer", peer, "--channel", "r"]) == 1
assert "transport" in capsys.readouterr().err