github.com/supabase/cli@v1.168.1/internal/secrets/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 TestSecretListCommand(t *testing.T) {
    17  	t.Run("lists all secrets", 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 + "/secrets").
    29  			Reply(200).
    30  			JSON(api.CreateSecretsJSONBody{
    31  				{
    32  					Name:  "Test Secret",
    33  					Value: "dummy-secret-value",
    34  				},
    35  			})
    36  		// Run test
    37  		err := Run(context.Background(), project, fsys)
    38  		// Check error
    39  		assert.NoError(t, err)
    40  		assert.Empty(t, apitest.ListUnmatchedRequests())
    41  	})
    42  
    43  	t.Run("throws error on missing access token", func(t *testing.T) {
    44  		t.Skip()
    45  		// Setup in-memory fs
    46  		fsys := afero.NewMemMapFs()
    47  		// Run test
    48  		err := Run(context.Background(), "", fsys)
    49  		// Check error
    50  		assert.ErrorContains(t, err, "Unexpected error retrieving project secrets")
    51  	})
    52  
    53  	t.Run("throws error on network error", func(t *testing.T) {
    54  		// Setup in-memory fs
    55  		fsys := afero.NewMemMapFs()
    56  		// Setup valid project ref
    57  		project := apitest.RandomProjectRef()
    58  		// Setup valid access token
    59  		token := apitest.RandomAccessToken(t)
    60  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    61  		// Flush pending mocks after test execution
    62  		defer gock.OffAll()
    63  		gock.New(utils.DefaultApiHost).
    64  			Get("/v1/projects/" + project + "/secrets").
    65  			ReplyError(errors.New("network error"))
    66  		// Run test
    67  		err := Run(context.Background(), project, fsys)
    68  		// Check error
    69  		assert.ErrorContains(t, err, "network error")
    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 project ref
    77  		project := apitest.RandomProjectRef()
    78  		// Setup valid access token
    79  		token := apitest.RandomAccessToken(t)
    80  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    81  		// Flush pending mocks after test execution
    82  		defer gock.OffAll()
    83  		gock.New(utils.DefaultApiHost).
    84  			Get("/v1/projects/" + project + "/secrets").
    85  			Reply(500).
    86  			JSON(map[string]string{"message": "unavailable"})
    87  		// Run test
    88  		err := Run(context.Background(), project, fsys)
    89  		// Check error
    90  		assert.ErrorContains(t, err, `Unexpected error retrieving project secrets: {"message":"unavailable"}`)
    91  		assert.Empty(t, apitest.ListUnmatchedRequests())
    92  	})
    93  
    94  	t.Run("throws error on malformed json", func(t *testing.T) {
    95  		// Setup in-memory fs
    96  		fsys := afero.NewMemMapFs()
    97  		// Setup valid project ref
    98  		project := apitest.RandomProjectRef()
    99  		// Setup valid access token
   100  		token := apitest.RandomAccessToken(t)
   101  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
   102  		// Flush pending mocks after test execution
   103  		defer gock.OffAll()
   104  		gock.New(utils.DefaultApiHost).
   105  			Get("/v1/projects/" + project + "/secrets").
   106  			Reply(200).
   107  			JSON(map[string]string{})
   108  		// Run test
   109  		err := Run(context.Background(), project, fsys)
   110  		// Check error
   111  		assert.ErrorContains(t, err, "json: cannot unmarshal object into Go value of type []api.SecretResponse")
   112  		assert.Empty(t, apitest.ListUnmatchedRequests())
   113  	})
   114  }