AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): hide trial offers for users with an existing plan (#15221)
Toran Bruce Richards6508e7e8ffeb
diff --git a/autogpt_platform/backend/backend/api/features/subscription_trial_routes.py b/autogpt_platform/backend/backend/api/features/subscription_trial_routes.pyindex ef9f88b852..c59aa8c3bd 100644--- a/autogpt_platform/backend/backend/api/features/subscription_trial_routes.py+++ b/autogpt_platform/backend/backend/api/features/subscription_trial_routes.py@@ -4,6 +4,7 @@ from typing import Annotated, Awaitableimport stripefrom autogpt_libs.auth import get_user_idfrom fastapi import APIRouter, Depends, Header, HTTPException, Security+from prisma.enums import SubscriptionTierfrom backend.api.features.billing.client_country import ( # noqa: F401 -- re-exportedCLIENT_COUNTRY_SCOPE,@@ -66,6 +67,8 @@ async def get_trial_status(and (offer := await get_trial_offer(user_id, country=country))is not Noneand await trial_seat_available(offer, trial_id=trial.id)+ and (await get_user_by_id(user_id)).subscription_tier+ == SubscriptionTier.NO_TIER),offer=TrialOfferResponse.from_offer(trial.offer),status=trial.status,diff --git a/autogpt_platform/backend/backend/api/features/subscription_trial_routes_test.py b/autogpt_platform/backend/backend/api/features/subscription_trial_routes_test.pyindex c2400c9998..98cce63469 100644--- a/autogpt_platform/backend/backend/api/features/subscription_trial_routes_test.py+++ b/autogpt_platform/backend/backend/api/features/subscription_trial_routes_test.py@@ -1,5 +1,5 @@from datetime import UTC, datetime, timedelta-from unittest.mock import AsyncMock, patch+from unittest.mock import AsyncMock, MagicMock, patchimport jwtimport pytest@@ -10,6 +10,7 @@ from cryptography.hazmat.primitives.asymmetric import ecfrom fastapi import FastAPIfrom httpx import ASGITransport, AsyncClientfrom jwt.algorithms import ECAlgorithm+from prisma.enums import SubscriptionTierfrom backend.api.features import subscription_trial_routes as routesfrom backend.data.subscription_trial import TrialState@@ -84,13 +85,31 @@ async def test_disabled_flag_hides_pending_enrollment(trial):@pytest.mark.asyncio-async def test_status_never_exposes_internal_spend_or_stripe_identifiers(trial):[email protected]("trial_status", ["checkout_pending", "trialing", "active"])+async def test_status_never_exposes_internal_spend_or_stripe_identifiers(+ trial, trial_status+):+ now = datetime.now(UTC)+ trial = trial.model_copy(+ update={+ "status": trial_status,+ "card_verified_at": now,+ "ends_at": now + timedelta(days=7),+ "consumed_at": None if trial_status == "checkout_pending" else now,+ "converted_at": now if trial_status == "active" else None,+ }+ )with (patch.object(routes, "get_subscription_trial", AsyncMock(return_value=trial)),patch.object(routes, "get_trial_offer", AsyncMock(return_value=trial.offer)),patch.object(routes, "has_received_onboarding_credit", AsyncMock(return_value=True)),+ patch.object(+ routes,+ "get_user_by_id",+ AsyncMock(return_value=MagicMock(subscription_tier=SubscriptionTier.PRO)),+ ),):status = await routes.get_trial_status(trial.user_id)public = status.model_dump_json()@@ -103,6 +122,9 @@ async def test_status_never_exposes_internal_spend_or_stripe_identifiers(trial):):assert private not in publicassert status.onboarding_credits_previously_received+ assert not status.eligible+ assert status.active is (trial_status == "trialing")+ assert status.converted is (trial_status == "active")@pytest.mark.asyncio@@ -242,7 +264,10 @@ async def test_full_trial_hides_a_pending_enrollments_offer(trial):@pytest.mark.asyncio-async def test_pending_enrollment_keeps_its_offer_while_it_holds_a_seat(trial):[email protected]("tier", list(SubscriptionTier))+async def test_pending_enrollment_only_offers_a_trial_without_an_existing_plan(+ trial, tier+):with (patch.object(routes, "get_subscription_trial", AsyncMock(return_value=trial)),patch.object(routes, "get_trial_offer", AsyncMock(return_value=trial.offer)),@@ -250,9 +275,15 @@ async def test_pending_enrollment_keeps_its_offer_while_it_holds_a_seat(trial):routes, "has_received_onboarding_credit", AsyncMock(return_value=False)),patch.object(routes, "trial_seat_available", AsyncMock(return_value=True)),+ patch.object(+ routes,+ "get_user_by_id",+ AsyncMock(return_value=MagicMock(subscription_tier=tier)),+ ),):status = await routes.get_trial_status(trial.user_id)- assert status.eligible+ assert status.eligible is (tier == SubscriptionTier.NO_TIER)+ assert status.status == "checkout_pending"@pytest.mark.asyncio