|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/h2non/gock" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/supabase/cli/internal/testing/apitest" |
| 11 | +) |
| 12 | + |
| 13 | +var planGateProjectJSON = map[string]interface{}{ |
| 14 | + "ref": "test-ref", |
| 15 | + "organization_slug": "my-org", |
| 16 | + "name": "test", |
| 17 | + "region": "us-east-1", |
| 18 | + "created_at": "2024-01-01T00:00:00Z", |
| 19 | + "status": "ACTIVE_HEALTHY", |
| 20 | + "database": map[string]interface{}{"host": "db.example.supabase.co", "version": "15.1.0.117"}, |
| 21 | +} |
| 22 | + |
| 23 | +func TestGetOrgSlugFromProjectRef(t *testing.T) { |
| 24 | + ref := apitest.RandomProjectRef() |
| 25 | + |
| 26 | + t.Run("returns org slug on success", func(t *testing.T) { |
| 27 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 28 | + gock.New(DefaultApiHost). |
| 29 | + Get("/v1/projects/" + ref). |
| 30 | + Reply(http.StatusOK). |
| 31 | + JSON(planGateProjectJSON) |
| 32 | + slug, err := GetOrgSlugFromProjectRef(context.Background(), ref) |
| 33 | + assert.NoError(t, err) |
| 34 | + assert.Equal(t, "my-org", slug) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("returns error on not found", func(t *testing.T) { |
| 38 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 39 | + gock.New(DefaultApiHost). |
| 40 | + Get("/v1/projects/" + ref). |
| 41 | + Reply(http.StatusNotFound) |
| 42 | + _, err := GetOrgSlugFromProjectRef(context.Background(), ref) |
| 43 | + assert.ErrorContains(t, err, "unexpected get project status 404") |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("returns error on network failure", func(t *testing.T) { |
| 47 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 48 | + gock.New(DefaultApiHost). |
| 49 | + Get("/v1/projects/" + ref). |
| 50 | + ReplyError(assert.AnError) |
| 51 | + _, err := GetOrgSlugFromProjectRef(context.Background(), ref) |
| 52 | + assert.ErrorContains(t, err, "failed to get project") |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func TestGetOrgBillingURL(t *testing.T) { |
| 57 | + url := GetOrgBillingURL("my-org") |
| 58 | + assert.Equal(t, GetSupabaseDashboardURL()+"/org/my-org/billing", url) |
| 59 | +} |
| 60 | + |
| 61 | +func entitlementsJSON(featureKey string, hasAccess bool) map[string]interface{} { |
| 62 | + return map[string]interface{}{ |
| 63 | + "entitlements": []map[string]interface{}{ |
| 64 | + { |
| 65 | + "feature": map[string]interface{}{"key": featureKey, "type": "numeric"}, |
| 66 | + "hasAccess": hasAccess, |
| 67 | + "type": "numeric", |
| 68 | + "config": map[string]interface{}{"enabled": hasAccess, "value": 0, "unlimited": false, "unit": "count"}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestSuggestUpgradeOnError(t *testing.T) { |
| 75 | + ref := apitest.RandomProjectRef() |
| 76 | + |
| 77 | + t.Run("sets specific suggestion on 402 with gated feature", func(t *testing.T) { |
| 78 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 79 | + t.Cleanup(func() { CmdSuggestion = "" }) |
| 80 | + gock.New(DefaultApiHost). |
| 81 | + Get("/v1/projects/" + ref). |
| 82 | + Reply(http.StatusOK). |
| 83 | + JSON(planGateProjectJSON) |
| 84 | + gock.New(DefaultApiHost). |
| 85 | + Get("/v1/organizations/my-org/entitlements"). |
| 86 | + Reply(http.StatusOK). |
| 87 | + JSON(entitlementsJSON("branching_limit", false)) |
| 88 | + SuggestUpgradeOnError(context.Background(), ref, "branching_limit", http.StatusPaymentRequired) |
| 89 | + assert.Contains(t, CmdSuggestion, "/org/my-org/billing") |
| 90 | + assert.Contains(t, CmdSuggestion, "does not have access") |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("sets generic suggestion when entitlements lookup fails", func(t *testing.T) { |
| 94 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 95 | + t.Cleanup(func() { CmdSuggestion = "" }) |
| 96 | + gock.New(DefaultApiHost). |
| 97 | + Get("/v1/projects/" + ref). |
| 98 | + Reply(http.StatusOK). |
| 99 | + JSON(planGateProjectJSON) |
| 100 | + gock.New(DefaultApiHost). |
| 101 | + Get("/v1/organizations/my-org/entitlements"). |
| 102 | + Reply(http.StatusInternalServerError) |
| 103 | + SuggestUpgradeOnError(context.Background(), ref, "branching_limit", http.StatusPaymentRequired) |
| 104 | + assert.Contains(t, CmdSuggestion, "/org/my-org/billing") |
| 105 | + assert.Contains(t, CmdSuggestion, "may require a plan upgrade") |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("sets fallback suggestion when project lookup fails", func(t *testing.T) { |
| 109 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 110 | + t.Cleanup(func() { CmdSuggestion = "" }) |
| 111 | + gock.New(DefaultApiHost). |
| 112 | + Get("/v1/projects/" + ref). |
| 113 | + Reply(http.StatusNotFound) |
| 114 | + SuggestUpgradeOnError(context.Background(), ref, "branching_limit", http.StatusPaymentRequired) |
| 115 | + assert.Contains(t, CmdSuggestion, "plan upgrade") |
| 116 | + assert.Contains(t, CmdSuggestion, GetSupabaseDashboardURL()) |
| 117 | + assert.NotContains(t, CmdSuggestion, "/org/") |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("sets generic suggestion when feature has access", func(t *testing.T) { |
| 121 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 122 | + t.Cleanup(func() { CmdSuggestion = "" }) |
| 123 | + gock.New(DefaultApiHost). |
| 124 | + Get("/v1/projects/" + ref). |
| 125 | + Reply(http.StatusOK). |
| 126 | + JSON(planGateProjectJSON) |
| 127 | + gock.New(DefaultApiHost). |
| 128 | + Get("/v1/organizations/my-org/entitlements"). |
| 129 | + Reply(http.StatusOK). |
| 130 | + JSON(entitlementsJSON("branching_limit", true)) |
| 131 | + SuggestUpgradeOnError(context.Background(), ref, "branching_limit", http.StatusPaymentRequired) |
| 132 | + assert.Contains(t, CmdSuggestion, "/org/my-org/billing") |
| 133 | + assert.Contains(t, CmdSuggestion, "may require a plan upgrade") |
| 134 | + }) |
| 135 | + |
| 136 | + t.Run("skips suggestion on 403 forbidden", func(t *testing.T) { |
| 137 | + CmdSuggestion = "" |
| 138 | + SuggestUpgradeOnError(context.Background(), ref, "branching_limit", http.StatusForbidden) |
| 139 | + assert.Empty(t, CmdSuggestion) |
| 140 | + }) |
| 141 | + |
| 142 | + t.Run("skips suggestion on non-billing status codes", func(t *testing.T) { |
| 143 | + CmdSuggestion = "" |
| 144 | + SuggestUpgradeOnError(context.Background(), ref, "branching_limit", http.StatusInternalServerError) |
| 145 | + assert.Empty(t, CmdSuggestion) |
| 146 | + }) |
| 147 | + |
| 148 | + t.Run("skips suggestion on success status codes", func(t *testing.T) { |
| 149 | + CmdSuggestion = "" |
| 150 | + SuggestUpgradeOnError(context.Background(), ref, "branching_limit", http.StatusOK) |
| 151 | + assert.Empty(t, CmdSuggestion) |
| 152 | + }) |
| 153 | +} |
0 commit comments