github.com/supabase/cli@v1.168.1/internal/projects/list/list_test.go (about)

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