Greg Claude Opus 4.7 (1M context) commited on
Commit
fad8e8a
·
1 Parent(s): 0eebffb

fix(dashboard): 400 on malformed section_ids; harden test cleanup

Browse files

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

backend/app/api/v1/endpoints/dashboard.py CHANGED
@@ -334,6 +334,8 @@ def sex_by_section(
334
  requested = [int(x) for x in section_ids.split(",") if x.strip()]
335
  except ValueError:
336
  raise HTTPException(status_code=400, detail="section_ids must be comma-separated ints")
 
 
337
  sec_q = sec_q.filter(Section.id.in_(requested))
338
  sections = sec_q.order_by(Section.name).all()
339
  sec_ids_in_scope = [s.id for s in sections]
 
334
  requested = [int(x) for x in section_ids.split(",") if x.strip()]
335
  except ValueError:
336
  raise HTTPException(status_code=400, detail="section_ids must be comma-separated ints")
337
+ if not requested:
338
+ raise HTTPException(status_code=400, detail="section_ids must contain at least one integer")
339
  sec_q = sec_q.filter(Section.id.in_(requested))
340
  sections = sec_q.order_by(Section.name).all()
341
  sec_ids_in_scope = [s.id for s in sections]
backend/tests/test_dashboard_sex_by_section.py CHANGED
@@ -82,6 +82,7 @@ def _login(client, email):
82
 
83
  def test_sex_by_section_admin_default_range(client):
84
  org_name = "SbsTest_Default"
 
85
  db = SessionLocal()
86
  try:
87
  _cleanup_by_org_name(db, org_name)
@@ -106,13 +107,15 @@ def test_sex_by_section_admin_default_range(client):
106
  finally:
107
  db = SessionLocal()
108
  try:
109
- _cleanup(db, acc_id)
 
110
  finally:
111
  db.close()
112
 
113
 
114
  def test_sex_by_section_filter_by_section_ids(client):
115
  org_name = "SbsTest_Filter"
 
116
  db = SessionLocal()
117
  try:
118
  _cleanup_by_org_name(db, org_name)
@@ -134,13 +137,15 @@ def test_sex_by_section_filter_by_section_ids(client):
134
  finally:
135
  db = SessionLocal()
136
  try:
137
- _cleanup(db, acc_id)
 
138
  finally:
139
  db.close()
140
 
141
 
142
  def test_sex_by_section_rejects_non_authorized_roles(client):
143
  org_name = "SbsTest_Forbid"
 
144
  db = SessionLocal()
145
  try:
146
  _cleanup_by_org_name(db, org_name)
@@ -162,6 +167,40 @@ def test_sex_by_section_rejects_non_authorized_roles(client):
162
  finally:
163
  db = SessionLocal()
164
  try:
165
- _cleanup(db, acc_id)
 
166
  finally:
167
  db.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  def test_sex_by_section_admin_default_range(client):
84
  org_name = "SbsTest_Default"
85
+ acc_id = None
86
  db = SessionLocal()
87
  try:
88
  _cleanup_by_org_name(db, org_name)
 
107
  finally:
108
  db = SessionLocal()
109
  try:
110
+ if acc_id is not None:
111
+ _cleanup(db, acc_id)
112
  finally:
113
  db.close()
114
 
115
 
116
  def test_sex_by_section_filter_by_section_ids(client):
117
  org_name = "SbsTest_Filter"
118
+ acc_id = None
119
  db = SessionLocal()
120
  try:
121
  _cleanup_by_org_name(db, org_name)
 
137
  finally:
138
  db = SessionLocal()
139
  try:
140
+ if acc_id is not None:
141
+ _cleanup(db, acc_id)
142
  finally:
143
  db.close()
144
 
145
 
146
  def test_sex_by_section_rejects_non_authorized_roles(client):
147
  org_name = "SbsTest_Forbid"
148
+ acc_id = None
149
  db = SessionLocal()
150
  try:
151
  _cleanup_by_org_name(db, org_name)
 
167
  finally:
168
  db = SessionLocal()
169
  try:
170
+ if acc_id is not None:
171
+ _cleanup(db, acc_id)
172
  finally:
173
  db.close()
174
+
175
+
176
+ def test_sex_by_section_400_on_malformed_section_ids(client):
177
+ # Reuse the admin from previous tests if it still exists, or seed fresh
178
+ db = SessionLocal()
179
+ try:
180
+ acc = db.query(Account).filter(Account.organization_name.like("SbsTest%")).first()
181
+ if not acc:
182
+ from app.models.plan import Plan
183
+ plan = db.query(Plan).first()
184
+ acc = Account(organization_name="SbsTest_Malformed", plan_id=plan.id, category="school")
185
+ db.add(acc); db.commit(); db.refresh(acc)
186
+ admin = User(account_id=acc.id, name="A", lastname="A",
187
+ email=f"sbs_admin_malformed_{acc.id}@talos.com",
188
+ password_hash=hash_password("x"),
189
+ role="admin", is_active="1", sex="M")
190
+ db.add(admin); db.commit()
191
+ email = f"sbs_admin_malformed_{acc.id}@talos.com"
192
+ else:
193
+ email = db.query(User).filter(
194
+ User.account_id == acc.id, User.role == "admin",
195
+ ).first().email
196
+ finally:
197
+ db.close()
198
+
199
+ token = _login(client, email)
200
+ # comma-only and bad string both 400
201
+ r1 = client.get("/api/v1/dashboard/sex-by-section?section_ids=,",
202
+ headers={"Authorization": f"Bearer {token}"})
203
+ assert r1.status_code == 400
204
+ r2 = client.get("/api/v1/dashboard/sex-by-section?section_ids=abc",
205
+ headers={"Authorization": f"Bearer {token}"})
206
+ assert r2.status_code == 400