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

9 downloads

dbbasic-object-server/tests/test_object_identity_cli.py
Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
test_object_identity_cli.py · 9.4 KB · 217 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
118
119
120def _leftover_password(tmp_path, user_id="dan"):
121 """A credential row with no user: what a user removed from users.tsv by hand leaves behind."""
122 object_credentials.set_password(user_id, "the old owner's pass", base_dir=tmp_path)
123
124
125def test_create_user_refuses_an_id_with_a_leftover_password(tmp_path, capsys):
126 _leftover_password(tmp_path)
127 assert run_cli(["create-user", "--user-id", "dan"], tmp_path) == 1
128 assert "already stored for user id dan" in capsys.readouterr().err
129 assert object_identity.list_users(base_dir=tmp_path) == []
130
131 # remove-password takes the leftover away even though there is no user, and then the id is free
132 assert run_cli(["remove-password", "--user-id", "dan"], tmp_path) == 0
133 assert "which has no user" in capsys.readouterr().out
134 assert run_cli(["create-user", "--user-id", "dan"], tmp_path) == 0
135 assert not object_credentials.has_password("dan", base_dir=tmp_path)
136
137
138def test_create_user_refuses_leftover_sessions_and_keys_and_clear_leftovers_frees_the_id(tmp_path, capsys):
139 import object_api_keys
140
141 run_cli(["create-user", "--user-id", "dan"], tmp_path)
142 _leftover_password(tmp_path)
143 token = object_identity.create_session({"user_id": "dan"}, base_dir=tmp_path)["token"]
144 _meta, key = object_api_keys.create_api_key("dan", "script", base_dir=tmp_path)
145 path = object_identity.users_path(tmp_path)
146 path.write_text(path.read_text().splitlines()[0] + "\n") # the user removed by hand
147 capsys.readouterr()
148
149 assert run_cli(["create-user", "--user-id", "dan"], tmp_path) == 1
150 err = capsys.readouterr().err
151 assert "a password" in err and "1 API key(s)" in err and "clear-leftovers --user-id dan" in err
152 assert object_identity.list_users(base_dir=tmp_path) == []
153
154 assert run_cli(["clear-leftovers", "--user-id", "dan"], tmp_path) == 0
155 assert "password removed, 1 session(s) signed out, 1 API key(s) deleted" in capsys.readouterr().out
156 assert run_cli(["create-user", "--user-id", "dan"], tmp_path) == 0
157 assert object_identity.resolve_session_token(token, base_dir=tmp_path) is None
158 assert object_api_keys.resolve_api_key(key, base_dir=tmp_path) is None
159 assert not object_credentials.has_password("dan", base_dir=tmp_path)
160
161
162def test_clear_leftovers_refuses_an_id_that_has_a_user(tmp_path, capsys):
163 run_cli(["create-user", "--user-id", "dan"], tmp_path)
164 assert run_cli(["clear-leftovers", "--user-id", "dan"], tmp_path) == 1
165 assert "not leftovers" in capsys.readouterr().err
166
167
168def test_create_superuser_deletes_api_keys_left_under_the_id(tmp_path, monkeypatch, capsys):
169 import object_api_keys
170
171 _meta, key = object_api_keys.create_api_key("dan", "the old owner's script", base_dir=tmp_path)
172 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
173 assert run_cli(["create-superuser", "--user-id", "dan", "--password-stdin"], tmp_path) == 0
174 assert object_api_keys.resolve_api_key(key, base_dir=tmp_path) is None
175 assert object_api_keys.list_api_keys("dan", base_dir=tmp_path) == []
176
177
178def test_remove_password_for_an_unknown_id_with_nothing_stored_still_fails(tmp_path, capsys):
179 assert run_cli(["remove-password", "--user-id", "nobody"], tmp_path) == 1
180
181
182def test_create_superuser_leaves_nothing_when_the_password_cannot_be_written(tmp_path, monkeypatch, capsys):
183 """The admin used to be created before its password was set, so a failure writing the password left an
184 admin without the password the operator just chose."""
185 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
186
187 def unwritable(*args, **kwargs):
188 raise OSError("credentials file is not writable")
189
190 monkeypatch.setattr(object_credentials, "_write_credentials", unwritable)
191 with pytest.raises(OSError):
192 run_cli(["create-superuser", "--user-id", "dan", "--password-stdin"], tmp_path)
193 assert object_identity.list_users(base_dir=tmp_path) == []
194
195
196def test_create_superuser_takes_the_password_back_when_the_user_cannot_be_made(tmp_path, monkeypatch, capsys):
197 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
198 assert run_cli(["create-superuser", "--user-id", "dan", "--account-id", "no-such-account",
199 "--password-stdin"], tmp_path) == 1
200 assert object_identity.list_users(base_dir=tmp_path) == []
201 assert not object_credentials.has_password("dan", base_dir=tmp_path)
202
203
204def test_create_superuser_never_touches_an_existing_users_password(tmp_path, monkeypatch, capsys):
205 run_cli(["create-user", "--user-id", "dan"], tmp_path)
206 object_credentials.set_password("dan", "dan's own password", base_dir=tmp_path)
207 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
208 assert run_cli(["create-superuser", "--user-id", "dan", "--password-stdin"], tmp_path) == 1
209 assert object_credentials.verify_password("dan", "dan's own password", base_dir=tmp_path)
210
211
212def test_create_superuser_replaces_a_leftover_password_with_the_chosen_one(tmp_path, monkeypatch, capsys):
213 _leftover_password(tmp_path)
214 monkeypatch.setattr("sys.stdin", __import__("io").StringIO("long enough password\n"))
215 assert run_cli(["create-superuser", "--user-id", "dan", "--password-stdin"], tmp_path) == 0
216 assert object_credentials.verify_password("dan", "long enough password", base_dir=tmp_path)
217 assert not object_credentials.verify_password("dan", "the old owner's pass", base_dir=tmp_path)