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_server_prefs.py · 7.6 KB · 224 lines
| 1 | """Tests for Phase 6 routes: GET/PUT /prefs, /prefs/{key}, GET /api/flags. |
| 2 | |
| 3 | Mirrors the route-testing conventions in tests/test_object_server.py (the |
| 4 | `request` ASGI harness, `write_records` TSV fixtures) and |
| 5 | tests/test_object_site_routes.py (importing that harness rather than |
| 6 | duplicating it). |
| 7 | """ |
| 8 | |
| 9 | import json |
| 10 | |
| 11 | import object_server |
| 12 | |
| 13 | from test_object_server import request, write_records |
| 14 | |
| 15 | |
| 16 | def _trust_headers(monkeypatch): |
| 17 | monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true") |
| 18 | |
| 19 | |
| 20 | def _user(user_id): |
| 21 | return [("x-dbbasic-user-id", user_id)] |
| 22 | |
| 23 | |
| 24 | # --------------------------------------------------------------------------- |
| 25 | # Helper function unit tests (factored out of the route handlers) |
| 26 | # --------------------------------------------------------------------------- |
| 27 | |
| 28 | |
| 29 | def test_valid_pref_key_rejects_traversal_and_bad_characters(): |
| 30 | assert object_server._valid_pref_key("theme") is True |
| 31 | assert object_server._valid_pref_key("flag:kanban_view") is True |
| 32 | assert object_server._valid_pref_key("a.b-c_9") is True |
| 33 | assert object_server._valid_pref_key("") is False |
| 34 | assert object_server._valid_pref_key("../etc/passwd") is False |
| 35 | assert object_server._valid_pref_key("a/b") is False |
| 36 | assert object_server._valid_pref_key("has space") is False |
| 37 | assert object_server._valid_pref_key("_leading_underscore") is False |
| 38 | |
| 39 | |
| 40 | def test_upsert_user_pref_creates_then_updates_in_place(monkeypatch, tmp_path): |
| 41 | data_dir = tmp_path / "data" |
| 42 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 43 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 44 | |
| 45 | created = object_server._upsert_user_pref("7", "theme", "dark") |
| 46 | assert created["owner_id"] == "7" |
| 47 | assert created["key"] == "theme" |
| 48 | assert created["value"] == "dark" |
| 49 | |
| 50 | updated = object_server._upsert_user_pref("7", "theme", "light") |
| 51 | assert updated["id"] == created["id"] |
| 52 | assert updated["value"] == "light" |
| 53 | |
| 54 | assert object_server._user_prefs_map("7") == {"theme": "light"} |
| 55 | assert object_server._find_user_pref("8", "theme") is None |
| 56 | |
| 57 | |
| 58 | def test_resolve_flags_user_override_beats_instance_value(monkeypatch, tmp_path): |
| 59 | data_dir = tmp_path / "data" |
| 60 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 61 | write_records( |
| 62 | data_dir, |
| 63 | "feature_flags", |
| 64 | "id\tflag\tvalue\tdescription\nf1\tkanban_view\toff\tKanban board\n", |
| 65 | ) |
| 66 | write_records( |
| 67 | data_dir, |
| 68 | "user_prefs", |
| 69 | "id\towner_id\tkey\tvalue\np1\t7\tflag:kanban_view\ton\n", |
| 70 | ) |
| 71 | |
| 72 | assert object_server._resolve_flags(None) == {"kanban_view": "off"} |
| 73 | assert object_server._resolve_flags("7") == {"kanban_view": "on"} |
| 74 | assert object_server._resolve_flags("9") == {"kanban_view": "off"} |
| 75 | |
| 76 | |
| 77 | # --------------------------------------------------------------------------- |
| 78 | # Route-level tests |
| 79 | # --------------------------------------------------------------------------- |
| 80 | |
| 81 | |
| 82 | def test_put_prefs_then_get_prefs_key_and_prefs_map(monkeypatch, tmp_path): |
| 83 | data_dir = tmp_path / "data" |
| 84 | _trust_headers(monkeypatch) |
| 85 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 86 | headers = _user("7") |
| 87 | |
| 88 | put_status, _, put_payload = request( |
| 89 | "/prefs/theme", |
| 90 | method="PUT", |
| 91 | body=json.dumps({"value": "dark"}).encode(), |
| 92 | headers=headers, |
| 93 | ) |
| 94 | assert put_status == 200 |
| 95 | assert put_payload == {"status": "ok", "key": "theme", "value": "dark"} |
| 96 | |
| 97 | get_status, _, get_payload = request("/prefs/theme", headers=headers) |
| 98 | assert get_status == 200 |
| 99 | assert get_payload == {"status": "ok", "key": "theme", "value": "dark"} |
| 100 | |
| 101 | map_status, _, map_payload = request("/prefs", headers=headers) |
| 102 | assert map_status == 200 |
| 103 | assert map_payload == {"status": "ok", "prefs": {"theme": "dark"}} |
| 104 | |
| 105 | put2_status, _, put2_payload = request( |
| 106 | "/prefs/theme", |
| 107 | method="PUT", |
| 108 | body=json.dumps({"value": "light"}).encode(), |
| 109 | headers=headers, |
| 110 | ) |
| 111 | assert put2_status == 200 |
| 112 | assert put2_payload == {"status": "ok", "key": "theme", "value": "light"} |
| 113 | |
| 114 | get2_status, _, get2_payload = request("/prefs/theme", headers=headers) |
| 115 | assert get2_payload == {"status": "ok", "key": "theme", "value": "light"} |
| 116 | |
| 117 | |
| 118 | def test_get_prefs_anonymous_returns_empty_map(tmp_path): |
| 119 | data_dir = tmp_path / "data" |
| 120 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 121 | |
| 122 | status, _, payload = request("/prefs") |
| 123 | |
| 124 | assert status == 200 |
| 125 | assert payload == {"status": "ok", "prefs": {}} |
| 126 | |
| 127 | |
| 128 | def test_get_prefs_key_anonymous_is_not_found(tmp_path): |
| 129 | data_dir = tmp_path / "data" |
| 130 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 131 | |
| 132 | status, _, payload = request("/prefs/theme") |
| 133 | |
| 134 | assert status == 404 |
| 135 | |
| 136 | |
| 137 | def test_put_prefs_anonymous_requires_session(tmp_path): |
| 138 | data_dir = tmp_path / "data" |
| 139 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 140 | |
| 141 | status, _, payload = request( |
| 142 | "/prefs/theme", method="PUT", body=json.dumps({"value": "dark"}).encode() |
| 143 | ) |
| 144 | |
| 145 | assert status == 401 |
| 146 | |
| 147 | |
| 148 | def test_prefs_key_rejects_invalid_characters(monkeypatch, tmp_path): |
| 149 | data_dir = tmp_path / "data" |
| 150 | _trust_headers(monkeypatch) |
| 151 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 152 | headers = _user("7") |
| 153 | |
| 154 | status, _, payload = request("/prefs/has space", headers=headers) |
| 155 | assert status == 400 |
| 156 | |
| 157 | status2, _, payload2 = request("/prefs/..%2Fadmin", headers=headers) |
| 158 | assert status2 == 400 |
| 159 | |
| 160 | |
| 161 | def test_prefs_route_rejects_unsupported_methods(tmp_path): |
| 162 | data_dir = tmp_path / "data" |
| 163 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 164 | |
| 165 | status, _, _ = request("/prefs", method="POST") |
| 166 | assert status == 405 |
| 167 | |
| 168 | status2, _, _ = request("/prefs/theme", method="DELETE") |
| 169 | assert status2 == 405 |
| 170 | |
| 171 | status3, _, _ = request("/api/flags", method="POST") |
| 172 | assert status3 == 405 |
| 173 | |
| 174 | |
| 175 | def test_user_cannot_read_another_users_pref(monkeypatch, tmp_path): |
| 176 | data_dir = tmp_path / "data" |
| 177 | _trust_headers(monkeypatch) |
| 178 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 179 | |
| 180 | request( |
| 181 | "/prefs/theme", |
| 182 | method="PUT", |
| 183 | body=json.dumps({"value": "dark"}).encode(), |
| 184 | headers=_user("7"), |
| 185 | ) |
| 186 | |
| 187 | own_status, _, own_payload = request("/prefs/theme", headers=_user("7")) |
| 188 | other_status, _, other_payload = request("/prefs/theme", headers=_user("8")) |
| 189 | other_map_status, _, other_map_payload = request("/prefs", headers=_user("8")) |
| 190 | |
| 191 | assert own_status == 200 |
| 192 | assert own_payload["value"] == "dark" |
| 193 | assert other_status == 404 |
| 194 | assert other_map_payload == {"status": "ok", "prefs": {}} |
| 195 | |
| 196 | |
| 197 | def test_api_flags_user_override_beats_instance_and_works_anonymously(monkeypatch, tmp_path): |
| 198 | data_dir = tmp_path / "data" |
| 199 | _trust_headers(monkeypatch) |
| 200 | write_records( |
| 201 | data_dir, |
| 202 | "feature_flags", |
| 203 | "id\tflag\tvalue\tdescription\nf1\tkanban_view\toff\tKanban board\n", |
| 204 | ) |
| 205 | write_records(data_dir, "user_prefs", "id\towner_id\tkey\tvalue\n") |
| 206 | |
| 207 | anon_status, _, anon_payload = request("/api/flags") |
| 208 | assert anon_status == 200 |
| 209 | assert anon_payload == {"status": "ok", "flags": {"kanban_view": "off"}} |
| 210 | |
| 211 | request( |
| 212 | "/prefs/flag:kanban_view", |
| 213 | method="PUT", |
| 214 | body=json.dumps({"value": "on"}).encode(), |
| 215 | headers=_user("7"), |
| 216 | ) |
| 217 | |
| 218 | user_status, _, user_payload = request("/api/flags", headers=_user("7")) |
| 219 | assert user_status == 200 |
| 220 | assert user_payload == {"status": "ok", "flags": {"kanban_view": "on"}} |
| 221 | |
| 222 | other_status, _, other_payload = request("/api/flags", headers=_user("8")) |
| 223 | assert other_status == 200 |
| 224 | assert other_payload == {"status": "ok", "flags": {"kanban_view": "off"}} |