gabriel / musehub public
test_musehub_ui_collaborators_ssr.py python
159 lines 5.2 KB
d4eb1c39 Theme overhaul: domains, new-repo, MCP docs, copy icons; legacy CSS rem… Gabriel Cardona <cgcardona@gmail.com> 3d ago
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 uuid
21
22 import pytest
23 from httpx import AsyncClient
24 from sqlalchemy.ext.asyncio import AsyncSession
25
26 from musehub.db.musehub_collaborator_models import MusehubCollaborator
27 from musehub.db.musehub_models import MusehubRepo
28
29 pytestmark = pytest.mark.anyio
30
31 _OWNER = "ssr-owner"
32 _SLUG = "ssr-collab-repo"
33
34
35 # ---------------------------------------------------------------------------
36 # Helpers
37 # ---------------------------------------------------------------------------
38
39
40 async def _make_repo(db: AsyncSession) -> str:
41 """Seed a minimal public repo and return its repo_id string."""
42 repo = MusehubRepo(
43 name=_SLUG,
44 owner=_OWNER,
45 slug=_SLUG,
46 visibility="public",
47 owner_user_id="ssr-owner-uid",
48 )
49 db.add(repo)
50 await db.commit()
51 await db.refresh(repo)
52 return str(repo.repo_id)
53
54
55 async def _add_collaborator(
56 db: AsyncSession,
57 repo_id: str,
58 *,
59 user_id: str | None = None,
60 permission: str = "write",
61 invited_by: str | None = None,
62 ) -> MusehubCollaborator:
63 """Seed a collaborator record and return it."""
64 collab = MusehubCollaborator(
65 id=str(uuid.uuid4()),
66 repo_id=repo_id,
67 user_id=user_id or str(uuid.uuid4()),
68 permission=permission,
69 invited_by=invited_by,
70 )
71 db.add(collab)
72 await db.commit()
73 await db.refresh(collab)
74 return collab
75
76
77 # ---------------------------------------------------------------------------
78 # Tests
79 # ---------------------------------------------------------------------------
80
81
82 async def test_collaborators_page_renders_collaborator_server_side(
83 client: AsyncClient,
84 db_session: AsyncSession,
85 ) -> None:
86 """Seed a collaborator, GET the page, assert user_id is in the HTML body.
87
88 The SSR migration means collaborators must be rendered server-side.
89 This test fails if the handler omits ``collaborators`` from the template
90 context or the template requires a client-side fetch to populate the list.
91 """
92 repo_id = await _make_repo(db_session)
93 known_user_id = str(uuid.uuid4())
94 await _add_collaborator(db_session, repo_id, user_id=known_user_id, permission="write")
95
96 resp = await client.get(f"/{_OWNER}/{_SLUG}/settings/collaborators")
97 assert resp.status_code == 200
98 assert known_user_id in resp.text
99
100
101 async def test_collaborators_page_invite_form_has_hx_post(
102 client: AsyncClient,
103 db_session: AsyncSession,
104 ) -> None:
105 """The invite form uses HTMX ``hx-post`` to call the collaborators API.
106
107 The SSR migration replaces the inline JS inviteCollab() function with an
108 HTMX form that posts directly to the JSON API endpoint.
109 """
110 await _make_repo(db_session)
111 resp = await client.get(f"/{_OWNER}/{_SLUG}/settings/collaborators")
112 assert resp.status_code == 200
113 assert "hx-post" in resp.text
114 assert "/collaborators" in resp.text
115
116
117 async def test_collaborators_page_remove_form_has_hx_delete(
118 client: AsyncClient,
119 db_session: AsyncSession,
120 ) -> None:
121 """Non-owner collaborator rows carry ``hx-delete`` on the remove form.
122
123 The SSR migration replaces the JS removeCollab() function with an HTMX
124 form targeting the collaborators API endpoint for the specific user.
125 """
126 repo_id = await _make_repo(db_session)
127 target_user_id = str(uuid.uuid4())
128 await _add_collaborator(db_session, repo_id, user_id=target_user_id, permission="write")
129
130 resp = await client.get(f"/{_OWNER}/{_SLUG}/settings/collaborators")
131 assert resp.status_code == 200
132 assert "hx-delete" in resp.text
133 assert target_user_id in resp.text
134
135
136 async def test_collaborators_page_htmx_request_returns_fragment(
137 client: AsyncClient,
138 db_session: AsyncSession,
139 ) -> None:
140 """GET with ``HX-Request: true`` returns only the bare collaborator fragment.
141
142 The fragment must not contain a full HTML document shell (<html>, <head>)
143 — it is swapped directly into ``#collaborator-rows`` by HTMX.
144 """
145 repo_id = await _make_repo(db_session)
146 known_user_id = str(uuid.uuid4())
147 await _add_collaborator(db_session, repo_id, user_id=known_user_id)
148
149 resp = await client.get(
150 f"/{_OWNER}/{_SLUG}/settings/collaborators",
151 headers={"HX-Request": "true"},
152 )
153 assert resp.status_code == 200
154 body = resp.text
155 # Fragment must contain the seeded collaborator
156 assert known_user_id in body
157 # Fragment must NOT be a full HTML document
158 assert "<html" not in body
159 assert "<head" not in body