github.com/supabase/cli@v1.168.1/internal/secrets/unset/unset_test.go (about)

     1  package unset
     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 TestSecretUnsetCommand(t *testing.T) {
    17  	t.Run("Unsets secret via cli args", 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  			Delete("/v1/projects/" + project + "/secrets").
    29  			MatchType("json").
    30  			JSON(api.DeleteSecretsJSONBody{"my-secret"}).
    31  			Reply(200)
    32  		// Run test
    33  		err := Run(context.Background(), project, []string{"my-secret"}, fsys)
    34  		// Check error
    35  		assert.NoError(t, err)
    36  		assert.Empty(t, apitest.ListUnmatchedRequests())
    37  	})
    38  
    39  	t.Run("throws error on network error", func(t *testing.T) {
    40  		// Setup in-memory fs
    41  		fsys := afero.NewMemMapFs()
    42  		// Setup valid project ref
    43  		project := apitest.RandomProjectRef()
    44  		// Setup valid access token
    45  		token := apitest.RandomAccessToken(t)
    46  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    47  		// Flush pending mocks after test execution
    48  		defer gock.OffAll()
    49  		gock.New(utils.DefaultApiHost).
    50  			Delete("/v1/projects/" + project + "/secrets").
    51  			MatchType("json").
    52  			JSON(api.DeleteSecretsJSONBody{"my-secret"}).
    53  			ReplyError(errors.New("network error"))
    54  		// Run test
    55  		err := Run(context.Background(), project, []string{"my-secret"}, fsys)
    56  		// Check error
    57  		assert.ErrorContains(t, err, "network error")
    58  		assert.Empty(t, apitest.ListUnmatchedRequests())
    59  	})
    60  
    61  	t.Run("throws error on server unavailable", func(t *testing.T) {
    62  		// Setup in-memory fs
    63  		fsys := afero.NewMemMapFs()
    64  		// Setup valid project ref
    65  		project := apitest.RandomProjectRef()
    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(utils.DefaultApiHost).
    72  			Delete("/v1/projects/" + project + "/secrets").
    73  			MatchType("json").
    74  			JSON(api.DeleteSecretsJSONBody{"my-secret"}).
    75  			Reply(500).
    76  			JSON(map[string]string{"message": "unavailable"})
    77  		// Run test
    78  		err := Run(context.Background(), project, []string{"my-secret"}, fsys)
    79  		// Check error
    80  		assert.ErrorContains(t, err, `Unexpected error unsetting project secrets: {"message":"unavailable"}`)
    81  		assert.Empty(t, apitest.ListUnmatchedRequests())
    82  	})
    83  }