github.com/supabase/cli@v1.168.1/internal/functions/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 TestFunctionsListCommand(t *testing.T) {
    17  	t.Run("lists all functions", func(t *testing.T) {
    18  		// Setup in-memory fs
    19  		fsys := afero.NewMemMapFs()
    20  		// Setup valid project ref
    21  		project := apitest.RandomProjectRef()
    22  		// Setup valid access token
    23  		token := apitest.RandomAccessToken(t)
    24  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    25  		// Flush pending mocks after test execution
    26  		defer gock.OffAll()
    27  
    28  		testEntrypointPath := "test-entrypoint-path"
    29  		testImportMapPath := "test-import-map-path"
    30  		testImportMap := false
    31  		testVerifyJwt := true
    32  
    33  		gock.New(utils.DefaultApiHost).
    34  			Get("/v1/projects/" + project + "/functions").
    35  			Reply(200).
    36  			JSON([]api.FunctionResponse{{
    37  				Id:             "test-id",
    38  				Name:           "Test Function",
    39  				Slug:           "test-function",
    40  				Status:         api.FunctionResponseStatusACTIVE,
    41  				UpdatedAt:      1687423025152.000000,
    42  				CreatedAt:      1687423025152.000000,
    43  				Version:        1.000000,
    44  				VerifyJwt:      &testVerifyJwt,
    45  				EntrypointPath: &testEntrypointPath,
    46  				ImportMap:      &testImportMap,
    47  				ImportMapPath:  &testImportMapPath,
    48  			}})
    49  		// Run test
    50  		err := Run(context.Background(), project, fsys)
    51  		// Check error
    52  		assert.NoError(t, err)
    53  		assert.Empty(t, apitest.ListUnmatchedRequests())
    54  	})
    55  
    56  	t.Run("throws error on missing access token", func(t *testing.T) {
    57  		// Setup in-memory fs
    58  		fsys := afero.NewMemMapFs()
    59  		// Run test
    60  		err := Run(context.Background(), "", fsys)
    61  		// Check error
    62  		assert.ErrorContains(t, err, "Unexpected error retrieving functions")
    63  	})
    64  
    65  	t.Run("throws error on network error", func(t *testing.T) {
    66  		// Setup in-memory fs
    67  		fsys := afero.NewMemMapFs()
    68  		// Setup valid project ref
    69  		project := apitest.RandomProjectRef()
    70  		// Setup valid access token
    71  		token := apitest.RandomAccessToken(t)
    72  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    73  		// Flush pending mocks after test execution
    74  		defer gock.OffAll()
    75  		gock.New(utils.DefaultApiHost).
    76  			Get("/v1/projects/" + project + "/functions").
    77  			ReplyError(errors.New("network error"))
    78  		// Run test
    79  		err := Run(context.Background(), project, fsys)
    80  		// Check error
    81  		assert.ErrorContains(t, err, "network error")
    82  		assert.Empty(t, apitest.ListUnmatchedRequests())
    83  	})
    84  }