|
| 1 | +package activate |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-errors/errors" |
| 9 | + "github.com/h2non/gock" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/supabase/cli/internal/testing/apitest" |
| 12 | + "github.com/supabase/cli/internal/testing/fstest" |
| 13 | + "github.com/supabase/cli/internal/utils" |
| 14 | + "github.com/supabase/cli/internal/utils/flags" |
| 15 | + "github.com/supabase/cli/pkg/api" |
| 16 | +) |
| 17 | + |
| 18 | +func TestActivateSubdomain(t *testing.T) { |
| 19 | + t.Run("actives vanity subdomain", func(t *testing.T) { |
| 20 | + t.Cleanup(fstest.MockStdout(t, "Activated vanity subdomain at example.com\n")) |
| 21 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 22 | + // Setup mock api |
| 23 | + gock.New(utils.DefaultApiHost). |
| 24 | + Post("v1/projects/" + flags.ProjectRef + "/vanity-subdomain"). |
| 25 | + Reply(http.StatusCreated). |
| 26 | + JSON(api.ActivateVanitySubdomainResponse{ |
| 27 | + CustomDomain: "example.com", |
| 28 | + }) |
| 29 | + // Run test |
| 30 | + err := Run(context.Background(), flags.ProjectRef, "example.com", nil) |
| 31 | + assert.NoError(t, err) |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("encodes toml output", func(t *testing.T) { |
| 35 | + utils.OutputFormat.Value = utils.OutputToml |
| 36 | + t.Cleanup(func() { utils.OutputFormat.Value = utils.OutputPretty }) |
| 37 | + t.Cleanup(fstest.MockStdout(t, `CustomDomain = "example.com" |
| 38 | +`)) |
| 39 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 40 | + // Setup mock api |
| 41 | + gock.New(utils.DefaultApiHost). |
| 42 | + Post("v1/projects/" + flags.ProjectRef + "/vanity-subdomain"). |
| 43 | + Reply(http.StatusCreated). |
| 44 | + JSON(api.ActivateVanitySubdomainResponse{ |
| 45 | + CustomDomain: "example.com", |
| 46 | + }) |
| 47 | + // Run test |
| 48 | + err := Run(context.Background(), flags.ProjectRef, "example.com", nil) |
| 49 | + assert.NoError(t, err) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("throws error on network error", func(t *testing.T) { |
| 53 | + errNetwork := errors.New("network error") |
| 54 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 55 | + // Setup mock api |
| 56 | + gock.New(utils.DefaultApiHost). |
| 57 | + Post("/v1/projects/" + flags.ProjectRef + "/vanity-subdomain"). |
| 58 | + ReplyError(errNetwork) |
| 59 | + // Run test |
| 60 | + err := Run(context.Background(), flags.ProjectRef, "example.com", nil) |
| 61 | + assert.ErrorIs(t, err, errNetwork) |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("throws error on service unavailable", func(t *testing.T) { |
| 65 | + utils.OutputFormat.Value = utils.OutputEnv |
| 66 | + t.Cleanup(func() { utils.OutputFormat.Value = utils.OutputPretty }) |
| 67 | + t.Cleanup(apitest.MockPlatformAPI(t)) |
| 68 | + // Setup mock api |
| 69 | + gock.New(utils.DefaultApiHost). |
| 70 | + Post("/v1/projects/" + flags.ProjectRef + "/vanity-subdomain"). |
| 71 | + Reply(http.StatusServiceUnavailable) |
| 72 | + // Run test |
| 73 | + err := Run(context.Background(), flags.ProjectRef, "example.com", nil) |
| 74 | + assert.ErrorContains(t, err, "unexpected activate vanity subdomain status 503:") |
| 75 | + }) |
| 76 | +} |
0 commit comments