github.com/supabase/cli@v1.168.1/internal/projects/create/create_test.go (about) 1 package create 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/spf13/afero" 9 "github.com/stretchr/testify/assert" 10 "github.com/supabase/cli/internal/testing/apitest" 11 "github.com/supabase/cli/internal/utils" 12 "github.com/supabase/cli/pkg/api" 13 "gopkg.in/h2non/gock.v1" 14 ) 15 16 func TestProjectCreateCommand(t *testing.T) { 17 var params = api.V1CreateProjectBody{ 18 Name: "Test Project", 19 OrganizationId: "combined-fuchsia-lion", 20 DbPass: "redacted", 21 Region: api.V1CreateProjectBodyRegionUsWest1, 22 } 23 24 t.Run("creates a new project", func(t *testing.T) { 25 // Setup in-memory fs 26 fsys := afero.NewMemMapFs() 27 // Setup valid access token 28 token := apitest.RandomAccessToken(t) 29 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 30 // Flush pending mocks after test execution 31 defer gock.OffAll() 32 gock.New(utils.DefaultApiHost). 33 Post("/v1/projects"). 34 MatchType("json"). 35 JSON(params). 36 Reply(201). 37 JSON(api.V1ProjectResponse{ 38 Id: apitest.RandomProjectRef(), 39 OrganizationId: params.OrganizationId, 40 Name: params.Name, 41 Region: string(params.Region), 42 CreatedAt: "2022-04-25T02:14:55.906498Z", 43 }) 44 // Run test 45 assert.NoError(t, Run(context.Background(), params, fsys)) 46 // Validate api 47 assert.Empty(t, apitest.ListUnmatchedRequests()) 48 }) 49 50 t.Run("throws error on failure to load token", func(t *testing.T) { 51 assert.Error(t, Run(context.Background(), params, afero.NewMemMapFs())) 52 }) 53 54 t.Run("throws error on network error", func(t *testing.T) { 55 // Setup in-memory fs 56 fsys := afero.NewMemMapFs() 57 // Setup valid access token 58 token := apitest.RandomAccessToken(t) 59 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 60 // Flush pending mocks after test execution 61 defer gock.OffAll() 62 gock.New(utils.DefaultApiHost). 63 Post("/v1/projects"). 64 MatchType("json"). 65 JSON(params). 66 ReplyError(errors.New("network error")) 67 // Run test 68 assert.Error(t, Run(context.Background(), params, fsys)) 69 // Validate api 70 assert.Empty(t, apitest.ListUnmatchedRequests()) 71 }) 72 73 t.Run("throws error on server unavailable", func(t *testing.T) { 74 // Setup in-memory fs 75 fsys := afero.NewMemMapFs() 76 // Setup valid access token 77 token := apitest.RandomAccessToken(t) 78 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 79 // Flush pending mocks after test execution 80 defer gock.OffAll() 81 gock.New(utils.DefaultApiHost). 82 Post("/v1/projects"). 83 MatchType("json"). 84 JSON(params). 85 Reply(500). 86 JSON(map[string]string{"message": "unavailable"}) 87 // Run test 88 assert.Error(t, Run(context.Background(), params, fsys)) 89 // Validate api 90 assert.Empty(t, apitest.ListUnmatchedRequests()) 91 }) 92 93 t.Run("throws error on malformed json", func(t *testing.T) { 94 // Setup in-memory fs 95 fsys := afero.NewMemMapFs() 96 // Setup valid access token 97 token := apitest.RandomAccessToken(t) 98 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 99 // Flush pending mocks after test execution 100 defer gock.OffAll() 101 gock.New(utils.DefaultApiHost). 102 Post("/v1/projects"). 103 MatchType("json"). 104 JSON(params). 105 Reply(200). 106 JSON([]string{}) 107 // Run test 108 assert.Error(t, Run(context.Background(), params, fsys)) 109 // Validate api 110 assert.Empty(t, apitest.ListUnmatchedRequests()) 111 }) 112 }