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_credentials.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_credentials.py · 3.5 KB · 82 lines
1"""Tests for file-backed password credentials."""
2
3import os
4import stat
5
6import pytest
7
8import object_credentials
9
10
11def test_set_and_verify_password_round_trip(tmp_path):
12 result = object_credentials.set_password("usr_1", "correct horse battery", base_dir=tmp_path)
13
14 assert result["user_id"] == "usr_1"
15 assert result["operation"] == "created"
16 assert object_credentials.verify_password("usr_1", "correct horse battery", base_dir=tmp_path)
17 assert not object_credentials.verify_password("usr_1", "wrong password!", base_dir=tmp_path)
18
19
20def test_verify_password_unknown_user_returns_false(tmp_path):
21 assert not object_credentials.verify_password("usr_missing", "whatever12", base_dir=tmp_path)
22
23
24def test_set_password_replaces_existing_hash(tmp_path):
25 object_credentials.set_password("usr_1", "first-password", base_dir=tmp_path)
26 result = object_credentials.set_password("usr_1", "second-password", base_dir=tmp_path)
27
28 assert result["operation"] == "replaced"
29 assert not object_credentials.verify_password("usr_1", "first-password", base_dir=tmp_path)
30 assert object_credentials.verify_password("usr_1", "second-password", base_dir=tmp_path)
31
32 stored = object_credentials.credentials_path(tmp_path).read_text()
33 assert stored.count("usr_1") == 1
34
35
36def test_password_length_rules(tmp_path):
37 with pytest.raises(object_credentials.InvalidPasswordError):
38 object_credentials.set_password("usr_1", "short", base_dir=tmp_path)
39 with pytest.raises(object_credentials.InvalidPasswordError):
40 object_credentials.set_password("usr_1", "x" * 1025, base_dir=tmp_path)
41 with pytest.raises(object_credentials.InvalidPasswordError):
42 object_credentials.set_password("usr_1", 12345678, base_dir=tmp_path) # type: ignore[arg-type]
43
44
45def test_user_id_required(tmp_path):
46 with pytest.raises(object_credentials.InvalidPasswordError):
47 object_credentials.set_password(" ", "long enough password", base_dir=tmp_path)
48
49
50def test_stored_hash_never_contains_password(tmp_path):
51 object_credentials.set_password("usr_1", "super secret password", base_dir=tmp_path)
52
53 stored = object_credentials.credentials_path(tmp_path).read_text()
54 assert "super secret password" not in stored
55 assert stored.splitlines()[1].split("\t")[1].startswith("scrypt:")
56
57
58def test_credentials_file_is_owner_only(tmp_path):
59 object_credentials.set_password("usr_1", "long enough password", base_dir=tmp_path)
60
61 mode = stat.S_IMODE(os.stat(object_credentials.credentials_path(tmp_path)).st_mode)
62 assert mode == 0o600
63
64
65def test_remove_and_has_password(tmp_path):
66 assert not object_credentials.has_password("usr_1", base_dir=tmp_path)
67 object_credentials.set_password("usr_1", "long enough password", base_dir=tmp_path)
68 assert object_credentials.has_password("usr_1", base_dir=tmp_path)
69
70 assert object_credentials.remove_password("usr_1", base_dir=tmp_path)
71 assert not object_credentials.remove_password("usr_1", base_dir=tmp_path)
72 assert not object_credentials.has_password("usr_1", base_dir=tmp_path)
73 assert not object_credentials.verify_password("usr_1", "long enough password", base_dir=tmp_path)
74
75
76def test_tampered_hash_fails_verification(tmp_path):
77 object_credentials.set_password("usr_1", "long enough password", base_dir=tmp_path)
78 path = object_credentials.credentials_path(tmp_path)
79 content = path.read_text().replace("scrypt:", "bcrypt:")
80 path.write_text(content)
81
82 assert not object_credentials.verify_password("usr_1", "long enough password", base_dir=tmp_path)