github.com/supabase/cli@v1.168.1/internal/secrets/set/set_test.go (about)

     1  package set
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/supabase/cli/internal/testing/apitest"
    12  	"github.com/supabase/cli/internal/utils"
    13  	"github.com/supabase/cli/pkg/api"
    14  	"gopkg.in/h2non/gock.v1"
    15  )
    16  
    17  func TestSecretSetCommand(t *testing.T) {
    18  	dummy := api.CreateSecretBody{Name: "my_name", Value: "my_value"}
    19  	dummyEnv := dummy.Name + "=" + dummy.Value
    20  
    21  	t.Run("Sets secret via cli args", func(t *testing.T) {
    22  		// Setup in-memory fs
    23  		fsys := afero.NewMemMapFs()
    24  		// Setup valid project ref
    25  		project := apitest.RandomProjectRef()
    26  		// Setup valid access token
    27  		token := apitest.RandomAccessToken(t)
    28  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    29  		// Flush pending mocks after test execution
    30  		defer gock.OffAll()
    31  		gock.New(utils.DefaultApiHost).
    32  			Post("/v1/projects/" + project + "/secrets").
    33  			MatchType("json").
    34  			JSON(api.CreateSecretsJSONBody{dummy}).
    35  			Reply(200)
    36  		// Run test
    37  		err := Run(context.Background(), project, "", []string{dummyEnv}, fsys)
    38  		// Check error
    39  		assert.NoError(t, err)
    40  		assert.Empty(t, apitest.ListUnmatchedRequests())
    41  	})
    42  
    43  	t.Run("Sets secret value via env file", func(t *testing.T) {
    44  		// Setup in-memory fs
    45  		fsys := afero.NewMemMapFs()
    46  		require.NoError(t, afero.WriteFile(fsys, "/tmp/.env", []byte(dummyEnv), 0644))
    47  		// Setup valid project ref
    48  		project := apitest.RandomProjectRef()
    49  		// Setup valid access token
    50  		token := apitest.RandomAccessToken(t)
    51  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    52  		// Flush pending mocks after test execution
    53  		defer gock.OffAll()
    54  		gock.New(utils.DefaultApiHost).
    55  			Post("/v1/projects/" + project + "/secrets").
    56  			MatchType("json").
    57  			JSON(api.CreateSecretsJSONBody{dummy}).
    58  			Reply(200)
    59  		// Run test
    60  		err := Run(context.Background(), project, "/tmp/.env", []string{}, fsys)
    61  		// Check error
    62  		assert.NoError(t, err)
    63  		assert.Empty(t, apitest.ListUnmatchedRequests())
    64  	})
    65  
    66  	t.Run("throws error on empty secret", func(t *testing.T) {
    67  		// Setup in-memory fs
    68  		fsys := afero.NewMemMapFs()
    69  		// Setup valid project ref
    70  		project := apitest.RandomProjectRef()
    71  		// Setup valid access token
    72  		token := apitest.RandomAccessToken(t)
    73  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    74  		// Run test
    75  		err := Run(context.Background(), project, "", []string{}, fsys)
    76  		// Check error
    77  		assert.ErrorContains(t, err, "No arguments found. Use --env-file to read from a .env file.")
    78  	})
    79  
    80  	t.Run("throws error on malformed secret", func(t *testing.T) {
    81  		// Setup in-memory fs
    82  		fsys := afero.NewMemMapFs()
    83  		// Setup valid project ref
    84  		project := apitest.RandomProjectRef()
    85  		// Setup valid access token
    86  		token := apitest.RandomAccessToken(t)
    87  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
    88  		// Run test
    89  		err := Run(context.Background(), project, "", []string{"malformed"}, fsys)
    90  		// Check error
    91  		assert.ErrorContains(t, err, "Invalid secret pair: malformed. Must be NAME=VALUE.")
    92  	})
    93  
    94  	t.Run("throws error on network error", 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  			Post("/v1/projects/" + project + "/secrets").
   106  			MatchType("json").
   107  			JSON(api.CreateSecretsJSONBody{dummy}).
   108  			ReplyError(errors.New("network error"))
   109  		// Run test
   110  		err := Run(context.Background(), project, "", []string{dummyEnv}, fsys)
   111  		// Check error
   112  		assert.ErrorContains(t, err, "network error")
   113  		assert.Empty(t, apitest.ListUnmatchedRequests())
   114  	})
   115  
   116  	t.Run("throws error on server unavailable", func(t *testing.T) {
   117  		// Setup in-memory fs
   118  		fsys := afero.NewMemMapFs()
   119  		// Setup valid project ref
   120  		project := apitest.RandomProjectRef()
   121  		// Setup valid access token
   122  		token := apitest.RandomAccessToken(t)
   123  		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
   124  		// Flush pending mocks after test execution
   125  		defer gock.OffAll()
   126  		gock.New(utils.DefaultApiHost).
   127  			Post("/v1/projects/" + project + "/secrets").
   128  			MatchType("json").
   129  			JSON(api.CreateSecretsJSONBody{dummy}).
   130  			Reply(500).
   131  			JSON(map[string]string{"message": "unavailable"})
   132  		// Run test
   133  		err := Run(context.Background(), project, "", []string{dummyEnv}, fsys)
   134  		// Check error
   135  		assert.ErrorContains(t, err, `Unexpected error setting project secrets: {"message":"unavailable"}`)
   136  		assert.Empty(t, apitest.ListUnmatchedRequests())
   137  	})
   138  }