Repository: supabase/cli
Pull request #1572 (2023-10-12 04:59:03)

fix: rename vault command to encryption
## What kind of change does this PR introduce?

fixes https://github.com/supabase/cli/issues/1569

## What is the current behavior?

Please link any relevant issues here.

## What is the new behavior?

Feel free to include screenshots if it includes visual changes.

## Additional context

Add any other context or screenshots.


coveralls: ## Pull Request Test Coverage Report for [Build 6491476581](https://coveralls.io/builds/63241714)

* **4** of **4**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* **5** unchanged lines in **1** file lost coverage.
* Overall coverage increased (+**0.2%**) to **57.044%**

---


|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [internal/gen/keys/keys.go](https://coveralls.io/builds/63241714/source?filename=internal%2Fgen%2Fkeys%2Fkeys.go#L98) | 5 | 12.31% |
<!-- | **Total:** | **5** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/63241714/badge)](https://coveralls.io/builds/63241714) |
| :-- | --: |
| Change from base [Build 6491460133](https://coveralls.io/builds/63241697): |  0.2% |
| Covered Lines: | 5171 |
| Relevant Lines: | 9065 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/cmd/vault.go b/cmd/encryption.go
similarity index 58%
rename from cmd/vault.go
rename to cmd/encryption.go
index 9f7d732c..d3b654af 100644
--- a/cmd/vault.go
+++ b/cmd/encryption.go
@@ -4,16 +4,16 @@ import (
 	"os"
 
 	"github.com/spf13/cobra"
+	"github.com/supabase/cli/internal/encryption/get"
+	"github.com/supabase/cli/internal/encryption/update"
 	"github.com/supabase/cli/internal/utils/flags"
-	"github.com/supabase/cli/internal/vault/get"
-	"github.com/supabase/cli/internal/vault/update"
 )
 
 var (
-	vaultCmd = &cobra.Command{
+	encryptionCmd = &cobra.Command{
 		GroupID: groupManagementAPI,
-		Use:     "vault",
-		Short:   "Manage column encryption of Supabase projects",
+		Use:     "encryption",
+		Short:   "Manage encryption keys of Supabase projects",
 	}
 
 	rootKeyGetCmd = &cobra.Command{
@@ -34,8 +34,8 @@ var (
 )
 
 func init() {
-	vaultCmd.PersistentFlags().StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
-	vaultCmd.AddCommand(rootKeyUpdateCmd)
-	vaultCmd.AddCommand(rootKeyGetCmd)
-	rootCmd.AddCommand(vaultCmd)
+	encryptionCmd.PersistentFlags().StringVar(&flags.ProjectRef, "project-ref", "", "Project ref of the Supabase project.")
+	encryptionCmd.AddCommand(rootKeyUpdateCmd)
+	encryptionCmd.AddCommand(rootKeyGetCmd)
+	rootCmd.AddCommand(encryptionCmd)
 }


Test patch:
diff --git a/internal/vault/get/get.go b/internal/encryption/get/get.go
similarity index 100%
rename from internal/vault/get/get.go
rename to internal/encryption/get/get.go
diff --git a/internal/encryption/get/get_test.go b/internal/encryption/get/get_test.go
new file mode 100644
index 00000000..6e148a50
--- /dev/null
+++ b/internal/encryption/get/get_test.go
@@ -0,0 +1,52 @@
+package get
+
+import (
+	"context"
+	"net/http"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/supabase/cli/internal/testing/apitest"
+	"github.com/supabase/cli/internal/utils"
+	"github.com/supabase/cli/pkg/api"
+	"gopkg.in/h2non/gock.v1"
+)
+
+func TestGetRootKey(t *testing.T) {
+	t.Run("fetches project encryption key", func(t *testing.T) {
+		// Setup valid project ref
+		project := apitest.RandomProjectRef()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Flush pending mocks after test execution
+		defer gock.OffAll()
+		gock.New(utils.DefaultApiHost).
+			Get("/v1/projects/" + project + "/pgsodium").
+			Reply(http.StatusOK).
+			JSON(api.PgsodiumConfigResponse{RootKey: "test-key"})
+		// Run test
+		err := Run(context.Background(), project)
+		// Check error
+		assert.NoError(t, err)
+		assert.Empty(t, apitest.ListUnmatchedRequests())
+	})
+
+	t.Run("throws on invalid credentials", func(t *testing.T) {
+		// Setup valid project ref
+		project := apitest.RandomProjectRef()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Flush pending mocks after test execution
+		defer gock.OffAll()
+		gock.New(utils.DefaultApiHost).
+			Get("/v1/projects/" + project + "/pgsodium").
+			Reply(http.StatusForbidden)
+		// Run test
+		err := Run(context.Background(), project)
+		// Check error
+		assert.ErrorContains(t, err, "Unexpected error retrieving project root key:")
+		assert.Empty(t, apitest.ListUnmatchedRequests())
+	})
+}
diff --git a/internal/vault/update/update.go b/internal/encryption/update/update.go
similarity index 100%
rename from internal/vault/update/update.go
rename to internal/encryption/update/update.go
diff --git a/internal/encryption/update/update_test.go b/internal/encryption/update/update_test.go
new file mode 100644
index 00000000..34ee5aa0
--- /dev/null
+++ b/internal/encryption/update/update_test.go
@@ -0,0 +1,61 @@
+package update
+
+import (
+	"context"
+	"net/http"
+	"os"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+	"github.com/supabase/cli/internal/testing/apitest"
+	"github.com/supabase/cli/internal/utils"
+	"github.com/supabase/cli/pkg/api"
+	"gopkg.in/h2non/gock.v1"
+)
+
+func TestUpdateRootKey(t *testing.T) {
+	t.Run("updates project encryption key", func(t *testing.T) {
+		// Setup valid project ref
+		project := apitest.RandomProjectRef()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Setup root key
+		r, w, err := os.Pipe()
+		require.NoError(t, err)
+		_, err = w.WriteString("test-key")
+		require.NoError(t, err)
+		require.NoError(t, w.Close())
+		// Flush pending mocks after test execution
+		defer gock.OffAll()
+		gock.New(utils.DefaultApiHost).
+			Put("/v1/projects/" + project + "/pgsodium").
+			JSON(api.UpdatePgsodiumConfigBody{RootKey: "test-key"}).
+			Reply(http.StatusOK).
+			JSON(api.PgsodiumConfigResponse{RootKey: "test-key"})
+		// Run test
+		err = Run(context.Background(), project, r)
+		// Check error
+		assert.NoError(t, err)
+		assert.Empty(t, apitest.ListUnmatchedRequests())
+	})
+
+	t.Run("throws on invalid credentials", func(t *testing.T) {
+		// Setup valid project ref
+		project := apitest.RandomProjectRef()
+		// Setup valid access token
+		token := apitest.RandomAccessToken(t)
+		t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
+		// Flush pending mocks after test execution
+		defer gock.OffAll()
+		gock.New(utils.DefaultApiHost).
+			Put("/v1/projects/" + project + "/pgsodium").
+			Reply(http.StatusForbidden)
+		// Run test
+		err := Run(context.Background(), project, nil)
+		// Check error
+		assert.ErrorContains(t, err, "Unexpected error updating project root key:")
+		assert.Empty(t, apitest.ListUnmatchedRequests())
+	})
+}