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_new_package.py · 3.9 KB · 87 lines
| 1 | """scripts/new_package.py writes a package that works as written. |
| 2 | |
| 3 | The claim the docs make ("Start From A Scaffold") is that the scaffold installs, |
| 4 | enforces its owner rule, lists its rows on its page, and is valid DRON. Each is |
| 5 | checked here on a package written into a scratch directory. |
| 6 | """ |
| 7 | |
| 8 | import json |
| 9 | import shutil |
| 10 | from pathlib import Path |
| 11 | |
| 12 | import pytest |
| 13 | |
| 14 | from scripts import new_package |
| 15 | from scripts.try_package import Sandbox, view_rows |
| 16 | from test_dron_schema import ( |
| 17 | COLLECTION_SCHEMA, PACKAGE_SCHEMA, PERMISSIONS_SCHEMA, ROUTE_COLUMNS, |
| 18 | _tsv_rows, _view_row_problems, schema_errors, |
| 19 | ) |
| 20 | |
| 21 | ROOT = Path(__file__).resolve().parents[1] |
| 22 | |
| 23 | |
| 24 | @pytest.fixture |
| 25 | def root(tmp_path): |
| 26 | packages = tmp_path / "packages" |
| 27 | for name in ("app-nav", "app-views"): |
| 28 | shutil.copytree(ROOT / "packages" / name, packages / name) |
| 29 | return packages |
| 30 | |
| 31 | |
| 32 | def test_the_scaffold_is_valid_dron(root): |
| 33 | package = new_package.create_package("app-tickets", packages_dir=root) |
| 34 | read = lambda relative: json.loads((package / relative).read_text()) # noqa: E731 |
| 35 | assert not schema_errors(read("dbbasic-package.json"), PACKAGE_SCHEMA) |
| 36 | assert not schema_errors(read("schemas/tickets.json"), COLLECTION_SCHEMA) |
| 37 | assert not schema_errors(read("permissions/rules.json"), PERMISSIONS_SCHEMA) |
| 38 | for row in _tsv_rows(package / "seed" / "views.tsv"): |
| 39 | assert not _view_row_problems(row) |
| 40 | for row in _tsv_rows(package / "seed" / "site_routes.tsv"): |
| 41 | assert set(row) <= ROUTE_COLUMNS |
| 42 | |
| 43 | |
| 44 | def test_the_scaffold_installs_and_each_person_sees_only_their_own_rows(root, monkeypatch): |
| 45 | monkeypatch.setenv("DBBASIC_ENABLE_SITE_ROUTES", "true") |
| 46 | new_package.create_package("app-tickets", packages_dir=root) |
| 47 | with Sandbox(["app-tickets"], packages_dir=root) as box: |
| 48 | assert box.installed == ["app-nav", "app-views", "app-tickets"] |
| 49 | box.add_user("ann") |
| 50 | box.add_user("bob") |
| 51 | path = "/collections/tickets/records" |
| 52 | assert box.call("ann", "POST", path, {"title": "first", "notes": "n", "owner_id": "ann"})[0] == 201 |
| 53 | assert box.call("ann", "POST", path, {"notes": "no title", "owner_id": "ann"})[0] == 400 |
| 54 | assert box.call("ann", "POST", path, {"title": "x", "owner_id": "bob"})[0] == 403 |
| 55 | assert box.call("ann", "GET", path)[1]["total"] == 1 |
| 56 | assert box.call("bob", "GET", path)[1]["total"] == 0 |
| 57 | assert box.call("anon", "GET", path)[0] in (401, 403) |
| 58 | assert box.call("ann", "GET", "/tickets")[0] == 200 |
| 59 | lines = view_rows(box, "ann", "/tickets") |
| 60 | assert any("first / n" in line for line in lines), lines |
| 61 | entries = box.call("ann", "GET", "/objects/action_nav_entries")[1]["entries"] |
| 62 | assert "tickets" in {entry["id"] for entry in entries} |
| 63 | |
| 64 | |
| 65 | def test_the_scaffold_names_the_dron_version_this_server_reads(root): |
| 66 | import object_packages |
| 67 | |
| 68 | package = new_package.create_package("app-tickets", packages_dir=root) |
| 69 | manifest = json.loads((package / "dbbasic-package.json").read_text()) |
| 70 | assert manifest["dron"] == object_packages.DRON_VERSION |
| 71 | |
| 72 | |
| 73 | def test_an_id_with_dashes_and_no_app_prefix_names_the_collection(root): |
| 74 | package = new_package.create_package("support-cases", packages_dir=root) |
| 75 | assert (package / "schemas" / "support_cases.json").is_file() |
| 76 | assert new_package.collection_for("app-time-logs") == "time_logs" |
| 77 | |
| 78 | |
| 79 | def test_it_will_not_overwrite_or_accept_a_bad_id(root, capsys): |
| 80 | new_package.create_package("app-tickets", packages_dir=root) |
| 81 | with pytest.raises(new_package.ScaffoldError, match="already exists"): |
| 82 | new_package.create_package("app-tickets", packages_dir=root) |
| 83 | assert new_package.main(["Bad Id", "--dir", str(root)]) == 2 |
| 84 | assert new_package.main(["app-tickets", "--dir", str(root)]) == 2 |
| 85 | assert "error:" in capsys.readouterr().err |
| 86 | assert new_package.main(["app-things", "--dir", str(root)]) == 0 |
| 87 | assert "try_package.py app-things" in capsys.readouterr().out |