File size: 33,003 Bytes
cd0c7a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | """
Fuzz / random data tests.
Verifies that every endpoint degrades gracefully with garbage inputs:
- Returns proper HTTP error codes (4xx/5xx), never an unhandled crash
- Does not leak stack traces in the response body
- Validates input before hitting external APIs or RDKit
"""
import asyncio
import string
import random
import pytest
from httpx import AsyncClient, ASGITransport
from app.main import app
transport = ASGITransport(app=app)
def _rand_str(n: int) -> str:
"""Random printable string — no control chars, safe for JSON string values."""
safe = [c for c in string.printable if c.isprintable() and ord(c) < 127]
return "".join(random.choices(safe, k=n))
def _rand_path_segment(n: int) -> str:
"""Random string safe for URL path segments (no control chars, no slashes)."""
safe = [c for c in string.printable if c.isprintable() and c not in ('/', '\\', '?', '#', '%')]
return "".join(random.choices(safe, k=n))
def _rand_pdb_id() -> str:
return "".join(random.choices(string.ascii_letters + string.digits, k=4))
def _rand_smiles(n: int) -> str:
return "".join(random.choices("CNOSPFcnos@#=-+0123456789()[]\\/", k=n))
def _rand_seq(n: int) -> str:
return "".join(random.choices("ACDEFGHIKLMNPQRSTVWYacdefghiklmnpqrstvwy", k=n))
def _rand_dna(n: int) -> str:
return "".join(random.choices("ACGTacgt", k=n))
# ============================================================================
# 1. ADMET — SMILES fuzzing
# ============================================================================
class TestFuzzADMET:
@pytest.mark.asyncio
async def test_random_smiles_no_crash(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(20):
s = _rand_smiles(random.randint(1, 50))
r = await ac.post("/api/admet/descriptors", json={"smiles": s})
assert r.status_code in (200, 400, 422, 500, 502), f"smiles={s!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_empty_smiles_rejected(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={"smiles": ""})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_500_char_smiles_rejected(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={"smiles": "C" * 501})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_unicode_smiles_no_crash(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
payloads = ["\u00e9\u00e8\u00ea", "\u4e2d\u6587\u5206\u5b50", "\U0001f600\U0001f601"]
for s in payloads:
r = await ac.post("/api/admet/descriptors", json={"smiles": s})
assert r.status_code in (200, 400, 422, 500, 502)
@pytest.mark.asyncio
async def test_sql_injection_smiles(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={"smiles": "'; DROP TABLE molecules; --"})
assert r.status_code in (200, 400, 422, 500, 502)
@pytest.mark.asyncio
async def test_missing_field(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_wrong_type_smiles(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={"smiles": 12345})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_list_smiles_rejected(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={"smiles": ["CCO", "c1ccccc1"]})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_newlines_in_smiles(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={"smiles": "CCO\nDROP TABLE\n;--"})
assert r.status_code in (200, 400, 422, 500, 502)
@pytest.mark.asyncio
async def test_null_smiles(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/admet/descriptors", json={"smiles": None})
assert r.status_code == 422
# ============================================================================
# 2. STRUCTURE endpoints — PDB ID fuzzing
# ============================================================================
class TestFuzzStructures:
@pytest.mark.asyncio
async def test_random_pdb_search_no_crash(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
q = _rand_str(random.randint(1, 30))
r = await ac.post("/api/structures/search", json={"query": q})
assert r.status_code in (200, 422, 500, 502), f"query={q!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_random_inventory_pdb_ids(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
pid = _rand_pdb_id()
r = await ac.post("/api/structures/inventory", json={"pdb_id": pid})
assert r.status_code in (200, 404, 422, 500, 502), f"pdb_id={pid!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_inventory_too_long_pdb_id(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/structures/inventory", json={"pdb_id": "ABCDEF"})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_inventory_special_chars_pdb_id(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for pid in ["'; --", "<script>", "AAAA/../../../etc", "\x00\x01\x02\x03"]:
r = await ac.post("/api/structures/inventory", json={"pdb_id": pid})
assert r.status_code == 422, f"pdb_id={pid!r} should be rejected"
@pytest.mark.asyncio
async def test_fetch_structure_empty_query(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/structures/fetch", json={"query": ""})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_fetch_structure_long_query(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/structures/fetch", json={"query": "A" * 10000})
assert r.status_code in (200, 404, 422, 500, 502)
# ============================================================================
# 3. RAMACHANDRAN / SECONDARY STRUCTURE / COMPARE — path param fuzzing
# ============================================================================
class TestFuzzStructureAnalysis:
@pytest.mark.asyncio
async def test_random_pdb_ramachandran(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
pid = _rand_path_segment(random.randint(1, 4)).lower()
r = await ac.get(f"/api/analysis/ramachandran/{pid}")
assert r.status_code in (200, 404, 500, 502), f"pdb={pid} status={r.status_code}"
@pytest.mark.asyncio
async def test_random_pdb_secondary_structure(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
pid = _rand_path_segment(random.randint(1, 4))
r = await ac.get(f"/api/analysis/secondary_structure/{pid}")
assert r.status_code in (200, 404, 500, 502), f"pdb={pid} status={r.status_code}"
@pytest.mark.asyncio
async def test_random_pdb_compare(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(5):
pid = _rand_path_segment(random.randint(1, 4))
r = await ac.get(f"/api/analysis/compare/{pid}", params={"chain": "A", "max_results": 5})
assert r.status_code in (200, 404, 500, 502, 408), f"pdb={pid} status={r.status_code}"
@pytest.mark.asyncio
async def test_ramachandran_long_pdb_id(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.get("/api/analysis/ramachandran/" + "A" * 100)
assert r.status_code in (404, 422, 500)
@pytest.mark.asyncio
async def test_compare_negative_max_results(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.get("/api/analysis/compare/1CRN", params={"max_results": -5})
assert r.status_code in (200, 422, 404, 500, 502)
# ============================================================================
# 4. DOMAINS — accession fuzzing
# ============================================================================
class TestFuzzDomains:
@pytest.mark.asyncio
async def test_random_accession_domains(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
acc = _rand_path_segment(random.randint(1, 20))
r = await ac.get(f"/api/domains/{acc}")
assert r.status_code in (200, 404, 422, 500, 502), f"accession={acc!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_empty_accession_domains(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.get("/api/domains/")
assert r.status_code in (404, 422, 500)
@pytest.mark.asyncio
async def test_numeric_accession(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.get("/api/domains/12345")
assert r.status_code in (200, 404, 422, 500, 502)
# ============================================================================
# 5. INTERACTIONS — gene name fuzzing
# ============================================================================
class TestFuzzInteractions:
@pytest.mark.asyncio
async def test_random_gene_interactions(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
gene = _rand_path_segment(random.randint(1, 20))
r = await ac.get(f"/api/interactions/{gene}")
assert r.status_code in (200, 404, 500, 502), f"gene={gene!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_gene_with_special_chars(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.get("/api/interactions/<script>alert(1)</script>")
assert r.status_code in (200, 404, 422, 500, 502)
# ============================================================================
# 6. PATHWAYS — query fuzzing
# ============================================================================
class TestFuzzPathways:
@pytest.mark.asyncio
async def test_random_pathway_search(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
q = _rand_str(random.randint(1, 50))
r = await ac.post("/api/pathways/search", json={"query": q})
assert r.status_code in (200, 422, 500, 502), f"query={q!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_single_char_pathway_search(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/pathways/search", json={"query": "x"})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_random_kegg_search(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(5):
q = _rand_str(random.randint(2, 30))
r = await ac.post("/api/pathways/kegg/search", json={"query": q})
assert r.status_code in (200, 422, 500, 502)
@pytest.mark.asyncio
async def test_random_enrichment(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
ids = [_rand_str(random.randint(2, 10)) for _ in range(5)]
r = await ac.post("/api/pathways/enrichment", json={"identifiers": ids})
assert r.status_code in (200, 422, 500, 502)
@pytest.mark.asyncio
async def test_empty_enrichment(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/pathways/enrichment", json={"identifiers": []})
assert r.status_code == 422
# ============================================================================
# 7. ALIGNMENT — sequence fuzzing
# ============================================================================
class TestFuzzAlignment:
@pytest.mark.asyncio
async def test_random_protein_alignment(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
seq = _rand_seq(random.randint(1, 200))
r = await ac.post("/api/alignment/run", json={"sequence": seq, "stype": "protein"})
assert r.status_code in (200, 422, 500, 502), f"seq_len={len(seq)} status={r.status_code}"
@pytest.mark.asyncio
async def test_random_dna_alignment(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(5):
seq = _rand_dna(random.randint(1, 200))
r = await ac.post("/api/alignment/run", json={"sequence": seq, "stype": "dna"})
assert r.status_code in (200, 422, 500, 502)
@pytest.mark.asyncio
async def test_empty_alignment(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/alignment/run", json={"sequence": "", "stype": "protein"})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_numeric_sequence(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/alignment/run", json={"sequence": "1234567890", "stype": "protein"})
assert r.status_code in (200, 422, 500, 502)
@pytest.mark.asyncio
async def test_very_long_alignment(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
seq = _rand_seq(10000)
r = await ac.post("/api/alignment/run", json={"sequence": seq, "stype": "protein"})
assert r.status_code in (200, 422, 500, 502, 408, 413)
# ============================================================================
# 8. UNIPROT — accession / query fuzzing
# ============================================================================
class TestFuzzUniProt:
@pytest.mark.asyncio
async def test_random_uniprot_search(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
q = _rand_str(random.randint(2, 30))
r = await ac.post("/api/uniprot/search", json={"query": q})
assert r.status_code in (200, 422, 500, 502), f"query={q!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_random_uniprot_detail(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
acc = _rand_str(random.randint(1, 20))
r = await ac.post("/api/uniprot/detail", json={"accession": acc})
assert r.status_code in (200, 404, 422, 500, 502), f"accession={acc!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_uniprot_search_single_char(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/uniprot/search", json={"query": "a"})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_uniprot_max_results_extreme(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/uniprot/search", json={"query": "insulin", "max_results": 99999})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_uniprot_max_results_zero(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/uniprot/search", json={"query": "insulin", "max_results": 0})
assert r.status_code == 422
@pytest.mark.asyncio
async def test_uniprot_negative_max_results(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/uniprot/search", json={"query": "insulin", "max_results": -1})
assert r.status_code == 422
# ============================================================================
# 9. FUNCTION PREDICTION — PDB ID pattern fuzzing
# ============================================================================
class TestFuzzFunctionPrediction:
@pytest.mark.asyncio
async def test_random_pdb_id_rejected(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(10):
pid = _rand_str(random.randint(1, 10))
r = await ac.post("/api/function/predict", json={"pdb_id": pid},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required — cannot test without valid token")
assert r.status_code in (200, 422, 401), f"pdb_id={pid!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_too_short_pdb_id(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for pid in ["A", "AB", "ABC", "ABCDE"]:
r = await ac.post("/api/function/predict", json={"pdb_id": pid},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code == 422, f"pid={pid!r} should be rejected (len != 4)"
@pytest.mark.asyncio
async def test_special_chars_pdb_id(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for pid in ["AB;D", "AA/BB", "AA'BB", "AA BB"]:
r = await ac.post("/api/function/predict", json={"pdb_id": pid},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code == 422, f"pid={pid!r} should be rejected (special chars)"
@pytest.mark.asyncio
async def test_missing_pdb_id(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/function/predict", json={},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code == 422
# ============================================================================
# 10. MD SIMULATION — mode fuzzing
# ============================================================================
class TestFuzzMD:
@pytest.mark.asyncio
async def test_random_pdb_id_md(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(5):
pid = _rand_pdb_id()
r = await ac.post("/api/md/run", json={"pdb_id": pid},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 401), f"pdb_id={pid!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_invalid_mode_rejected(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for mode in ["destroy", "explode", "minimize; rm -rf /", "production\n"]:
r = await ac.post("/api/md/run", json={"pdb_id": "1CRN", "mode": mode},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code == 422, f"mode={mode!r} should be rejected"
@pytest.mark.asyncio
async def test_empty_pdb_id_md(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/md/run", json={"pdb_id": ""},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code == 422
# ============================================================================
# 11. DOCKING — SMILES + grid fuzzing
# ============================================================================
class TestFuzzDocking:
@pytest.mark.asyncio
async def test_random_smiles_docking(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(5):
s = _rand_smiles(random.randint(1, 30))
r = await ac.post("/api/docking/run", json={"smiles": s, "pdb_id": _rand_pdb_id()},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 401), f"smiles={s!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_extreme_grid_size(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for gs in [[-1, -1, -1], [0, 0, 0], [99999, 99999, 99999], [0.001, 0.001, 0.001]]:
r = await ac.post("/api/docking/run",
json={"smiles": "CCO", "pdb_id": "1CRN", "grid_size": gs},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 400, 401), f"grid={gs} status={r.status_code}"
@pytest.mark.asyncio
async def test_extreme_exhaustiveness(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for ex in [0, -1, 99999]:
r = await ac.post("/api/docking/run",
json={"smiles": "CCO", "pdb_id": "1CRN", "exhaustiveness": ex},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 400, 401), f"exhaustiveness={ex} status={r.status_code}"
@pytest.mark.asyncio
async def test_empty_smiles_docking(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/docking/run", json={"smiles": ""},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 401)
@pytest.mark.asyncio
async def test_grid_center_wrong_type(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/docking/run",
json={"smiles": "CCO", "pdb_id": "1CRN", "grid_center": "not_a_list"},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code == 422
# ============================================================================
# 12. PIPELINE — sequence + step fuzzing
# ============================================================================
class TestFuzzPipeline:
@pytest.mark.asyncio
async def test_random_sequence_pipeline(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(5):
seq = _rand_seq(random.randint(6, 100))
r = await ac.post("/api/pipeline/v2/run", json={"sequence": seq})
assert r.status_code in (200, 422, 500, 502), f"seq_len={len(seq)} status={r.status_code}"
@pytest.mark.asyncio
async def test_too_short_sequence(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for seq in ["", "A", "AC", "ACD", "ACDE", "ACDEF"]:
r = await ac.post("/api/pipeline/v2/run", json={"sequence": seq})
assert r.status_code == 422, f"seq={seq!r} should be rejected (len < 6)"
@pytest.mark.asyncio
async def test_invalid_steps(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/pipeline/v2/run",
json={"sequence": "ACDEFG", "steps": ["nonexistent", "fake_step"]})
assert r.status_code in (200, 422, 400, 500, 502)
@pytest.mark.asyncio
async def test_empty_steps_list(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/pipeline/v2/run", json={"sequence": "ACDEFG", "steps": []})
assert r.status_code in (200, 422, 400, 500)
@pytest.mark.asyncio
async def test_numeric_sequence_pipeline(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/pipeline/v2/run", json={"sequence": "123456"})
assert r.status_code in (200, 400, 422, 500, 502)
# ============================================================================
# 13. SEQUENCING — URL + reference fuzzing
# ============================================================================
class TestFuzzSequencing:
@pytest.mark.asyncio
async def test_random_fastq_url(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
for _ in range(5):
url = _rand_str(random.randint(5, 50))
r = await ac.post("/api/sequencing/run", json={"fastq_url": url},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 400, 500, 502), f"url={url!r} status={r.status_code}"
@pytest.mark.asyncio
async def test_empty_fastq_url(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/sequencing/run", json={"fastq_url": ""},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 400, 500)
@pytest.mark.asyncio
async def test_random_reference(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
ref = _rand_str(random.randint(1, 30))
r = await ac.post("/api/sequencing/run",
json={"fastq_url": "https://example.com/file.fastq", "reference": ref},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code in (200, 422, 400, 500, 502)
@pytest.mark.asyncio
async def test_missing_fastq_url(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.post("/api/sequencing/run", json={},
headers={"Authorization": "Bearer test-token"})
if r.status_code == 401:
pytest.skip("Auth required")
assert r.status_code == 422
# ============================================================================
# 14. CROSS-CUTTING: Content-Type / body fuzzing
# ============================================================================
class TestFuzzCrossCutting:
@pytest.mark.asyncio
async def test_empty_body_post_endpoints(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
endpoints = [
"/api/admet/descriptors",
"/api/pathways/search",
"/api/pathways/kegg/search",
"/api/alignment/run",
"/api/uniprot/search",
"/api/uniprot/detail",
]
for ep in endpoints:
r = await ac.post(ep, content=b"", headers={"Content-Type": "application/json"})
assert r.status_code in (422, 400), f"endpoint={ep} empty body status={r.status_code}"
@pytest.mark.asyncio
async def test_malformed_json(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
endpoints = [
"/api/admet/descriptors",
"/api/pathways/search",
"/api/alignment/run",
]
for ep in endpoints:
r = await ac.post(ep, content=b"{bad json!!!", headers={"Content-Type": "application/json"})
assert r.status_code in (422, 400), f"endpoint={ep} bad JSON status={r.status_code}"
@pytest.mark.asyncio
async def test_json_array_instead_of_object(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
endpoints = [
"/api/admet/descriptors",
"/api/pathways/search",
"/api/alignment/run",
]
for ep in endpoints:
r = await ac.post(ep, content=b'[1,2,3]', headers={"Content-Type": "application/json"})
assert r.status_code in (422, 400), f"endpoint={ep} array body status={r.status_code}"
@pytest.mark.asyncio
async def test_huge_payload_rejection(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
huge = "A" * 1_000_000
r = await ac.post("/api/admet/descriptors", json={"smiles": huge})
assert r.status_code in (422, 413, 400)
@pytest.mark.asyncio
async def test_get_on_post_endpoint(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.get("/api/admet/descriptors")
assert r.status_code in (405, 404)
@pytest.mark.asyncio
async def test_nonexistent_endpoint(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
r = await ac.get("/api/totally_fake_endpoint/xyz")
assert r.status_code == 404
@pytest.mark.asyncio
async def test_path_traversal_pdb_id(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
paths = [
"/api/analysis/ramachandran/../../etc/passwd",
"/api/analysis/secondary_structure/..\\..\\windows\\system32",
"/api/domains/../../../etc/shadow",
]
for p in paths:
r = await ac.get(p)
assert r.status_code in (404, 422, 500), f"path={p} status={r.status_code}"
@pytest.mark.asyncio
async def test_xss_in_queries(self):
async with AsyncClient(transport=transport, base_url="http://test") as ac:
xss = "<script>alert('xss')</script>"
r = await ac.post("/api/structures/search", json={"query": xss})
assert r.status_code in (200, 422, 500, 502)
if r.status_code == 200:
assert "<script>" not in r.text
|