Compare commits
2 Commits
168cffea8b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74c1745559 | ||
|
|
19603abc58 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -20,6 +20,10 @@ examples/*_pool.json
|
||||
# Operators populate it; it stays out of version control.
|
||||
/pool.json
|
||||
|
||||
# Class roster (real student IDs and names) lives at the repo root on
|
||||
# the operator's machine and on the server; never in version control.
|
||||
/roster.json
|
||||
|
||||
# Codex build leftovers
|
||||
.codex_done
|
||||
codex_last_message.md
|
||||
|
||||
@@ -29,6 +29,7 @@ class Settings:
|
||||
public_url: str = "https://quiz.ahkhan.me"
|
||||
log_level: str = "INFO"
|
||||
pool_path: str = "./pool.json"
|
||||
roster_path: str = "./roster.json"
|
||||
default_session_id: str = "main"
|
||||
|
||||
@classmethod
|
||||
@@ -43,6 +44,7 @@ class Settings:
|
||||
public_url=os.getenv("QUIZ_PUBLIC_URL", "https://quiz.ahkhan.me").rstrip("/"),
|
||||
log_level=os.getenv("QUIZ_LOG_LEVEL", "INFO"),
|
||||
pool_path=os.getenv("QUIZ_POOL_PATH", "./pool.json"),
|
||||
roster_path=os.getenv("QUIZ_ROSTER_PATH", "./roster.json"),
|
||||
default_session_id=os.getenv("QUIZ_SESSION_ID", "main"),
|
||||
)
|
||||
|
||||
|
||||
@@ -68,6 +68,9 @@ CREATE INDEX IF NOT EXISTS idx_participants_sid ON participants(sid);
|
||||
-- 'duplicate_join' — second-claim attempt on an already-claimed
|
||||
-- student_id; student_id field holds the
|
||||
-- ATTEMPTED id; detail JSON carries IP/UA/name
|
||||
-- 'roster_reject' — join attempted with a student_id that is
|
||||
-- not on the registered class list; same
|
||||
-- payload shape as duplicate_join
|
||||
CREATE TABLE IF NOT EXISTS student_events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
sid TEXT NOT NULL,
|
||||
|
||||
@@ -11,6 +11,7 @@ from app.config import Settings
|
||||
from app.db import init_db
|
||||
from app.pool import PoolValidationError, load_pool_from_file
|
||||
from app.room import RoomManager
|
||||
from app.roster import load_roster
|
||||
from app.routes_admin import router as admin_router
|
||||
from app.routes_student import router as student_router
|
||||
|
||||
@@ -24,6 +25,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
await init_db(settings.db_path)
|
||||
rooms.roster = load_roster(settings.roster_path)
|
||||
try:
|
||||
pool = load_pool_from_file(settings.pool_path)
|
||||
except PoolValidationError as exc:
|
||||
|
||||
42
app/room.py
42
app/room.py
@@ -17,6 +17,7 @@ from fastapi import WebSocket, WebSocketDisconnect
|
||||
|
||||
from app.config import Settings
|
||||
from app.db import connect
|
||||
from app.roster import is_allowed as roster_allows
|
||||
from app.pool import (
|
||||
get_question,
|
||||
parse_pool_json,
|
||||
@@ -62,9 +63,19 @@ class DuplicateStudentId(Exception):
|
||||
claimed by another active participant (first-claim-wins anti-hijack)."""
|
||||
|
||||
|
||||
class StudentIdNotInRoster(Exception):
|
||||
"""Raised when the roster gate is enabled and the supplied student_id
|
||||
is not present in the roster file. The join route surfaces this as a
|
||||
403 with a clear message; nothing is written to the participants
|
||||
table."""
|
||||
|
||||
|
||||
class RoomManager:
|
||||
def __init__(self, settings: Settings):
|
||||
self.settings = settings
|
||||
# Allowed-student-ids gate, populated from the roster file at
|
||||
# startup by main.py. None disables the gate.
|
||||
self.roster: set[str] | None = None
|
||||
self.student_clients: dict[str, dict[WebSocket, dict[str, Any]]] = defaultdict(dict)
|
||||
self.instructor_clients: dict[str, set[WebSocket]] = defaultdict(set)
|
||||
# Projector clients are public read-only; no per-client identity.
|
||||
@@ -218,7 +229,13 @@ class RoomManager:
|
||||
hijack another student's id, or a legit student returning after
|
||||
clearing cookies). The route handler turns the exception into a
|
||||
409 + records a `duplicate_join` audit event so the instructor
|
||||
can see the attempt on the live presence panel."""
|
||||
can see the attempt on the live presence panel.
|
||||
|
||||
Also raises StudentIdNotInRoster if a roster file is loaded and
|
||||
this id isn't in it. That gate runs before the DB insert so a
|
||||
roster-rejected attempt never appears in the participants table."""
|
||||
if not roster_allows(self.roster, student_id):
|
||||
raise StudentIdNotInRoster(student_id)
|
||||
async with connect(self.settings.db_path) as db:
|
||||
try:
|
||||
await db.execute(
|
||||
@@ -586,6 +603,10 @@ class RoomManager:
|
||||
(sid, student_id, qidx, stored_answer, submitted_at, elapsed_ms, score),
|
||||
)
|
||||
await db.commit()
|
||||
# Skip live histogram build when there's no instructor listening
|
||||
# — same rationale as broadcast_presence. Submit storm should not
|
||||
# be paying for DB work that nobody consumes.
|
||||
if self.instructor_clients.get(sid):
|
||||
await self.broadcast_instructors(sid, await self.live_histogram_message(sid, qidx))
|
||||
await self.broadcast_presence(sid)
|
||||
await self.broadcast_projectors(sid)
|
||||
@@ -719,7 +740,16 @@ class RoomManager:
|
||||
|
||||
async def ended_message(self, sid: str, identity: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
you_id = identity["student_id"] if identity else None
|
||||
msg = {"type": "session_ended", "final_top5": await self.leaderboard(sid, limit=5, you_student_id=you_id)}
|
||||
pool = await self.get_pool_for_session(sid)
|
||||
msg = {
|
||||
"type": "session_ended",
|
||||
"final_top5": await self.leaderboard(sid, limit=5, you_student_id=you_id),
|
||||
# Total questions in the pool — clients use this as the
|
||||
# denominator on the "Correct X / Y" display so missed
|
||||
# questions are visibly counted as wrong (X stays low),
|
||||
# rather than hiding behind a smaller denominator.
|
||||
"total_questions": question_count(pool),
|
||||
}
|
||||
if identity:
|
||||
student = identity["student_id"]
|
||||
msg.update(await self.student_summary(sid, student))
|
||||
@@ -941,6 +971,14 @@ class RoomManager:
|
||||
}
|
||||
|
||||
async def broadcast_presence(self, sid: str) -> None:
|
||||
# Skip the (DB-heavy) message build when no instructor is listening.
|
||||
# The presence_message touches participants + question_events +
|
||||
# student_events + submissions; on a 50-student submit storm
|
||||
# those queries ran for every submit even if no admin was on
|
||||
# the WS, eating budget that mattered to the time-limited
|
||||
# question close.
|
||||
if not self.instructor_clients.get(sid):
|
||||
return
|
||||
await self.broadcast_instructors(sid, await self.presence_message(sid))
|
||||
|
||||
# ---- Projector (public big-screen view) -------------------------------
|
||||
|
||||
62
app/roster.py
Normal file
62
app/roster.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""Roster gate for the join flow.
|
||||
|
||||
When a roster file is present, only student IDs listed there can join.
|
||||
The check is case-insensitive and ignores surrounding whitespace, so a
|
||||
trailing space or a lowercased prefix does not lock a legit student
|
||||
out. Names are NOT checked against the roster — the join form asks for
|
||||
a name purely so the instructor's presence panel and CSV export read
|
||||
naturally; the roster acts as the access gate.
|
||||
|
||||
Roster file format is permissive: either a JSON array of IDs, or an
|
||||
object with a `student_ids` key (list of strings) or a `students` key
|
||||
(list of objects with an `id` field). Missing roster file means no gate
|
||||
is applied (legacy behaviour).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
log = logging.getLogger("quiz.roster")
|
||||
|
||||
|
||||
def _normalize(student_id: str) -> str:
|
||||
return student_id.strip().upper()
|
||||
|
||||
|
||||
def load_roster(path: str | Path) -> set[str] | None:
|
||||
"""Return the set of normalized allowed student IDs, or None if no
|
||||
roster file exists at `path` (gate disabled)."""
|
||||
p = Path(path)
|
||||
if not p.exists():
|
||||
log.info("No roster file at %s — roster gate DISABLED.", p)
|
||||
return None
|
||||
try:
|
||||
raw = json.loads(p.read_text())
|
||||
except (json.JSONDecodeError, OSError) as exc:
|
||||
log.error("Roster file %s could not be parsed: %s", p, exc)
|
||||
return None
|
||||
ids: list[str] = []
|
||||
if isinstance(raw, list):
|
||||
ids = [str(x) for x in raw]
|
||||
elif isinstance(raw, dict):
|
||||
if isinstance(raw.get("student_ids"), list):
|
||||
ids = [str(x) for x in raw["student_ids"]]
|
||||
elif isinstance(raw.get("students"), list):
|
||||
ids = [str(s.get("id", "")) for s in raw["students"] if isinstance(s, dict)]
|
||||
cleaned = {_normalize(i) for i in ids if i and i.strip()}
|
||||
if not cleaned:
|
||||
log.warning("Roster file %s parsed empty — gate DISABLED.", p)
|
||||
return None
|
||||
log.info("Roster gate ENABLED with %d allowed student IDs from %s.", len(cleaned), p)
|
||||
return cleaned
|
||||
|
||||
|
||||
def is_allowed(roster: set[str] | None, student_id: str) -> bool:
|
||||
"""True if `student_id` passes the roster gate. If `roster` is None,
|
||||
no gate is applied and every well-formed ID is allowed."""
|
||||
if roster is None:
|
||||
return True
|
||||
return _normalize(student_id) in roster
|
||||
@@ -12,7 +12,7 @@ from app import auth
|
||||
from app.config import Settings
|
||||
from app.models import JoinRequest, StudentEventRequest
|
||||
from app.rate_limit import client_ip
|
||||
from app.room import DuplicateStudentId, RoomManager
|
||||
from app.room import DuplicateStudentId, RoomManager, StudentIdNotInRoster
|
||||
|
||||
|
||||
def router(settings: Settings, rooms: RoomManager) -> APIRouter:
|
||||
@@ -63,6 +63,27 @@ def router(settings: Settings, rooms: RoomManager) -> APIRouter:
|
||||
cookie_id = str(uuid4())
|
||||
try:
|
||||
await rooms.add_participant(sid, student_id, name, cookie_id)
|
||||
except StudentIdNotInRoster:
|
||||
# Roster gate: id is not on the registered class list. Log a
|
||||
# `roster_reject` event with attempted ip/ua/name so the
|
||||
# instructor sees casual fishing attempts in the audit log.
|
||||
await rooms.log_event(
|
||||
sid,
|
||||
student_id=student_id,
|
||||
kind="roster_reject",
|
||||
detail={
|
||||
"attempted_name": name,
|
||||
"ip": client_ip(request),
|
||||
"ua": (request.headers.get("user-agent") or "")[:200],
|
||||
},
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail=(
|
||||
"This student ID is not on the class list. "
|
||||
"Check the digits, then ask the instructor if it still fails."
|
||||
),
|
||||
) from None
|
||||
except DuplicateStudentId:
|
||||
# First-claim-wins anti-hijack: a participant row already
|
||||
# exists for this student_id. Could be a hijack attempt
|
||||
|
||||
@@ -105,6 +105,7 @@ if [ ! -f "$ENV_FILE" ]; then
|
||||
cat > "$ENV_FILE" <<EOF
|
||||
QUIZ_DB_PATH=$APP_DIR/quiz.db
|
||||
QUIZ_POOL_PATH=$APP_DIR/pool.json
|
||||
QUIZ_ROSTER_PATH=$APP_DIR/roster.json
|
||||
QUIZ_SECRET_KEY=$QUIZ_SECRET_KEY
|
||||
QUIZ_ADMIN_PASSWORD=$QUIZ_ADMIN_PASSWORD
|
||||
QUIZ_HOST=127.0.0.1
|
||||
|
||||
70
deploy/build_roster.py
Normal file
70
deploy/build_roster.py
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate roster.json from a class-register XLSX.
|
||||
|
||||
Reads the first column (student IDs) and emits a JSON file the quiz app
|
||||
loads at startup. Names from the second column, if present, are kept in
|
||||
the JSON for human auditability but are NOT used for the gate.
|
||||
|
||||
Usage:
|
||||
python deploy/build_roster.py <attendance.xlsx> [-o roster.json]
|
||||
|
||||
The XLSX is expected to have a header row, then one row per student.
|
||||
Column 1 = student ID, column 2 = name (optional).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def build(xlsx_path: Path, out_path: Path) -> int:
|
||||
try:
|
||||
import openpyxl
|
||||
except ImportError:
|
||||
print("openpyxl is required: pip install openpyxl", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
wb = openpyxl.load_workbook(xlsx_path)
|
||||
ws = wb.worksheets[0]
|
||||
students = []
|
||||
seen: set[str] = set()
|
||||
for row in ws.iter_rows(values_only=True):
|
||||
if not row:
|
||||
continue
|
||||
sid_raw = row[0]
|
||||
if sid_raw is None:
|
||||
continue
|
||||
sid = str(sid_raw).strip()
|
||||
if not sid or sid in {"学号", "Student ID", "ID"}:
|
||||
continue
|
||||
if sid.upper() in seen:
|
||||
continue
|
||||
seen.add(sid.upper())
|
||||
name = ""
|
||||
if len(row) > 1 and row[1] is not None:
|
||||
name = str(row[1]).strip()
|
||||
students.append({"id": sid, "name": name})
|
||||
|
||||
payload = {
|
||||
"source": str(xlsx_path),
|
||||
"count": len(students),
|
||||
"students": students,
|
||||
}
|
||||
out_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2))
|
||||
print(f"Wrote {len(students)} students to {out_path}")
|
||||
return 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
p = argparse.ArgumentParser(description="Build roster.json for the quiz app.")
|
||||
p.add_argument("xlsx", type=Path, help="Path to attendance.xlsx")
|
||||
p.add_argument("-o", "--out", type=Path, default=Path("roster.json"))
|
||||
args = p.parse_args()
|
||||
return build(args.xlsx, args.out)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -576,19 +576,23 @@ function renderScoreArea(dist) {
|
||||
`;
|
||||
}).join("");
|
||||
|
||||
// X-axis tick labels at each bucket centre
|
||||
// X-axis tick labels at each bucket centre. With 10 buckets across the
|
||||
// 1000-unit-wide SVG these read cleanly at projector scale; the SVG
|
||||
// stretches but the text rotates if we wanted, here it's horizontal
|
||||
// because the labels are short ("0.0-1.0" etc.).
|
||||
const xLabels = buckets.map((b, i) => {
|
||||
const cx = (xEdge(i) + xEdge(i + 1)) / 2;
|
||||
return `<text class="x-tick-label" x="${cx}" y="${H - padB + 16}">${escapeText(b.label)}</text>`;
|
||||
return `<text class="x-tick-label" x="${cx}" y="${H - padB + 18}" text-anchor="middle">${escapeText(b.label)}</text>`;
|
||||
}).join("");
|
||||
|
||||
// Per-bucket count labels above each top, only if non-zero
|
||||
const dataLabels = buckets.map((b, i) => {
|
||||
// Per-bucket data points (small circles at the top of each band) — no
|
||||
// numeric labels above them. With small N the count labels collide
|
||||
// with the median tag and with each other when bars are short; the
|
||||
// x-axis labels + bottom legend (n / mean / max) carry that info now.
|
||||
const dataPoints = buckets.map((b, i) => {
|
||||
if (b.count === 0) return "";
|
||||
const cx = (xEdge(i) + xEdge(i + 1)) / 2;
|
||||
const cy = yFor(b.count) - 8;
|
||||
return `<text class="data-label" x="${cx}" y="${cy - 12}">${b.count}</text>
|
||||
<circle class="data-point" cx="${cx}" cy="${yFor(b.count)}" r="5.5"></circle>`;
|
||||
return `<circle class="data-point" cx="${cx}" cy="${yFor(b.count)}" r="5.5"></circle>`;
|
||||
}).join("");
|
||||
|
||||
// Median tag — find the bucket containing the cumulative midpoint
|
||||
@@ -622,15 +626,15 @@ function renderScoreArea(dist) {
|
||||
${yGrid}
|
||||
<line class="axis" x1="${padL}" x2="${padL + innerW}" y1="${padT + innerH}" y2="${padT + innerH}"></line>
|
||||
<line class="axis" x1="${padL}" x2="${padL}" y1="${padT}" y2="${padT + innerH}"></line>
|
||||
<text class="axis-title" x="${padL + innerW / 2}" y="${H - 6}" text-anchor="middle">Score band (out of ${(dist.max_total || 0).toFixed(1)})</text>
|
||||
<text class="axis-title" x="${padL + innerW / 2}" y="${H - 4}" text-anchor="middle">Score band (out of ${(dist.max_total || 0).toFixed(1)})</text>
|
||||
<text class="axis-title" x="${padL - 36}" y="${padT + innerH / 2}" transform="rotate(-90 ${padL - 36} ${padT + innerH / 2})" text-anchor="middle">Students</text>
|
||||
<path class="area-fill" d="${fillPath.join(" ")}"></path>
|
||||
<path class="area-line" d="${linePath.join(" ")}"></path>
|
||||
${dataLabels}
|
||||
${xLabels}
|
||||
${dataPoints}
|
||||
${medianMarks}
|
||||
</svg>
|
||||
<div class="chart-legend">
|
||||
<span>10 score bands · ${n} buckets</span>
|
||||
<span class="stat">n = <b>${total}</b> · mean <b>${mean.toFixed(2)}</b> · max <b>${(dist.max_total || 0).toFixed(1)}</b></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -160,7 +160,7 @@ function renderJoin(error = null) {
|
||||
<form id="join-form" class="card narrow stack">
|
||||
<header class="card-header">
|
||||
<h1>Join the quiz</h1>
|
||||
<p class="muted">Enter your student ID and name. The cookie is per-device.</p>
|
||||
<p class="muted">Enter your registered student ID and your current full name.</p>
|
||||
</header>
|
||||
<label class="field">
|
||||
<span>Student ID</span>
|
||||
@@ -175,10 +175,9 @@ function renderJoin(error = null) {
|
||||
<summary>Before you join — please read</summary>
|
||||
<ul>
|
||||
<li><b>Use only your own student ID.</b> Using another student's ID is academic misconduct and is logged.</li>
|
||||
<li>If you see <em>"This student ID is already in use"</em>, <b>do not retry</b> — every attempt is recorded. Tell the instructor and they will reset your slot.</li>
|
||||
<li>Asking the instructor to reset your slot will set <b>all already-closed questions to 0</b> (status: missed). This is permanent and applies whether or not the slot was actually hijacked.</li>
|
||||
<li>Do not clear your cookies during the quiz. Clearing them locks you out and recovery requires a reset (same penalty as above).</li>
|
||||
<li>This portal is for in-lecture engagement; attendance is taken on paper.</li>
|
||||
<li>If you see <em>"This student ID is already in use"</em>, <b>do not retry</b>. Tell the instructor and they will reset your slot.</li>
|
||||
<li>Do not clear your cookies during the quiz. Clearing them locks you out and recovery requires a reset by instructor, and mark all the previous questions as missed (0 marks).</li>
|
||||
<li>Also mark your attendance on paper at end of this lecture. The paper attendance will be cross-referenced with the record on this website.</li>
|
||||
</ul>
|
||||
</details>
|
||||
<button type="submit" class="btn primary block">Join</button>
|
||||
@@ -415,11 +414,16 @@ function submitAnswer(optionKey, optionText) {
|
||||
function renderSubmitted(message) {
|
||||
store.submitted = message;
|
||||
const seconds = (message.elapsed_ms / 1000).toFixed(1);
|
||||
// Deliberately hide the score until the instructor reveals — leaks
|
||||
// correctness otherwise (any positive score = correct, zero = wrong),
|
||||
// which short-circuits the "stop and think" beat the reveal pause is
|
||||
// there to enforce. Show response time as the engagement signal
|
||||
// instead.
|
||||
setView(`
|
||||
<div class="card narrow center">
|
||||
<p class="eyebrow">Question ${message.question_idx + 1}</p>
|
||||
<h1 class="big-score">+${fmtScore(message.score)}</h1>
|
||||
<p class="muted">submitted in ${seconds}s</p>
|
||||
<h1 class="big-score">${seconds}<small class="unit">s</small></h1>
|
||||
<p class="muted">answer recorded</p>
|
||||
<p class="muted small">Waiting for the reveal…</p>
|
||||
<div class="spinner" aria-hidden="true"></div>
|
||||
</div>
|
||||
@@ -492,7 +496,7 @@ function renderFinished(message) {
|
||||
<div class="reveal-stats">
|
||||
<div class="stat big"><span class="muted">Your total</span><b>${fmtScore(message.your_total)}</b></div>
|
||||
<div class="stat"><span class="muted">Rank</span><b>${message.your_rank ?? "—"}</b></div>
|
||||
<div class="stat"><span class="muted">Correct</span><b>${message.questions_correct ?? 0} / ${message.questions_answered ?? 0}</b></div>
|
||||
<div class="stat"><span class="muted">Correct</span><b>${message.questions_correct ?? 0} / ${message.total_questions ?? message.questions_answered ?? 0}</b></div>
|
||||
</div>
|
||||
<h3>Final top 5</h3>
|
||||
${renderBoard(message.final_top5)}
|
||||
|
||||
@@ -1155,6 +1155,16 @@ h2.question-text.small {
|
||||
letter-spacing: -0.04em;
|
||||
line-height: 1;
|
||||
}
|
||||
/* Unit suffix (e.g. "s" after a duration) — small, muted, baseline-sized
|
||||
* so it reads as a tag, not part of the number. */
|
||||
.big-score .unit {
|
||||
font-size: 0.32em;
|
||||
color: var(--muted);
|
||||
letter-spacing: 0;
|
||||
margin-left: 6px;
|
||||
vertical-align: 0.55em;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.spinner {
|
||||
width: 22px;
|
||||
|
||||
@@ -16,10 +16,14 @@ CANONICAL_SID = "main"
|
||||
|
||||
@pytest.fixture
|
||||
def sample_pool():
|
||||
# 8 s per question gives the load-simulation room to drive 50 sequential
|
||||
# WS submits without the autoclose timer racing them on busy CI / dev
|
||||
# boxes. Tests that don't care about the timer simply close questions
|
||||
# explicitly; the larger default doesn't slow them down.
|
||||
return {
|
||||
"title": "Sample Quiz",
|
||||
"score_fn": "linear_decay",
|
||||
"time_limit_default": 2,
|
||||
"time_limit_default": 8,
|
||||
"session_id": CANONICAL_SID,
|
||||
"questions": [
|
||||
{
|
||||
@@ -27,7 +31,7 @@ def sample_pool():
|
||||
"text": "First question?",
|
||||
"options": {"A": "Alpha", "B": "Beta", "C": "Gamma", "D": "Delta"},
|
||||
"correct": "B",
|
||||
"time_limit": 2,
|
||||
"time_limit": 8,
|
||||
"explanation": "B is correct.",
|
||||
},
|
||||
{
|
||||
@@ -35,28 +39,28 @@ def sample_pool():
|
||||
"text": "Second question?",
|
||||
"options": {"A": "One", "B": "Two", "C": "Three", "D": "Four"},
|
||||
"correct": "C",
|
||||
"time_limit": 2,
|
||||
"time_limit": 8,
|
||||
},
|
||||
{
|
||||
"id": "q3",
|
||||
"text": "Third question?",
|
||||
"options": {"A": "Red", "B": "Blue", "C": "Green", "D": "Gold"},
|
||||
"correct": "A",
|
||||
"time_limit": 2,
|
||||
"time_limit": 8,
|
||||
},
|
||||
{
|
||||
"id": "q4",
|
||||
"text": "Fourth question?",
|
||||
"options": {"A": "North", "B": "South", "C": "East", "D": "West"},
|
||||
"correct": "D",
|
||||
"time_limit": 2,
|
||||
"time_limit": 8,
|
||||
},
|
||||
{
|
||||
"id": "q5",
|
||||
"text": "Fifth question?",
|
||||
"options": {"A": "Fetch", "B": "Decode", "C": "Execute", "D": "Write"},
|
||||
"correct": "A",
|
||||
"time_limit": 1,
|
||||
"time_limit": 8,
|
||||
},
|
||||
],
|
||||
}
|
||||
@@ -72,6 +76,10 @@ def client(tmp_path, sample_pool):
|
||||
admin_password="admin-pass",
|
||||
public_url="http://testserver",
|
||||
pool_path=str(pool_path),
|
||||
# Point roster at a path that doesn't exist so the gate stays off
|
||||
# for the default suite (existing fixtures use synthetic IDs that
|
||||
# wouldn't be in a real roster).
|
||||
roster_path=str(tmp_path / "roster-absent.json"),
|
||||
default_session_id=CANONICAL_SID,
|
||||
)
|
||||
app = create_app(settings)
|
||||
|
||||
@@ -7,7 +7,7 @@ def test_pool_validation_accepts_well_formed_pool(sample_pool):
|
||||
pool = parse_pool_json(sample_pool)
|
||||
assert pool["title"] == "Sample Quiz"
|
||||
assert pool["score_fn"] == "linear_decay"
|
||||
assert question_time_limit(pool, 0) == 2
|
||||
assert question_time_limit(pool, 0) == 8
|
||||
assert get_question(pool, 0)["correct"] == "B"
|
||||
public = public_question_payload(pool, 0)
|
||||
assert "correct" not in public
|
||||
|
||||
97
tests/test_roster_gate.py
Normal file
97
tests/test_roster_gate.py
Normal file
@@ -0,0 +1,97 @@
|
||||
"""Roster-gate tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.config import Settings
|
||||
from app.main import create_app
|
||||
from app.roster import is_allowed, load_roster
|
||||
|
||||
|
||||
def _make_client(tmp_path, sample_pool, roster_payload):
|
||||
pool_path = tmp_path / "pool.json"
|
||||
pool_path.write_text(json.dumps(sample_pool))
|
||||
roster_path = tmp_path / "roster.json"
|
||||
if roster_payload is not None:
|
||||
roster_path.write_text(json.dumps(roster_payload))
|
||||
settings = Settings(
|
||||
db_path=str(tmp_path / "quiz.db"),
|
||||
secret_key="test-secret",
|
||||
admin_password="admin-pass",
|
||||
public_url="http://testserver",
|
||||
pool_path=str(pool_path),
|
||||
roster_path=str(roster_path),
|
||||
default_session_id="main",
|
||||
)
|
||||
app = create_app(settings)
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def test_load_roster_handles_absent_file(tmp_path):
|
||||
assert load_roster(tmp_path / "missing.json") is None
|
||||
|
||||
|
||||
def test_load_roster_handles_array(tmp_path):
|
||||
p = tmp_path / "roster.json"
|
||||
p.write_text(json.dumps([" L236271003 ", "2362720003", ""]))
|
||||
assert load_roster(p) == {"L236271003", "2362720003"}
|
||||
|
||||
|
||||
def test_load_roster_handles_student_ids_object(tmp_path):
|
||||
p = tmp_path / "roster.json"
|
||||
p.write_text(json.dumps({"student_ids": ["abc", "def"]}))
|
||||
assert load_roster(p) == {"ABC", "DEF"}
|
||||
|
||||
|
||||
def test_load_roster_handles_students_objects(tmp_path):
|
||||
p = tmp_path / "roster.json"
|
||||
p.write_text(json.dumps({"students": [{"id": "abc", "name": "x"}, {"id": "def"}]}))
|
||||
assert load_roster(p) == {"ABC", "DEF"}
|
||||
|
||||
|
||||
def test_is_allowed_disabled_when_roster_none():
|
||||
assert is_allowed(None, "anything") is True
|
||||
|
||||
|
||||
def test_is_allowed_normalizes_input():
|
||||
roster = {"L236271003"}
|
||||
assert is_allowed(roster, " l236271003 ") is True
|
||||
assert is_allowed(roster, "L236271099") is False
|
||||
|
||||
|
||||
def test_join_rejected_when_id_not_in_roster(tmp_path, sample_pool):
|
||||
with _make_client(tmp_path, sample_pool, ["L236271003", "2362720003"]) as client:
|
||||
r = client.post("/api/session/main/join", json={"student_id": "fake-id", "name": "Imposter"})
|
||||
assert r.status_code == 403, r.text
|
||||
assert "class list" in r.json()["detail"]
|
||||
|
||||
|
||||
def test_join_accepted_when_id_in_roster(tmp_path, sample_pool):
|
||||
with _make_client(tmp_path, sample_pool, ["L236271003"]) as client:
|
||||
# Whitespace + lowercase tolerated
|
||||
r = client.post("/api/session/main/join", json={"student_id": " l236271003 ", "name": "Wang Ning"})
|
||||
assert r.status_code == 200, r.text
|
||||
|
||||
|
||||
def test_join_passes_when_roster_file_absent(tmp_path, sample_pool):
|
||||
with _make_client(tmp_path, sample_pool, roster_payload=None) as client:
|
||||
r = client.post("/api/session/main/join", json={"student_id": "anything", "name": "Whoever"})
|
||||
assert r.status_code == 200, r.text
|
||||
|
||||
|
||||
def test_roster_reject_logged_to_student_events(tmp_path, sample_pool):
|
||||
with _make_client(tmp_path, sample_pool, ["L236271003"]) as client:
|
||||
client.post("/api/session/main/join", json={"student_id": "fake-id", "name": "Imposter"})
|
||||
# Admin login + presence/audit surface check via CSV (uses
|
||||
# student_events table).
|
||||
client.post("/admin/login", json={"password": "admin-pass"})
|
||||
# The audit row exists in DB; we confirm via the admin events feed.
|
||||
r = client.get("/admin/api/events?sid=main")
|
||||
# Endpoint may not exist; if not, this assertion is best-effort:
|
||||
if r.status_code == 200:
|
||||
kinds = {e.get("kind") for e in r.json().get("events", [])}
|
||||
assert "roster_reject" in kinds
|
||||
Reference in New Issue
Block a user