diff options
| author | Christophe Besson <cbesson@gmail.com> | 2026-08-28 10:09:38 +0200 |
|---|---|---|
| committer | Christophe Besson <cbesson@gmail.com> | 2026-08-28 10:09:38 +0200 |
| commit | 58ca879b017da62e77f40752d8d94fef3f315a1e (patch) | |
| tree | 38cb9b1f8007009e39b462bcda16830aa2b0ede3 /packages/meshbay-hub/tests/test_group_name_unique.py | |
| parent | 471fa6242fc56a035a8c9639722a82b9341328c2 (diff) | |
| download | meshbay-58ca879b017da62e77f40752d8d94fef3f315a1e.tar.gz | |
feat(hub): group names unique per owner, shown as name@owner
A group's identity stays its UUID. What changes is that "the name is
unique" — until now an unenforced expectation — becomes real, scoped to the
owner account, and the owner's username is surfaced so two groups called
"photos" on different nodes can be told apart.
Hub:
- `groups` gains a functional unique index `uq_groups_owner_name` on
`(admin_id, lower(name))` (model + migration c3d4e5f6a7b8). The migration
pre-flights: if the data already clashes it aborts and lists the
offending (admin_id, name) pairs rather than renaming anyone's group.
meshbay.org checked clean.
- `create_group` trims the name, rejects blank (422) and an owner-scoped
case-insensitive clash (409), with an IntegrityError backstop for the
race, and returns `owner_username`.
- `owner_username` added to `/v1/groups/mine`, `GET /v1/groups` (local
rows), `POST /v1/groups/{id}/join`, `GET /v1/admin/groups`.
SPA:
- new `static/group-name.js` — `<GroupName name owner [inline]>` renders the
name with the `@owner` handle on a smaller grey line under it.
- used in the sidebar, the group-page header, Explore cards, the Admin
groups table, and cross-group Search (via a widened `cacheGroupIndex`
carrying the owner). Delete/leave confirmations show `name@owner` inline.
- federated Explore rows show `@<source_hub>` instead of an account.
Design record and the locked decisions: ~/next/groupnames.md (out of repo).
MNP unchanged. Tests: test_group_name_unique.py, test_group_name_migration.py.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018gKJ85aZyvEwarXMFzFEwi
Diffstat (limited to 'packages/meshbay-hub/tests/test_group_name_unique.py')
| -rw-r--r-- | packages/meshbay-hub/tests/test_group_name_unique.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/packages/meshbay-hub/tests/test_group_name_unique.py b/packages/meshbay-hub/tests/test_group_name_unique.py new file mode 100644 index 0000000..2bc109c --- /dev/null +++ b/packages/meshbay-hub/tests/test_group_name_unique.py @@ -0,0 +1,78 @@ +""" +A group name is unique per owner account, case-insensitively — the group's +identity stays its UUID, this only makes `name@owner` a dependable handle. +""" + +import base64 +import hashlib + +import pytest + + +def _auth_key(password: str, username: str) -> str: + salt = hashlib.sha256(f"meshbay:auth:v1:{username}".encode()).digest() + return base64.b64encode( + hashlib.pbkdf2_hmac("sha512", password.encode(), salt, 600_000, 32)).decode() + + +async def _user(client, username, password="a-long-enough-passphrase"): + await client.post("/v1/users/register", json={ + "username": username, "email": f"{username}@example.com", + "auth_key": _auth_key(password, username)}) + r = await client.post("/v1/users/login", json={ + "username": username, "auth_key": _auth_key(password, username)}) + return {"Authorization": f"Bearer {r.json()['access_token']}"} + + +async def _create(client, headers, name): + return await client.post("/v1/groups", json={"name": name}, headers=headers) + + +@pytest.mark.asyncio +async def test_same_owner_same_name_is_refused(client): + alice = await _user(client, "alice") + r1 = await _create(client, alice, "photos") + assert r1.status_code == 201 + assert r1.json()["owner_username"] == "alice" + + r2 = await _create(client, alice, "photos") + assert r2.status_code == 409 + assert "photos" in r2.json()["detail"] + + +@pytest.mark.asyncio +async def test_same_owner_different_case_is_refused(client): + alice = await _user(client, "alice") + assert (await _create(client, alice, "Photos")).status_code == 201 + assert (await _create(client, alice, " photos ")).status_code == 409 + + +@pytest.mark.asyncio +async def test_two_owners_may_share_a_name(client): + alice = await _user(client, "alice") + bob = await _user(client, "bob") + assert (await _create(client, alice, "photos")).status_code == 201 + assert (await _create(client, bob, "photos")).status_code == 201 + + +@pytest.mark.asyncio +async def test_name_is_trimmed_on_create(client): + alice = await _user(client, "alice") + r = await _create(client, alice, " spaced out ") + assert r.status_code == 201 + assert r.json()["name"] == "spaced out" + + +@pytest.mark.asyncio +async def test_blank_name_is_refused(client): + alice = await _user(client, "alice") + assert (await _create(client, alice, " ")).status_code == 422 + + +@pytest.mark.asyncio +async def test_owner_username_is_reported_in_listings(client): + alice = await _user(client, "alice") + await _create(client, alice, "photos") + + mine = (await client.get("/v1/groups/mine", headers=alice)).json()["groups"] + assert mine and mine[0]["owner_username"] == "alice" |