|
| 1 | +package push |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/h2non/gock" |
| 10 | + "github.com/spf13/afero" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/supabase/cli/internal/testing/apitest" |
| 14 | + "github.com/supabase/cli/internal/utils" |
| 15 | +) |
| 16 | + |
| 17 | +func TestPushConfig(t *testing.T) { |
| 18 | + project := apitest.RandomProjectRef() |
| 19 | + // Setup valid access token |
| 20 | + token := apitest.RandomAccessToken(t) |
| 21 | + t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) |
| 22 | + |
| 23 | + t.Run("throws error on malformed config", func(t *testing.T) { |
| 24 | + // Setup in-memory fs |
| 25 | + fsys := afero.NewMemMapFs() |
| 26 | + require.NoError(t, utils.WriteFile(utils.ConfigPath, []byte("malformed"), fsys)) |
| 27 | + // Run test |
| 28 | + err := Run(context.Background(), "", fsys) |
| 29 | + // Check error |
| 30 | + assert.ErrorContains(t, err, "toml: expected = after a key, but the document ends there") |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("throws error on service unavailable", func(t *testing.T) { |
| 34 | + // Setup in-memory fs |
| 35 | + fsys := afero.NewMemMapFs() |
| 36 | + // Setup mock api |
| 37 | + defer gock.OffAll() |
| 38 | + gock.New(utils.DefaultApiHost). |
| 39 | + Get("/v1/projects/" + project + "/billing/addons"). |
| 40 | + Reply(http.StatusServiceUnavailable) |
| 41 | + // Run test |
| 42 | + err := Run(context.Background(), project, fsys) |
| 43 | + // Check error |
| 44 | + assert.ErrorContains(t, err, "unexpected list addons status 503:") |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func TestCostMatrix(t *testing.T) { |
| 49 | + project := apitest.RandomProjectRef() |
| 50 | + // Setup valid access token |
| 51 | + token := apitest.RandomAccessToken(t) |
| 52 | + t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) |
| 53 | + |
| 54 | + t.Run("fetches cost matrix", func(t *testing.T) { |
| 55 | + // Setup mock api |
| 56 | + defer gock.OffAll() |
| 57 | + gock.New(utils.DefaultApiHost). |
| 58 | + Get("/v1/projects/"+project+"/billing/addons"). |
| 59 | + Reply(http.StatusOK). |
| 60 | + SetHeader("Content-Type", "application/json"). |
| 61 | + BodyString(`{ |
| 62 | + "available_addons":[{ |
| 63 | + "name": "Advanced MFA - Phone", |
| 64 | + "type": "auth_mfa_phone", |
| 65 | + "variants": [{ |
| 66 | + "id": "auth_mfa_phone_default", |
| 67 | + "name": "Advanced MFA - Phone", |
| 68 | + "price": { |
| 69 | + "amount": 0.1027, |
| 70 | + "description": "$75/month, then $10/month", |
| 71 | + "interval": "hourly", |
| 72 | + "type": "usage" |
| 73 | + } |
| 74 | + }] |
| 75 | + }, { |
| 76 | + "name": "Advanced MFA - WebAuthn", |
| 77 | + "type": "auth_mfa_web_authn", |
| 78 | + "variants": [{ |
| 79 | + "id": "auth_mfa_web_authn_default", |
| 80 | + "name": "Advanced MFA - WebAuthn", |
| 81 | + "price": { |
| 82 | + "amount": 0.1027, |
| 83 | + "description": "$75/month, then $10/month", |
| 84 | + "interval": "hourly", |
| 85 | + "type": "usage" |
| 86 | + } |
| 87 | + }] |
| 88 | + }] |
| 89 | + }`) |
| 90 | + // Run test |
| 91 | + cost, err := getCostMatrix(context.Background(), project) |
| 92 | + // Check error |
| 93 | + assert.NoError(t, err) |
| 94 | + require.Len(t, cost, 2) |
| 95 | + assert.Equal(t, "Advanced MFA - Phone", cost["auth_mfa_phone"].Name) |
| 96 | + assert.Equal(t, "$75/month, then $10/month", cost["auth_mfa_phone"].Price) |
| 97 | + assert.Equal(t, "Advanced MFA - WebAuthn", cost["auth_mfa_web_authn"].Name) |
| 98 | + assert.Equal(t, "$75/month, then $10/month", cost["auth_mfa_web_authn"].Price) |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("throws error on network error", func(t *testing.T) { |
| 102 | + errNetwork := errors.New("network error") |
| 103 | + // Setup mock api |
| 104 | + defer gock.OffAll() |
| 105 | + gock.New(utils.DefaultApiHost). |
| 106 | + Get("/v1/projects/" + project + "/billing/addons"). |
| 107 | + ReplyError(errNetwork) |
| 108 | + // Run test |
| 109 | + cost, err := getCostMatrix(context.Background(), project) |
| 110 | + // Check error |
| 111 | + assert.ErrorIs(t, err, errNetwork) |
| 112 | + assert.Nil(t, cost) |
| 113 | + }) |
| 114 | +} |
0 commit comments