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_generated_ui.py · 7.1 KB · 164 lines
| 1 | """Browser end-to-end smoke over the generated-UI pipeline: schema -> |
| 2 | seeded `views` record -> site_view_render -> window.dbbasicList / |
| 3 | window.dbbasicForm. See tests/e2e/conftest.py for the server + user |
| 4 | fixtures every test here drives. |
| 5 | |
| 6 | Kept to four tests, each proving something the sixteen bespoke pages this |
| 7 | pipeline replaced used to require writing separately: |
| 8 | |
| 9 | 1. the real /login form (not a shortcut) grants a browser session. |
| 10 | 2. /notes: the Add button opens a form built FROM THE SCHEMA, and |
| 11 | saving it updates the list live (no reload) -- the full generated |
| 12 | create path, end to end. |
| 13 | 3. /tasks: the same create-through-generated-form path on a second, |
| 14 | differently-shaped collection (tasks render as a Kanban board, not |
| 15 | a row list) -- proof the pipeline is shared, not notes-specific. |
| 16 | 4. permissions: owner_scoped + row-filtered reads hold up when driven |
| 17 | from a real signed-in browser, not just asserted at the API layer. |
| 18 | |
| 19 | Every wait below is an `expect(...)` (Playwright's own auto-retrying |
| 20 | assertion) or a `get_by_role`/`get_by_text` locator that itself waits for |
| 21 | the element to appear -- no bare sleep(), since the thing actually being |
| 22 | proven (the websocket-pushed live list refresh) is exactly the kind of |
| 23 | timing a sleep would either flake on or silently paper over. |
| 24 | """ |
| 25 | |
| 26 | from __future__ import annotations |
| 27 | |
| 28 | import uuid |
| 29 | |
| 30 | import pytest |
| 31 | from playwright.sync_api import BrowserContext, Page, expect |
| 32 | |
| 33 | pytestmark = pytest.mark.e2e |
| 34 | |
| 35 | # The live-list-refresh assertions cross a real POST -> websocket broadcast |
| 36 | # -> dbbasicSubscribe -> re-fetch round trip; a little more headroom than |
| 37 | # Playwright's 5s default keeps this from flaking under CI/CPU contention |
| 38 | # without hiding a genuinely broken push (a real failure still times out). |
| 39 | LIVE_UPDATE_TIMEOUT_MS = 10_000 |
| 40 | |
| 41 | |
| 42 | def _login(page: Page, base_url: str, email: str, password: str, next_path: str = "/") -> None: |
| 43 | """Drive the real /login page (object_server._handle_login) exactly |
| 44 | like a person would: GET the form, fill it in, submit, follow the |
| 45 | redirect. The hidden `next` field (pre-filled from the query string |
| 46 | on GET, see _send_login_page) is how the handler decides where to |
| 47 | land -- http_api_contract.LOGIN_PATH POSTs back to itself and 302s to |
| 48 | `next` on success. |
| 49 | """ |
| 50 | page.goto(f"{base_url}/login?next={next_path}") |
| 51 | page.get_by_label("Email").fill(email) |
| 52 | page.get_by_label("Password").fill(password) |
| 53 | page.get_by_role("button", name="Sign in").click() |
| 54 | expect(page).to_have_url(f"{base_url}{next_path}") |
| 55 | |
| 56 | |
| 57 | def test_login_through_the_browser_lands_on_next_and_sets_session_cookie( |
| 58 | e2e_server: dict, page: Page |
| 59 | ) -> None: |
| 60 | base_url = e2e_server["base_url"] |
| 61 | alice = e2e_server["users"]["alice"] |
| 62 | |
| 63 | _login(page, base_url, alice["email"], alice["password"], next_path="/notes") |
| 64 | |
| 65 | cookie_names = {cookie["name"] for cookie in page.context.cookies()} |
| 66 | assert "dbbasic_session" in cookie_names |
| 67 | |
| 68 | # Landed on a real generated page, not an error page or a login |
| 69 | # bounce -- the seeded `views` record's title lands in #viewtitle |
| 70 | # once the page's own script fetches it (see view_render.py's GET() |
| 71 | # and _SCRIPT). |
| 72 | expect(page.locator("#viewtitle")).to_have_text("Notes") |
| 73 | |
| 74 | |
| 75 | def test_notes_add_button_renders_schema_form_and_new_note_appears_live( |
| 76 | e2e_server: dict, page: Page |
| 77 | ) -> None: |
| 78 | base_url = e2e_server["base_url"] |
| 79 | alice = e2e_server["users"]["alice"] |
| 80 | _login(page, base_url, alice["email"], alice["password"], next_path="/notes") |
| 81 | |
| 82 | add_button = page.get_by_role("button", name="+ New Note") |
| 83 | expect(add_button).to_be_visible() |
| 84 | add_button.click() |
| 85 | |
| 86 | # The field that renders is FROM THE SCHEMA |
| 87 | # (packages/app-notes/schemas/notes.json): `content` is a required |
| 88 | # textarea, not hand-authored markup -- see form.py's control(). |
| 89 | content_field = page.locator('textarea[name="content"]') |
| 90 | expect(content_field).to_be_visible() |
| 91 | |
| 92 | note_text = f"e2e note {uuid.uuid4()}" |
| 93 | content_field.fill(note_text) |
| 94 | page.get_by_role("button", name="Save", exact=True).click() |
| 95 | |
| 96 | # No manual reload: window.dbbasicList re-renders over the websocket |
| 97 | # push (dbbasicSubscribe, wired in app-theme's nav.py) once the POST |
| 98 | # that created the record lands. |
| 99 | expect(page.get_by_text(note_text)).to_be_visible(timeout=LIVE_UPDATE_TIMEOUT_MS) |
| 100 | |
| 101 | |
| 102 | def test_tasks_add_button_renders_schema_form_and_new_task_appears_live( |
| 103 | e2e_server: dict, page: Page |
| 104 | ) -> None: |
| 105 | base_url = e2e_server["base_url"] |
| 106 | alice = e2e_server["users"]["alice"] |
| 107 | _login(page, base_url, alice["email"], alice["password"], next_path="/tasks") |
| 108 | |
| 109 | add_button = page.get_by_role("button", name="+ New Task") |
| 110 | expect(add_button).to_be_visible() |
| 111 | add_button.click() |
| 112 | |
| 113 | # tasks.json's schema-driven form -- `title` is a required text |
| 114 | # input. Same /form generator as notes' textarea, and the same |
| 115 | # Add-button/inline-panel chrome from view_render.py's `list` block |
| 116 | # -- proof one pipeline draws both collections with zero extra page |
| 117 | # code, even though tasks render as a Kanban board (views.list_mode |
| 118 | # == "board") rather than notes' plain row list. |
| 119 | title_field = page.locator('input[name="title"]') |
| 120 | expect(title_field).to_be_visible() |
| 121 | |
| 122 | task_title = f"e2e task {uuid.uuid4()}" |
| 123 | title_field.fill(task_title) |
| 124 | page.get_by_role("button", name="Save", exact=True).click() |
| 125 | |
| 126 | expect(page.get_by_text(task_title)).to_be_visible(timeout=LIVE_UPDATE_TIMEOUT_MS) |
| 127 | |
| 128 | |
| 129 | def test_owner_scoped_notes_are_invisible_to_a_second_signed_in_user( |
| 130 | e2e_server: dict, new_context |
| 131 | ) -> None: |
| 132 | base_url = e2e_server["base_url"] |
| 133 | alice = e2e_server["users"]["alice"] |
| 134 | bob = e2e_server["users"]["bob"] |
| 135 | |
| 136 | # Two independent browser contexts (separate cookie jars) rather than |
| 137 | # two sequential logins in one context/page -- this is really two |
| 138 | # different people, and reusing one page could hide a cookie-scoping |
| 139 | # bug behind app-level state cleanup. |
| 140 | alice_context: BrowserContext = new_context() |
| 141 | alice_page = alice_context.new_page() |
| 142 | _login(alice_page, base_url, alice["email"], alice["password"], next_path="/notes") |
| 143 | |
| 144 | private_note = f"e2e private note {uuid.uuid4()}" |
| 145 | alice_page.get_by_role("button", name="+ New Note").click() |
| 146 | alice_page.locator('textarea[name="content"]').fill(private_note) |
| 147 | alice_page.get_by_role("button", name="Save", exact=True).click() |
| 148 | # Confirm it actually saved (and is visible to its owner) before |
| 149 | # checking who else can see it. |
| 150 | expect(alice_page.get_by_text(private_note)).to_be_visible(timeout=LIVE_UPDATE_TIMEOUT_MS) |
| 151 | alice_context.close() |
| 152 | |
| 153 | bob_context: BrowserContext = new_context() |
| 154 | bob_page = bob_context.new_page() |
| 155 | _login(bob_page, base_url, bob["email"], bob["password"], next_path="/notes") |
| 156 | |
| 157 | # Bob is a real, freshly signed-in user with zero notes of his own. |
| 158 | # The `list` block asked for VIEWER_ID-scoped rows (owner_scoped: |
| 159 | # true), but what actually enforces it is the server-side row_filter |
| 160 | # {"owner_id": "$user_id"} in packages/app-notes/permissions/ |
| 161 | # rules.json -- this is checking that gate, not the client's ask. |
| 162 | expect(bob_page.get_by_text("Nothing yet.")).to_be_visible() |
| 163 | expect(bob_page.get_by_text(private_note)).to_have_count(0) |
| 164 | bob_context.close() |