msgboard-chatbot / tests /test_space_bootstrap.py
cmpatino's picture
cmpatino HF Staff
Clone msgboard demo with empty general channel and owner admin sign-in
742e389 verified
Raw History Blame Contribute Delete
2.91 kB
"""Deployment bootstrap checks; each test uses a fresh, disposable database."""
import importlib.util
import time
from pathlib import Path
from msgboard import auth, db, store
spec = importlib.util.spec_from_file_location("space_app", Path(__file__).parents[1] / "hfspace" / "app.py")
space_app = importlib.util.module_from_spec(spec)
spec.loader.exec_module(space_app)
def test_fresh_board_is_empty_and_owner_can_sign_in(tmp_path, monkeypatch):
monkeypatch.setenv("MSGBOARD_OWNER_HF_USERNAME", "cmpatino")
_, token, _ = auth.new_token()
space_app.ensure_board(tmp_path, token)
conn = db.connect(tmp_path / "board.db")
try:
p = store.authenticate(conn, token, time.time())
channels = store.list_channels(conn, p)
assert [(c["name"], c["kind"], c["last_seq"]) for c in channels] == [("general", "public", 0)]
for table in ("messages", "attachments", "invites", "channel_doc_versions"):
assert conn.execute(f"SELECT count(*) FROM {table}").fetchone()[0] == 0
assert conn.execute("SELECT count(*) FROM principals").fetchone()[0] == 1
signed_in = store.sign_in_with_hf(conn, "cmpatino", time.time())
assert store.authenticate(conn, signed_in["token"], time.time()).is_admin
finally:
conn.close()
def test_restart_preserves_channels_and_owner_preferences(tmp_path, monkeypatch):
monkeypatch.setenv("MSGBOARD_OWNER_HF_USERNAME", "cmpatino")
_, token, _ = auth.new_token()
space_app.ensure_board(tmp_path, token)
conn = db.connect(tmp_path / "board.db")
try:
p = store.authenticate(conn, token, time.time())
store.create_channel(conn, p, "later", None, False, time.time())
store.set_hf_username(conn, p.id, None, time.time())
finally:
conn.close()
space_app.ensure_board(tmp_path, token)
conn = db.connect(tmp_path / "board.db")
try:
p = store.authenticate(conn, token, time.time())
assert [c["name"] for c in store.list_channels(conn, p)] == ["general", "later"]
assert conn.execute("SELECT hf_username FROM principals WHERE id=?", (p.id,)).fetchone()[0] is None
assert conn.execute("SELECT count(*) FROM principals").fetchone()[0] == 1
finally:
conn.close()
def test_restart_does_not_recreate_deleted_general(tmp_path):
_, token, _ = auth.new_token()
space_app.ensure_board(tmp_path, token)
conn = db.connect(tmp_path / "board.db")
try:
p = store.authenticate(conn, token, time.time())
c = store.list_channels(conn, p)[0]
store.delete_channel(conn, p, c["id"], "general", time.time())
finally:
conn.close()
space_app.ensure_board(tmp_path, token)
conn = db.connect(tmp_path / "board.db")
try:
p = store.authenticate(conn, token, time.time())
assert store.list_channels(conn, p) == []
finally:
conn.close()