"""The upload start limit: a large cart fits, and the per-source bucket still refuses above its own limit (Marcus's bugs of 2026-11-11, B1). Run against the local stack: python -m tests.upload_limits_test """ import secrets from fastapi import HTTPException from app.api.uploads import UPLOAD_START_IP_LIMIT, UPLOAD_START_LIMIT from app.core.auth import rate_limit from tests.smoke_test import Client def run(): assert (UPLOAD_START_LIMIT, UPLOAD_START_IP_LIMIT) == (1000, 3000) client = Client() client.call('/session') # A 66-file cart, with retries, fits in one session. Each reservation is # cancelled so the pending-upload quota (unchanged) does not stop the test. for index in range(70): uid = client.call('/uploads', {'name': f'LIMIT-{index}.png', 'size': 1})['id'] client.call(f'/uploads/{uid}', expected=200, method='DELETE') # The per-source bucket, exercised directly with a small limit: 3000 real # starts would only slow CI. A unique key keeps other suites out of it. key = 'upload-start-ip-test-' + secrets.token_hex(8) for _ in range(3): rate_limit('upload-start-ip', key, 3, 900) try: rate_limit('upload-start-ip', key, 3, 900) except HTTPException as error: assert error.status_code == 429, error assert error.headers.get('Retry-After') == '900', error.headers else: raise AssertionError('the per-source upload bucket accepted above its limit') print('PASS: 70 upload starts in one session; the per-source bucket refuses above its limit') if __name__ == '__main__': run()