github.com/supabase/cli@v1.168.1/internal/branches/create/create_test.go (about) 1 package create 2 3 import ( 4 "context" 5 "net" 6 "net/http" 7 "testing" 8 9 "github.com/spf13/afero" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "github.com/supabase/cli/internal/testing/apitest" 13 "github.com/supabase/cli/internal/utils" 14 "github.com/supabase/cli/internal/utils/flags" 15 "github.com/supabase/cli/pkg/api" 16 "gopkg.in/h2non/gock.v1" 17 ) 18 19 func TestCreateCommand(t *testing.T) { 20 // Setup valid project ref 21 flags.ProjectRef = apitest.RandomProjectRef() 22 23 t.Run("creates preview branch", func(t *testing.T) { 24 // Setup valid access token 25 token := apitest.RandomAccessToken(t) 26 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 27 // Setup in-memory fs 28 fsys := afero.NewMemMapFs() 29 // Setup mock api 30 defer gock.OffAll() 31 gock.New(utils.DefaultApiHost). 32 Post("/v1/projects/" + flags.ProjectRef + "/branches"). 33 Reply(http.StatusCreated). 34 JSON(api.BranchResponse{ 35 Id: "test-uuid", 36 }) 37 // Run test 38 err := Run(context.Background(), "", "sin", fsys) 39 // Check error 40 assert.NoError(t, err) 41 }) 42 43 t.Run("throws error on network disconnected", func(t *testing.T) { 44 // Setup in-memory fs 45 fsys := afero.NewMemMapFs() 46 require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(flags.ProjectRef), 0644)) 47 // Setup mock api 48 defer gock.OffAll() 49 gock.New(utils.DefaultApiHost). 50 Post("/v1/projects/" + flags.ProjectRef + "/branches"). 51 ReplyError(net.ErrClosed) 52 // Run test 53 err := Run(context.Background(), "", "sin", fsys) 54 // Check error 55 assert.ErrorIs(t, err, net.ErrClosed) 56 }) 57 58 t.Run("throws error on service unavailable", func(t *testing.T) { 59 // Setup in-memory fs 60 fsys := afero.NewMemMapFs() 61 require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(flags.ProjectRef), 0644)) 62 // Setup mock api 63 defer gock.OffAll() 64 gock.New(utils.DefaultApiHost). 65 Post("/v1/projects/" + flags.ProjectRef + "/branches"). 66 Reply(http.StatusServiceUnavailable) 67 // Run test 68 err := Run(context.Background(), "", "sin", fsys) 69 // Check error 70 assert.ErrorContains(t, err, "Unexpected error creating preview branch:") 71 }) 72 }