github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/projects/create/create_test.go (about) 1 package create 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/Redstoneguy129/cli/internal/testing/apitest" 9 "github.com/Redstoneguy129/cli/pkg/api" 10 "github.com/spf13/afero" 11 "github.com/stretchr/testify/assert" 12 "gopkg.in/h2non/gock.v1" 13 ) 14 15 func TestProjectCreateCommand(t *testing.T) { 16 var params = api.CreateProjectBody{ 17 Name: "Test Project", 18 OrganizationId: "combined-fuchsia-lion", 19 DbPass: "redacted", 20 Region: api.UsWest1, 21 Plan: api.Free, 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("https://api.supabase.io"). 33 Post("/v1/projects"). 34 MatchType("json"). 35 JSON(params). 36 Reply(201). 37 JSON(api.ProjectResponse{ 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("https://api.supabase.io"). 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("https://api.supabase.io"). 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("https://api.supabase.io"). 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 }