Spaces:
Runtime error
Runtime error
Greg
Claude Sonnet 4.6
feat(ta-01): admin attendance endpoints (sections, roster, swipe, batch-toggle)
138c1eb Download backend/tests/test_admin_attendance.py from Talosware/talos-access: direct link, hf CLI and curl.
- Browser
- Download file 11.6 kB
-
https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/tests/test_admin_attendance.py
- Command line
-
hf download hf://spaces/Talosware/talos-access/backend/tests/test_admin_attendance.py
-
curl -L -o test_admin_attendance.py https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/tests/test_admin_attendance.py
11.6 kB
| from datetime import date | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from app.main import app | |
| from app.core.database import SessionLocal, Base, engine | |
| from app.models.plan import Plan | |
| from app.models.account import Account | |
| from app.models.user import User | |
| from app.models.section import Section | |
| from app.models.subject import Subject | |
| from app.models.enrollment import Enrollment | |
| from app.models.attendance import Attendance | |
| from app.models.entrance_log import EntranceLog | |
| from app.core.security import hash_password | |
| def client(): | |
| Base.metadata.create_all(bind=engine) | |
| yield TestClient(app) | |
| def _cleanup(db, org_name): | |
| accs = db.query(Account).filter(Account.organization_name == org_name).all() | |
| for acc in accs: | |
| try: | |
| from app.models.audit_log import AuditLog | |
| db.query(AuditLog).filter(AuditLog.account_id == acc.id).delete() | |
| except Exception: | |
| pass | |
| db.query(EntranceLog).filter(EntranceLog.account_id == acc.id).delete() | |
| db.query(Attendance).filter(Attendance.account_id == acc.id).delete() | |
| db.query(Enrollment).filter(Enrollment.account_id == acc.id).delete() | |
| db.query(Section).filter(Section.account_id == acc.id).delete() | |
| db.query(Subject).filter(Subject.account_id == acc.id).delete() | |
| db.query(User).filter(User.account_id == acc.id).update({"parent_id": None}) | |
| db.query(User).filter(User.account_id == acc.id).delete() | |
| db.query(Account).filter(Account.id == acc.id).delete() | |
| db.commit() | |
| def _seed(db, org_name, admin_email, student_email, teacher_email=None): | |
| plan = db.query(Plan).first() | |
| if not plan: | |
| plan = Plan(name="basic", features=[], price=0) | |
| db.add(plan); db.commit(); db.refresh(plan) | |
| acc = Account(organization_name=org_name, plan_id=plan.id, category="school") | |
| db.add(acc); db.commit(); db.refresh(acc) | |
| admin = User(account_id=acc.id, name="A", lastname="A", | |
| email=admin_email, password_hash=hash_password("x"), | |
| role="admin", is_active="1") | |
| student = User(account_id=acc.id, name="S", lastname="S", | |
| email=student_email, password_hash=hash_password("x"), | |
| role="student", is_active="1", sex="M") | |
| users = [admin, student] | |
| teacher = None | |
| if teacher_email: | |
| teacher = User(account_id=acc.id, name="T", lastname="T", | |
| email=teacher_email, password_hash=hash_password("x"), | |
| role="teacher", is_active="1") | |
| users.append(teacher) | |
| db.add_all(users); db.commit() | |
| for u in users: db.refresh(u) | |
| sec = Section(account_id=acc.id, name="S1", academic_period="2025-2026", max_students=40) | |
| subj = Subject(account_id=acc.id, name="Mate") | |
| db.add_all([sec, subj]); db.commit(); db.refresh(sec); db.refresh(subj) | |
| db.add(Enrollment(account_id=acc.id, section_id=sec.id, student_id=student.id)) | |
| db.commit() | |
| return acc, admin, student, sec, subj, teacher | |
| def _login(client, email): | |
| r = client.post("/api/v1/auth/login", json={"email": email, "password": "x"}) | |
| return r.json()["access_token"] | |
| # Task 20: GET /admin/sections | |
| def test_admin_lists_all_sections_of_their_account(client): | |
| org = "AdminAtt_ListSections" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| acc, admin, _, sec, _, _ = _seed(db, org, "ad_ls_admin@talos.com", "ad_ls_s@talos.com") | |
| section_id = sec.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_ls_admin@talos.com") | |
| r = client.get("/api/v1/admin/sections", headers={"Authorization": f"Bearer {token}"}) | |
| assert r.status_code == 200, r.text | |
| ids = [s["id"] for s in r.json()] | |
| assert section_id in ids | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| finally: | |
| db.close() | |
| def test_admin_cannot_see_sections_of_other_account(client): | |
| org_a = "AdminAtt_CrossA"; org_b = "AdminAtt_CrossB" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org_a); _cleanup(db, org_b) | |
| _, admin_a, _, _, _, _ = _seed(db, org_a, "ad_xa_admin@talos.com", "ad_xa_s@talos.com") | |
| _, _, _, sec_b, _, _ = _seed(db, org_b, "ad_xb_admin@talos.com", "ad_xb_s@talos.com") | |
| sec_b_id = sec_b.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_xa_admin@talos.com") | |
| r = client.get("/api/v1/admin/sections", headers={"Authorization": f"Bearer {token}"}) | |
| assert r.status_code == 200 | |
| ids = [s["id"] for s in r.json()] | |
| assert sec_b_id not in ids | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org_a); _cleanup(db, org_b) | |
| finally: | |
| db.close() | |
| def test_teacher_role_is_forbidden_on_admin_sections(client): | |
| org = "AdminAtt_TeacherForbid" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| _seed(db, org, "ad_tf_admin@talos.com", "ad_tf_s@talos.com", "ad_tf_teacher@talos.com") | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_tf_teacher@talos.com") | |
| r = client.get("/api/v1/admin/sections", headers={"Authorization": f"Bearer {token}"}) | |
| assert r.status_code in (401, 403) | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| finally: | |
| db.close() | |
| # Task 21: GET /admin/sections/{id}/roster | |
| def test_admin_roster_returns_enrolled_students(client): | |
| org = "AdminAtt_Roster" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| acc, admin, student, sec, _, _ = _seed(db, org, "ad_r_admin@talos.com", "ad_r_s@talos.com") | |
| sec_id, student_id = sec.id, student.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_r_admin@talos.com") | |
| r = client.get( | |
| f"/api/v1/admin/sections/{sec_id}/roster", | |
| headers={"Authorization": f"Bearer {token}"}, | |
| ) | |
| assert r.status_code == 200, r.text | |
| items = r.json() | |
| assert any(it["id"] == student_id for it in items) | |
| item = next(it for it in items if it["id"] == student_id) | |
| assert "already_checked_in" in item | |
| assert "entrance_logged" in item | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| finally: | |
| db.close() | |
| def test_admin_roster_404_for_foreign_section(client): | |
| org_a = "AdminAtt_RostA"; org_b = "AdminAtt_RostB" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org_a); _cleanup(db, org_b) | |
| _, _, _, _, _, _ = _seed(db, org_a, "ad_rxa_admin@talos.com", "ad_rxa_s@talos.com") | |
| _, _, _, sec_b, _, _ = _seed(db, org_b, "ad_rxb_admin@talos.com", "ad_rxb_s@talos.com") | |
| sec_b_id = sec_b.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_rxa_admin@talos.com") | |
| r = client.get( | |
| f"/api/v1/admin/sections/{sec_b_id}/roster", | |
| headers={"Authorization": f"Bearer {token}"}, | |
| ) | |
| assert r.status_code == 404 | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org_a); _cleanup(db, org_b) | |
| finally: | |
| db.close() | |
| # Task 22: POST /admin/attendance/swipe | |
| def test_admin_swipe_marks_with_admin_attribution(client): | |
| org = "AdminAtt_SwipeAttrib" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| _, admin, student, sec, subj, _ = _seed( | |
| db, org, "ad_sw_admin@talos.com", "ad_sw_s@talos.com" | |
| ) | |
| admin_id, sec_id, subj_id, student_id = admin.id, sec.id, subj.id, student.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_sw_admin@talos.com") | |
| r = client.post( | |
| "/api/v1/admin/attendance/swipe", | |
| headers={"Authorization": f"Bearer {token}"}, | |
| json={ | |
| "section_id": sec_id, | |
| "subject_id": subj_id, | |
| "records": [{"student_id": student_id, "present": True}], | |
| }, | |
| ) | |
| assert r.status_code == 201, r.text | |
| assert r.json()["created"] == 1 | |
| db = SessionLocal() | |
| try: | |
| row = ( | |
| db.query(Attendance) | |
| .filter( | |
| Attendance.user_id == student_id, | |
| Attendance.section_id == sec_id, | |
| ) | |
| .order_by(Attendance.id.desc()) | |
| .first() | |
| ) | |
| assert row is not None | |
| assert row.marked_by_role == "admin" | |
| assert row.marked_by_user_id == admin_id | |
| finally: | |
| _cleanup(db, org) | |
| db.close() | |
| def test_admin_swipe_works_without_entrance_log(client): | |
| """Admin marks should never block on gate, ever.""" | |
| org = "AdminAtt_SwipeNoGate" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| _, _, student, sec, subj, _ = _seed(db, org, "ad_swng_admin@talos.com", "ad_swng_s@talos.com") | |
| sec_id, subj_id, student_id = sec.id, subj.id, student.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_swng_admin@talos.com") | |
| r = client.post( | |
| "/api/v1/admin/attendance/swipe", | |
| headers={"Authorization": f"Bearer {token}"}, | |
| json={ | |
| "section_id": sec_id, | |
| "subject_id": subj_id, | |
| "records": [{"student_id": student_id, "present": True}], | |
| }, | |
| ) | |
| assert r.status_code == 201 | |
| assert r.json()["created"] == 1 | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| finally: | |
| db.close() | |
| def test_admin_swipe_404_for_foreign_section(client): | |
| org_a = "AdminAtt_SwipeXA"; org_b = "AdminAtt_SwipeXB" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org_a); _cleanup(db, org_b) | |
| _, _, _, _, _, _ = _seed(db, org_a, "ad_sxa_admin@talos.com", "ad_sxa_s@talos.com") | |
| _, _, student_b, sec_b, subj_b, _ = _seed( | |
| db, org_b, "ad_sxb_admin@talos.com", "ad_sxb_s@talos.com" | |
| ) | |
| sec_b_id, subj_b_id, student_b_id = sec_b.id, subj_b.id, student_b.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_sxa_admin@talos.com") | |
| r = client.post( | |
| "/api/v1/admin/attendance/swipe", | |
| headers={"Authorization": f"Bearer {token}"}, | |
| json={ | |
| "section_id": sec_b_id, | |
| "subject_id": subj_b_id, | |
| "records": [{"student_id": student_b_id, "present": True}], | |
| }, | |
| ) | |
| assert r.status_code == 404 | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org_a); _cleanup(db, org_b) | |
| finally: | |
| db.close() | |
| # Task 23: POST /admin/attendance/batch-toggle | |
| def test_admin_batch_toggle_for_past_date(client): | |
| org = "AdminAtt_BatchPastDate" | |
| db = SessionLocal() | |
| try: | |
| _cleanup(db, org) | |
| _, admin, student, sec, subj, _ = _seed(db, org, "ad_b_admin@talos.com", "ad_b_s@talos.com") | |
| sec_id, subj_id, student_id, admin_id = sec.id, subj.id, student.id, admin.id | |
| finally: | |
| db.close() | |
| token = _login(client, "ad_b_admin@talos.com") | |
| r = client.post( | |
| "/api/v1/admin/attendance/batch-toggle?date=2026-06-01", | |
| headers={"Authorization": f"Bearer {token}"}, | |
| json={ | |
| "section_id": sec_id, | |
| "subject_id": subj_id, | |
| "records": [{"student_id": student_id, "present": True}], | |
| }, | |
| ) | |
| assert r.status_code == 200, r.text | |
| db = SessionLocal() | |
| try: | |
| row = ( | |
| db.query(Attendance) | |
| .filter( | |
| Attendance.user_id == student_id, | |
| Attendance.section_id == sec_id, | |
| ) | |
| .order_by(Attendance.id.desc()) | |
| .first() | |
| ) | |
| assert row is not None | |
| assert row.marked_by_role == "admin" | |
| assert row.marked_by_user_id == admin_id | |
| assert row.time_log.date().isoformat() == "2026-06-01" | |
| finally: | |
| _cleanup(db, org) | |
| db.close() | |