aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/tests')
-rw-r--r--packages/meshbay-hub/tests/test_group_name_migration.py64
-rw-r--r--packages/meshbay-hub/tests/test_group_name_unique.py78
2 files changed, 142 insertions, 0 deletions
diff --git a/packages/meshbay-hub/tests/test_group_name_migration.py b/packages/meshbay-hub/tests/test_group_name_migration.py
new file mode 100644
index 0000000..9813e04
--- /dev/null
+++ b/packages/meshbay-hub/tests/test_group_name_migration.py
@@ -0,0 +1,64 @@
+"""
+The uq_groups_owner_name migration refuses to run over data that already
+violates it, naming the offending pairs — it must never silently rename a
+group.
+"""
+
+import sqlite3
+from pathlib import Path
+
+import pytest
+from alembic import command
+from alembic.config import Config
+
+HUB = Path(__file__).resolve().parents[1]
+BEFORE = "b1c2d3e4f5a6"
+AFTER = "c3d4e5f6a7b8"
+
+_USER = ("INSERT INTO users (id, username, email, pw_hash, pw_salt, hub_id) "
+ "VALUES ('u1', 'alice', 'a@x', x'00', x'00', 'h')")
+_GROUP = ("INSERT INTO groups (id, name, admin_id, visibility, join_policy, status) "
+ "VALUES (?, ?, 'u1', 'private', 'invite', 'active')")
+
+
+@pytest.fixture
+def at_before(tmp_path, monkeypatch):
+ """A hub DB migrated up to the revision just before uq_groups_owner_name."""
+ db = tmp_path / "hub.db"
+ monkeypatch.setenv("MESHBAY_DATABASE_URL", f"sqlite+aiosqlite:///{db}")
+ cfg = Config(str(HUB / "alembic.ini"))
+ command.upgrade(cfg, BEFORE)
+ return db, cfg
+
+
+def test_migration_aborts_on_a_pre_existing_duplicate(at_before):
+ db, cfg = at_before
+ con = sqlite3.connect(db)
+ con.execute(_USER)
+ con.execute(_GROUP, ("g1", "Photos"))
+ con.execute(_GROUP, ("g2", "photos")) # same owner, same name bar case
+ con.commit()
+ con.close()
+
+ with pytest.raises(Exception) as exc:
+ command.upgrade(cfg, AFTER)
+ assert "uq_groups_owner_name" in str(exc.value)
+ assert "photos" in str(exc.value).lower()
+
+
+def test_migration_runs_when_data_is_clean(at_before):
+ db, cfg = at_before
+ con = sqlite3.connect(db)
+ con.execute(_USER)
+ con.execute(_GROUP, ("g1", "Photos"))
+ con.execute(_GROUP, ("g2", "Videos"))
+ con.commit()
+ con.close()
+
+ command.upgrade(cfg, AFTER)
+
+ con = sqlite3.connect(db)
+ idx = [r[0] for r in con.execute(
+ "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='groups'")]
+ con.close()
+ assert "uq_groups_owner_name" in idx
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"