|
| 1 | +"""Export-entitlement gate (pricing-truth). |
| 2 | +
|
| 3 | +The landing pricing page promises Free "PDF export, ATS theme" and |
| 4 | +Pro/Business "PDF + DOCX export, all themes". Before this gate that |
| 5 | +differentiation was UNENFORCED -- any tier (and `/workspace/artifacts/ |
| 6 | +export` had no auth at all) could download DOCX / any theme, making |
| 7 | +the Free vs Pro pricing bullets a fabricated claim. |
| 8 | +
|
| 9 | +`backend.tiers.export_entitlement_block_reason` is the policy (single |
| 10 | +source of truth, lock-stepped with the pricing copy); |
| 11 | +`backend.quota.enforce_export_entitlement` is the raiser that reuses |
| 12 | +the canonical `QuotaExceededError` 429 path (exactly like the |
| 13 | +`premium_applications` "Pro+ feature" rejection) so the frontend |
| 14 | +renders the uniform upgrade nudge. |
| 15 | +
|
| 16 | +These tests are deliberately hermetic (pure functions, no Supabase / |
| 17 | +OpenAI) so they run regardless of local `.env` credentials. |
| 18 | +""" |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from backend.quota import enforce_export_entitlement |
| 24 | +from backend.tiers import ( |
| 25 | + FREE_EXPORT_FORMAT, |
| 26 | + FREE_EXPORT_THEME, |
| 27 | + export_entitlement_block_reason, |
| 28 | +) |
| 29 | +from src.errors import QuotaExceededError |
| 30 | + |
| 31 | + |
| 32 | +# ── policy: export_entitlement_block_reason ───────────────────────── |
| 33 | + |
| 34 | + |
| 35 | +def test_free_pdf_classic_ats_is_allowed(): |
| 36 | + assert ( |
| 37 | + export_entitlement_block_reason( |
| 38 | + "free", export_format="pdf", themes=("classic_ats", "classic_ats") |
| 39 | + ) |
| 40 | + is None |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def test_free_docx_is_blocked_with_docx_label(): |
| 45 | + assert ( |
| 46 | + export_entitlement_block_reason("free", export_format="docx") |
| 47 | + == "DOCX export" |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def test_free_non_ats_theme_is_blocked_with_theme_label(): |
| 52 | + assert ( |
| 53 | + export_entitlement_block_reason( |
| 54 | + "free", export_format="pdf", themes=("professional_neutral",) |
| 55 | + ) |
| 56 | + == "Custom export themes" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def test_free_format_violation_takes_precedence_over_theme(): |
| 61 | + # DOCX + a custom theme: the format label is the one we surface. |
| 62 | + assert ( |
| 63 | + export_entitlement_block_reason( |
| 64 | + "free", export_format="docx", themes=("professional_neutral",) |
| 65 | + ) |
| 66 | + == "DOCX export" |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def test_blank_or_default_values_never_upsell_free(): |
| 71 | + # A caller omitting a theme (cover-letter export with default |
| 72 | + # resume_theme, etc.) must not be upsold. |
| 73 | + assert export_entitlement_block_reason("free") is None |
| 74 | + assert ( |
| 75 | + export_entitlement_block_reason( |
| 76 | + "free", export_format="", themes=("", " ") |
| 77 | + ) |
| 78 | + is None |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_free_comparison_is_case_and_whitespace_insensitive(): |
| 83 | + # Mirrors the request models' _strip_theme normalisation. |
| 84 | + assert ( |
| 85 | + export_entitlement_block_reason( |
| 86 | + "free", export_format=" PDF ", themes=(" Classic_ATS ",) |
| 87 | + ) |
| 88 | + is None |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("tier", ["pro", "business"]) |
| 93 | +def test_paid_tiers_have_full_entitlement(tier): |
| 94 | + assert ( |
| 95 | + export_entitlement_block_reason( |
| 96 | + tier, export_format="docx", themes=("professional_neutral",) |
| 97 | + ) |
| 98 | + is None |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def test_free_constants_match_pricing_copy(): |
| 103 | + # If these change, the Free pricing bullet ("PDF export, ATS |
| 104 | + # theme") changed too -- a pricing decision, caught here. |
| 105 | + assert FREE_EXPORT_FORMAT == "pdf" |
| 106 | + assert FREE_EXPORT_THEME == "classic_ats" |
| 107 | + |
| 108 | + |
| 109 | +# ── raiser: enforce_export_entitlement ────────────────────────────── |
| 110 | + |
| 111 | + |
| 112 | +def test_enforce_allows_free_pdf_classic_ats(): |
| 113 | + # No exception == allowed. |
| 114 | + enforce_export_entitlement( |
| 115 | + "free", export_format="pdf", themes=("classic_ats",) |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize("tier", ["pro", "business"]) |
| 120 | +def test_enforce_allows_paid_docx(tier): |
| 121 | + enforce_export_entitlement( |
| 122 | + tier, export_format="docx", themes=("professional_neutral",) |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +def test_enforce_blocks_free_docx_as_quota_error(): |
| 127 | + with pytest.raises(QuotaExceededError) as exc_info: |
| 128 | + enforce_export_entitlement("free", export_format="docx") |
| 129 | + err = exc_info.value |
| 130 | + # Reuses the canonical 429 shape (same as premium_applications): |
| 131 | + assert err.tier == "free" |
| 132 | + assert err.counter == "premium_export" |
| 133 | + assert err.cap == 0 |
| 134 | + assert err.current == 0 |
| 135 | + # lifetime, NOT a monthly counter -> frontend won't render a wrong |
| 136 | + # "resets on the 1st" line. |
| 137 | + assert err.reset_period == "lifetime" |
| 138 | + assert "Pro+" in err.user_message |
| 139 | + |
| 140 | + |
| 141 | +def test_enforce_blocks_free_custom_theme(): |
| 142 | + with pytest.raises(QuotaExceededError): |
| 143 | + enforce_export_entitlement( |
| 144 | + "free", export_format="pdf", themes=("professional_neutral",) |
| 145 | + ) |
0 commit comments