Spaces:
Build error
Build error
| import os | |
| import pytest | |
| from unittest.mock import patch, MagicMock | |
| from fastapi.testclient import TestClient | |
| from app.main import app | |
| client = TestClient(app) | |
| # Skip all tests in this module if Stripe secret key is not set | |
| STRIPE_SECRET_KEY = os.getenv("STRIPE_SECRET_KEY") | |
| if not STRIPE_SECRET_KEY: | |
| pytest.skip( | |
| "Stripe not configured – skipping payment tests", | |
| allow_module_level=True) | |
| def mock_stripe(): | |
| with patch("stripe.checkout.Session.create") as mock: | |
| yield mock | |
| def test_create_checkout_session_missing_stripe_key(monkeypatch): | |
| monkeypatch.setenv("STRIPE_SECRET_KEY", "") | |
| response = client.post( | |
| "/api/v1/payments/create-checkout-session", | |
| json={ | |
| "api_key": "test_key", | |
| "success_url": "https://example.com/success", | |
| "cancel_url": "https://example.com/cancel"}) | |
| assert response.status_code == 500 | |
| assert "Stripe not configured" in response.json()["detail"] | |
| def test_create_checkout_session_free_key(mock_stripe): | |
| # Mock tracker.get_tier to return Tier.FREE | |
| with patch("app.core.usage_tracker.tracker") as mock_tracker: | |
| mock_tracker.get_tier.return_value = "free" | |
| mock_stripe.return_value = MagicMock( | |
| id="cs_test_123", url="https://checkout.stripe.com/pay") | |
| response = client.post( | |
| "/api/v1/payments/create-checkout-session", | |
| json={ | |
| "api_key": "test_key", | |
| "success_url": "https://example.com/success", | |
| "cancel_url": "https://example.com/cancel"}) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "sessionId" in data | |
| assert "url" in data | |
| def test_create_checkout_session_pro_key(): | |
| with patch("app.core.usage_tracker.tracker") as mock_tracker: | |
| mock_tracker.get_tier.return_value = "pro" | |
| response = client.post( | |
| "/api/v1/payments/create-checkout-session", | |
| json={ | |
| "api_key": "test_key", | |
| "success_url": "https://example.com/success", | |
| "cancel_url": "https://example.com/cancel"}) | |
| assert response.status_code == 400 | |
| assert "Only free tier keys can be upgraded" in response.json()[ | |
| "detail"] | |