Files
sales-trainer/backend/scripts/test_m1.py
Macky e8a95bf561 Add username-based login + mandatory first-time admin setup; push-ready
- User id/login = username (was email). Email is a separate settable field.
- Default admin: username admin / password 1234, must_setup=True.
- Login forces /setup on first login: set email + change password, then clears must_setup.
- New /api/auth/setup endpoint; JWT sub = username; admin routes use username.
- Frontend: Login uses username, router guard forces /setup, new Setup.vue (email + new
  password + confirm), i18n EN/TH.
- Tests: test_setup.py added; all suites adapted (m0/m1/routes/security/setup/e2e) PASS.
2026-08-07 17:56:59 +07:00

80 lines
2.8 KiB
Python

"""Verify the whole backend imports without errors (no LLM calls)."""
import os
import sys
import tempfile
import warnings
from pathlib import Path
warnings.filterwarnings("ignore", message="The HMAC key is")
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
tempdir = tempfile.mkdtemp(prefix="st_import_")
os.environ["DATA_DIR"] = tempdir
os.environ["JWT_SECRET"] = "test-secret-key-0123456789abcdef"
from app.factory import create_app # noqa: E402
from app.config import Config # noqa: E402
Config.DATA_DIR = Path(tempdir)
# Force no LLM for import/structural test (analyze path tested separately)
Config.LLM_API_KEY = ""
Config.LLM_BASE_URL = ""
def main():
app = create_app()
# LLM should be None (no API key in test env)
assert app.extensions["llm"] is None, "expect no LLM in test env"
client = app.test_client()
# login as super-admin
r = client.post("/api/auth/login", json={
"username": "admin", "password": "1234"})
assert r.status_code == 200, r.get_json()
token = r.get_json()["token"]
H = {"Authorization": f"Bearer {token}"}
# create a group via JSON form (no files)
r = client.post("/api/groups", json={
"product": "Cloud POS system for small restaurants",
"segment": "SME restaurants",
"description": "Target Bangkok SME restaurants, 1-3 branches",
"channel": "line",
"language": "th",
}, headers=H)
assert r.status_code == 201, r.get_json()
gid = r.get_json()["group"]["id"]
assert r.get_json()["group"]["status"] == "draft"
print("[ok] group created (draft)")
# analyze should fail cleanly (LLM None)
r = client.post(f"/api/groups/{gid}/analyze", headers=H)
assert r.status_code == 500, r.get_json()
print("[ok] analyze fails cleanly when LLM unset (500)")
# list groups as admin
r = client.get("/api/groups", headers=H)
assert r.status_code == 200 and len(r.get_json()["groups"]) == 1
print("[ok] admin lists 1 group")
# personas empty until analyze
r = client.get(f"/api/groups/{gid}", headers=H)
assert r.get_json()["group"]["personas"] == []
print("[ok] group has no personas before analyze")
# trainee created; can list groups but only 'ready' ones (this one is 'failed' -> hidden)
client.post("/api/admin/users", json={
"name": "Trainee", "username": "trainee1", "password": "pass123", "role": "user"}, headers=H)
r = client.post("/api/auth/login", json={"username": "trainee1", "password": "pass123"})
ut = r.get_json()["token"]
UH = {"Authorization": f"Bearer {ut}"}
r = client.get("/api/groups", headers=UH)
assert r.get_json()["groups"] == [], "trainee should not see non-ready groups"
print("[ok] trainee cannot see non-ready groups")
print("\nALL M1/M2-IMPORT TESTS PASSED")
if __name__ == "__main__":
main()