Skip to content

Commit

Permalink
Merge branch 'main' into del-pro
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge authored Oct 12, 2023
2 parents 57e97c7 + 9e052f2 commit 528935e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 9 deletions.
18 changes: 9 additions & 9 deletions cmd/vault.go → cmd/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
File renamed without changes.
52 changes: 52 additions & 0 deletions internal/encryption/get/get_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
File renamed without changes.
61 changes: 61 additions & 0 deletions internal/encryption/update/update_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}

0 comments on commit 528935e

Please sign in to comment.