Spaces:
Running
Running
File size: 2,913 Bytes
742e389 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """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()
|