github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/internal/projects/list/list_test.go (about) 1 package list 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 TestProjectListCommand(t *testing.T) { 16 t.Run("lists all projects", func(t *testing.T) { 17 // Setup in-memory fs 18 fsys := afero.NewMemMapFs() 19 // Setup valid access token 20 token := apitest.RandomAccessToken(t) 21 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 22 // Flush pending mocks after test execution 23 defer gock.OffAll() 24 gock.New("https://api.supabase.io"). 25 Get("/v1/projects"). 26 Reply(200). 27 JSON([]api.ProjectResponse{ 28 { 29 Id: apitest.RandomProjectRef(), 30 OrganizationId: "combined-fuchsia-lion", 31 Name: "Test Project", 32 Region: "us-west-1", 33 CreatedAt: "2022-04-25T02:14:55.906498Z", 34 }, 35 }) 36 // Run test 37 assert.NoError(t, Run(context.Background(), fsys)) 38 // Validate api 39 assert.Empty(t, apitest.ListUnmatchedRequests()) 40 }) 41 42 t.Run("throws error on failure to load token", func(t *testing.T) { 43 assert.Error(t, Run(context.Background(), afero.NewMemMapFs())) 44 }) 45 46 t.Run("throws error on network error", func(t *testing.T) { 47 // Setup in-memory fs 48 fsys := afero.NewMemMapFs() 49 // Setup valid access token 50 token := apitest.RandomAccessToken(t) 51 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 52 // Flush pending mocks after test execution 53 defer gock.OffAll() 54 gock.New("https://api.supabase.io"). 55 Get("/v1/projects"). 56 ReplyError(errors.New("network error")) 57 // Run test 58 assert.Error(t, Run(context.Background(), fsys)) 59 // Validate api 60 assert.Empty(t, apitest.ListUnmatchedRequests()) 61 }) 62 63 t.Run("throws error on server unavailable", func(t *testing.T) { 64 // Setup in-memory fs 65 fsys := afero.NewMemMapFs() 66 // Setup valid access token 67 token := apitest.RandomAccessToken(t) 68 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 69 // Flush pending mocks after test execution 70 defer gock.OffAll() 71 gock.New("https://api.supabase.io"). 72 Get("/v1/projects"). 73 Reply(500). 74 JSON(map[string]string{"message": "unavailable"}) 75 // Run test 76 assert.Error(t, Run(context.Background(), fsys)) 77 // Validate api 78 assert.Empty(t, apitest.ListUnmatchedRequests()) 79 }) 80 81 t.Run("throws error on malformed json", func(t *testing.T) { 82 // Setup in-memory fs 83 fsys := afero.NewMemMapFs() 84 // Setup valid access token 85 token := apitest.RandomAccessToken(t) 86 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 87 // Flush pending mocks after test execution 88 defer gock.OffAll() 89 gock.New("https://api.supabase.io"). 90 Get("/v1/projects"). 91 Reply(200). 92 JSON(map[string]string{}) 93 // Run test 94 assert.Error(t, Run(context.Background(), fsys)) 95 // Validate api 96 assert.Empty(t, apitest.ListUnmatchedRequests()) 97 }) 98 }