|
13 | 13 | from fastapi import HTTPException |
14 | 14 | from fastapi.testclient import TestClient |
15 | 15 | from jose import jwt |
16 | | -from main import UserInDB, app, get_current_user |
| 16 | +from main import IPConfig, UserInDB, app, get_current_user, is_ip_allowed |
17 | 17 | from meetup_query import ( |
18 | 18 | build_batched_group_query, |
19 | 19 | export_to_file, |
@@ -466,6 +466,141 @@ def test_invalid_token(raw_test_client): |
466 | 466 | assert "detail" in response.json() |
467 | 467 |
|
468 | 468 |
|
| 469 | +# ── IP whitelisting tests ────────────────────────────────────────── |
| 470 | + |
| 471 | + |
| 472 | +@pytest.mark.unit |
| 473 | +class TestIPConfigPublicIps: |
| 474 | + """IPConfig.public_ips should be configurable via PUBLIC_IPS env var.""" |
| 475 | + |
| 476 | + def test_default_public_ips_empty(self): |
| 477 | + cfg = IPConfig() |
| 478 | + assert cfg.public_ips == [] |
| 479 | + |
| 480 | + def test_public_ips_from_env_single(self): |
| 481 | + with patch.dict(os.environ, {"PUBLIC_IPS": "10.0.0.1"}): |
| 482 | + from main import _parse_public_ips |
| 483 | + |
| 484 | + ips = _parse_public_ips() |
| 485 | + assert ips == ["10.0.0.1"] |
| 486 | + |
| 487 | + def test_public_ips_from_env_multiple(self): |
| 488 | + with patch.dict(os.environ, {"PUBLIC_IPS": "10.0.0.1,192.168.1.1,172.16.0.1"}): |
| 489 | + from main import _parse_public_ips |
| 490 | + |
| 491 | + ips = _parse_public_ips() |
| 492 | + assert ips == ["10.0.0.1", "192.168.1.1", "172.16.0.1"] |
| 493 | + |
| 494 | + def test_public_ips_from_env_empty(self): |
| 495 | + with patch.dict(os.environ, {"PUBLIC_IPS": ""}): |
| 496 | + from main import _parse_public_ips |
| 497 | + |
| 498 | + ips = _parse_public_ips() |
| 499 | + assert ips == [] |
| 500 | + |
| 501 | + def test_public_ips_strips_whitespace(self): |
| 502 | + with patch.dict(os.environ, {"PUBLIC_IPS": " 10.0.0.1 , 192.168.1.1 "}): |
| 503 | + from main import _parse_public_ips |
| 504 | + |
| 505 | + ips = _parse_public_ips() |
| 506 | + assert ips == ["10.0.0.1", "192.168.1.1"] |
| 507 | + |
| 508 | + |
| 509 | +@pytest.mark.unit |
| 510 | +class TestIsIpAllowedWithPublicIps: |
| 511 | + """is_ip_allowed should match against both whitelist and public_ips.""" |
| 512 | + |
| 513 | + def test_allowed_via_public_ips(self): |
| 514 | + mock_request = MagicMock() |
| 515 | + mock_request.client.host = "203.0.113.50" |
| 516 | + with patch("main.ip_config", IPConfig(public_ips=["203.0.113.50"])): |
| 517 | + assert is_ip_allowed(mock_request) is True |
| 518 | + |
| 519 | + def test_denied_when_not_in_either_list(self): |
| 520 | + mock_request = MagicMock() |
| 521 | + mock_request.client.host = "203.0.113.99" |
| 522 | + with patch("main.ip_config", IPConfig(public_ips=["203.0.113.50"])): |
| 523 | + assert is_ip_allowed(mock_request) is False |
| 524 | + |
| 525 | + def test_allowed_via_whitelist(self): |
| 526 | + mock_request = MagicMock() |
| 527 | + mock_request.client.host = "127.0.0.1" |
| 528 | + with patch("main.ip_config", IPConfig()): |
| 529 | + assert is_ip_allowed(mock_request) is True |
| 530 | + |
| 531 | + |
| 532 | +# ── Cookie auth tests ───────────────────────────────────────────── |
| 533 | + |
| 534 | + |
| 535 | +@pytest.mark.unit |
| 536 | +class TestCookieAuth: |
| 537 | + """get_current_user should fall back to session_token cookie when no Bearer token.""" |
| 538 | + |
| 539 | + def test_cookie_auth_returns_user(self, raw_test_client): |
| 540 | + from main import ALGORITHM, SECRET_KEY, get_password_hash |
| 541 | + |
| 542 | + token = jwt.encode({"sub": "testuser"}, SECRET_KEY, algorithm=ALGORITHM) |
| 543 | + with ( |
| 544 | + patch("main.DEV", False), |
| 545 | + patch("main.is_ip_allowed", return_value=False), |
| 546 | + patch( |
| 547 | + "main.get_user", |
| 548 | + return_value=UserInDB( |
| 549 | + username="testuser", |
| 550 | + email="test@example.com", |
| 551 | + hashed_password=get_password_hash("pass"), |
| 552 | + ), |
| 553 | + ), |
| 554 | + ): |
| 555 | + raw_test_client.cookies.set("session_token", token) |
| 556 | + response = raw_test_client.get("/api/events") |
| 557 | + raw_test_client.cookies.clear() |
| 558 | + assert response.status_code == 200 |
| 559 | + |
| 560 | + def test_bearer_takes_precedence_over_cookie(self, raw_test_client): |
| 561 | + from main import ALGORITHM, SECRET_KEY, get_password_hash |
| 562 | + |
| 563 | + good_token = jwt.encode({"sub": "testuser"}, SECRET_KEY, algorithm=ALGORITHM) |
| 564 | + bad_cookie = "invalid_cookie_token" |
| 565 | + with ( |
| 566 | + patch("main.DEV", False), |
| 567 | + patch("main.is_ip_allowed", return_value=False), |
| 568 | + patch( |
| 569 | + "main.get_user", |
| 570 | + return_value=UserInDB( |
| 571 | + username="testuser", |
| 572 | + email="test@example.com", |
| 573 | + hashed_password=get_password_hash("pass"), |
| 574 | + ), |
| 575 | + ), |
| 576 | + ): |
| 577 | + raw_test_client.cookies.set("session_token", bad_cookie) |
| 578 | + response = raw_test_client.get( |
| 579 | + "/api/events", |
| 580 | + headers={"Authorization": f"Bearer {good_token}"}, |
| 581 | + ) |
| 582 | + raw_test_client.cookies.clear() |
| 583 | + assert response.status_code == 200 |
| 584 | + |
| 585 | + def test_invalid_cookie_returns_401(self, raw_test_client): |
| 586 | + with ( |
| 587 | + patch("main.DEV", False), |
| 588 | + patch("main.is_ip_allowed", return_value=False), |
| 589 | + ): |
| 590 | + raw_test_client.cookies.set("session_token", "invalid") |
| 591 | + response = raw_test_client.get("/api/events") |
| 592 | + raw_test_client.cookies.clear() |
| 593 | + assert response.status_code == 401 |
| 594 | + |
| 595 | + def test_no_token_no_cookie_returns_401(self, raw_test_client): |
| 596 | + with ( |
| 597 | + patch("main.DEV", False), |
| 598 | + patch("main.is_ip_allowed", return_value=False), |
| 599 | + ): |
| 600 | + response = raw_test_client.get("/api/events") |
| 601 | + assert response.status_code == 401 |
| 602 | + |
| 603 | + |
469 | 604 | # ── Deprecation / bcrypt tests ────────────────────────────────────── |
470 | 605 |
|
471 | 606 |
|
|
0 commit comments