askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_identity.py · 9.2 KB · 285 lines
| 1 | from datetime import datetime, timedelta, timezone |
| 2 | from uuid import UUID |
| 3 | |
| 4 | import object_identity |
| 5 | |
| 6 | |
| 7 | def test_create_account_stores_public_payload(tmp_path): |
| 8 | account = object_identity.create_account( |
| 9 | { |
| 10 | "account_id": "acme", |
| 11 | "name": "Acme Corp", |
| 12 | "subscriptions": ["pro", "pro", "support"], |
| 13 | }, |
| 14 | base_dir=tmp_path, |
| 15 | ) |
| 16 | |
| 17 | assert account["account_id"] == "acme" |
| 18 | assert account["name"] == "Acme Corp" |
| 19 | assert account["status"] == "active" |
| 20 | assert account["subscriptions"] == ["pro", "support"] |
| 21 | |
| 22 | assert object_identity.get_account("acme", base_dir=tmp_path) == account |
| 23 | assert object_identity.list_accounts(base_dir=tmp_path) == [account] |
| 24 | |
| 25 | |
| 26 | def test_create_account_generates_uuid_id_when_missing(tmp_path): |
| 27 | account = object_identity.create_account({"name": "Acme Corp"}, base_dir=tmp_path) |
| 28 | |
| 29 | parsed = UUID(account["account_id"]) |
| 30 | assert parsed.version == 4 |
| 31 | assert object_identity.get_account(account["account_id"], base_dir=tmp_path) == account |
| 32 | |
| 33 | |
| 34 | def test_create_user_stores_roles_and_requires_known_account(tmp_path): |
| 35 | object_identity.create_account({"account_id": "acme"}, base_dir=tmp_path) |
| 36 | |
| 37 | user = object_identity.create_user( |
| 38 | { |
| 39 | "user_id": "u_7", |
| 40 | "account_id": "acme", |
| 41 | "email": "alice@example.com", |
| 42 | "display_name": "Alice", |
| 43 | "roles": ["sales", "sales", "manager"], |
| 44 | "subscriptions": ["team"], |
| 45 | }, |
| 46 | base_dir=tmp_path, |
| 47 | ) |
| 48 | |
| 49 | assert user["user_id"] == "u_7" |
| 50 | assert user["account_id"] == "acme" |
| 51 | assert user["email"] == "alice@example.com" |
| 52 | assert user["roles"] == ["sales", "manager"] |
| 53 | |
| 54 | assert object_identity.get_user("u_7", base_dir=tmp_path) == user |
| 55 | assert object_identity.list_users(base_dir=tmp_path) == [user] |
| 56 | assert object_identity.list_users(account_id="acme", base_dir=tmp_path) == [user] |
| 57 | assert object_identity.list_users(account_id="other", base_dir=tmp_path) == [] |
| 58 | |
| 59 | try: |
| 60 | object_identity.create_user( |
| 61 | {"user_id": "u_8", "account_id": "missing"}, |
| 62 | base_dir=tmp_path, |
| 63 | ) |
| 64 | except object_identity.AccountNotFoundError as exc: |
| 65 | assert str(exc) == "Account not found: missing" |
| 66 | else: |
| 67 | raise AssertionError("unknown account should fail") |
| 68 | |
| 69 | |
| 70 | def test_create_user_generates_uuid_id_when_missing(tmp_path): |
| 71 | user = object_identity.create_user({"display_name": "Alice"}, base_dir=tmp_path) |
| 72 | |
| 73 | parsed = UUID(user["user_id"]) |
| 74 | assert parsed.version == 4 |
| 75 | assert object_identity.get_user(user["user_id"], base_dir=tmp_path) == user |
| 76 | |
| 77 | |
| 78 | def test_create_identity_records_reject_duplicates(tmp_path): |
| 79 | object_identity.create_account({"account_id": "acme"}, base_dir=tmp_path) |
| 80 | object_identity.create_user({"user_id": "u_7"}, base_dir=tmp_path) |
| 81 | |
| 82 | try: |
| 83 | object_identity.create_account({"account_id": "acme"}, base_dir=tmp_path) |
| 84 | except object_identity.InvalidIdentityPayloadError as exc: |
| 85 | assert str(exc) == "Account already exists: acme" |
| 86 | else: |
| 87 | raise AssertionError("duplicate account should fail") |
| 88 | |
| 89 | try: |
| 90 | object_identity.create_user({"user_id": "u_7"}, base_dir=tmp_path) |
| 91 | except object_identity.InvalidIdentityPayloadError as exc: |
| 92 | assert str(exc) == "User already exists: u_7" |
| 93 | else: |
| 94 | raise AssertionError("duplicate user should fail") |
| 95 | |
| 96 | |
| 97 | def test_registered_user_defaults_session_roles_account_and_subscriptions(tmp_path): |
| 98 | object_identity.create_account( |
| 99 | {"account_id": "acme", "subscriptions": ["pro"]}, |
| 100 | base_dir=tmp_path, |
| 101 | ) |
| 102 | object_identity.create_user( |
| 103 | { |
| 104 | "user_id": "u_7", |
| 105 | "account_id": "acme", |
| 106 | "roles": ["sales"], |
| 107 | "subscriptions": ["team"], |
| 108 | }, |
| 109 | base_dir=tmp_path, |
| 110 | ) |
| 111 | |
| 112 | result = object_identity.create_session({"user_id": "u_7"}, base_dir=tmp_path) |
| 113 | |
| 114 | assert result["session"]["account_id"] == "acme" |
| 115 | assert result["session"]["roles"] == ["sales"] |
| 116 | assert result["session"]["subscriptions"] == ["team", "pro"] |
| 117 | |
| 118 | |
| 119 | def test_session_rejects_disabled_user_or_account(tmp_path): |
| 120 | object_identity.create_account({"account_id": "acme"}, base_dir=tmp_path) |
| 121 | object_identity.create_user( |
| 122 | {"user_id": "u_7", "account_id": "acme", "status": "disabled"}, |
| 123 | base_dir=tmp_path, |
| 124 | ) |
| 125 | |
| 126 | try: |
| 127 | object_identity.create_session({"user_id": "u_7"}, base_dir=tmp_path) |
| 128 | except object_identity.InvalidSessionPayloadError as exc: |
| 129 | assert str(exc) == "User is not active: u_7" |
| 130 | else: |
| 131 | raise AssertionError("disabled user should not receive a session") |
| 132 | |
| 133 | object_identity.create_account( |
| 134 | {"account_id": "closed", "status": "disabled"}, |
| 135 | base_dir=tmp_path, |
| 136 | ) |
| 137 | object_identity.create_user( |
| 138 | {"user_id": "u_8", "account_id": "closed"}, |
| 139 | base_dir=tmp_path, |
| 140 | ) |
| 141 | |
| 142 | try: |
| 143 | object_identity.create_session({"user_id": "u_8"}, base_dir=tmp_path) |
| 144 | except object_identity.InvalidSessionPayloadError as exc: |
| 145 | assert str(exc) == "Account is not active: closed" |
| 146 | else: |
| 147 | raise AssertionError("disabled account should not receive a session") |
| 148 | |
| 149 | |
| 150 | def test_session_can_require_registered_user(tmp_path): |
| 151 | try: |
| 152 | object_identity.create_session( |
| 153 | {"user_id": "u_missing"}, |
| 154 | base_dir=tmp_path, |
| 155 | require_known_user=True, |
| 156 | ) |
| 157 | except object_identity.UserNotFoundError as exc: |
| 158 | assert str(exc) == "User not found: u_missing" |
| 159 | else: |
| 160 | raise AssertionError("strict sessions should require a known user") |
| 161 | |
| 162 | |
| 163 | def test_create_session_stores_hash_and_resolves_subject(tmp_path): |
| 164 | now = datetime.now(timezone.utc) |
| 165 | |
| 166 | result = object_identity.create_session( |
| 167 | { |
| 168 | "user_id": "7", |
| 169 | "account_id": "acme", |
| 170 | "roles": ["sales", "sales", "manager"], |
| 171 | "subscriptions": ["pro"], |
| 172 | "label": "scroll", |
| 173 | "ttl_seconds": 60, |
| 174 | }, |
| 175 | base_dir=tmp_path, |
| 176 | now=now, |
| 177 | ) |
| 178 | |
| 179 | token = result["token"] |
| 180 | session = result["session"] |
| 181 | parsed = UUID(session["session_id"]) |
| 182 | assert parsed.version == 4 |
| 183 | assert token |
| 184 | assert session["user_id"] == "7" |
| 185 | assert session["roles"] == ["sales", "manager"] |
| 186 | assert session["active"] is True |
| 187 | |
| 188 | session_file = object_identity.sessions_path(tmp_path) |
| 189 | stored = session_file.read_text() |
| 190 | assert token not in stored |
| 191 | assert "sha256:" in stored |
| 192 | |
| 193 | resolved = object_identity.resolve_session_token( |
| 194 | token, |
| 195 | base_dir=tmp_path, |
| 196 | now=now + timedelta(seconds=1), |
| 197 | ) |
| 198 | |
| 199 | assert resolved is not None |
| 200 | assert resolved.subject().user_id == "7" |
| 201 | assert resolved.subject().account_id == "acme" |
| 202 | assert resolved.subject().roles == ("sales", "manager") |
| 203 | |
| 204 | |
| 205 | def test_expired_session_does_not_resolve(tmp_path): |
| 206 | now = datetime.now(timezone.utc) |
| 207 | result = object_identity.create_session( |
| 208 | {"user_id": "7", "ttl_seconds": 1}, |
| 209 | base_dir=tmp_path, |
| 210 | now=now, |
| 211 | ) |
| 212 | |
| 213 | resolved = object_identity.resolve_session_token( |
| 214 | result["token"], |
| 215 | base_dir=tmp_path, |
| 216 | now=now + timedelta(seconds=2), |
| 217 | ) |
| 218 | |
| 219 | assert resolved is None |
| 220 | |
| 221 | |
| 222 | def test_revoke_session_prevents_resolution(tmp_path): |
| 223 | now = datetime.now(timezone.utc) |
| 224 | result = object_identity.create_session( |
| 225 | {"user_id": "7", "ttl_seconds": 60}, |
| 226 | base_dir=tmp_path, |
| 227 | now=now, |
| 228 | ) |
| 229 | |
| 230 | revoked = object_identity.revoke_session( |
| 231 | result["session"]["session_id"], |
| 232 | base_dir=tmp_path, |
| 233 | now=now + timedelta(seconds=10), |
| 234 | ) |
| 235 | |
| 236 | assert revoked["active"] is False |
| 237 | assert revoked["revoked_at"] is not None |
| 238 | assert object_identity.resolve_session_token(result["token"], base_dir=tmp_path, now=now) is None |
| 239 | |
| 240 | |
| 241 | def test_list_sessions_omits_token_material(tmp_path): |
| 242 | result = object_identity.create_session({"user_id": "7"}, base_dir=tmp_path) |
| 243 | |
| 244 | sessions = object_identity.list_sessions(base_dir=tmp_path) |
| 245 | |
| 246 | assert sessions == [result["session"]] |
| 247 | assert "token" not in sessions[0] |
| 248 | assert "token_hash" not in sessions[0] |
| 249 | |
| 250 | |
| 251 | def test_list_sessions_skips_corrupt_rows(tmp_path): |
| 252 | result = object_identity.create_session({"user_id": "7"}, base_dir=tmp_path) |
| 253 | session_file = object_identity.sessions_path(tmp_path) |
| 254 | session_file.write_text( |
| 255 | session_file.read_text() |
| 256 | + "not-a-session\tbad-hash\t\t\t[]\t[]\t\tbad\tbad\t\n" |
| 257 | ) |
| 258 | |
| 259 | sessions = object_identity.list_sessions(base_dir=tmp_path) |
| 260 | |
| 261 | assert sessions == [result["session"]] |
| 262 | |
| 263 | |
| 264 | def test_list_sessions_skips_naive_timestamp_rows(tmp_path): |
| 265 | result = object_identity.create_session({"user_id": "7"}, base_dir=tmp_path) |
| 266 | session_file = object_identity.sessions_path(tmp_path) |
| 267 | session_file.write_text( |
| 268 | session_file.read_text() |
| 269 | + "sess_badbadbadbad\tsha256:" |
| 270 | + "0" * 64 |
| 271 | + "\t8\t\t[]\t[]\t\t2026-06-30T18:00:00\t2026-07-01T18:00:00\t\n" |
| 272 | ) |
| 273 | |
| 274 | sessions = object_identity.list_sessions(base_dir=tmp_path) |
| 275 | |
| 276 | assert sessions == [result["session"]] |
| 277 | |
| 278 | |
| 279 | def test_create_session_rejects_missing_user_id(tmp_path): |
| 280 | try: |
| 281 | object_identity.create_session({"roles": ["sales"]}, base_dir=tmp_path) |
| 282 | except object_identity.InvalidSessionPayloadError as exc: |
| 283 | assert str(exc) == "user_id is required" |
| 284 | else: |
| 285 | raise AssertionError("missing user_id should fail") |