github.com/supabase/cli@v1.168.1/internal/projects/apiKeys/api_keys_test.go (about)

     1  package apiKeys
     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 TestProjectApiKeysCommand(t *testing.T) {
    17  	t.Run("lists all api-keys", 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  		gock.New(utils.DefaultApiHost).
    28  			Get("/v1/projects/" + project + "/api-keys").
    29  			Reply(200).
    30  			JSON([]api.ApiKeyResponse{{
    31  				Name:   "Test ApiKey",
    32  				ApiKey: "dummy-api-key-value",
    33  			}})
    34  		// Run test
    35  		err := Run(context.Background(), project, fsys)
    36  		// Check error
    37  		assert.NoError(t, err)
    38  		assert.Empty(t, apitest.ListUnmatchedRequests())
    39  	})
    40  
    41  	t.Run("throws error on missing access token", func(t *testing.T) {
    42  		// Setup in-memory fs
    43  		fsys := afero.NewMemMapFs()
    44  		// Run test
    45  		err := Run(context.Background(), "", fsys)
    46  		// Check error
    47  		assert.ErrorContains(t, err, "Unexpected error retrieving project api-keys")
    48  	})
    49  
    50  	t.Run("throws error on network error", func(t *testing.T) {
    51  		// Setup in-memory fs
    52  		fsys := afero.NewMemMapFs()
    53  		// Setup valid project ref
    54  		project := apitest.RandomProjectRef()
    55  		// Setup valid access token
    56  		token := apitest.RandomAccessToken(t)
    57  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    58  		// Flush pending mocks after test execution
    59  		defer gock.OffAll()
    60  		gock.New(utils.DefaultApiHost).
    61  			Get("/v1/projects/" + project + "/api-keys").
    62  			ReplyError(errors.New("network error"))
    63  		// Run test
    64  		err := Run(context.Background(), project, fsys)
    65  		// Check error
    66  		assert.ErrorContains(t, err, "network error")
    67  		assert.Empty(t, apitest.ListUnmatchedRequests())
    68  	})
    69  }