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.
test_account_disable.py · 22.0 KB · 505 lines
| 1 | """Disabling an account, and why a billing lapse is not one. |
| 2 | |
| 3 | Before: there was no way to disable an account (only a hand edit of |
| 4 | accounts.tsv), and a disabled account only refused new sign-ins and dropped |
| 5 | its subscriptions. Its users kept their sessions, roles, own subscriptions, |
| 6 | account: rules and $account_id row filters, and API keys never looked at the |
| 7 | account at all. |
| 8 | |
| 9 | Now PUT /identity/accounts/{id}/status (and the disable-account / |
| 10 | enable-account CLI commands, object_identity.set_account_status) disable and |
| 11 | re-enable an account. While it is disabled every session and API key of its |
| 12 | users is refused on each request; disabling also ends their sessions, so |
| 13 | re-enabling does not bring them back. API keys are kept, refused while |
| 14 | disabled, and work again unchanged after re-enable: deleting them would break |
| 15 | a customer's integrations for good over a mistaken or temporary disable. The |
| 16 | admin token and admins outside the account are untouched. A member who is an |
| 17 | admin is refused like any member, as a disabled admin user is. |
| 18 | |
| 19 | A billing lapse is the account's subscription being removed: the next request |
| 20 | no longer matches subscription: rules, and the customer can still sign in to |
| 21 | pay. The first test rehearses that end to end; the second rehearses a disable. |
| 22 | |
| 23 | Everything runs through the ASGI app, with enforcement on and off. |
| 24 | """ |
| 25 | |
| 26 | import json |
| 27 | import os |
| 28 | import time |
| 29 | from dataclasses import replace |
| 30 | |
| 31 | import pytest |
| 32 | |
| 33 | from test_object_server import TEST_ADMIN_TOKEN, request, save_permission_policy, write_records |
| 34 | |
| 35 | import object_api_keys |
| 36 | import object_identity |
| 37 | import object_identity_cli |
| 38 | import object_identity_sources |
| 39 | import object_ops_log |
| 40 | import object_server |
| 41 | |
| 42 | PASSWORD = "a-good-password-1" |
| 43 | |
| 44 | POLICY = { |
| 45 | "access_mode": "role_based", |
| 46 | "admin_roles": ["admin"], |
| 47 | "rules": [ |
| 48 | {"effect": "allow", "principal": "registered", "actions": ["read"], |
| 49 | "collection": "notes", "reason": "every signed-in user reads notes"}, |
| 50 | {"effect": "allow", "principal": "subscription:pro", "actions": ["read"], |
| 51 | "collection": "reports", "reason": "pro subscribers read reports"}, |
| 52 | {"effect": "allow", "principal": "account:acme", "actions": ["read"], |
| 53 | "collection": "ledger", "reason": "acme members read the ledger"}, |
| 54 | ], |
| 55 | } |
| 56 | |
| 57 | |
| 58 | @pytest.fixture(params=[True, False], ids=["enforced", "unenforced"]) |
| 59 | def data(request, tmp_path, monkeypatch): |
| 60 | data = tmp_path / "data" |
| 61 | for name in ("notes", "reports", "ledger"): |
| 62 | write_records(data, name, "id\ttitle\nr1\thello\n") |
| 63 | schema = data / "schemas" / f"{name}.json" |
| 64 | schema.parent.mkdir(parents=True, exist_ok=True) |
| 65 | schema.write_text(json.dumps({"fields": [{"name": "id"}, {"name": "title"}]})) |
| 66 | save_permission_policy(data, POLICY) |
| 67 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data)) |
| 68 | monkeypatch.setenv(object_server.ADMIN_TOKEN_ENV, TEST_ADMIN_TOKEN) |
| 69 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true" if request.param else "false") |
| 70 | monkeypatch.setenv(object_server.SESSION_ADMIN_GATES_ENV, "true") |
| 71 | monkeypatch.setenv(object_server.PASSWORD_LOGIN_ENV, "true") |
| 72 | monkeypatch.setattr(object_server, "PASSWORD_LOGIN_FAILURE_DELAY_SECONDS", 0) |
| 73 | for name in (object_server.REQUIRE_KNOWN_IDENTITY_USERS_ENV, object_server.PERMISSION_TRUST_HEADERS_ENV, |
| 74 | object_identity_sources.IDENTITY_VERIFY_URL_ENV): |
| 75 | monkeypatch.delenv(name, raising=False) |
| 76 | return data |
| 77 | |
| 78 | |
| 79 | def enforced(): |
| 80 | return os.environ.get(object_server.PERMISSION_ENFORCEMENT_ENV) == "true" |
| 81 | |
| 82 | |
| 83 | def call(method, path, body=None, token=None, admin=False, headers=()): |
| 84 | sent = list(headers) |
| 85 | if admin: |
| 86 | sent.append(("authorization", f"Token {TEST_ADMIN_TOKEN}")) |
| 87 | elif token: |
| 88 | sent.append(("authorization", f"Bearer {token}")) |
| 89 | raw = b"" |
| 90 | if body is not None: |
| 91 | raw = json.dumps(body).encode() |
| 92 | sent.append(("content-type", "application/json")) |
| 93 | status, _, payload = request(path, method=method, body=raw, headers=sent) |
| 94 | return status, payload |
| 95 | |
| 96 | |
| 97 | def add_account(account_id, subscriptions=()): |
| 98 | status, payload = call("POST", "/identity/accounts", |
| 99 | {"account_id": account_id, "subscriptions": list(subscriptions)}, admin=True) |
| 100 | assert status == 201, payload |
| 101 | |
| 102 | |
| 103 | def add_user(user_id, account_id=None, roles=()): |
| 104 | body = {"user_id": user_id, "roles": list(roles)} |
| 105 | if account_id: |
| 106 | body["account_id"] = account_id |
| 107 | status, payload = call("POST", "/identity/users", body, admin=True) |
| 108 | assert status == 201, payload |
| 109 | status, payload = call("POST", f"/identity/users/{user_id}/password", {"password": PASSWORD}, admin=True) |
| 110 | assert status == 200, payload |
| 111 | |
| 112 | |
| 113 | def login(user_id): |
| 114 | """A password sign-in; the token, or None when refused.""" |
| 115 | status, payload = call("POST", "/identity/session", {"user_id": user_id, "password": PASSWORD}) |
| 116 | if status == 201: |
| 117 | return payload["token"] |
| 118 | assert status in (401, 403), (status, payload) |
| 119 | return None |
| 120 | |
| 121 | |
| 122 | def admin_session(user_id): |
| 123 | status, payload = call("POST", "/identity/sessions", {"user_id": user_id}, admin=True) |
| 124 | assert status == 201, payload |
| 125 | return payload["token"] |
| 126 | |
| 127 | |
| 128 | def new_key(user_id): |
| 129 | status, payload = call("POST", f"/identity/users/{user_id}/api-keys", {"name": "integration"}, admin=True) |
| 130 | assert status == 200, payload |
| 131 | return payload["token"] |
| 132 | |
| 133 | |
| 134 | def seen(token): |
| 135 | """(user_id, account_id, subscriptions) the server sees for a credential.""" |
| 136 | status, payload = call("GET", "/identity", token=token) |
| 137 | assert status == 200, payload |
| 138 | subject = payload["subject"] |
| 139 | return subject["user_id"], subject["account_id"], subject["subscriptions"] |
| 140 | |
| 141 | |
| 142 | def refused_as_unusable(token): |
| 143 | """A member of an account whose row is broken: 403 account_unusable |
| 144 | under enforcement, no credential otherwise (test_account_row_fail_closed.py).""" |
| 145 | status, payload = call("GET", "/identity", token=token) |
| 146 | if enforced(): |
| 147 | return status == 403 and payload["code"] == "account_unusable" |
| 148 | return status == 200 and payload["subject"]["user_id"] is None |
| 149 | |
| 150 | |
| 151 | def can_read(token, collection): |
| 152 | status, _ = call("GET", f"/collections/{collection}/records", token=token) |
| 153 | assert status in (200, 401, 403), status |
| 154 | return status == 200 |
| 155 | |
| 156 | |
| 157 | def is_admin(token): |
| 158 | status, _ = call("GET", "/identity/users", token=token) |
| 159 | assert status in (200, 401, 403), status |
| 160 | return status == 200 |
| 161 | |
| 162 | |
| 163 | def set_status(account_id, status, **auth): |
| 164 | return call("PUT", f"/identity/accounts/{account_id}/status", {"status": status}, **(auth or {"admin": True})) |
| 165 | |
| 166 | |
| 167 | def change_account(data, account_id, **fields): |
| 168 | """A write by another process or by hand (the supported way is |
| 169 | set_account_subscriptions; test_account_subscriptions.py).""" |
| 170 | path = object_identity.accounts_path(data) |
| 171 | accounts = [ |
| 172 | replace(account, **fields) if account.account_id == account_id else account |
| 173 | for account in object_identity._read_accounts(path) |
| 174 | ] |
| 175 | object_identity._write_accounts(path, accounts) |
| 176 | |
| 177 | |
| 178 | def stored_session(data, token): |
| 179 | wanted = object_identity.hash_token(token) |
| 180 | [row] = [s for s in object_identity._read_sessions(object_identity.sessions_path(data)) if s.token_hash == wanted] |
| 181 | return row |
| 182 | |
| 183 | |
| 184 | def key_ids(data, user_id="bob"): |
| 185 | return [k["key_id"] for k in object_api_keys.list_api_keys(user_id, base_dir=data)] |
| 186 | |
| 187 | |
| 188 | def acme_with_bob(): |
| 189 | """acme pays for pro; bob is in it, signed in twice, with one API key.""" |
| 190 | add_account("acme", ["pro"]) |
| 191 | add_user("bob", "acme", roles=["staff"]) |
| 192 | sessions = [login("bob"), admin_session("bob")] |
| 193 | key = new_key("bob") |
| 194 | for token in sessions: |
| 195 | assert seen(token) == ("bob", "acme", ["pro"]) |
| 196 | # A key has the user's own subscriptions, not the account's (as before). |
| 197 | assert seen(key) == ("bob", "acme", []) |
| 198 | return sessions, key |
| 199 | |
| 200 | |
| 201 | # ---- the two rehearsals ------------------------------------------------------ |
| 202 | |
| 203 | def test_a_billing_lapse_removes_the_subscription_and_leaves_the_customer_signed_in(data): |
| 204 | """Non-payment is not a disable: the operator (or the billing |
| 205 | integration) removes the account's subscription. The next request no |
| 206 | longer matches the subscription rule; the user stays signed in, can sign |
| 207 | in fresh to pay, and still has the account. Restoring the subscription |
| 208 | applies on the next request with no new sign-in.""" |
| 209 | (session, _), key = acme_with_bob() |
| 210 | if enforced(): |
| 211 | assert can_read(session, "reports") |
| 212 | |
| 213 | change_account(data, "acme", subscriptions=()) |
| 214 | |
| 215 | fresh = login("bob") |
| 216 | assert fresh is not None # can still sign in to pay |
| 217 | for token in (session, fresh): |
| 218 | assert seen(token) == ("bob", "acme", []) # signed in, no longer pro |
| 219 | if enforced(): |
| 220 | assert not can_read(token, "reports") |
| 221 | assert can_read(token, "notes") and can_read(token, "ledger") |
| 222 | assert seen(key)[:2] == ("bob", "acme") # the key still works |
| 223 | assert not stored_session(data, session).revoked_at |
| 224 | |
| 225 | change_account(data, "acme", subscriptions=("pro",)) # paid again |
| 226 | for token in (session, fresh): |
| 227 | assert seen(token) == ("bob", "acme", ["pro"]) |
| 228 | if enforced(): |
| 229 | assert can_read(token, "reports") |
| 230 | |
| 231 | |
| 232 | def test_disabling_an_account_ends_sessions_refuses_keys_and_re_enabling_restores_keys_only(data): |
| 233 | sessions, key = acme_with_bob() |
| 234 | [key_id] = key_ids(data) |
| 235 | |
| 236 | status, result = set_status("acme", "disabled") |
| 237 | assert status == 200, result |
| 238 | assert result["account"]["status"] == "disabled" |
| 239 | assert result["sessions_revoked"] == 2 |
| 240 | |
| 241 | for token in sessions: |
| 242 | assert seen(token) == (None, None, []) |
| 243 | assert stored_session(data, token).revoked_at # ended, not only refused |
| 244 | assert seen(key) == (None, None, []) # refused ... |
| 245 | assert key_ids(data) == [key_id] # ... but still there |
| 246 | assert login("bob") is None # no new sign-in |
| 247 | if enforced(): |
| 248 | for token in (*sessions, key): |
| 249 | assert not can_read(token, "notes") |
| 250 | assert not can_read(token, "ledger") |
| 251 | |
| 252 | [line] = object_ops_log.read_events(base_dir=data, kind="auth", event="account_status_set") |
| 253 | assert line["identifier"] == "acme" and line["auth_method"] == "admin_token" |
| 254 | assert line["user_id"] == "admin_token" |
| 255 | assert line["label"] == "status=disabled sessions_revoked=2" |
| 256 | |
| 257 | status, result = set_status("acme", "active") |
| 258 | assert status == 200 and result["sessions_revoked"] == 0, result |
| 259 | |
| 260 | fresh = login("bob") |
| 261 | assert fresh is not None and seen(fresh) == ("bob", "acme", ["pro"]) |
| 262 | assert seen(key) == ("bob", "acme", []) # the same key, unchanged |
| 263 | assert key_ids(data) == [key_id] |
| 264 | for token in sessions: |
| 265 | assert seen(token) == (None, None, []) # no session is restored |
| 266 | if enforced(): |
| 267 | assert can_read(fresh, "reports") and can_read(key, "ledger") |
| 268 | |
| 269 | |
| 270 | # ---- who is and is not affected ---------------------------------------------- |
| 271 | |
| 272 | def test_admins_outside_the_account_and_the_admin_token_are_untouched(data): |
| 273 | """An operator cannot lock themselves out by disabling an account they |
| 274 | are not in. An admin who IS a member is refused like any member: the |
| 275 | same as a disabled user who holds the admin role.""" |
| 276 | add_account("acme") |
| 277 | add_account("ops") |
| 278 | add_user("bob", "acme") |
| 279 | add_user("carol", "ops", roles=["admin"]) |
| 280 | add_user("dave", "acme", roles=["admin"]) |
| 281 | add_user("erin", roles=["admin"]) # no account at all |
| 282 | carol, dave, erin = login("carol"), login("dave"), login("erin") |
| 283 | assert is_admin(carol) and is_admin(dave) and is_admin(erin) |
| 284 | |
| 285 | status, result = set_status("acme", "disabled", token=carol) |
| 286 | assert status == 200 and result["sessions_revoked"] == 1, result # dave's |
| 287 | [line] = object_ops_log.read_events(base_dir=data, kind="auth", event="account_status_set") |
| 288 | assert line["user_id"] == "carol" and line["auth_method"] == "admin_session" |
| 289 | |
| 290 | assert is_admin(carol) and is_admin(erin) |
| 291 | assert not is_admin(dave) and seen(dave) == (None, None, []) |
| 292 | assert login("dave") is None |
| 293 | status, _ = call("GET", "/identity/accounts", admin=True) |
| 294 | assert status == 200 |
| 295 | status, result = set_status("acme", "active", token=erin) |
| 296 | assert status == 200, result |
| 297 | assert is_admin(login("dave")) |
| 298 | |
| 299 | |
| 300 | def test_other_accounts_and_users_without_an_account_are_untouched(data): |
| 301 | add_account("acme") |
| 302 | add_account("globex") |
| 303 | add_user("bob", "acme") |
| 304 | add_user("gail", "globex") |
| 305 | add_user("nina") |
| 306 | tokens = {name: (login(name), new_key(name)) for name in ("gail", "nina")} |
| 307 | set_status("acme", "disabled") |
| 308 | for name, (session, key) in tokens.items(): |
| 309 | assert seen(session)[0] == name and seen(key)[0] == name |
| 310 | assert not stored_session(data, session).revoked_at |
| 311 | |
| 312 | |
| 313 | def test_a_session_minted_naming_the_account_for_an_unregistered_id_is_refused_and_ended(data): |
| 314 | add_account("acme", ["pro"]) |
| 315 | status, payload = call("POST", "/identity/sessions", {"user_id": "gateway-user", "account_id": "acme"}, admin=True) |
| 316 | assert status == 201, payload |
| 317 | token = payload["token"] |
| 318 | assert seen(token) == ("gateway-user", "acme", ["pro"]) |
| 319 | status, result = set_status("acme", "disabled") |
| 320 | assert result["sessions_revoked"] == 1 |
| 321 | assert seen(token) == (None, None, []) |
| 322 | |
| 323 | |
| 324 | def test_a_member_of_a_disabled_account_is_issued_no_new_key_or_session(data): |
| 325 | add_account("acme") |
| 326 | add_user("bob", "acme") |
| 327 | set_status("acme", "disabled") |
| 328 | status, payload = call("POST", "/identity/users/bob/api-keys", {"name": "x"}, admin=True) |
| 329 | assert status == 400 and "Account is not active" in payload["error"], payload |
| 330 | status, payload = call("POST", "/identity/sessions", {"user_id": "bob"}, admin=True) |
| 331 | assert status == 400, payload |
| 332 | assert key_ids(data) == [] |
| 333 | |
| 334 | |
| 335 | # ---- disables written elsewhere, broken rows, unreadable files ---------------- |
| 336 | |
| 337 | def test_a_disable_written_by_hand_refuses_on_the_next_request(data): |
| 338 | """accounts.tsv edited by hand or by another process: refused at once, |
| 339 | the session ended when next presented, the key never deleted.""" |
| 340 | (session, _), key = acme_with_bob() |
| 341 | change_account(data, "acme", status="disabled") |
| 342 | assert seen(session) == (None, None, []) and seen(key) == (None, None, []) |
| 343 | assert stored_session(data, session).revoked_at |
| 344 | assert len(key_ids(data)) == 1 |
| 345 | change_account(data, "acme", status="active") |
| 346 | assert seen(key)[0] == "bob" and seen(session) == (None, None, []) |
| 347 | |
| 348 | |
| 349 | def test_an_account_row_that_does_not_parse_refuses_without_ending_anything(data): |
| 350 | (session, _), key = acme_with_bob() |
| 351 | path = object_identity.accounts_path(data) |
| 352 | good = path.read_text() |
| 353 | path.write_text(good.replace("\tactive\t", "\tsuspended\t")) |
| 354 | assert refused_as_unusable(session) and refused_as_unusable(key) |
| 355 | assert not stored_session(data, session).revoked_at and len(key_ids(data)) == 1 |
| 356 | path.write_text(good) |
| 357 | assert seen(session)[0] == "bob" and seen(key)[0] == "bob" |
| 358 | |
| 359 | |
| 360 | def test_the_status_route_keeps_account_rows_it_cannot_parse(data): |
| 361 | """A rewrite through the parsed rows would drop a hand-edited |
| 362 | 'suspended' row, and an account with no row refuses nobody.""" |
| 363 | add_account("acme") |
| 364 | add_account("globex") |
| 365 | add_user("gail", "globex") |
| 366 | gail = login("gail") |
| 367 | path = object_identity.accounts_path(data) |
| 368 | lines = path.read_text().splitlines(keepends=True) |
| 369 | path.write_text("".join(line.replace("\tactive\t", "\tsuspended\t") if line.startswith("globex\t") else line |
| 370 | for line in lines)) |
| 371 | status, result = set_status("acme", "disabled") |
| 372 | assert status == 200, result |
| 373 | assert "globex\t\tsuspended\t" in path.read_text() |
| 374 | assert object_identity.account_refuses("globex", base_dir=data) |
| 375 | assert refused_as_unusable(gail) |
| 376 | |
| 377 | |
| 378 | def test_an_unreadable_accounts_file_is_503_for_a_key_under_enforcement_and_deletes_nothing(data): |
| 379 | _, key = acme_with_bob() |
| 380 | path = object_identity.accounts_path(data) |
| 381 | path.unlink() |
| 382 | path.mkdir() |
| 383 | status, payload = call("GET", "/collections/notes/records", token=key) |
| 384 | if enforced(): |
| 385 | assert status == 503 and payload["code"] == "identity_store_unavailable" |
| 386 | else: |
| 387 | assert seen(key) == (None, None, []) |
| 388 | assert len(key_ids(data)) == 1 |
| 389 | |
| 390 | |
| 391 | def test_resolving_a_key_does_not_re_read_accounts_tsv_when_unchanged(data, monkeypatch): |
| 392 | _, key = acme_with_bob() |
| 393 | old = time.time() - 600 |
| 394 | for path in (data / "identity").glob("*.tsv"): |
| 395 | os.utime(path, (old, old)) |
| 396 | assert object_api_keys.resolve_api_key(key, base_dir=data) == "bob" |
| 397 | parses = [] |
| 398 | real = object_identity._parse_account_records |
| 399 | monkeypatch.setattr(object_identity, "_parse_account_records", lambda path: parses.append(path) or real(path)) |
| 400 | for _ in range(5): |
| 401 | assert object_api_keys.resolve_api_key(key, base_dir=data) == "bob" |
| 402 | assert parses == [] |
| 403 | object_identity.set_account_status("acme", "disabled", base_dir=data) |
| 404 | assert object_api_keys.resolve_api_key(key, base_dir=data) is None |
| 405 | assert len(parses) == 1 |
| 406 | |
| 407 | |
| 408 | # ---- a revocation that fails ------------------------------------------------- |
| 409 | |
| 410 | def test_if_the_sessions_cannot_be_ended_the_route_says_so_and_the_account_stays_disabled(data, monkeypatch): |
| 411 | (session, _), key = acme_with_bob() |
| 412 | |
| 413 | real = object_identity._revoke_sessions_where |
| 414 | |
| 415 | def broken(*args, **kwargs): |
| 416 | raise OSError("sessions.tsv is read-only") |
| 417 | |
| 418 | monkeypatch.setattr(object_identity, "_revoke_sessions_where", broken) |
| 419 | status, payload = set_status("acme", "disabled") |
| 420 | assert status == 500, payload |
| 421 | assert payload["code"] == "account_sessions_not_ended" and payload["account_disabled"] is True |
| 422 | assert object_identity.get_account("acme", base_dir=data)["status"] == "disabled" |
| 423 | assert [line["identifier"] for line in object_ops_log.read_events( |
| 424 | base_dir=data, kind="auth", event="account_sessions_not_ended")] == ["acme"] |
| 425 | assert not object_ops_log.read_events(base_dir=data, kind="auth", event="account_status_set") |
| 426 | # Not ended, but refused anyway: the per-request check does not depend on it. |
| 427 | assert seen(session) == (None, None, []) and seen(key) == (None, None, []) |
| 428 | assert not stored_session(data, session).revoked_at |
| 429 | |
| 430 | monkeypatch.setattr(object_identity, "_revoke_sessions_where", real) |
| 431 | status, payload = set_status("acme", "disabled") # the retry finishes the job |
| 432 | assert status == 200 and payload["sessions_revoked"] == 2, payload |
| 433 | assert stored_session(data, session).revoked_at |
| 434 | assert seen(session) == (None, None, []) and seen(key) == (None, None, []) |
| 435 | |
| 436 | |
| 437 | def test_the_cli_reports_a_failed_revocation_and_exits_1(data, monkeypatch, capsys): |
| 438 | acme_with_bob() |
| 439 | monkeypatch.setattr(object_identity, "_revoke_sessions_where", |
| 440 | lambda *a, **k: (_ for _ in ()).throw(OSError("read-only"))) |
| 441 | assert object_identity_cli.main(["--data-dir", str(data), "disable-account", "--account-id", "acme"]) == 1 |
| 442 | assert "could not be ended" in capsys.readouterr().err |
| 443 | assert object_identity.get_account("acme", base_dir=data)["status"] == "disabled" |
| 444 | |
| 445 | |
| 446 | # ---- the CLI and the route's edges ------------------------------------------- |
| 447 | |
| 448 | def test_the_cli_disables_and_enables_an_account(data, capsys): |
| 449 | sessions, key = acme_with_bob() |
| 450 | assert object_identity_cli.main(["--data-dir", str(data), "disable-account", "--account-id", "acme"]) == 0 |
| 451 | assert "Disabled account acme; 2 session(s) signed out" in capsys.readouterr().out |
| 452 | assert all(seen(token) == (None, None, []) for token in (*sessions, key)) |
| 453 | assert object_identity_cli.main(["--data-dir", str(data), "list-accounts"]) == 0 |
| 454 | assert "acme\tdisabled" in capsys.readouterr().out |
| 455 | assert object_identity_cli.main(["--data-dir", str(data), "enable-account", "--account-id", "acme"]) == 0 |
| 456 | assert "Enabled account acme" in capsys.readouterr().out |
| 457 | assert seen(key)[0] == "bob" and login("bob") is not None |
| 458 | assert object_identity_cli.main(["--data-dir", str(data), "enable-account", "--account-id", "nope"]) == 1 |
| 459 | |
| 460 | |
| 461 | @pytest.mark.parametrize( |
| 462 | "method, path, body, expected", |
| 463 | [ |
| 464 | ("PUT", "/identity/accounts/acme/status", {"status": "frozen"}, 400), |
| 465 | ("PUT", "/identity/accounts/acme/status", {}, 400), |
| 466 | ("PUT", "/identity/accounts/acme/status", {"status": "disabled", "reason": "x"}, 400), |
| 467 | ("PUT", "/identity/accounts/nope/status", {"status": "disabled"}, 404), |
| 468 | ("GET", "/identity/accounts/acme/status", None, 405), |
| 469 | ("DELETE", "/identity/accounts/acme/status", None, 405), |
| 470 | ("PUT", "/admin/identity/accounts/acme/status", {"status": "disabled"}, 405), |
| 471 | ], |
| 472 | ) |
| 473 | def test_the_status_route_refuses_bad_requests_without_changing_anything(data, method, path, body, expected): |
| 474 | add_account("acme") |
| 475 | status, _ = call(method, path, body, admin=True) |
| 476 | assert status == expected |
| 477 | assert object_identity.get_account("acme", base_dir=data)["status"] == "active" |
| 478 | |
| 479 | |
| 480 | def test_the_status_route_is_admin_only(data): |
| 481 | add_account("acme") |
| 482 | add_user("bob", "acme") |
| 483 | for auth in ({"token": login("bob")}, {"token": "nonsense"}, {"headers": ()}): |
| 484 | status, _ = call("PUT", "/identity/accounts/acme/status", {"status": "disabled"}, **auth) |
| 485 | assert status in (401, 403) |
| 486 | assert object_identity.get_account("acme", base_dir=data)["status"] == "active" |
| 487 | status, result = call("POST", "/identity/accounts/acme/status", {"status": "disabled"}, admin=True) |
| 488 | assert status == 200 and result["account"]["status"] == "disabled" |
| 489 | |
| 490 | |
| 491 | # ---- identity vouched for by someone else ------------------------------------ |
| 492 | |
| 493 | def test_trusted_headers_cannot_assert_a_member_or_the_account_of_a_disabled_account(data, monkeypatch): |
| 494 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 495 | add_account("acme") |
| 496 | add_user("bob", "acme") |
| 497 | as_bob = [("x-dbbasic-user-id", "bob")] |
| 498 | as_acme = [("x-dbbasic-user-id", "stranger"), ("x-dbbasic-account-id", "acme")] |
| 499 | for headers, user_id in ((as_bob, "bob"), (as_acme, "stranger")): |
| 500 | status, payload = call("GET", "/identity", headers=headers) |
| 501 | assert payload["subject"]["user_id"] == user_id |
| 502 | set_status("acme", "disabled") |
| 503 | for headers in (as_bob, as_acme): |
| 504 | status, payload = call("GET", "/identity", headers=headers) |
| 505 | assert payload["subject"]["user_id"] is None and payload["auth"]["method"] == "anonymous" |