94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
import os
|
|
import tempfile
|
|
|
|
|
|
# Ensure repo root is on sys.path when running as a script.
|
|
import sys
|
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
|
|
from app import create_app
|
|
from app.extensions import db
|
|
from app.models import Company, Playlist, PlaylistItem, User
|
|
|
|
|
|
def main():
|
|
# Use a temporary SQLite DB so this doesn't touch your real instance DB.
|
|
fd, path = tempfile.mkstemp(prefix="rssfeed-test-", suffix=".sqlite")
|
|
os.close(fd)
|
|
try:
|
|
os.environ["DATABASE_URL"] = f"sqlite:///{path}"
|
|
os.environ["SECRET_KEY"] = "test-secret"
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
db.create_all()
|
|
|
|
c = Company(name="TestCo")
|
|
db.session.add(c)
|
|
db.session.commit()
|
|
|
|
u = User(username="test@example.com", email="test@example.com", is_admin=False, company_id=c.id)
|
|
u.set_password("passw0rd123")
|
|
db.session.add(u)
|
|
db.session.commit()
|
|
|
|
p = Playlist(company_id=c.id, name="Playlist")
|
|
db.session.add(p)
|
|
db.session.commit()
|
|
|
|
i1 = PlaylistItem(playlist_id=p.id, item_type="webpage", url="https://example.com/1", position=1)
|
|
i2 = PlaylistItem(playlist_id=p.id, item_type="webpage", url="https://example.com/2", position=2)
|
|
i3 = PlaylistItem(playlist_id=p.id, item_type="webpage", url="https://example.com/3", position=3)
|
|
db.session.add_all([i1, i2, i3])
|
|
db.session.commit()
|
|
|
|
client = app.test_client()
|
|
|
|
# Login
|
|
res = client.post(
|
|
"/auth/login",
|
|
data={"email": "test@example.com", "password": "passw0rd123"},
|
|
follow_redirects=False,
|
|
)
|
|
if res.status_code not in (302, 303):
|
|
raise SystemExit(f"Login failed: {res.status_code}")
|
|
|
|
# Reorder: 3,2,1
|
|
reorder_url = f"/company/playlists/{p.id}/items/reorder"
|
|
res = client.post(
|
|
reorder_url,
|
|
data={"order": f"{i3.id},{i2.id},{i1.id}"},
|
|
headers={"Accept": "application/json", "X-Requested-With": "XMLHttpRequest"},
|
|
)
|
|
if res.status_code != 200:
|
|
raise SystemExit(f"Reorder failed: {res.status_code} {res.data!r}")
|
|
data = res.get_json(silent=True) or {}
|
|
if not data.get("ok"):
|
|
raise SystemExit(f"Unexpected reorder response: {data}")
|
|
|
|
db.session.expire_all()
|
|
ordered = (
|
|
PlaylistItem.query.filter_by(playlist_id=p.id)
|
|
.order_by(PlaylistItem.position.asc())
|
|
.all()
|
|
)
|
|
got = [x.id for x in ordered]
|
|
expected = [i3.id, i2.id, i1.id]
|
|
if got != expected:
|
|
raise SystemExit(f"Order not persisted. expected={expected} got={got}")
|
|
|
|
print("OK: reorder endpoint persists positions")
|
|
finally:
|
|
try:
|
|
os.remove(path)
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|