Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_identity_cli.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_identity_cli.py · 4.0 KB · 117 lines
1"""Tests for the identity management CLI."""
2
3import json
4
5import pytest
6
7import object_credentials
8import object_identity
9import object_identity_cli
10
11
12def run_cli(args, tmp_path):
13 return object_identity_cli.main(["--data-dir", str(tmp_path), *args])
14
15
16def test_create_account_and_user(tmp_path, capsys):
17 assert run_cli(["create-account", "--account-id", "acme", "--name", "Acme"], tmp_path) == 0
18 assert (
19 run_cli(
20 [
21 "create-user",
22 "--user-id",
23 "dan",
24 "--email",
25 "dan@example.com",
26 "--account-id",
27 "acme",
28 "--roles",
29 "admin,ops",
30 ],
31 tmp_path,
32 )
33 == 0
34 )
35
36 users = object_identity.list_users(base_dir=tmp_path)
37 assert users[0]["user_id"] == "dan"
38 assert users[0]["roles"] == ["admin", "ops"]
39 output = capsys.readouterr().out
40 assert "Created account acme" in output
41 assert "Created user dan" in output
42
43
44def test_create_superuser_sets_admin_role_and_password(tmp_path, monkeypatch, capsys):
45 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
46
47 exit_code = run_cli(
48 ["create-superuser", "--user-id", "dan", "--email", "dan@example.com", "--password-stdin"],
49 tmp_path,
50 )
51
52 assert exit_code == 0
53 user = object_identity.get_user("dan", base_dir=tmp_path)
54 assert user["roles"] == ["admin"]
55 assert object_credentials.verify_password("dan", "long enough password", base_dir=tmp_path)
56 output = capsys.readouterr().out
57 assert "long enough password" not in output
58
59
60def test_set_password_prompts_and_rejects_mismatch(tmp_path, monkeypatch, capsys):
61 run_cli(["create-user", "--user-id", "dan"], tmp_path)
62
63 prompts = iter(["first password!", "different password"])
64 monkeypatch.setattr(object_identity_cli.getpass, "getpass", lambda _: next(prompts))
65
66 assert run_cli(["set-password", "--user-id", "dan"], tmp_path) == 1
67 assert "passwords do not match" in capsys.readouterr().err
68 assert not object_credentials.has_password("dan", base_dir=tmp_path)
69
70
71def test_set_password_unknown_user_fails(tmp_path, capsys):
72 assert run_cli(["set-password", "--user-id", "ghost"], tmp_path) == 1
73 assert "User not found" in capsys.readouterr().err
74
75
76def test_remove_password(tmp_path, monkeypatch, capsys):
77 run_cli(["create-user", "--user-id", "dan"], tmp_path)
78 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
79 run_cli(["set-password", "--user-id", "dan", "--password-stdin"], tmp_path)
80
81 assert run_cli(["remove-password", "--user-id", "dan"], tmp_path) == 0
82 assert not object_credentials.has_password("dan", base_dir=tmp_path)
83 assert "Password removed" in capsys.readouterr().out
84
85
86def test_list_users_reports_password_state(tmp_path, monkeypatch, capsys):
87 run_cli(["create-user", "--user-id", "dan", "--email", "dan@example.com"], tmp_path)
88 run_cli(["create-user", "--user-id", "sam"], tmp_path)
89 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
90 run_cli(["set-password", "--user-id", "dan", "--password-stdin"], tmp_path)
91 capsys.readouterr()
92
93 assert run_cli(["list-users"], tmp_path) == 0
94 output = capsys.readouterr().out
95
96 assert "dan" in output and "password" in output
97 assert "sam" in output and "no-password" in output
98 assert "2 user(s)" in output
99 assert "scrypt" not in output
100
101
102def test_list_users_json(tmp_path, capsys):
103 run_cli(["create-user", "--user-id", "dan"], tmp_path)
104 capsys.readouterr()
105
106 assert run_cli(["list-users", "--json"], tmp_path) == 0
107 payload = json.loads(capsys.readouterr().out)
108
109 assert payload[0]["user_id"] == "dan"
110 assert "password_hash" not in payload[0]
111
112
113def test_duplicate_user_fails_cleanly(tmp_path, capsys):
114 run_cli(["create-user", "--user-id", "dan"], tmp_path)
115
116 assert run_cli(["create-user", "--user-id", "dan"], tmp_path) == 1
117 assert "already exists" in capsys.readouterr().err