"""Store data to subscription status: pure mapping rules.""" from datetime import UTC, datetime, timedelta import pytest from app.integrations.app_store import AppleNotification, AppleTransaction from app.integrations.play_store import PlayLineItem, PlaySubscription, parse_subscription from app.modules.billing.service import apple_state, play_state NOW = datetime(2026, 10, 22, 12, tzinfo=UTC) LATER, EARLIER = NOW + timedelta(days=20), NOW - timedelta(days=1) PRO = ["pro_monthly"] def _tx(expires=LATER, revoked=None) -> AppleTransaction: return AppleTransaction("1000", "pro_monthly", expires, revoked) def _note(kind, subtype=None, grace=None, tx=None) -> AppleNotification: return AppleNotification(kind, subtype, tx or _tx(), grace) def test_apple_transaction_states(): assert apple_state(_tx(), NOW).status == "active" assert apple_state(_tx(EARLIER), NOW).status == "expired" assert apple_state(_tx(revoked=NOW), NOW).status == "refunded" assert apple_state(_tx(expires=None), NOW) is None @pytest.mark.parametrize(("kind", "subtype", "status"), [ ("DID_RENEW", None, "active"), ("SUBSCRIBED", "RESUBSCRIBE", "active"), ("DID_CHANGE_RENEWAL_STATUS", "AUTO_RENEW_DISABLED", "active"), # cancelled: pro until it expires ("EXPIRED", "VOLUNTARY", "expired"), ("GRACE_PERIOD_EXPIRED", None, "expired"), ("REFUND", None, "refunded"), ("REVOKE", None, "refunded"), ("DID_FAIL_TO_RENEW", None, "expired"), ]) def test_apple_notification_states(kind, subtype, status): tx = _tx(EARLIER) if kind == "DID_FAIL_TO_RENEW" else _tx() assert apple_state(tx, NOW, _note(kind, subtype, tx=tx)).status == status def test_apple_grace_period_extends_expiry(): tx = _tx(EARLIER) state = apple_state(tx, NOW, _note("DID_FAIL_TO_RENEW", "GRACE_PERIOD", grace=LATER, tx=tx)) assert (state.status, state.expires_at) == ("grace", LATER) def _sub(state="ACTIVE", expires=LATER, order="GPA.1111-2222-3333-44444..3", items=None) -> PlaySubscription: return PlaySubscription(state, items or (PlayLineItem("pro_monthly", expires),), order, None, True) @pytest.mark.parametrize(("state", "expires", "status"), [ ("ACTIVE", LATER, "active"), ("IN_GRACE_PERIOD", LATER, "grace"), ("CANCELED", LATER, "active"), # cancelled: pro until it expires ("CANCELED", EARLIER, "expired"), ("EXPIRED", EARLIER, "expired"), ("ON_HOLD", EARLIER, "expired"), ("PAUSED", LATER, "expired"), ]) def test_play_states(state, expires, status): result = play_state(_sub(state, expires), "token", PRO, NOW) assert (result.status, result.expires_at, result.platform) == (status, expires, "android") def test_play_stable_id_is_the_base_order_id(): assert play_state(_sub(), "token", PRO, NOW).original_transaction_id == "GPA.1111-2222-3333-44444" assert play_state(_sub(order="GPA.9"), "t", PRO, NOW).original_transaction_id == "GPA.9" assert play_state(_sub(order=None), "token-x", PRO, NOW).original_transaction_id == "token-x" def test_play_pending_is_not_a_purchase(): assert play_state(_sub("PENDING"), "t", PRO, NOW) is None assert play_state(PlaySubscription("ACTIVE", (), None, None, False), "t", PRO, NOW) is None def test_play_prefers_the_pro_line_item(): items = (PlayLineItem("other", LATER + timedelta(days=5)), PlayLineItem("pro_monthly", LATER)) assert play_state(_sub(items=items), "t", PRO, NOW).product_id == "pro_monthly" def test_parse_subscription_v2_response(): sub = parse_subscription({ "subscriptionState": "SUBSCRIPTION_STATE_IN_GRACE_PERIOD", "latestOrderId": "GPA.1-2-3-4..0", "linkedPurchaseToken": "old-token", "acknowledgementState": "ACKNOWLEDGEMENT_STATE_PENDING", "lineItems": [{"productId": "pro_monthly", "expiryTime": "2026-11-22T12:00:00.123456789Z"}], }) assert sub.state == "IN_GRACE_PERIOD" assert sub.linked_purchase_token == "old-token" and not sub.acknowledged assert sub.line_items[0].expires_at == datetime(2026, 11, 22, 12, 0, 0, 123456, tzinfo=UTC)