gabriel / musehub public

test_musehub_ui_collaborators_ssr.py file-level

at sha256:d · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 💥 blast risk
sha256:9 docs: document all 6 HD path segments; fix deep link anchors Three new… · gabriel · Jun 17, 2026
1 """SSR tests for the MuseHub collaborators settings page (issue #564).
2
3 Covers GET /{owner}/{repo_slug}/settings/collaborators after SSR migration:
4
5 - test_collaborators_page_renders_collaborator_server_side
6 Seed a collaborator, GET the page, assert the user_id appears in the HTML body
7 — confirming server-side render rather than client-side JS fetch.
8
9 - test_collaborators_page_invite_form_has_hx_post
10 The invite form carries ``hx-post`` pointing to the collaborators API.
11
12 - test_collaborators_page_remove_form_has_hx_delete
13 Each non-owner collaborator row's remove form carries ``hx-delete``.
14
15 - test_collaborators_page_htmx_request_returns_fragment
16 GET with ``HX-Request: true`` returns only the bare fragment (no <html> wrapper).
17 """
18 from __future__ import annotations
19
20 import secrets
21
22 import pytest
23 from httpx import AsyncClient
24 from sqlalchemy.ext.asyncio import AsyncSession
25
26 from datetime import datetime, timezone
27
28 from musehub.core.genesis import compute_collaborator_id, compute_identity_id, compute_repo_id
29 from musehub.db.musehub_collaborator_models import MusehubCollaborator
30 from musehub.db.musehub_repo_models import MusehubRepo
31
32
33 # Must match the auth_headers test identity ("testuser") so the owner-only gate
34 # on the collaborators settings page (claims.handle == owner) passes.
35 _OWNER = "testuser"
36 _SLUG = "ssr-collab-repo"
37
38
39 # ---------------------------------------------------------------------------
40 # Helpers
41 # ---------------------------------------------------------------------------
42
43
44 async def _make_repo(db: AsyncSession) -> str:
45 """Seed a minimal public repo and return its repo_id string."""
46 owner_id = compute_identity_id(_OWNER.encode())
47 created_at = datetime.now(tz=timezone.utc)
48 repo = MusehubRepo(
49 repo_id=compute_repo_id(owner_id, _SLUG, "code", created_at.isoformat()),
50 name=_SLUG,
51 owner=_OWNER,
52 slug=_SLUG,
53 visibility="public",
54 owner_user_id=owner_id,
55 created_at=created_at,
56 updated_at=created_at,
57 )
58 db.add(repo)
59 await db.commit()
60 await db.refresh(repo)
61 return str(repo.repo_id)
62
63
64 async def _add_collaborator(
65 db: AsyncSession,
66 repo_id: str,
67 *,
68 user_id: str | None = None,
69 permission: str = "write",
70 invited_by: str | None = None,
71 ) -> MusehubCollaborator:
72 """Seed a collaborator record and return it."""
73 handle = user_id or f"collab-{secrets.token_hex(4)}"
74 identity_id = compute_identity_id(handle.encode())
75 created_at = datetime.now(tz=timezone.utc)
76 collab = MusehubCollaborator(
77 id=compute_collaborator_id(repo_id, identity_id, created_at.isoformat()),
78 repo_id=repo_id,
79 identity_handle=handle,
80 permission=permission,
81 invited_by_handle=invited_by,
82 )
83 db.add(collab)
84 await db.commit()
85 await db.refresh(collab)
86 return collab
87
88
89 # ---------------------------------------------------------------------------
90 # Tests
91 # ---------------------------------------------------------------------------
92
93
94 async def test_collaborators_page_renders_collaborator_server_side(
95 client: AsyncClient,
96 db_session: AsyncSession,
97 auth_headers: dict[str, str],
98 ) -> None:
99 """Seed a collaborator, GET the page, assert user_id is in the HTML body.
100
101 The SSR migration means collaborators must be rendered server-side.
102 This test fails if the handler omits ``collaborators`` from the template
103 context or the template requires a client-side fetch to populate the list.
104 """
105 repo_id = await _make_repo(db_session)
106 known_user_id = secrets.token_hex(16)
107 await _add_collaborator(db_session, repo_id, user_id=known_user_id, permission="write")
108
109 resp = await client.get(f"/{_OWNER}/{_SLUG}/settings/collaborators")
110 assert resp.status_code == 200
111 assert known_user_id in resp.text
112
113
114 async def test_collaborators_page_invite_form_has_hx_post(
115 client: AsyncClient,
116 db_session: AsyncSession,
117 auth_headers: dict[str, str],
118 ) -> None:
119 """The invite form uses HTMX ``hx-post`` to call the collaborators API.
120
121 The SSR migration replaces the inline JS inviteCollab() function with an
122 HTMX form that posts directly to the JSON API endpoint.
123 """
124 await _make_repo(db_session)
125 resp = await client.get(f"/{_OWNER}/{_SLUG}/settings/collaborators")
126 assert resp.status_code == 200
127 assert "hx-post" in resp.text
128 assert "/collaborators" in resp.text
129
130
131 async def test_collaborators_page_remove_form_has_hx_delete(
132 client: AsyncClient,
133 db_session: AsyncSession,
134 auth_headers: dict[str, str],
135 ) -> None:
136 """Non-owner collaborator rows carry ``hx-delete`` on the remove form.
137
138 The SSR migration replaces the JS removeCollab() function with an HTMX
139 form targeting the collaborators API endpoint for the specific user.
140 """
141 repo_id = await _make_repo(db_session)
142 target_user_id = secrets.token_hex(16)
143 await _add_collaborator(db_session, repo_id, user_id=target_user_id, permission="write")
144
145 resp = await client.get(f"/{_OWNER}/{_SLUG}/settings/collaborators")
146 assert resp.status_code == 200
147 assert "hx-delete" in resp.text
148 assert target_user_id in resp.text
149
150
151 async def test_collaborators_page_htmx_request_returns_fragment(
152 client: AsyncClient,
153 db_session: AsyncSession,
154 auth_headers: dict[str, str],
155 ) -> None:
156 """GET with ``HX-Request: true`` returns only the bare collaborator fragment.
157
158 The fragment must not contain a full HTML document shell (<html>, <head>)
159 — it is swapped directly into ``#collaborator-rows`` by HTMX.
160 """
161 repo_id = await _make_repo(db_session)
162 known_user_id = secrets.token_hex(16)
163 await _add_collaborator(db_session, repo_id, user_id=known_user_id)
164
165 resp = await client.get(
166 f"/{_OWNER}/{_SLUG}/settings/collaborators",
167 headers={"HX-Request": "true"},
168 )
169 assert resp.status_code == 200
170 body = resp.text
171 # Fragment must contain the seeded collaborator
172 assert known_user_id in body
173 # Fragment must NOT be a full HTML document
174 assert "<html" not in body
175 assert "<head" not in body