github.com/supabase/cli@v1.168.1/internal/encryption/update/update_test.go (about) 1 package update 2 3 import ( 4 "context" 5 "net/http" 6 "os" 7 "testing" 8 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 TestUpdateRootKey(t *testing.T) { 18 t.Run("updates project encryption key", func(t *testing.T) { 19 // Setup valid project ref 20 project := apitest.RandomProjectRef() 21 // Setup valid access token 22 token := apitest.RandomAccessToken(t) 23 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 24 // Setup root key 25 r, w, err := os.Pipe() 26 require.NoError(t, err) 27 _, err = w.WriteString("test-key") 28 require.NoError(t, err) 29 require.NoError(t, w.Close()) 30 // Flush pending mocks after test execution 31 defer gock.OffAll() 32 gock.New(utils.DefaultApiHost). 33 Put("/v1/projects/" + project + "/pgsodium"). 34 JSON(api.UpdatePgsodiumConfigBody{RootKey: "test-key"}). 35 Reply(http.StatusOK). 36 JSON(api.PgsodiumConfigResponse{RootKey: "test-key"}) 37 // Run test 38 err = Run(context.Background(), project, r) 39 // Check error 40 assert.NoError(t, err) 41 assert.Empty(t, apitest.ListUnmatchedRequests()) 42 }) 43 44 t.Run("throws on invalid credentials", func(t *testing.T) { 45 // Setup valid project ref 46 project := apitest.RandomProjectRef() 47 // Setup valid access token 48 token := apitest.RandomAccessToken(t) 49 t.Setenv("SUPABASE_ACCESS_TOKEN", string(token)) 50 // Flush pending mocks after test execution 51 defer gock.OffAll() 52 gock.New(utils.DefaultApiHost). 53 Put("/v1/projects/" + project + "/pgsodium"). 54 Reply(http.StatusForbidden) 55 // Run test 56 err := Run(context.Background(), project, nil) 57 // Check error 58 assert.ErrorContains(t, err, "Unexpected error updating project root key:") 59 assert.Empty(t, apitest.ListUnmatchedRequests()) 60 }) 61 }